Skip to content

Commit 4e7fcce

Browse files
ks233allcontributors[bot]cvyl
authored
v0.4更新 (#24)
* 更新readme,新增贡献者板块 * docs: add ks233 as a contributor for code (#10) * docs: update README.md * docs: update .all-contributorsrc --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> * docs: add shaka0919 as a contributor for code (#11) * docs: update README.md * docs: update .all-contributorsrc --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> * Update README.md (#12) * Update .all-contributorsrc (#13) * 修改错字、删除多余代码 * Update MainForm.cs * Update MainForm.cs * 沉浸模式 * 更新文档 * HTTP代理、Anki、更多设置 * GPT自定义Prompt * 修复unidic下标错误 * 沉浸模式不限制窗口最小尺寸 * Update appsettings.json * 更新文档 * Update README.md --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> Co-authored-by: 跨性别 <github@lgbt.sh>
1 parent 8c1ec82 commit 4e7fcce

14 files changed

+275
-77
lines changed

.all-contributorsrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
]
2828
},
2929
{
30-
"login": "ly-nld",
31-
"name": "跨性别",
30+
"login": "cvyl",
31+
"name": "Mikka",
3232
"avatar_url": "https://avatars.githubusercontent.com/u/38471793?v=4",
3333
"profile": "http://lgbt.sh",
3434
"contributions": [

GUI/DictForm.Designer.cs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/DictForm.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text;
1010
using System.Threading.Tasks;
1111
using System.Windows.Forms;
12+
using static System.Net.Mime.MediaTypeNames;
1213

1314
namespace ja_learner
1415
{
@@ -75,9 +76,29 @@ private async void DictForm_Load(object sender, EventArgs e)
7576
#endif
7677
}
7778

78-
public void UpdateTranslationPanelText(string text)
79+
async public void UpdateTranslationPanelText(string text)
7980
{
81+
await webView.ExecuteScriptAsync($"setCurrentText('{text}')");
8082
translationPanel.UpdateText(text);
8183
}
84+
85+
async private void webView_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
86+
{
87+
string message = e.TryGetWebMessageAsString();
88+
if (message == "ANKI_INIT")
89+
{
90+
if (Program.APP_SETTING.AnkiEnabled)
91+
{
92+
await webView.ExecuteScriptAsync($"setAnkiEnabled(true)");
93+
AnkiOptions o = Program.APP_SETTING.Anki;
94+
await webView.ExecuteScriptAsync($"setAnkiConfig('{o.Deck}','{o.Model}','{o.FieldNames.Word}','{o.FieldNames.Example}','{o.FieldNames.Explain}')");
95+
}
96+
else
97+
{
98+
await webView.ExecuteScriptAsync($"setAnkiEnabled(false)");
99+
}
100+
return;
101+
}
102+
}
82103
}
83104
}

GUI/MainForm.Designer.cs

+27-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/MainForm.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public partial class MainForm : Form
88
{
99
DictForm dictForm;
1010

11+
Size defaultMinSize;
12+
1113
TextAnalyzer textAnalyzer = new TextAnalyzer();
1214
private string sentence = "";
1315
private bool immersiveMode = false;
@@ -22,13 +24,15 @@ public bool ImmersiveMode
2224
tabControl.Hide();
2325
panel1.Hide();
2426
FormBorderStyle = FormBorderStyle.None;
27+
MinimumSize = new Size(0,0);
2528
}
2629
else
2730
{
2831
webView.Parent = tabControl.TabPages[0];
2932
tabControl.Show();
3033
panel1.Show();
3134
FormBorderStyle = FormBorderStyle.Sizable;
35+
MinimumSize = defaultMinSize;
3236
}
3337
immersiveMode = value;
3438
}
@@ -76,14 +80,23 @@ private async void MainForm_Load(object sender, EventArgs e)
7680
// 初始化 HTTP 服务器
7781
HttpServer.StartServer();
7882
webView.Source = new Uri($"http://localhost:{HttpServer.Port}/"); // build
79-
Text += $" - 已占用端口:{HttpServer.Port} -";
83+
Text += $" - {HttpServer.Port} -";
8084
#endif
8185

86+
// 初始化 DictForm
8287
dictForm = new DictForm(this);
8388
dictForm.Show();
8489
dictForm.Hide();
8590
UpdateExtraPromptCombobox();
91+
92+
// 初始化 MainForm
93+
if (Program.APP_SETTING.HttpProxy != string.Empty)
94+
{
95+
checkBoxUseProxy.Enabled = true;
96+
checkBoxUseProxy.Text = $"HTTP代理:{Program.APP_SETTING.HttpProxy}";
97+
}
8698
comboBoxTranslator.SelectedIndex = 0;
99+
defaultMinSize = MinimumSize;
87100
}
88101

89102
private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
@@ -418,5 +431,11 @@ private void Drag()
418431
ReleaseCapture();
419432
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
420433
}
434+
435+
private void checkBoxUseProxy_CheckedChanged(object sender, EventArgs e)
436+
{
437+
UserConfig.UseProxy = checkBoxUseProxy.Checked;
438+
GptCaller.SetProxy(UserConfig.UseProxy);
439+
}
421440
}
422441
}

GptCaller.cs

+40-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
using OpenAI_API;
22
using OpenAI_API.Chat;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
3+
using System.Net;
84

95
namespace ja_learner
106
{
117
internal class GptCaller
128
{
139
private static OpenAIAPI api;
1410

11+
private static IHttpClientFactory defaultFactory;
12+
private static IHttpClientFactory proxyFactory;
13+
1514
public static void Initialize()
1615
{
17-
api = new(Program.APP_SETTING.ApiKey) { ApiUrlFormat = Program.APP_SETTING.ApiUrl };
16+
api = new(Program.APP_SETTING.GPT.ApiKey) { ApiUrlFormat = Program.APP_SETTING.GPT.ApiUrl };
17+
defaultFactory = api.HttpClientFactory;
18+
proxyFactory = new MyHttpClientFactory(Program.APP_SETTING.HttpProxy);
19+
}
20+
21+
public static void SetProxy(bool useProxy)
22+
{
23+
if (useProxy)
24+
{
25+
api.HttpClientFactory = proxyFactory;
26+
}
27+
else
28+
{
29+
api.HttpClientFactory = defaultFactory;
30+
}
1831
}
1932

2033
public static Conversation CreateTranslateConversation(string text)
2134
{
2235
Conversation conversation = api.Chat.CreateConversation();
23-
conversation.AppendSystemMessage("You are a translation engine, translate the text to Simplified Chinese. Don't output anything other than translation results.");
36+
conversation.AppendSystemMessage(Program.APP_SETTING.GPT.TranslatePrompt);
2437
if (UserConfig.useExtraPrompt)
2538
{
2639
AddExtraSystemPrompt(conversation);
@@ -32,7 +45,7 @@ public static Conversation CreateTranslateConversation(string text)
3245
public static Conversation CreateInterpretConversation(string text)
3346
{
3447
Conversation conversation = api.Chat.CreateConversation();
35-
conversation.AppendSystemMessage("You are a Japanese teacher, List and explain the vocabulary (except prepositions) and grammar of the given text in Simplified Chinese. Your output consists of three parts: translation, vocabulary, grammar. Don't use English and romaji.");
48+
conversation.AppendSystemMessage(Program.APP_SETTING.GPT.ExplainPrompt);
3649
if (UserConfig.useExtraPrompt)
3750
{
3851
AddExtraSystemPrompt(conversation);
@@ -64,4 +77,23 @@ async public static void StreamResponse(Conversation conversation, Action<string
6477
}
6578
}
6679
}
80+
81+
class MyHttpClientFactory : IHttpClientFactory
82+
{
83+
private string proxy;
84+
public MyHttpClientFactory(string proxy)
85+
{
86+
this.proxy = proxy;
87+
}
88+
HttpClient IHttpClientFactory.CreateClient(string name)
89+
{
90+
HttpClientHandler handler = new HttpClientHandler()
91+
{
92+
Proxy = new WebProxy($"http://{proxy}")
93+
};
94+
var client = new HttpClient(handler);
95+
return client;
96+
}
97+
}
98+
6799
}

0 commit comments

Comments
 (0)