Skip to content

Commit

Permalink
fix mainwindow position issue #823 #1172
Browse files Browse the repository at this point in the history
thanks @slav for #960
  • Loading branch information
bao-qian committed Feb 23, 2017
1 parent a044765 commit 576c375
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Wox/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
47 changes: 29 additions & 18 deletions Wox/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public partial class MainWindow

public MainWindow(Settings settings, MainViewModel mainVM)
{
InitializeComponent();
DataContext = mainVM;
_viewModel = mainVM;
_settings = settings;
InitializeComponent();
}
public MainWindow()
{
Expand All @@ -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) =>
{
Expand All @@ -73,7 +77,7 @@ private void OnLoaded(object sender, RoutedEventArgs _)
{
Activate();
QueryTextBox.Focus();
SetWindowPosition();
UpdatePosition();
_settings.ActivateTimes++;
if (!_viewModel.LastQuerySelected)
{
Expand All @@ -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()
Expand Down Expand Up @@ -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();
}
}

/// <summary>
// 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)
private void OnLocationChanged(object sender, EventArgs e)
{
Left = WindowLeft();
Top = WindowTop();
if (_settings.RememberLastLaunchLocation)
{
_settings.WindowLeft = Left;
_settings.WindowTop = Top;
}
}

private double WindowLeft()
Expand Down Expand Up @@ -270,5 +279,7 @@ private void OnTextChanged(object sender, TextChangedEventArgs e)
_viewModel.QueryTextCursorMovedToEnd = false;
}
}


}
}

0 comments on commit 576c375

Please sign in to comment.