Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.4更新 #24

Merged
merged 18 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
]
},
{
"login": "ly-nld",
"name": "跨性别",
"login": "cvyl",
"name": "Mikka",
"avatar_url": "https://avatars.githubusercontent.com/u/38471793?v=4",
"profile": "http://lgbt.sh",
"contributions": [
Expand Down
1 change: 1 addition & 0 deletions GUI/DictForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion GUI/DictForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace ja_learner
{
public partial class DictForm : Form
{
public Form mainForm;
public DictForm()

Check warning on line 19 in GUI/DictForm.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'mainForm' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
InitializeComponent();
}
Expand All @@ -31,7 +32,7 @@
webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
webView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
// 处理打开新窗口(用默认浏览器打开)
webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

Check warning on line 35 in GUI/DictForm.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void DictForm.CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)' doesn't match the target delegate 'EventHandler<CoreWebView2NewWindowRequestedEventArgs>' (possibly because of nullability attributes).
}
private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
Expand Down Expand Up @@ -75,9 +76,29 @@
#endif
}

public void UpdateTranslationPanelText(string text)
async public void UpdateTranslationPanelText(string text)
{
await webView.ExecuteScriptAsync($"setCurrentText('{text}')");
translationPanel.UpdateText(text);
}

async private void webView_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
string message = e.TryGetWebMessageAsString();
if (message == "ANKI_INIT")
{
if (Program.APP_SETTING.AnkiEnabled)
{
await webView.ExecuteScriptAsync($"setAnkiEnabled(true)");
AnkiOptions o = Program.APP_SETTING.Anki;
await webView.ExecuteScriptAsync($"setAnkiConfig('{o.Deck}','{o.Model}','{o.FieldNames.Word}','{o.FieldNames.Example}','{o.FieldNames.Explain}')");
}
else
{
await webView.ExecuteScriptAsync($"setAnkiEnabled(false)");
}
return;
}
}
}
}
39 changes: 27 additions & 12 deletions GUI/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
{
DictForm dictForm;

Size defaultMinSize;

TextAnalyzer textAnalyzer = new TextAnalyzer();
private string sentence = "";
private bool immersiveMode = false;
Expand All @@ -22,13 +24,15 @@
tabControl.Hide();
panel1.Hide();
FormBorderStyle = FormBorderStyle.None;
MinimumSize = new Size(0,0);
}
else
{
webView.Parent = tabControl.TabPages[0];
tabControl.Show();
panel1.Show();
FormBorderStyle = FormBorderStyle.Sizable;
MinimumSize = defaultMinSize;
}
immersiveMode = value;
}
Expand All @@ -49,7 +53,7 @@
}
}

public MainForm()

Check warning on line 56 in GUI/MainForm.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'dictForm' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
InitializeComponent();
}
Expand All @@ -59,7 +63,7 @@
webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
webView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
// 处理打开新窗口(用默认浏览器打开)
webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

Check warning on line 66 in GUI/MainForm.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void MainForm.CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)' doesn't match the target delegate 'EventHandler<CoreWebView2NewWindowRequestedEventArgs>' (possibly because of nullability attributes).
// 接收js消息(查词典)
webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
}
Expand All @@ -76,14 +80,23 @@
// 初始化 HTTP 服务器
HttpServer.StartServer();
webView.Source = new Uri($"http://localhost:{HttpServer.Port}/"); // build
Text += $" - 已占用端口:{HttpServer.Port} -";
Text += $" - {HttpServer.Port} -";
#endif

// 初始化 DictForm
dictForm = new DictForm(this);
dictForm.Show();
dictForm.Hide();
UpdateExtraPromptCombobox();

// 初始化 MainForm
if (Program.APP_SETTING.HttpProxy != string.Empty)
{
checkBoxUseProxy.Enabled = true;
checkBoxUseProxy.Text = $"HTTP代理:{Program.APP_SETTING.HttpProxy}";
}
comboBoxTranslator.SelectedIndex = 0;
defaultMinSize = MinimumSize;
}

private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
Expand Down Expand Up @@ -418,5 +431,11 @@
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}

private void checkBoxUseProxy_CheckedChanged(object sender, EventArgs e)
{
UserConfig.UseProxy = checkBoxUseProxy.Checked;
GptCaller.SetProxy(UserConfig.UseProxy);
}
}
}
48 changes: 40 additions & 8 deletions GptCaller.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
using OpenAI_API;
using OpenAI_API.Chat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace ja_learner
{
internal class GptCaller
{
private static OpenAIAPI api;

Check warning on line 9 in GptCaller.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'api' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

private static IHttpClientFactory defaultFactory;

Check warning on line 11 in GptCaller.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'defaultFactory' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
private static IHttpClientFactory proxyFactory;

Check warning on line 12 in GptCaller.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'proxyFactory' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

public static void Initialize()
{
api = new(Program.APP_SETTING.ApiKey) { ApiUrlFormat = Program.APP_SETTING.ApiUrl };
api = new(Program.APP_SETTING.GPT.ApiKey) { ApiUrlFormat = Program.APP_SETTING.GPT.ApiUrl };

Check warning on line 16 in GptCaller.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
defaultFactory = api.HttpClientFactory;
proxyFactory = new MyHttpClientFactory(Program.APP_SETTING.HttpProxy);
}

public static void SetProxy(bool useProxy)
{
if (useProxy)
{
api.HttpClientFactory = proxyFactory;
}
else
{
api.HttpClientFactory = defaultFactory;
}
}

public static Conversation CreateTranslateConversation(string text)
{
Conversation conversation = api.Chat.CreateConversation();
conversation.AppendSystemMessage("You are a translation engine, translate the text to Simplified Chinese. Don't output anything other than translation results.");
conversation.AppendSystemMessage(Program.APP_SETTING.GPT.TranslatePrompt);

Check warning on line 36 in GptCaller.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
if (UserConfig.useExtraPrompt)
{
AddExtraSystemPrompt(conversation);
Expand All @@ -32,7 +45,7 @@
public static Conversation CreateInterpretConversation(string text)
{
Conversation conversation = api.Chat.CreateConversation();
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.");
conversation.AppendSystemMessage(Program.APP_SETTING.GPT.ExplainPrompt);

Check warning on line 48 in GptCaller.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
if (UserConfig.useExtraPrompt)
{
AddExtraSystemPrompt(conversation);
Expand Down Expand Up @@ -64,4 +77,23 @@
}
}
}

class MyHttpClientFactory : IHttpClientFactory
{
private string proxy;
public MyHttpClientFactory(string proxy)
{
this.proxy = proxy;
}
HttpClient IHttpClientFactory.CreateClient(string name)
{
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy($"http://{proxy}")
};
var client = new HttpClient(handler);
return client;
}
}

}
Loading
Loading