Skip to content

Commit

Permalink
feat: 添加prompt管理
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Mar 13, 2024
1 parent 4c65808 commit 5da724b
Show file tree
Hide file tree
Showing 22 changed files with 616 additions and 15 deletions.
52 changes: 49 additions & 3 deletions src/Attachments/iconfont/demo_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ <h1 class="logo"><a href="https://www.iconfont.cn/" title="iconfont 首页" targ
<div class="content unicode" style="display: block;">
<ul class="icon_lists dib-box">

<li class="dib">
<span class="icon iconfont">&#xe609;</span>
<div class="name">编辑</div>
<div class="code-name">&amp;#xe609;</div>
</li>

<li class="dib">
<span class="icon iconfont">&#xe61b;</span>
<div class="name">编辑</div>
<div class="code-name">&amp;#xe61b;</div>
</li>

<li class="dib">
<span class="icon iconfont">&#xeabb;</span>
<div class="name">24gl-clipboardEmpty</div>
Expand Down Expand Up @@ -414,9 +426,9 @@ <h3 id="-font-face">第一步:拷贝项目下面生成的 <code>@font-face</co
<pre><code class="language-css"
>@font-face {
font-family: 'iconfont';
src: url('iconfont.woff2?t=1709607636773') format('woff2'),
url('iconfont.woff?t=1709607636773') format('woff'),
url('iconfont.ttf?t=1709607636773') format('truetype');
src: url('iconfont.woff2?t=1710297275859') format('woff2'),
url('iconfont.woff?t=1710297275859') format('woff'),
url('iconfont.ttf?t=1710297275859') format('truetype');
}
</code></pre>
<h3 id="-iconfont-">第二步:定义使用 iconfont 的样式</h3>
Expand All @@ -442,6 +454,24 @@ <h3 id="-">第三步:挑选相应图标并获取字体编码,应用于页面
<div class="content font-class">
<ul class="icon_lists dib-box">

<li class="dib">
<span class="icon iconfont icon-bianji"></span>
<div class="name">
编辑
</div>
<div class="code-name">.icon-bianji
</div>
</li>

<li class="dib">
<span class="icon iconfont icon-bianji1"></span>
<div class="name">
编辑
</div>
<div class="code-name">.icon-bianji1
</div>
</li>

<li class="dib">
<span class="icon iconfont icon-24gl-clipboardEmpty"></span>
<div class="name">
Expand Down Expand Up @@ -982,6 +1012,22 @@ <h3 id="-">第二步:挑选相应图标并获取类名,应用于页面:</h
<div class="content symbol">
<ul class="icon_lists dib-box">

<li class="dib">
<svg class="icon svg-icon" aria-hidden="true">
<use xlink:href="#icon-bianji"></use>
</svg>
<div class="name">编辑</div>
<div class="code-name">#icon-bianji</div>
</li>

<li class="dib">
<svg class="icon svg-icon" aria-hidden="true">
<use xlink:href="#icon-bianji1"></use>
</svg>
<div class="name">编辑</div>
<div class="code-name">#icon-bianji1</div>
</li>

<li class="dib">
<svg class="icon svg-icon" aria-hidden="true">
<use xlink:href="#icon-24gl-clipboardEmpty"></use>
Expand Down
14 changes: 11 additions & 3 deletions src/Attachments/iconfont/iconfont.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@font-face {
font-family: "iconfont"; /* Project id 4294789 */
src: url('iconfont.woff2?t=1709607636773') format('woff2'),
url('iconfont.woff?t=1709607636773') format('woff'),
url('iconfont.ttf?t=1709607636773') format('truetype');
src: url('iconfont.woff2?t=1710297275859') format('woff2'),
url('iconfont.woff?t=1710297275859') format('woff'),
url('iconfont.ttf?t=1710297275859') format('truetype');
}

.iconfont {
Expand All @@ -13,6 +13,14 @@
-moz-osx-font-smoothing: grayscale;
}

.icon-bianji:before {
content: "\e609";
}

.icon-bianji1:before {
content: "\e61b";
}

.icon-24gl-clipboardEmpty:before {
content: "\eabb";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Attachments/iconfont/iconfont.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/Attachments/iconfont/iconfont.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
"css_prefix_text": "icon-",
"description": "基于.Net 6 开发的新款翻译工具",
"glyphs": [
{
"icon_id": "11833219",
"name": "编辑",
"font_class": "bianji",
"unicode": "e609",
"unicode_decimal": 58889
},
{
"icon_id": "25426240",
"name": "编辑",
"font_class": "bianji1",
"unicode": "e61b",
"unicode_decimal": 58907
},
{
"icon_id": "7594790",
"name": "24gl-clipboardEmpty",
Expand Down
Binary file modified src/Attachments/iconfont/iconfont.ttf
Binary file not shown.
Binary file modified src/Attachments/iconfont/iconfont.woff
Binary file not shown.
Binary file modified src/Attachments/iconfont/iconfont.woff2
Binary file not shown.
2 changes: 2 additions & 0 deletions src/STranslate.Model/ITranslatorAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ namespace STranslate.Model
public interface ITranslatorAI : ITranslator
{
BindingList<Prompt> Prompts { get; set; }

BindingList<UserDefinePrompt> UserDefinePrompts { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/STranslate.Model/Prompt.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json;
using System;
using System.ComponentModel;

namespace STranslate.Model
{
public partial class UserDefinePrompt : ObservableObject, ICloneable
{
[JsonIgnore]
[ObservableProperty]
[property: JsonProperty("name")]
private string _name;

[JsonIgnore]
[ObservableProperty]
[property: JsonProperty("prompts")]
private BindingList<Prompt> _prompts;

public UserDefinePrompt(string name, BindingList<Prompt> prompts)
{
Name = name;
Prompts = prompts;
}

public object Clone()
{
return new UserDefinePrompt(Name, Prompts);
}
}

/// <summary>
/// Prompt Definition
/// </summary>
Expand Down
Binary file modified src/STranslate.Style/Fonts/iconfont.ttf
Binary file not shown.
118 changes: 118 additions & 0 deletions src/STranslate.Style/Styles/ButtonStyle.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,122 @@
</Setter.Value>
</Setter>
</Style>

<!-- // Preset Prompt Start // -->
<ControlTemplate x:Key="lbtnt" TargetType="Button">
<Border x:Name="border"
MinHeight="20"
Background="{TemplateBinding Background}"
CornerRadius="5,0,0,5"
SnapsToDevicePixels="true">
<TextBlock x:Name="tb"
Padding="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
Text="{TemplateBinding Content}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnMouseOverBackground}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnPressedBackground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnDisabledBackground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="tb" Property="Foreground" Value="{DynamicResource TextBlockDisabledForeground}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="rbtnt" TargetType="Button">
<Border x:Name="border"
MinHeight="20"
Background="{TemplateBinding Background}"
CornerRadius="0,5,5,0"
SnapsToDevicePixels="true">
<TextBlock x:Name="tb"
Padding="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
Text="{TemplateBinding Content}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnMouseOverBackground}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnPressedBackground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnDisabledBackground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="tb" Property="Foreground" Value="{DynamicResource TextBlockDisabledForeground}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="btnt" TargetType="Button">
<Border x:Name="border"
MinHeight="20"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<TextBlock x:Name="tb"
Padding="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
Text="{TemplateBinding Content}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnMouseOverBackground}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnPressedBackground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Background" Value="{DynamicResource BtnDisabledBackground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="tb" Property="Foreground" Value="{DynamicResource TextBlockDisabledForeground}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="promptbase" TargetType="Button">
<Setter Property="Background" Value="{DynamicResource BtnBackground}" />
<Setter Property="Foreground" Value="{DynamicResource TextForeground}" />
<Setter Property="BorderThickness" Value="0" />
</Style>
<Style x:Key="btn" BasedOn="{StaticResource promptbase}" TargetType="Button">
<Setter Property="Template" Value="{DynamicResource btnt}" />
</Style>
<Style x:Key="lbtn" BasedOn="{StaticResource promptbase}" TargetType="Button">
<Setter Property="Template" Value="{DynamicResource lbtnt}" />
</Style>
<Style x:Key="rbtn" BasedOn="{StaticResource promptbase}" TargetType="Button">
<Setter Property="Template" Value="{DynamicResource rbtnt}" />
</Style>
<Style x:Key="verSep" TargetType="Separator">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrushColor}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Width" Value="16" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="90" />
</Setter.Value>
</Setter>
</Style>
<!-- // Preset Prompt End // -->
</ResourceDictionary>
1 change: 1 addition & 0 deletions src/STranslate/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ public override ITranslator ReadJson(JsonReader reader, Type objectType, ITransl
if (translator is ITranslatorAI ai)
{
ai.Prompts.Clear();
ai.UserDefinePrompts.Clear();
}

serializer.Populate(jsonObject.CreateReader(), translator);
Expand Down
3 changes: 3 additions & 0 deletions src/STranslate/STranslate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<Compile Update="Views\Preference\AboutPage.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="Views\Preference\Service\PromptDialog.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="Views\Preference\TTSPage.xaml.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
3 changes: 1 addition & 2 deletions src/STranslate/ViewModels/Preference/ServiceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ public void NavigationPage(string name, ITranslator translator)
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("param name is null or empty", nameof(name));

if (translator == null)
throw new ArgumentNullException(nameof(translator));
ArgumentNullException.ThrowIfNull(translator);

Type? type = Type.GetType(name) ?? throw new Exception($"{nameof(NavigationPage)} get {name} exception");

Expand Down
63 changes: 63 additions & 0 deletions src/STranslate/ViewModels/Preference/Services/PromptViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using STranslate.Model;
using System.Linq;
using System.Windows;

namespace STranslate.ViewModels.Preference.Services
{
public partial class PromptViewModel : ObservableObject
{
[ObservableProperty]
private UserDefinePrompt _userDefinePrompt;

private readonly ServiceType _serviceType;

public PromptViewModel(ServiceType type, UserDefinePrompt definePrompt)
{
_serviceType = type;
UserDefinePrompt = definePrompt;
}

[RelayCommand]
private void Add()
{
var last = UserDefinePrompt.Prompts.LastOrDefault()?.Role ?? "";
var newOne = _serviceType switch
{
ServiceType.GeminiService
=> last switch
{
"user" => new Prompt("model"),
_ => new Prompt("user")
},
ServiceType.ChatglmService
=> last switch
{
"user" => new Prompt("assistant"),
_ => new Prompt("user")
},
_
=> last switch
{
"" => new Prompt("system"),
"user" => new Prompt("assistant"),
_ => new Prompt("user")
},
};
UserDefinePrompt.Prompts.Add(newOne);
}

[RelayCommand]
private void Del(Prompt prompt)
{
UserDefinePrompt.Prompts.Remove(prompt);
}

[RelayCommand]
private void Save(Window window)
{
window.DialogResult = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public TranslatorChatglm(Guid guid, string url, string name = "", IconType icon

#endregion Show/Hide Encrypt Info

[JsonIgnore]
[ObservableProperty]
private BindingList<UserDefinePrompt> userDefinePrompts = [];

[JsonIgnore]
[ObservableProperty]
private BindingList<Prompt> prompts =
Expand Down
Loading

0 comments on commit 5da724b

Please sign in to comment.