diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index ce23705669..f5787f8bf8 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -20,13 +20,10 @@ Closing="OnClosing" Drop="OnDrop" SizeChanged="OnSizeChanged" - IsVisibleChanged="OnMainWindowVisible" Deactivated="OnDeactivated" PreviewKeyDown="OnKeyDown" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" - d:DataContext="{d:DesignInstance vm:MainViewModel}" - FocusManager.FocusedElement="{Binding ElementName=QueryTextBox}" - FocusManager.IsFocusScope="True"> + d:DataContext="{d:DesignInstance vm:MainViewModel}"> @@ -56,10 +53,7 @@ diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index a1f611ca11..a76642328d 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -51,6 +51,29 @@ namespace Wox WindowIntelopHelper.DisableControlBox(this); ThemeManager.Instance.ChangeTheme(_settings.Theme); InitializeNotifyIcon(); + InitProgressbarAnimation(); + + _viewModel.PropertyChanged += (o, e) => + { + if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility)) + { + if (_viewModel.MainWindowVisibility.IsVisible()) + { + Activate(); + QueryTextBox.Focus(); + SetWindowPosition(); + _settings.ActivateTimes++; + if (_viewModel.QueryTextSelected) + { + QueryTextBox.SelectAll(); + _viewModel.QueryTextSelected = false; + } + } + } + }; + // since the default main window visibility is visible + // so we need set focus during startup + QueryTextBox.Focus(); } private void InitializeNotifyIcon() @@ -145,38 +168,6 @@ namespace Wox } } - private void OnQueryVisible(object sender, DependencyPropertyChangedEventArgs e) - { - var visible = (bool)e.NewValue; - if (visible) - { - // the Focusable and the IsVisible both needs to be true when set focus - // logical is set in xaml - QueryTextBox.Focus(); - - if (_viewModel.QueryTextSelected) - { - QueryTextBox.SelectAll(); - _viewModel.QueryTextSelected = false; - } - } - } - - private void OnQueryChanged(object sender, TextChangedEventArgs e) - { - QueryTextBox.CaretIndex = QueryTextBox.Text.Length; - } - - private void OnMainWindowVisible(object sender, DependencyPropertyChangedEventArgs e) - { - var visible = (bool)e.NewValue; - if (visible) - { - SetWindowPosition(); - _settings.ActivateTimes++; - } - } - private bool _startup = true; private void SetWindowPosition() { @@ -191,8 +182,6 @@ namespace Wox } } - - /// // used to set correct position on windows first startup // since the actual width and actual height will be avaiable after this event @@ -201,7 +190,6 @@ namespace Wox { Left = WindowLeft(); Top = WindowTop(); - InitProgressbarAnimation(); } private double WindowLeft() @@ -239,7 +227,5 @@ namespace Wox e.Handled = true; } } - - } } \ No newline at end of file