Skip to content

Commit 109796c

Browse files
authored
Merge pull request #1978 from onesounds/PrimaryMonitor
2 parents 2c7c6fd + 4f0ea01 commit 109796c

File tree

6 files changed

+306
-77
lines changed

6 files changed

+306
-77
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ public string QuerySearchPrecisionString
195195

196196
public double WindowLeft { get; set; }
197197
public double WindowTop { get; set; }
198+
199+
/// <summary>
200+
/// Custom left position on selected monitor
201+
/// </summary>
202+
public double CustomWindowLeft { get; set; } = 0;
203+
204+
/// <summary>
205+
/// Custom top position on selected monitor
206+
/// </summary>
207+
public double CustomWindowTop { get; set; } = 0;
208+
198209
public int MaxResultsToShow { get; set; } = 5;
199210
public int ActivateTimes { get; set; }
200211

@@ -227,7 +238,15 @@ public bool HideNotifyIcon
227238
}
228239
public bool LeaveCmdOpen { get; set; }
229240
public bool HideWhenDeactivated { get; set; } = true;
230-
public SearchWindowPositions SearchWindowPosition { get; set; } = SearchWindowPositions.MouseScreenCenter;
241+
242+
[JsonConverter(typeof(JsonStringEnumConverter))]
243+
public SearchWindowScreens SearchWindowScreen { get; set; } = SearchWindowScreens.RememberLastLaunchLocation;
244+
245+
[JsonConverter(typeof(JsonStringEnumConverter))]
246+
public SearchWindowAligns SearchWindowAlign { get; set; } = SearchWindowAligns.Center;
247+
248+
public int CustomScreenNumber { get; set; } = 1;
249+
231250
public bool IgnoreHotkeysOnFullscreen { get; set; }
232251

233252
public HttpProxy Proxy { get; set; } = new HttpProxy();
@@ -253,12 +272,22 @@ public enum ColorSchemes
253272
Light,
254273
Dark
255274
}
256-
public enum SearchWindowPositions
275+
276+
public enum SearchWindowScreens
257277
{
258278
RememberLastLaunchLocation,
259-
MouseScreenCenter,
260-
MouseScreenCenterTop,
261-
MouseScreenLeftTop,
262-
MouseScreenRightTop
279+
Cursor,
280+
Focus,
281+
Primary,
282+
Custom
283+
}
284+
285+
public enum SearchWindowAligns
286+
{
287+
Center,
288+
CenterTop,
289+
LeftTop,
290+
RightTop,
291+
Custom
263292
}
264293
}

Flow.Launcher/Helper/WindowsInteropHelper.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,25 @@ private static IntPtr HWND_DESKTOP
3535
}
3636

3737
[DllImport("user32.dll", SetLastError = true)]
38-
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
39-
38+
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
39+
4040
[DllImport("user32.dll")]
41-
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
41+
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
4242

4343
[DllImport("user32.dll")]
44-
private static extern IntPtr GetForegroundWindow();
44+
internal static extern IntPtr GetForegroundWindow();
4545

4646
[DllImport("user32.dll")]
47-
private static extern IntPtr GetDesktopWindow();
47+
internal static extern IntPtr GetDesktopWindow();
4848

4949
[DllImport("user32.dll")]
50-
private static extern IntPtr GetShellWindow();
50+
internal static extern IntPtr GetShellWindow();
5151

5252
[DllImport("user32.dll", SetLastError = true)]
53-
private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
53+
internal static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
5454

5555
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
56-
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
56+
internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
5757

5858
[DllImport("user32.DLL")]
5959
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

Flow.Launcher/Languages/en.xaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@
3838
<system:String x:Key="hideFlowLauncherWhenLoseFocus">Hide Flow Launcher when focus is lost</system:String>
3939
<system:String x:Key="dontPromptUpdateMsg">Do not show new version notifications</system:String>
4040
<system:String x:Key="SearchWindowPosition">Search Window Position</system:String>
41-
<system:String x:Key="SearchWindowPositionRememberLastLaunchLocation">Remember Last Position</system:String>
42-
<system:String x:Key="SearchWindowPositionMouseScreenCenter">Mouse Focused Screen - Center</system:String>
43-
<system:String x:Key="SearchWindowPositionMouseScreenCenterTop">Mouse Focused Screen - Center Top</system:String>
44-
<system:String x:Key="SearchWindowPositionMouseScreenLeftTop">Mouse Focused Screen - Left Top</system:String>
45-
<system:String x:Key="SearchWindowPositionMouseScreenRightTop">Mouse Focused Screen - Right Top</system:String>
41+
<system:String x:Key="SearchWindowScreenRememberLastLaunchLocation">Remember Last Position</system:String>
42+
<system:String x:Key="SearchWindowScreenCursor">Monitor with Mouse Cursor</system:String>
43+
<system:String x:Key="SearchWindowScreenFocus">Monitor with Focused Window</system:String>
44+
<system:String x:Key="SearchWindowScreenPrimary">Primary Monitor</system:String>
45+
<system:String x:Key="SearchWindowScreenCustom">Custom Monitor</system:String>
46+
<system:String x:Key="SearchWindowAlignCenter">Center</system:String>
47+
<system:String x:Key="SearchWindowAlignCenterTop">Center Top</system:String>
48+
<system:String x:Key="SearchWindowAlignLeftTop">Left Top</system:String>
49+
<system:String x:Key="SearchWindowAlignRightTop">Right Top</system:String>
50+
<system:String x:Key="SearchWindowAlignCustom">Custom Position</system:String>
4651
<system:String x:Key="language">Language</system:String>
4752
<system:String x:Key="lastQueryMode">Last Query Style</system:String>
4853
<system:String x:Key="lastQueryModeToolTip">Show/Hide previous results when Flow Launcher is reactivated.</system:String>

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using ModernWpf.Controls;
2626
using Key = System.Windows.Input.Key;
2727
using System.Media;
28+
using System.Linq;
2829

2930
namespace Flow.Launcher
3031
{
@@ -200,29 +201,39 @@ private void OnLoaded(object sender, RoutedEventArgs _)
200201

201202
private void InitializePosition()
202203
{
203-
switch (_settings.SearchWindowPosition)
204+
if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation)
204205
{
205-
case SearchWindowPositions.RememberLastLaunchLocation:
206-
Top = _settings.WindowTop;
207-
Left = _settings.WindowLeft;
208-
break;
209-
case SearchWindowPositions.MouseScreenCenter:
210-
Left = HorizonCenter();
211-
Top = VerticalCenter();
212-
break;
213-
case SearchWindowPositions.MouseScreenCenterTop:
214-
Left = HorizonCenter();
215-
Top = 10;
216-
break;
217-
case SearchWindowPositions.MouseScreenLeftTop:
218-
Left = 10;
219-
Top = 10;
220-
break;
221-
case SearchWindowPositions.MouseScreenRightTop:
222-
Left = HorizonRight();
223-
Top = 10;
224-
break;
206+
Top = _settings.WindowTop;
207+
Left = _settings.WindowLeft;
208+
}
209+
else
210+
{
211+
var screen = SelectedScreen();
212+
switch (_settings.SearchWindowAlign)
213+
{
214+
case SearchWindowAligns.Center:
215+
Left = HorizonCenter(screen);
216+
Top = VerticalCenter(screen);
217+
break;
218+
case SearchWindowAligns.CenterTop:
219+
Left = HorizonCenter(screen);
220+
Top = 10;
221+
break;
222+
case SearchWindowAligns.LeftTop:
223+
Left = HorizonLeft(screen);
224+
Top = 10;
225+
break;
226+
case SearchWindowAligns.RightTop:
227+
Left = HorizonRight(screen);
228+
Top = 10;
229+
break;
230+
case SearchWindowAligns.Custom:
231+
Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X;
232+
Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y;
233+
break;
234+
}
225235
}
236+
226237
}
227238

228239
private void UpdateNotifyIconText()
@@ -339,8 +350,9 @@ private async void PositionReset()
339350
{
340351
_viewModel.Show();
341352
await Task.Delay(300); // If don't give a time, Positioning will be weird.
342-
Left = HorizonCenter();
343-
Top = VerticalCenter();
353+
var screen = SelectedScreen();
354+
Left = HorizonCenter(screen);
355+
Top = VerticalCenter(screen);
344356
}
345357

346358
private void InitProgressbarAnimation()
@@ -502,7 +514,7 @@ private void OnLocationChanged(object sender, EventArgs e)
502514
{
503515
if (_animating)
504516
return;
505-
if (_settings.SearchWindowPosition == SearchWindowPositions.RememberLastLaunchLocation)
517+
if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation)
506518
{
507519
_settings.WindowLeft = Left;
508520
_settings.WindowTop = Top;
@@ -522,30 +534,62 @@ public void HideStartup()
522534
}
523535
}
524536

525-
public double HorizonCenter()
537+
public Screen SelectedScreen()
538+
{
539+
Screen screen = null;
540+
switch(_settings.SearchWindowScreen)
541+
{
542+
case SearchWindowScreens.Cursor:
543+
screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
544+
break;
545+
case SearchWindowScreens.Primary:
546+
screen = Screen.PrimaryScreen;
547+
break;
548+
case SearchWindowScreens.Focus:
549+
IntPtr foregroundWindowHandle = WindowsInteropHelper.GetForegroundWindow();
550+
screen = Screen.FromHandle(foregroundWindowHandle);
551+
break;
552+
case SearchWindowScreens.Custom:
553+
if (_settings.CustomScreenNumber <= Screen.AllScreens.Length)
554+
screen = Screen.AllScreens[_settings.CustomScreenNumber - 1];
555+
else
556+
screen = Screen.AllScreens[0];
557+
break;
558+
default:
559+
screen = Screen.AllScreens[0];
560+
break;
561+
}
562+
return screen ?? Screen.AllScreens[0];
563+
}
564+
565+
public double HorizonCenter(Screen screen)
526566
{
527-
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
528567
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
529568
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
530569
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
531570
return left;
532571
}
533572

534-
public double VerticalCenter()
573+
public double VerticalCenter(Screen screen)
535574
{
536-
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
537575
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
538576
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
539577
var top = (dip2.Y - QueryTextBox.ActualHeight) / 4 + dip1.Y;
540578
return top;
541579
}
542580

543-
public double HorizonRight()
581+
public double HorizonRight(Screen screen)
544582
{
545-
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
546583
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
547584
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
548-
var left = (dip2.X - ActualWidth) - 10;
585+
var left = (dip1.X + dip2.X - ActualWidth) - 10;
586+
return left;
587+
}
588+
589+
public double HorizonLeft(Screen screen)
590+
{
591+
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
592+
var left = dip1.X + 10;
549593
return left;
550594
}
551595

0 commit comments

Comments
 (0)