-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMain.cs
390 lines (332 loc) · 12.6 KB
/
FormMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using AntdUI;
using HuExtend;
using Microsoft.Playwright;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using NewLife;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AIComposer
{
public partial class AIComposer : AntdUI.Window
{
public static string SettingFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AIComposerSetting.json");
private void LoadSetting()
{
try
{
Program.Setting = SettingFilePath.FromJsonConfig<SystemSetting>() ?? new SystemSetting();
}
catch (System.Exception)
{
Program.Setting = new SystemSetting();
}
var setting = Program.Setting;
//默认热键
if (setting.ShowHotkey == null)
{
setting.ShowHotkey = new HotkeySetting()
{
Modifiers = KeyModifiers.Alt,
Key = Keys.D1
};
}
if (setting.AskHotkey == null)
{
setting.AskHotkey = new HotkeySetting()
{
Modifiers = KeyModifiers.Alt,
Key = Keys.D2
};
}
//默认AI配置
if (setting.AISettings == null || setting.AISettings.Count <= 0)
{
setting.AISettings = new List<AISetting>();
foreach (var ai in Program.AIList)
{
setting.AISettings.Add(new AISetting() { Key = ai.Key, Enabled = true });
}
}
}
private void SaveSetting()
{
Program.Setting.ToJsonConfig(SettingFilePath);
}
public AIComposer()
{
InitializeComponent();
//设置窗口类名
this.Name = "AIComposer";
CenterToScreen();
}
private const int _top = 60;
private const int _bottom = 20;
private const int _left = 10;
private const int _right = 0;
private List<WebView2> _webViews = new List<WebView2>();
//根据url列表,初始化webview2
private async Task InitializeWebViewList()
{
//先关闭所有webview2
foreach (var webView in _webViews)
{
this.Controls.Remove(webView);
webView.Dispose();
}
_webViews.Clear();
//输出窗口内所有控件
foreach (Control control in this.Controls)
{
Console.WriteLine(control.Name);
}
foreach (var aiSetting in Program.Setting.AISettings)
{
if (aiSetting.Enabled == false)
continue;
var ai = Program.AIList.FirstOrDefault(x => x.Key == aiSetting.Key);
if (ai == null)
{
continue;
}
var webView = new WebView2();
webView.CoreWebView2InitializationCompleted += WebViewInitializeCompleted;
await InitializeWebView(webView);
webView.Source = new Uri(ai.Url);
webView.Tag = ai;
_webViews.Add(webView);
}
for (int i = 0; i < _webViews.Count; i++)
{
this.Controls.Add(_webViews[i]);
}
}
private async Task InitializeWebView(WebView2 webView)
{
string userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AIComposerCache");
Directory.CreateDirectory(userDataFolder);
// 创建CoreWebView2Environment实例,并指定缓存目录
var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder, new CoreWebView2EnvironmentOptions()
{
AdditionalBrowserArguments = "--remote-debugging-port=10083",
});
// 确保WebView2控件使用指定的环境
await webView.EnsureCoreWebView2Async(environment);
}
private const int KEY_SHOW = 1;
private const int KEY_QUESTION = 2;
private async void FormMain_Load(object sender, EventArgs e)
{
AntdUI.Config.ShowInWindow = true;
LoadSetting();
RefreshSetting();
await InitializeWebViewList();
//当前屏幕宽度的80%和高度的80%
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
this.Size = new Size((int)(screenWidth * 0.8), (int)(screenHeight * 0.8));
//FormMain_Resize(this, null);
//自己居中显示窗口
this.Location = new Point((screenWidth - this.Width) / 2, (screenHeight - this.Height) / 2);
}
// 监听窗口消息
protected override void WndProc(ref System.Windows.Forms.Message m)
{
const int WM_HOTKEY = 0x0312;
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == KEY_SHOW)
{
//显示或者隐藏
if (this.Visible)
{
this.Hide();
}
else
{
this.Show();
// 检查窗体是否是最小化状态
if (this.WindowState == FormWindowState.Minimized)
{
// 将窗体恢复到正常状态
this.WindowState = FormWindowState.Normal;
}
this.Activate();
TopLevel = true;
}
}
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == KEY_QUESTION)
{
OnQuestion().ConfigureAwait(false);
}
base.WndProc(ref m);
}
private AIInputForm _inputForm = null;
private async Task OnQuestion()
{
if (_inputForm == null || _inputForm.IsDisposed)
{
_inputForm = new AIInputForm();
var dlgRes = _inputForm.ShowDialog();
if (dlgRes == DialogResult.OK)
{
// 检查窗体是否是最小化状态
if (this.WindowState == FormWindowState.Minimized)
{
// 将窗体恢复到正常状态
this.WindowState = FormWindowState.Normal;
}
// 将窗体显示到前台
this.Show();
// 激活窗体,使其成为活动窗口
this.Activate();
TopLevel = true;
await AskQuestion(_inputForm.Content);
}
_inputForm.Dispose();
}
else
{
_inputForm.Close();
}
}
private async Task AskQuestion(string content)
{
var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.ConnectOverCDPAsync("http://localhost:10083");
var context = browser.Contexts[0];
foreach (var page in context.Pages)
{
foreach (var ai in Program.AIList)
{
if (page.Url.Contains(ai.Key))
{
var res = await ai.AskAsync(content, page);
if (res.code == 0)
{
AntdUI.Notification.error(this, "错误", $"向{ai.Name}提问失败,请调整窗口布局!", TAlignFrom.Top);
}
else
{
AntdUI.Message.success(this, $"提问成功:{ai.Name}");
}
}
}
}
}
private void FormMain_Resize(object sender, EventArgs e)
{
if (_webViews.Count <= 0)
return;
Console.WriteLine("FormMain_Resize:" + this.Width + " " + this.Height);
var realHeight = this.Height - _top - _bottom;
int webViewWidth = this.Width / _webViews.Count - _left - _right;
for (int i = 0; i < _webViews.Count; i++)
{
_webViews[i].Size = new Size(webViewWidth, realHeight);
_webViews[i].Left = _left + webViewWidth * i;
_webViews[i].Top = _top;
}
}
private void WebViewInitializeCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
Console.WriteLine("WebView2 initialized");
}
}
private bool _isNotify = false;
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
//GlobalHotkey.UnregisterHotKey(this.Handle, KEY_SHOW);
//GlobalHotkey.UnregisterHotKey(this.Handle, KEY_QUESTION);
if (_isNotify == false)
{
notifyIcon.ShowBalloonTip(3000, "AI创作者", "我会在小托盘常驻,可以在小托盘图标右键退出哦!", ToolTipIcon.Info);
_isNotify = true;
}
e.Cancel = true;
Hide();
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
private void menuExit_Click(object sender, EventArgs e)
{
// 清理托盘图标资源
notifyIcon.Visible = false;
// 关闭应用
Environment.Exit(0);
}
private async void buttonAsk_Click(object sender, EventArgs e)
{
await OnQuestion();
}
private async void buttonSetting_Click(object sender, EventArgs e)
{
//注销热键
GlobalHotkey.UnregisterHotKey(this.Handle, KEY_SHOW);
GlobalHotkey.UnregisterHotKey(this.Handle, KEY_QUESTION);
var form = new SystemSettingForm();
var res = form.ShowDialog();
if (res == DialogResult.OK)
{
SaveSetting();
await InitializeWebViewList();
FormMain_Resize(this, null);
}
RefreshSetting();
form.Dispose();
//删除焦点
this.ActiveControl = null;
}
private void RefreshSetting()
{
var sb = new StringBuilder();
var setting = Program.Setting;
if (setting.ShowHotkey != null)
{
bool registered = GlobalHotkey.RegisterHotKey(this.Handle, KEY_SHOW, (uint)setting.ShowHotkey.Modifiers, (uint)setting.ShowHotkey.Key);
if (!registered)
{
AntdUI.Message.error(this, "显示热键注册失败:" + setting.ShowHotkey, autoClose: 3);
}
else
{
AntdUI.Message.success(this, "显示热键注册成功:" + setting.ShowHotkey, autoClose: 3);
sb.Append("显示:" + setting.ShowHotkey).Append(" ");
}
}
if (setting.AskHotkey != null)
{
bool registered = GlobalHotkey.RegisterHotKey(this.Handle, KEY_QUESTION, (uint)setting.AskHotkey.Modifiers, (uint)setting.AskHotkey.Key);
if (!registered)
{
AntdUI.Message.error(this, "提问热键注册失败:" + setting.AskHotkey, autoClose: 3);
}
else
{
AntdUI.Message.success(this, "提问热键注册成功:" + setting.AskHotkey, autoClose: 3);
sb.Append("提问:" + setting.AskHotkey);
}
}
sb.Append(" QQ交流群:654387244 持续更新");
windowBar.SubText = sb.ToString();
}
private async void buttonRefresh_Click(object sender, EventArgs e)
{
await InitializeWebViewList();
FormMain_Resize(this,null);
}
}
}