From f44109abae8c9976ada9b8b175b4d899076cf6b6 Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Fri, 1 May 2020 14:25:06 -0700 Subject: [PATCH] [launcher] Location and multi monitor support (#2446) * Fixed left and top window * Added dpi Aware launcher positioning code * Code cleanup * Added support to drag window * Multi monitor support added Remaining fix : Launcher doesn't open first time on changing monitor * removed code handling change in DPI manually * Code cleanup * Fix to support multimonitor display * Code cleanup * Revert "Code cleanup" This reverts commit 38f39924f03cabca4071357f9baf0662e1475713. * Revert back to WOX helper for calculating normalized DPI --- .../launcher/PowerLauncher/MainWindow.xaml | 11 +++- .../launcher/PowerLauncher/MainWindow.xaml.cs | 59 +++++++++++++++---- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml b/src/modules/launcher/PowerLauncher/MainWindow.xaml index 9e3b7d8c4a..c11d25e326 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml @@ -23,9 +23,11 @@ LocationChanged="OnLocationChanged" Deactivated="OnDeactivated" Background="Transparent" + Width="720" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" d:DataContext="{d:DesignInstance vm:MainViewModel}"> - + @@ -39,7 +41,9 @@ - @@ -53,7 +57,8 @@ - diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs index 2bbfa0b555..022082107c 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs @@ -23,6 +23,9 @@ using Windows.UI.Xaml; using Windows.UI.Core; using System.Windows.Media; using Windows.UI.Xaml.Data; +using System.Diagnostics; +using Mages.Core.Runtime.Converters; +using System.Runtime.InteropServices; namespace PowerLauncher { @@ -36,6 +39,9 @@ namespace PowerLauncher private MainViewModel _viewModel; private bool _isTextSetProgramatically; const int ROW_HEIGHT = 75; + const int MAX_LIST_HEIGHT = 300; + bool isDPIChanged = false; + #endregion public MainWindow(Settings settings, MainViewModel mainVM) @@ -68,9 +74,9 @@ namespace PowerLauncher private void InitializePosition() { - //Top = WindowTop(); + Top = WindowTop(); Left = WindowLeft(); - //_settings.WindowTop = Top; + _settings.WindowTop = Top; _settings.WindowLeft = Left; } @@ -106,8 +112,16 @@ namespace PowerLauncher { if (_settings.HideWhenDeactive) { - Hide(); - } + if (isDPIChanged) + { + isDPIChanged = false; + InitializePosition(); + } + else + { + Hide(); + } + } } private void UpdatePosition() @@ -119,8 +133,18 @@ namespace PowerLauncher } else { + double prevTop = Top; + double prevLeft = Left; + Top = WindowTop(); Left = WindowLeft(); - //Top = WindowTop(); + if (prevTop != Top || prevLeft != Left) + { + isDPIChanged = true; + } + else + { + isDPIChanged = false; + } } } @@ -133,15 +157,32 @@ namespace PowerLauncher } } + /// + /// Calculates X co-ordinate of main window top left corner. + /// + /// X co-ordinate of main window top left corner private double WindowLeft() { var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); - var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0); - var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0); - var left = (dip2.X - ActualWidth) / 2 + dip1.X; + var dpi1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0); + var dpi2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0); + var left = (dpi2.X - this.Width) / 2 + dpi1.X; return left; } + /// + /// Calculates Y co-ordinate of main window top left corner + /// + /// Y co-ordinate of main window top left corner + private double WindowTop() + { + var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); + var dpi1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y); + var dpi2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height); + var totalHeight = this.SearchBoxBorder.Margin.Top + this.SearchBoxBorder.Margin.Bottom + this.SearchBox.Height + this.ListBoxBorder.Margin.Top + this.ListBoxBorder.Margin.Bottom + MAX_LIST_HEIGHT; + var top = (dpi2.Y - totalHeight) / 4 + dpi1.Y; + return top; + } private PowerLauncher.UI.LauncherControl _launcher = null; private void WindowsXamlHostTextBox_ChildChanged(object sender, EventArgs ev) @@ -184,8 +225,6 @@ namespace PowerLauncher }; } - - private void UserControl_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "SolidBorderBrush")