perf: update view viewmodel split

dev_ocr_translate
DESKTOP-3BO4HSG\ksat 2 years ago
parent 7a4aa1deb6
commit ec121dbb58

@ -11,13 +11,10 @@
x:Name="Mwin" x:Name="Mwin"
Background="Transparent" Background="Transparent"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
AllowsTransparency="True" AllowsTransparency="True"
ShowInTaskbar="False" ShowInTaskbar="False"
ResizeMode="NoResize" ResizeMode="NoResize"
Topmost="True" Topmost="True"
Deactivated="Window_Deactivated"
Closing="Mwin_Closing"
WindowStyle="None" WindowStyle="None"
SizeToContent="Height" SizeToContent="Height"
Width="400" Width="400"
@ -52,6 +49,16 @@
Command="{Binding EscCmd}"/> Command="{Binding EscCmd}"/>
</Window.InputBindings> </Window.InputBindings>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding MouseLeftDownCmd}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Deactivated">
<i:InvokeCommandAction Command="{Binding DeactivatedCmd}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Border Style="{DynamicResource BorderMainStyle}"> <Border Style="{DynamicResource BorderMainStyle}">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>

@ -14,15 +14,14 @@ namespace STranslate.View
private MainVM vm; private MainVM vm;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); //InitializeComponent();
DataContext = new MainVM(this); DataContext = new MainVM(this);
vm = (MainVM)DataContext; vm = (MainVM)DataContext;
//if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict)
//{ //{
// MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); // MessageBox.Show("全局快捷键有冲突...");
//} //}
} }
@ -32,7 +31,10 @@ namespace STranslate.View
/// <param name="e"></param> /// <param name="e"></param>
protected override void OnSourceInitialized(EventArgs e) protected override void OnSourceInitialized(EventArgs e)
{ {
//base.OnSourceInitialized(e); base.OnSourceInitialized(e);
this.Hide();
HotkeysHelper.InitialHook(this); HotkeysHelper.InitialHook(this);
HotkeysHelper.Register(HotkeysHelper.InputTranslateId, () => HotkeysHelper.Register(HotkeysHelper.InputTranslateId, () =>
{ {
@ -54,36 +56,5 @@ namespace STranslate.View
vm.OpenMainWin(); vm.OpenMainWin();
}); });
} }
/// <summary>
/// 移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
/// <summary>
/// 非激活窗口则隐藏起来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
} }
} }

@ -18,7 +18,6 @@ namespace STranslate.ViewModel
public MainVM(Window win) public MainVM(Window win)
{ {
_Mainwin = (MainWindow)win; _Mainwin = (MainWindow)win;
OpenMainWin();
if (!ReadConfig()) if (!ReadConfig())
{ {
@ -107,6 +106,20 @@ namespace STranslate.ViewModel
#endregion #endregion
#region Common #region Common
//移动
MouseLeftDownCmd = new RelayCommand((_) => true, (_) =>
{
_Mainwin.DragMove();
});
//失去焦点
DeactivatedCmd = new RelayCommand((_) => true, (_) =>
{
if (!IsTopmost)
{
Speech.SpeakAsyncCancelAll();
_Mainwin.Hide();
}
});
//source speak //source speak
SourceSpeakCmd = new RelayCommand((_) => true, (_) => SourceSpeakCmd = new RelayCommand((_) => true, (_) =>
{ {
@ -222,6 +235,8 @@ namespace STranslate.ViewModel
IsVisibility = false; IsVisibility = false;
//语音合成销毁 //语音合成销毁
Speech.Dispose(); Speech.Dispose();
//注销快捷键
HotkeysHelper.UnRegisterHotKey();
if (id == 0) if (id == 0)
{ {
//写入配置 //写入配置
@ -389,6 +404,8 @@ namespace STranslate.ViewModel
#region Params #region Params
private string translateResp; private string translateResp;
public ICommand MouseLeftDownCmd { get; private set; }
public ICommand DeactivatedCmd { get; private set; }
public ICommand SourceSpeakCmd { get; private set; } public ICommand SourceSpeakCmd { get; private set; }
public ICommand TargetSpeakCmd { get; private set; } public ICommand TargetSpeakCmd { get; private set; }
public ICommand TranslateCmd { get; private set; } public ICommand TranslateCmd { get; private set; }

Loading…
Cancel
Save