From 576c375cfeaa06811c38c09aafe5816058f081a4 Mon Sep 17 00:00:00 2001 From: happlebao Date: Thu, 23 Feb 2017 02:00:39 +0000 Subject: [PATCH] fix mainwindow position issue #823 #1172 thanks @slav for https://github.com/Wox-launcher/Wox/pull/960 --- Wox/MainWindow.xaml | 2 +- Wox/MainWindow.xaml.cs | 47 ++++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index 5106b8b9b..9c221f5df 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -20,7 +20,7 @@ Initialized="OnInitialized" Closing="OnClosing" Drop="OnDrop" - SizeChanged="OnSizeChanged" + LocationChanged="OnLocationChanged" Deactivated="OnDeactivated" PreviewKeyDown="OnKeyDown" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 62876d32f..367d84c4e 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -36,10 +36,10 @@ public partial class MainWindow public MainWindow(Settings settings, MainViewModel mainVM) { - InitializeComponent(); DataContext = mainVM; _viewModel = mainVM; _settings = settings; + InitializeComponent(); } public MainWindow() { @@ -64,6 +64,10 @@ private void OnLoaded(object sender, RoutedEventArgs _) ThemeManager.Instance.SetBlurForWindow(); WindowsInteropHelper.DisableControlBox(this); InitProgressbarAnimation(); + InitializePosition(); + // since the default main window visibility is visible + // so we need set focus during startup + QueryTextBox.Focus(); _viewModel.PropertyChanged += (o, e) => { @@ -73,7 +77,7 @@ private void OnLoaded(object sender, RoutedEventArgs _) { Activate(); QueryTextBox.Focus(); - SetWindowPosition(); + UpdatePosition(); _settings.ActivateTimes++; if (!_viewModel.LastQuerySelected) { @@ -83,9 +87,15 @@ private void OnLoaded(object sender, RoutedEventArgs _) } } }; - // since the default main window visibility is visible - // so we need set focus during startup - QueryTextBox.Focus(); + InitializePosition(); + } + + private void InitializePosition() + { + Top = WindowTop(); + Left = WindowLeft(); + _settings.WindowTop = Top; + _settings.WindowLeft = Left; } private void InitializeNotifyIcon() @@ -202,28 +212,27 @@ private void OnDeactivated(object sender, EventArgs e) } } - private bool _startup = true; - private void SetWindowPosition() + private void UpdatePosition() { - if (!_settings.RememberLastLaunchLocation && !_startup) + if (_settings.RememberLastLaunchLocation) { - Left = WindowLeft(); - Top = WindowTop(); + Left = _settings.WindowLeft; + Top = _settings.WindowTop; } else { - _startup = false; + Left = WindowLeft(); + Top = WindowTop(); } } - /// - // used to set correct position on windows first startup - // since the actual width and actual height will be avaiable after this event - /// - private void OnSizeChanged(object sender, SizeChangedEventArgs e) + private void OnLocationChanged(object sender, EventArgs e) { - Left = WindowLeft(); - Top = WindowTop(); + if (_settings.RememberLastLaunchLocation) + { + _settings.WindowLeft = Left; + _settings.WindowTop = Top; + } } private double WindowLeft() @@ -270,5 +279,7 @@ private void OnTextChanged(object sender, TextChangedEventArgs e) _viewModel.QueryTextCursorMovedToEnd = false; } } + + } } \ No newline at end of file