fix mainwindow position issue #823 #1172

thanks @slav for https://github.com/Wox-launcher/Wox/pull/960
This commit is contained in:
bao-qian
2017-02-23 02:00:39 +00:00
parent 13290efac1
commit f8e400deb9
2 changed files with 31 additions and 20 deletions

View File

@@ -20,7 +20,7 @@
Initialized="OnInitialized" Initialized="OnInitialized"
Closing="OnClosing" Closing="OnClosing"
Drop="OnDrop" Drop="OnDrop"
SizeChanged="OnSizeChanged" LocationChanged="OnLocationChanged"
Deactivated="OnDeactivated" Deactivated="OnDeactivated"
PreviewKeyDown="OnKeyDown" PreviewKeyDown="OnKeyDown"
Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

View File

@@ -36,10 +36,10 @@ namespace Wox
public MainWindow(Settings settings, MainViewModel mainVM) public MainWindow(Settings settings, MainViewModel mainVM)
{ {
InitializeComponent();
DataContext = mainVM; DataContext = mainVM;
_viewModel = mainVM; _viewModel = mainVM;
_settings = settings; _settings = settings;
InitializeComponent();
} }
public MainWindow() public MainWindow()
{ {
@@ -64,6 +64,10 @@ namespace Wox
ThemeManager.Instance.SetBlurForWindow(); ThemeManager.Instance.SetBlurForWindow();
WindowsInteropHelper.DisableControlBox(this); WindowsInteropHelper.DisableControlBox(this);
InitProgressbarAnimation(); InitProgressbarAnimation();
InitializePosition();
// since the default main window visibility is visible
// so we need set focus during startup
QueryTextBox.Focus();
_viewModel.PropertyChanged += (o, e) => _viewModel.PropertyChanged += (o, e) =>
{ {
@@ -73,7 +77,7 @@ namespace Wox
{ {
Activate(); Activate();
QueryTextBox.Focus(); QueryTextBox.Focus();
SetWindowPosition(); UpdatePosition();
_settings.ActivateTimes++; _settings.ActivateTimes++;
if (!_viewModel.LastQuerySelected) if (!_viewModel.LastQuerySelected)
{ {
@@ -83,9 +87,15 @@ namespace Wox
} }
} }
}; };
// since the default main window visibility is visible InitializePosition();
// so we need set focus during startup }
QueryTextBox.Focus();
private void InitializePosition()
{
Top = WindowTop();
Left = WindowLeft();
_settings.WindowTop = Top;
_settings.WindowLeft = Left;
} }
private void InitializeNotifyIcon() private void InitializeNotifyIcon()
@@ -202,28 +212,27 @@ namespace Wox
} }
} }
private bool _startup = true; private void UpdatePosition()
private void SetWindowPosition()
{ {
if (!_settings.RememberLastLaunchLocation && !_startup) if (_settings.RememberLastLaunchLocation)
{
Left = _settings.WindowLeft;
Top = _settings.WindowTop;
}
else
{ {
Left = WindowLeft(); Left = WindowLeft();
Top = WindowTop(); Top = WindowTop();
} }
else
{
_startup = false;
}
} }
/// <summary> private void OnLocationChanged(object sender, EventArgs e)
// used to set correct position on windows first startup
// since the actual width and actual height will be avaiable after this event
/// </summary>
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{ {
Left = WindowLeft(); if (_settings.RememberLastLaunchLocation)
Top = WindowTop(); {
_settings.WindowLeft = Left;
_settings.WindowTop = Top;
}
} }
private double WindowLeft() private double WindowLeft()
@@ -270,5 +279,7 @@ namespace Wox
_viewModel.QueryTextCursorMovedToEnd = false; _viewModel.QueryTextCursorMovedToEnd = false;
} }
} }
} }
} }