diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 9e18f40..c1ffdc5 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -51,7 +51,7 @@ Visibility="{Binding IsVisibility, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipText="STranslate" ContextMenu="{StaticResource TrayMenu}" - LeftClickCommand="{Binding ShowMainWinCmd}" + DoubleClickCommand="{Binding InputTranslateCmd}" IconSource="/Images/translate.ico"> diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index e27efc0..36f642e 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -21,70 +21,12 @@ namespace STranslate DataContext = new MainVM(this); vm = (MainVM)DataContext; - InitView(); - - //InitialTray(); - //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //{ // MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); //} } - private void InitialTray() - { - var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); - notifyIcon.Text = $"STranslate_{version}\n" + - $"划词翻译快捷键: ALT + D\n" + - $"输入翻译快捷键: ALT + A\n"; - //notifyIcon.BalloonTipText = "STranslate 已启动"; - notifyIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative)).Stream); - notifyIcon.Visible = true; - //notifyIcon.ShowBalloonTip(1000); - - notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick; - - System.Windows.Forms.MenuItem CrossWordTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("划词翻译"); - CrossWordTranslateMenuItemBTN.Click += new EventHandler(CrossWordTranslateMenuItem_Click); - //当失去焦点后无法从托盘处获取选中文本 - CrossWordTranslateMenuItemBTN.Visible = false; - - System.Windows.Forms.MenuItem ScreenshotTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("截图翻译"); - ScreenshotTranslateMenuItemBTN.Click += new EventHandler(ScreenshotTranslateMenuItem_Click); - ScreenshotTranslateMenuItemBTN.Enabled = false; - - System.Windows.Forms.MenuItem InputTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("输入翻译"); - InputTranslateMenuItemBTN.Click += new EventHandler(InputTranslateMenuItem_Click); - - System.Windows.Forms.MenuItem OpenMainWinBTN = new System.Windows.Forms.MenuItem("显示主界面"); - OpenMainWinBTN.Click += new EventHandler(OpenMainWin_Click); - - System.Windows.Forms.MenuItem AutoStartBTN = new System.Windows.Forms.MenuItem("开机自启"); - AutoStartBTN.Click += new EventHandler(AutoStart_Click); - - AutoStartBTN.Checked = StartupHelper.IsStartup(); - - System.Windows.Forms.MenuItem ExitBTN = new System.Windows.Forms.MenuItem("退出"); - ExitBTN.Click += new EventHandler(Exit_Click); - - System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { - CrossWordTranslateMenuItemBTN, - ScreenshotTranslateMenuItemBTN, - InputTranslateMenuItemBTN, - OpenMainWinBTN, - AutoStartBTN, - ExitBTN, - }; - notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); - } - - private void AutoStart_Click(object sender, EventArgs e) - { - if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup(); - else StartupHelper.SetStartup(); - (sender as System.Windows.Forms.MenuItem).Checked = StartupHelper.IsStartup(); - } - /// /// 移动 @@ -127,7 +69,7 @@ namespace STranslate && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift) && e.Key == Key.Q) { - Exit_Click(null, null); + vm.ExitApp(); } } @@ -147,7 +89,7 @@ namespace STranslate HotkeysHelper.Register(HotkeysHelper.CrosswordTranslateId, () => { - CrossWordTranslateMenuItem_Click(null, null); + vm.CrossWordTranslate(); }); //HotkeysHelper.Register(HotkeysHelper.ScreenShotTranslateId, () => @@ -157,7 +99,7 @@ namespace STranslate HotkeysHelper.Register(HotkeysHelper.OpenMainWindowId, () => { - OpenMainWin_Click(null, null); + vm.OpenMainWin(); }); } @@ -175,78 +117,6 @@ namespace STranslate } } - /// - /// 清空输入输出框 - /// - private void ClearTextBox() - { - vm.InputTxt = string.Empty; - vm.OutputTxt = string.Empty; - vm.SnakeRet = string.Empty; - vm.SmallHumpRet = string.Empty; - vm.LargeHumpRet = string.Empty; - vm.IdentifyLanguage = string.Empty; - } - - /// - /// 显示主窗口 - /// - /// - /// - private void OpenMainWin_Click(object sender, EventArgs e) - { - this.Show(); - this.Activate(); - } - - - /// - /// 左键双击 - /// - /// - /// - private void NotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e) - { - InputTranslateMenuItem_Click(null, null); - } - - /// - /// 输入翻译 - /// - /// - /// - private void InputTranslateMenuItem_Click(object sender, EventArgs e) - { - ClearTextBox(); - OpenMainWin_Click(null, null); - TextBoxInput.Focus(); - } - - /// - /// 划词翻译 - /// - /// - /// - private void CrossWordTranslateMenuItem_Click(object sender, EventArgs e) - { - ClearTextBox(); - var sentence = GetWordsHelper.Get(); - this.Show(); - this.Activate(); - this.TextBoxInput.Text = sentence.Trim(); - _ = vm.Translate(); - } - - /// - /// 截图翻译 - /// - /// - /// - private void ScreenshotTranslateMenuItem_Click(object sender, EventArgs e) - { - MessageBox.Show("开发中"); - } - /// /// 是否置顶(不好拆) /// @@ -264,23 +134,6 @@ namespace STranslate } _IsTopmost = !_IsTopmost; } - /// - /// 退出 - /// - /// - /// - private void Exit_Click(object sender, EventArgs e) - { - vm.Dispose(); - notifyIcon.Dispose(); - Environment.Exit(0); - } - private void InitView() - { - this.Activate(); - this.TextBoxInput.Focus(); - } - private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon(); private bool _IsTopmost { get; set; } private readonly string _TopmostTemplateName = "ButtonTemplateTopmost"; private readonly string _UnTopmostTemplateName = "ButtonTemplateUnTopmost"; diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 9c15890..c75671b 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -35,6 +35,7 @@ namespace STranslate.ViewModel #region 托盘程序 //运行前检查是否开机自启 IsStartup = StartupHelper.IsStartup(); + //输入翻译 InputTranslateCmd = new RelayCommand((_) => true, (_) => { @@ -46,6 +47,7 @@ namespace STranslate.ViewModel { OpenMainWin(); }); + //开机自启 StartupCmd = new RelayCommand((_) => true, (_) => { @@ -57,9 +59,7 @@ namespace STranslate.ViewModel //退出App ExitCmd = new RelayCommand((_) => true, (_) => { - IsVisibility = false; - Dispose(); - Environment.Exit(0); + ExitApp(); }); #endregion @@ -150,7 +150,18 @@ namespace STranslate.ViewModel } public void CrossWordTranslate() { - + ClearAll(); + var sentence = GetWordsHelper.Get(); + OpenMainWin(); + //TODO: add textbox focus + //this.TextBoxInput.Text = sentence.Trim(); + _ = Translate(); + } + public void ExitApp() + { + IsVisibility = false; + Dispose(); + Environment.Exit(0); } ///