diff --git a/Flow.Launcher.Infrastructure/NativeMethods.txt b/Flow.Launcher.Infrastructure/NativeMethods.txt index 53c877c4f05..edc71feef24 100644 --- a/Flow.Launcher.Infrastructure/NativeMethods.txt +++ b/Flow.Launcher.Infrastructure/NativeMethods.txt @@ -42,6 +42,11 @@ MONITORINFOEXW WM_ENTERSIZEMOVE WM_EXITSIZEMOVE +WM_NCLBUTTONDBLCLK +WM_SYSCOMMAND + +SC_MAXIMIZE +SC_MINIMIZE OleInitialize OleUninitialize diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 1be803fd4bd..86e7b7c971c 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -324,6 +324,11 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni public const int WM_ENTERSIZEMOVE = (int)PInvoke.WM_ENTERSIZEMOVE; public const int WM_EXITSIZEMOVE = (int)PInvoke.WM_EXITSIZEMOVE; + public const int WM_NCLBUTTONDBLCLK = (int)PInvoke.WM_NCLBUTTONDBLCLK; + public const int WM_SYSCOMMAND = (int)PInvoke.WM_SYSCOMMAND; + + public const int SC_MAXIMIZE = (int)PInvoke.SC_MAXIMIZE; + public const int SC_MINIMIZE = (int)PInvoke.SC_MINIMIZE; #endregion diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 13504fc93af..a77d6471c7a 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -465,6 +465,9 @@ private void OnPreviewMouseMove(object sender, MouseEventArgs e) private void OnMouseDown(object sender, MouseButtonEventArgs e) { + // When the window is maximized via Snap, + // dragging attempts will first switch the window from Maximized to Normal state, + // and adjust the drag position accordingly. if (e.ChangedButton == MouseButton.Left) { try @@ -472,8 +475,8 @@ private void OnMouseDown(object sender, MouseButtonEventArgs e) if (WindowState == WindowState.Maximized) { // Calculate ratio based on maximized window dimensions - double maxWidth = this.ActualWidth; - double maxHeight = this.ActualHeight; + double maxWidth = ActualWidth; + double maxHeight = ActualHeight; var mousePos = e.GetPosition(this); double xRatio = mousePos.X / maxWidth; double yRatio = mousePos.Y / maxHeight; @@ -486,7 +489,7 @@ private void OnMouseDown(object sender, MouseButtonEventArgs e) // Switch to Normal state WindowState = WindowState.Normal; - Dispatcher.BeginInvoke(new Action(() => + Application.Current?.Dispatcher.Invoke(new Action(() => { double normalWidth = Width; double normalHeight = Height; @@ -535,83 +538,78 @@ private async void OnContextMenusForSettingsClick(object sender, RoutedEventArgs #region Window WndProc - private const int WM_NCLBUTTONDBLCLK = 0x00A3; - private const int WM_SYSCOMMAND = 0x0112; - private const int SC_MAXIMIZE = 0xF030; - private const int SC_RESTORE = 0xF120; - private const int SC_MINIMIZE = 0xF020; private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) - { - if (msg == Win32Helper.WM_ENTERSIZEMOVE) - { - _initialWidth = (int)Width; - _initialHeight = (int)Height; - handled = true; - } - else if (msg == Win32Helper.WM_EXITSIZEMOVE) + { + switch (msg) { - //Prevent updating the number of results when the window height is below the height of a single result item. - //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. - //(Without this check, releasing from a snap can cause the window height to hit the minimum, resulting in only 2 results being shown.) - if (_initialHeight != (int)Height && Height> (_settings.WindowHeightSize + _settings.ItemHeightSize)) - { - if (!_settings.KeepMaxResults) + case Win32Helper.WM_ENTERSIZEMOVE: + _initialWidth = (int)Width; + _initialHeight = (int)Height; + handled = true; + break; + case Win32Helper.WM_EXITSIZEMOVE: + //Prevent updating the number of results when the window height is below the height of a single result item. + //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. + //(Without this check, releasing from a snap can cause the window height to hit the minimum, resulting in only 2 results being shown.) + if (_initialHeight != (int)Height && Height > (_settings.WindowHeightSize + _settings.ItemHeightSize)) { - // Get shadow margin - var shadowMargin = 0; - var (_, useDropShadowEffect) = _theme.GetActualValue(); - if (useDropShadowEffect) + if (!_settings.KeepMaxResults) { - shadowMargin = 32; - } + // Get shadow margin + var shadowMargin = 0; + var (_, useDropShadowEffect) = _theme.GetActualValue(); + if (useDropShadowEffect) + { + shadowMargin = 32; + } - // Calculate max results to show - var itemCount = (Height - (_settings.WindowHeightSize + 14) - shadowMargin) / _settings.ItemHeightSize; - if (itemCount < 2) - { - _settings.MaxResultsToShow = 2; - } - else - { - _settings.MaxResultsToShow = Convert.ToInt32(Math.Truncate(itemCount)); + // Calculate max results to show + var itemCount = (Height - (_settings.WindowHeightSize + 14) - shadowMargin) / _settings.ItemHeightSize; + if (itemCount < 2) + { + _settings.MaxResultsToShow = 2; + } + else + { + _settings.MaxResultsToShow = Convert.ToInt32(Math.Truncate(itemCount)); + } } - } - - SizeToContent = SizeToContent.Height; - } - else - { - // Update height when exiting maximized snap state. - SizeToContent = SizeToContent.Height; - } - if (_initialWidth != (int)Width) - { - if (!_settings.KeepMaxResults) + SizeToContent = SizeToContent.Height; + } + else { - // Update width - _viewModel.MainWindowWidth = Width; + // Update height when exiting maximized snap state. + SizeToContent = SizeToContent.Height; } - SizeToContent = SizeToContent.Height; - } + if (_initialWidth != (int)Width) + { + if (!_settings.KeepMaxResults) + { + // Update width + _viewModel.MainWindowWidth = Width; + } - handled = true; - } - if (msg == WM_NCLBUTTONDBLCLK) - { - SizeToContent = SizeToContent.Height; - handled = true; - } - else if (msg == WM_SYSCOMMAND) - { - int command = wParam.ToInt32() & 0xFFF0; - if (command == SC_MAXIMIZE || command == SC_MINIMIZE) - { + SizeToContent = SizeToContent.Height; + } + + handled = true; + break; + case Win32Helper.WM_NCLBUTTONDBLCLK: // Block the double click in frame SizeToContent = SizeToContent.Height; handled = true; - } + break; + case Win32Helper.WM_SYSCOMMAND: // Block Maximize/Minimize by Win+Up and Win+Down Arrow + var command = wParam.ToInt32() & 0xFFF0; + if (command == Win32Helper.SC_MAXIMIZE || command == Win32Helper.SC_MINIMIZE) + { + SizeToContent = SizeToContent.Height; + handled = true; + } + break; } + return IntPtr.Zero; }