Skip to content

Commit a337c8a

Browse files
Refactor multi monitor support
1 parent 975ea3c commit a337c8a

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +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>
198202
public double CustomWindowLeft { get; set; } = 0;
203+
204+
/// <summary>
205+
/// Custom top position on selected monitor
206+
/// </summary>
199207
public double CustomWindowTop { get; set; } = 0;
208+
200209
public int MaxResultsToShow { get; set; } = 5;
201210
public int ActivateTimes { get; set; }
202211

@@ -229,9 +238,15 @@ public bool HideNotifyIcon
229238
}
230239
public bool LeaveCmdOpen { get; set; }
231240
public bool HideWhenDeactivated { get; set; } = true;
232-
241+
242+
[JsonConverter(typeof(JsonStringEnumConverter))]
233243
public SearchWindowScreens SearchWindowScreen { get; set; } = SearchWindowScreens.RememberLastLaunchLocation;
244+
245+
[JsonConverter(typeof(JsonStringEnumConverter))]
234246
public SearchWindowAligns SearchWindowAlign { get; set; } = SearchWindowAligns.Center;
247+
248+
public string CustomScreenDeviceName { get; set; } = string.Empty;
249+
235250
public bool IgnoreHotkeysOnFullscreen { get; set; }
236251

237252
public HttpProxy Proxy { get; set; } = new HttpProxy();
@@ -257,19 +272,22 @@ public enum ColorSchemes
257272
Light,
258273
Dark
259274
}
275+
260276
public enum SearchWindowScreens
261277
{
262278
RememberLastLaunchLocation,
263-
MouseScreen,
264-
PrimaryScreen,
265-
SecondaryScreen,
266-
CustomPosition
279+
Cursor,
280+
Focus,
281+
Primary,
282+
Custom
267283
}
284+
268285
public enum SearchWindowAligns
269286
{
270287
Center,
271288
CenterTop,
272289
LeftTop,
273-
RightTop
290+
RightTop,
291+
Custom
274292
}
275293
}

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
using System.Windows.Data;
2525
using ModernWpf.Controls;
2626
using Key = System.Windows.Input.Key;
27-
using System.Threading;
2827
using System.Media;
29-
28+
using System.Linq;
3029

3130
namespace Flow.Launcher
3231
{
@@ -207,31 +206,31 @@ private void InitializePosition()
207206
Top = _settings.WindowTop;
208207
Left = _settings.WindowLeft;
209208
}
210-
else if (_settings.SearchWindowScreen == SearchWindowScreens.CustomPosition)
211-
{
212-
Top = _settings.CustomWindowTop;
213-
Left = _settings.CustomWindowLeft;
214-
}
215209
else
216210
{
211+
var screen = SelectedScreen();
217212
switch (_settings.SearchWindowAlign)
218213
{
219214
case SearchWindowAligns.Center:
220-
Left = HorizonCenter();
221-
Top = VerticalCenter();
215+
Left = HorizonCenter(screen);
216+
Top = VerticalCenter(screen);
222217
break;
223218
case SearchWindowAligns.CenterTop:
224-
Left = HorizonCenter();
219+
Left = HorizonCenter(screen);
225220
Top = 10;
226221
break;
227222
case SearchWindowAligns.LeftTop:
228-
Left = HorizonLeft();
223+
Left = HorizonLeft(screen);
229224
Top = 10;
230225
break;
231226
case SearchWindowAligns.RightTop:
232-
Left = HorizonRight();
227+
Left = HorizonRight(screen);
233228
Top = 10;
234229
break;
230+
case SearchWindowAligns.Custom:
231+
Left = screen.WorkingArea.Left + _settings.CustomWindowLeft;
232+
Top = screen.WorkingArea.Top + _settings.CustomWindowTop;
233+
break;
235234
}
236235
}
237236

@@ -351,8 +350,9 @@ private async void PositionReset()
351350
{
352351
_viewModel.Show();
353352
await Task.Delay(300); // If don't give a time, Positioning will be weird.
354-
Left = HorizonCenter();
355-
Top = VerticalCenter();
353+
var screen = SelectedScreen();
354+
Left = HorizonCenter(screen);
355+
Top = VerticalCenter(screen);
356356
}
357357

358358
private void InitProgressbarAnimation()
@@ -536,59 +536,56 @@ public void HideStartup()
536536

537537
public Screen SelectedScreen()
538538
{
539-
if (_settings.SearchWindowScreen == SearchWindowScreens.MouseScreen)
540-
{
541-
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
542-
return screen;
543-
}
544-
else if (_settings.SearchWindowScreen == SearchWindowScreens.PrimaryScreen)
545-
{
546-
var screen = Screen.PrimaryScreen;
547-
return screen;
548-
}
549-
else if (_settings.SearchWindowScreen == SearchWindowScreens.SecondaryScreen)
550-
{
551-
var screen = Screen.AllScreens[1];
552-
return screen;
553-
}
554-
else
539+
Screen screen = null;
540+
switch(_settings.SearchWindowScreen)
555541
{
556-
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
557-
return screen;
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+
screen = Screen.AllScreens.FirstOrDefault(s => s.DeviceName == _settings.CustomScreenDeviceName);
554+
break;
555+
default:
556+
screen = Screen.AllScreens[0];
557+
break;
558558
}
559+
return screen ?? Screen.AllScreens[0];
559560
}
560-
public double HorizonCenter()
561+
562+
public double HorizonCenter(Screen screen)
561563
{
562-
var screen = SelectedScreen();
563564
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
564565
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
565566
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
566567
return left;
567568
}
568569

569-
public double VerticalCenter()
570+
public double VerticalCenter(Screen screen)
570571
{
571-
var screen = SelectedScreen();
572572
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
573573
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
574574
var top = (dip2.Y - QueryTextBox.ActualHeight) / 4 + dip1.Y;
575575
return top;
576576
}
577577

578-
public double HorizonRight()
578+
public double HorizonRight(Screen screen)
579579
{
580-
var screen = SelectedScreen();
581580
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
582581
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
583582
var left = (dip1.X + dip2.X - ActualWidth) - 10;
584583
return left;
585584
}
586585

587-
public double HorizonLeft()
586+
public double HorizonLeft(Screen screen)
588587
{
589-
var screen = SelectedScreen();
590588
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
591-
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
592589
var left = dip1.X + 10;
593590
return left;
594591
}

0 commit comments

Comments
 (0)