弹窗显示翻译结果
Browse files
src/WpfEditor/TextEditorControl.cs
CHANGED
|
@@ -314,6 +314,14 @@ namespace WpfEditor
|
|
| 314 |
};
|
| 315 |
ContextMenu.Items.Add(selectAllMenuItem);
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
// 设置菜单打开前的事件处理,用于控制菜单项的可用性
|
| 318 |
ContextMenu.Opened += OnContextMenuOpened;
|
| 319 |
}
|
|
@@ -347,6 +355,25 @@ namespace WpfEditor
|
|
| 347 |
}
|
| 348 |
}
|
| 349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
#region 属性
|
| 351 |
|
| 352 |
public string Text
|
|
|
|
| 314 |
};
|
| 315 |
ContextMenu.Items.Add(selectAllMenuItem);
|
| 316 |
|
| 317 |
+
// 翻译菜单项
|
| 318 |
+
MenuItem translateMenuItem = new MenuItem
|
| 319 |
+
{
|
| 320 |
+
Header = "翻译"
|
| 321 |
+
};
|
| 322 |
+
translateMenuItem.Click += OnTranslateMenuItemClick;
|
| 323 |
+
ContextMenu.Items.Add(translateMenuItem);
|
| 324 |
+
|
| 325 |
// 设置菜单打开前的事件处理,用于控制菜单项的可用性
|
| 326 |
ContextMenu.Opened += OnContextMenuOpened;
|
| 327 |
}
|
|
|
|
| 355 |
}
|
| 356 |
}
|
| 357 |
|
| 358 |
+
// "翻译"菜单项点击事件处理
|
| 359 |
+
private async void OnTranslateMenuItemClick(object sender, RoutedEventArgs e)
|
| 360 |
+
{
|
| 361 |
+
|
| 362 |
+
if (HasSelection()) {
|
| 363 |
+
string text = GetSelectedText();
|
| 364 |
+
string res = await DeepSeek.translate(text);
|
| 365 |
+
if (res != "")
|
| 366 |
+
{
|
| 367 |
+
JObject jsn = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(res);
|
| 368 |
+
string zh = jsn["choices"][0]["message"]["content"].Value<string>();
|
| 369 |
+
var win = new TranslationResultWindow(zh);
|
| 370 |
+
win.Owner = Window.GetWindow(this);
|
| 371 |
+
win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
| 372 |
+
win.ShowDialog();
|
| 373 |
+
}
|
| 374 |
+
}
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
#region 属性
|
| 378 |
|
| 379 |
public string Text
|
src/WpfEditor/TranslationResultWindow.xaml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Window x:Class="WpfEditor.TranslationResultWindow"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
Title="翻译结果" Height="300" Width="500">
|
| 5 |
+
<Grid>
|
| 6 |
+
<TextBox x:Name="ResultTextBox"
|
| 7 |
+
Margin="16"
|
| 8 |
+
FontSize="16"
|
| 9 |
+
TextWrapping="Wrap"
|
| 10 |
+
VerticalScrollBarVisibility="Auto"
|
| 11 |
+
HorizontalScrollBarVisibility="Auto"
|
| 12 |
+
IsReadOnly="True"
|
| 13 |
+
Background="#FAFAFA"
|
| 14 |
+
BorderThickness="0"
|
| 15 |
+
Foreground="#333"/>
|
| 16 |
+
</Grid>
|
| 17 |
+
</Window>
|
src/WpfEditor/TranslationResultWindow.xaml.cs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows;
|
| 2 |
+
|
| 3 |
+
namespace WpfEditor
|
| 4 |
+
{
|
| 5 |
+
public partial class TranslationResultWindow : Window
|
| 6 |
+
{
|
| 7 |
+
public TranslationResultWindow(string result)
|
| 8 |
+
{
|
| 9 |
+
InitializeComponent();
|
| 10 |
+
ResultTextBox.Text = result;
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
}
|