Skip to content

Commit 3680440

Browse files
authored
Merge pull request #3652 from Flow-Launcher/pinvoke_improvement
Use PInvoke & Improve code comments & Use switch sentences
2 parents 9be5938 + 5fcb166 commit 3680440

File tree

3 files changed

+74
-66
lines changed

3 files changed

+74
-66
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ MONITORINFOEXW
4242

4343
WM_ENTERSIZEMOVE
4444
WM_EXITSIZEMOVE
45+
WM_NCLBUTTONDBLCLK
46+
WM_SYSCOMMAND
47+
48+
SC_MAXIMIZE
49+
SC_MINIMIZE
4550

4651
OleInitialize
4752
OleUninitialize

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
324324

325325
public const int WM_ENTERSIZEMOVE = (int)PInvoke.WM_ENTERSIZEMOVE;
326326
public const int WM_EXITSIZEMOVE = (int)PInvoke.WM_EXITSIZEMOVE;
327+
public const int WM_NCLBUTTONDBLCLK = (int)PInvoke.WM_NCLBUTTONDBLCLK;
328+
public const int WM_SYSCOMMAND = (int)PInvoke.WM_SYSCOMMAND;
329+
330+
public const int SC_MAXIMIZE = (int)PInvoke.SC_MAXIMIZE;
331+
public const int SC_MINIMIZE = (int)PInvoke.SC_MINIMIZE;
327332

328333
#endregion
329334

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,18 @@ private void OnPreviewMouseMove(object sender, MouseEventArgs e)
465465

466466
private void OnMouseDown(object sender, MouseButtonEventArgs e)
467467
{
468+
// When the window is maximized via Snap,
469+
// dragging attempts will first switch the window from Maximized to Normal state,
470+
// and adjust the drag position accordingly.
468471
if (e.ChangedButton == MouseButton.Left)
469472
{
470473
try
471474
{
472475
if (WindowState == WindowState.Maximized)
473476
{
474477
// Calculate ratio based on maximized window dimensions
475-
double maxWidth = this.ActualWidth;
476-
double maxHeight = this.ActualHeight;
478+
double maxWidth = ActualWidth;
479+
double maxHeight = ActualHeight;
477480
var mousePos = e.GetPosition(this);
478481
double xRatio = mousePos.X / maxWidth;
479482
double yRatio = mousePos.Y / maxHeight;
@@ -486,7 +489,7 @@ private void OnMouseDown(object sender, MouseButtonEventArgs e)
486489
// Switch to Normal state
487490
WindowState = WindowState.Normal;
488491

489-
Dispatcher.BeginInvoke(new Action(() =>
492+
Application.Current?.Dispatcher.Invoke(new Action(() =>
490493
{
491494
double normalWidth = Width;
492495
double normalHeight = Height;
@@ -535,83 +538,78 @@ private async void OnContextMenusForSettingsClick(object sender, RoutedEventArgs
535538

536539
#region Window WndProc
537540

538-
private const int WM_NCLBUTTONDBLCLK = 0x00A3;
539-
private const int WM_SYSCOMMAND = 0x0112;
540-
private const int SC_MAXIMIZE = 0xF030;
541-
private const int SC_RESTORE = 0xF120;
542-
private const int SC_MINIMIZE = 0xF020;
543541
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
544-
{
545-
if (msg == Win32Helper.WM_ENTERSIZEMOVE)
546-
{
547-
_initialWidth = (int)Width;
548-
_initialHeight = (int)Height;
549-
handled = true;
550-
}
551-
else if (msg == Win32Helper.WM_EXITSIZEMOVE)
542+
{
543+
switch (msg)
552544
{
553-
//Prevent updating the number of results when the window height is below the height of a single result item.
554-
//This situation occurs not only when the user manually resizes the window, but also when the window is released from a side snap, as the OS automatically adjusts the window height.
555-
//(Without this check, releasing from a snap can cause the window height to hit the minimum, resulting in only 2 results being shown.)
556-
if (_initialHeight != (int)Height && Height> (_settings.WindowHeightSize + _settings.ItemHeightSize))
557-
{
558-
if (!_settings.KeepMaxResults)
545+
case Win32Helper.WM_ENTERSIZEMOVE:
546+
_initialWidth = (int)Width;
547+
_initialHeight = (int)Height;
548+
handled = true;
549+
break;
550+
case Win32Helper.WM_EXITSIZEMOVE:
551+
//Prevent updating the number of results when the window height is below the height of a single result item.
552+
//This situation occurs not only when the user manually resizes the window, but also when the window is released from a side snap, as the OS automatically adjusts the window height.
553+
//(Without this check, releasing from a snap can cause the window height to hit the minimum, resulting in only 2 results being shown.)
554+
if (_initialHeight != (int)Height && Height > (_settings.WindowHeightSize + _settings.ItemHeightSize))
559555
{
560-
// Get shadow margin
561-
var shadowMargin = 0;
562-
var (_, useDropShadowEffect) = _theme.GetActualValue();
563-
if (useDropShadowEffect)
556+
if (!_settings.KeepMaxResults)
564557
{
565-
shadowMargin = 32;
566-
}
558+
// Get shadow margin
559+
var shadowMargin = 0;
560+
var (_, useDropShadowEffect) = _theme.GetActualValue();
561+
if (useDropShadowEffect)
562+
{
563+
shadowMargin = 32;
564+
}
567565

568-
// Calculate max results to show
569-
var itemCount = (Height - (_settings.WindowHeightSize + 14) - shadowMargin) / _settings.ItemHeightSize;
570-
if (itemCount < 2)
571-
{
572-
_settings.MaxResultsToShow = 2;
573-
}
574-
else
575-
{
576-
_settings.MaxResultsToShow = Convert.ToInt32(Math.Truncate(itemCount));
566+
// Calculate max results to show
567+
var itemCount = (Height - (_settings.WindowHeightSize + 14) - shadowMargin) / _settings.ItemHeightSize;
568+
if (itemCount < 2)
569+
{
570+
_settings.MaxResultsToShow = 2;
571+
}
572+
else
573+
{
574+
_settings.MaxResultsToShow = Convert.ToInt32(Math.Truncate(itemCount));
575+
}
577576
}
578-
}
579-
580-
SizeToContent = SizeToContent.Height;
581-
}
582-
else
583-
{
584-
// Update height when exiting maximized snap state.
585-
SizeToContent = SizeToContent.Height;
586-
}
587577

588-
if (_initialWidth != (int)Width)
589-
{
590-
if (!_settings.KeepMaxResults)
578+
SizeToContent = SizeToContent.Height;
579+
}
580+
else
591581
{
592-
// Update width
593-
_viewModel.MainWindowWidth = Width;
582+
// Update height when exiting maximized snap state.
583+
SizeToContent = SizeToContent.Height;
594584
}
595585

596-
SizeToContent = SizeToContent.Height;
597-
}
586+
if (_initialWidth != (int)Width)
587+
{
588+
if (!_settings.KeepMaxResults)
589+
{
590+
// Update width
591+
_viewModel.MainWindowWidth = Width;
592+
}
598593

599-
handled = true;
600-
}
601-
if (msg == WM_NCLBUTTONDBLCLK)
602-
{
603-
SizeToContent = SizeToContent.Height;
604-
handled = true;
605-
}
606-
else if (msg == WM_SYSCOMMAND)
607-
{
608-
int command = wParam.ToInt32() & 0xFFF0;
609-
if (command == SC_MAXIMIZE || command == SC_MINIMIZE)
610-
{
594+
SizeToContent = SizeToContent.Height;
595+
}
596+
597+
handled = true;
598+
break;
599+
case Win32Helper.WM_NCLBUTTONDBLCLK: // Block the double click in frame
611600
SizeToContent = SizeToContent.Height;
612601
handled = true;
613-
}
602+
break;
603+
case Win32Helper.WM_SYSCOMMAND: // Block Maximize/Minimize by Win+Up and Win+Down Arrow
604+
var command = wParam.ToInt32() & 0xFFF0;
605+
if (command == Win32Helper.SC_MAXIMIZE || command == Win32Helper.SC_MINIMIZE)
606+
{
607+
SizeToContent = SizeToContent.Height;
608+
handled = true;
609+
}
610+
break;
614611
}
612+
615613
return IntPtr.Zero;
616614
}
617615

0 commit comments

Comments
 (0)