From ec121dbb58ebbbccdd39878cc7b15db5ac515c5b Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Mon, 9 Jan 2023 17:20:35 +0800 Subject: [PATCH] perf: update view viewmodel split --- STranslate/View/MainWindow.xaml | 13 +++++++--- STranslate/View/MainWindow.xaml.cs | 41 +++++------------------------- STranslate/ViewModel/MainVM.cs | 19 +++++++++++++- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/STranslate/View/MainWindow.xaml b/STranslate/View/MainWindow.xaml index 9a62b02..e067d01 100644 --- a/STranslate/View/MainWindow.xaml +++ b/STranslate/View/MainWindow.xaml @@ -11,13 +11,10 @@ x:Name="Mwin" Background="Transparent" WindowStartupLocation="CenterScreen" - MouseLeftButtonDown="Window_MouseLeftButtonDown" AllowsTransparency="True" ShowInTaskbar="False" ResizeMode="NoResize" Topmost="True" - Deactivated="Window_Deactivated" - Closing="Mwin_Closing" WindowStyle="None" SizeToContent="Height" Width="400" @@ -52,6 +49,16 @@ Command="{Binding EscCmd}"/> + + + + + + + + + + diff --git a/STranslate/View/MainWindow.xaml.cs b/STranslate/View/MainWindow.xaml.cs index 1f6f4df..64a7d1c 100644 --- a/STranslate/View/MainWindow.xaml.cs +++ b/STranslate/View/MainWindow.xaml.cs @@ -14,15 +14,14 @@ namespace STranslate.View private MainVM vm; public MainWindow() { - InitializeComponent(); + //InitializeComponent(); DataContext = new MainVM(this); vm = (MainVM)DataContext; - //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //{ - // MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); + // MessageBox.Show("全局快捷键有冲突..."); //} } @@ -32,7 +31,10 @@ namespace STranslate.View /// protected override void OnSourceInitialized(EventArgs e) { - //base.OnSourceInitialized(e); + base.OnSourceInitialized(e); + + this.Hide(); + HotkeysHelper.InitialHook(this); HotkeysHelper.Register(HotkeysHelper.InputTranslateId, () => { @@ -54,36 +56,5 @@ namespace STranslate.View vm.OpenMainWin(); }); } - - - /// - /// 移动 - /// - /// - /// - private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) - { - DragMove(); - } - - /// - /// 非激活窗口则隐藏起来 - /// - /// - /// - private void Window_Deactivated(object sender, EventArgs e) - { - if (vm == null) return; - if (!vm.IsTopmost) - { - vm.Speech.SpeakAsyncCancelAll(); - this.Hide(); - } - } - - private void Mwin_Closing(object sender, System.ComponentModel.CancelEventArgs e) - { - HotkeysHelper.UnRegisterHotKey(); - } } } \ No newline at end of file diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 9441fd8..fbfe68f 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -18,7 +18,6 @@ namespace STranslate.ViewModel public MainVM(Window win) { _Mainwin = (MainWindow)win; - OpenMainWin(); if (!ReadConfig()) { @@ -107,6 +106,20 @@ namespace STranslate.ViewModel #endregion #region Common + //移动 + MouseLeftDownCmd = new RelayCommand((_) => true, (_) => + { + _Mainwin.DragMove(); + }); + //失去焦点 + DeactivatedCmd = new RelayCommand((_) => true, (_) => + { + if (!IsTopmost) + { + Speech.SpeakAsyncCancelAll(); + _Mainwin.Hide(); + } + }); //source speak SourceSpeakCmd = new RelayCommand((_) => true, (_) => { @@ -222,6 +235,8 @@ namespace STranslate.ViewModel IsVisibility = false; //语音合成销毁 Speech.Dispose(); + //注销快捷键 + HotkeysHelper.UnRegisterHotKey(); if (id == 0) { //写入配置 @@ -389,6 +404,8 @@ namespace STranslate.ViewModel #region Params private string translateResp; + public ICommand MouseLeftDownCmd { get; private set; } + public ICommand DeactivatedCmd { get; private set; } public ICommand SourceSpeakCmd { get; private set; } public ICommand TargetSpeakCmd { get; private set; } public ICommand TranslateCmd { get; private set; }