Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[launcher] Location and multi monitor support #2446

Merged
Merged
11 changes: 8 additions & 3 deletions src/modules/launcher/PowerLauncher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}">
<Grid Width="720">
<Grid Width="720"
MouseDown="OnMouseDown">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
Expand All @@ -39,7 +41,9 @@
<Border.Effect>
<DropShadowEffect BlurRadius="16" Opacity="0.8" ShadowDepth="0" />
</Border.Effect>
<xaml:WindowsXamlHost
<xaml:WindowsXamlHost
Height="60"
x:Name="SearchBox"
InitialTypeName="PowerLauncher.UI.LauncherControl"
ChildChanged="WindowsXamlHostTextBox_ChildChanged" />
</Border>
Expand All @@ -53,7 +57,8 @@
<Border.Effect>
<DropShadowEffect BlurRadius="16" Opacity="0.8" ShadowDepth="0" />
</Border.Effect>
<xaml:WindowsXamlHost
<xaml:WindowsXamlHost
x:Name="ListBox"
InitialTypeName="PowerLauncher.UI.ResultList"
ChildChanged="WindowsXamlHostListView_ChildChanged"
PreviewMouseDown="WindowsXamlHost_PreviewMouseDown" />
Expand Down
59 changes: 49 additions & 10 deletions src/modules/launcher/PowerLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
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
{
Expand All @@ -36,6 +39,9 @@ public partial class MainWindow
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)
Expand Down Expand Up @@ -68,9 +74,9 @@ private void OnLoaded(object sender, System.Windows.RoutedEventArgs _)

private void InitializePosition()
{
//Top = WindowTop();
Top = WindowTop();
Left = WindowLeft();
//_settings.WindowTop = Top;
_settings.WindowTop = Top;
_settings.WindowLeft = Left;
}

Expand Down Expand Up @@ -106,8 +112,16 @@ private void OnDeactivated(object sender, EventArgs e)
{
if (_settings.HideWhenDeactive)
{
Hide();
}
if (isDPIChanged)
{
isDPIChanged = false;
InitializePosition();
}
else
{
Hide();
}
}
}

private void UpdatePosition()
Expand All @@ -119,8 +133,18 @@ private void UpdatePosition()
}
else
{
double prevTop = Top;
double prevLeft = Left;
Top = WindowTop();
Left = WindowLeft();
//Top = WindowTop();
if (prevTop != Top || prevLeft != Left)
{
isDPIChanged = true;
}
else
{
isDPIChanged = false;
}
}
}

Expand All @@ -133,15 +157,32 @@ private void OnLocationChanged(object sender, EventArgs e)
}
}

/// <summary>
/// Calculates X co-ordinate of main window top left corner.
/// </summary>
/// <returns>X co-ordinate of main window top left corner</returns>
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;
}

/// <summary>
/// Calculates Y co-ordinate of main window top left corner
/// </summary>
/// <returns>Y co-ordinate of main window top left corner</returns>
private double WindowTop()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for commenting out of context, does WindowTop() get the window position, and calculates the Y coordinate using dip1 and dip2? Some comments on the function would be helpful to facilitate a walkthrough of the function :) I will run your branch on my side to test that it launches on the top center of the screen :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laviusmotileng-ms Sure! Thanks for suggestion.

{
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)
Expand Down Expand Up @@ -184,8 +225,6 @@ private void WindowsXamlHostTextBox_ChildChanged(object sender, EventArgs ev)
};
}



private void UserControl_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SolidBorderBrush")
Expand Down