From df0ba7829841367545825c196f998e1401d81b1c Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Thu, 23 Nov 2017 22:20:25 +0200 Subject: [PATCH] Address #122: final solution for window resizing by bring macOS's logic --- .../ViewerPanel.xaml | 6 +- ....cs => WindowStateToThicknessConverter.cs} | 14 ++-- QuickLook/GlobalKeyboardHook.cs | 8 +- QuickLook/Plugin/ContextObject.cs | 2 +- QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs | 2 +- QuickLook/QuickLook.csproj | 2 +- QuickLook/Styles/MainWindowStyles.xaml | 4 +- QuickLook/ViewerWindow.Actions.cs | 45 ++++++----- QuickLook/ViewerWindow.xaml | 79 ++++++++----------- QuickLook/ViewerWindow.xaml.cs | 24 +++++- 10 files changed, 99 insertions(+), 87 deletions(-) rename QuickLook/Converters/{BooleanToResizeBorderThicknessConverter.cs => WindowStateToThicknessConverter.cs} (67%) diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml index 070601d..33a8f79 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml @@ -68,13 +68,13 @@ - - - + + + = 0) - { if (!IsWindowsKeyPressed()) { - var key = (Keys)lParam.vkCode; - if (HookedKeys.Contains(key)) + var key = (Keys) lParam.vkCode; + if (HookedKeys.Contains(key)) { - key = AddModifiers(key); + key = AddModifiers(key); var kea = new KeyEventArgs(key); if (wParam == User32.WM_KEYDOWN || wParam == User32.WM_SYSKEYDOWN) @@ -95,7 +94,6 @@ namespace QuickLook return 1; } } - } return User32.CallNextHookEx(_hhook, code, wParam, ref lParam); } diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index 66861f2..7fe5c02 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -229,7 +229,7 @@ namespace QuickLook.Plugin var heightRatio = max.Height * maxRatio / size.Height; var ratio = Math.Min(widthRatio, heightRatio); - //if (ratio > 1) ratio = 1; + if (ratio > 1) ratio = 1; PreferredSize = new Size {Width = size.Width * ratio, Height = size.Height * ratio}; diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs index 6d8016b..efbb8e5 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs @@ -99,7 +99,7 @@ namespace QuickLook.Plugin.InfoPanel else if (Directory.Exists(path)) { FileHelper.CountFolder(path, ref _stop, - out long totalDirsL, out long totalFilesL, out long totalSizeL); + out var totalDirsL, out var totalFilesL, out var totalSizeL); if (!Stop) Dispatcher.Invoke(() => diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index d788dd0..135bac4 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -120,7 +120,7 @@ - + diff --git a/QuickLook/Styles/MainWindowStyles.xaml b/QuickLook/Styles/MainWindowStyles.xaml index 98eae58..8bd7c10 100644 --- a/QuickLook/Styles/MainWindowStyles.xaml +++ b/QuickLook/Styles/MainWindowStyles.xaml @@ -1,8 +1,8 @@  - 1 - 6 + 1 + 6 32 Gray diff --git a/QuickLook/ViewerWindow.Actions.cs b/QuickLook/ViewerWindow.Actions.cs index 10b9575..71107bd 100644 --- a/QuickLook/ViewerWindow.Actions.cs +++ b/QuickLook/ViewerWindow.Actions.cs @@ -80,40 +80,36 @@ namespace QuickLook BeginClose(); } - private static void ResizeAndCenter(Window window, Size size, bool canOldPluginResize, bool canNextPluginResize) + private void ResizeAndCenter(Size size, bool canOldPluginResize, bool canNextPluginResize) { // resize to MinSize first - size.Width = Math.Max(size.Width, window.MinWidth); - size.Height = Math.Max(size.Height, window.MinHeight); + size.Width = Math.Max(size.Width, MinWidth); + size.Height = Math.Max(size.Height, MinHeight); - if (!window.IsLoaded) + if (!IsLoaded) { // if the window is not loaded yet, just leave the problem to WPF - window.Width = size.Width; - window.Height = size.Height; - window.WindowStartupLocation = WindowStartupLocation.CenterScreen; - window.Dispatcher.BeginInvoke(new Action(window.BringToFront), DispatcherPriority.Render); + Width = size.Width; + Height = size.Height; + WindowStartupLocation = WindowStartupLocation.CenterScreen; + Dispatcher.BeginInvoke(new Action(this.BringToFront), DispatcherPriority.Render); return; } // is the window is now now maximized, do not move it - if (window.WindowState == WindowState.Maximized) + if (WindowState == WindowState.Maximized) return; // if this is a new window, place it to top - if (window.Visibility != Visibility.Visible) - window.BringToFront(); + if (Visibility != Visibility.Visible) + this.BringToFront(); var screen = WindowHelper.GetCurrentWindowRect(); - // do not resize or reposition the window is it is visible - unless the next window is size-fixed - if (window.Visibility == Visibility.Visible && canOldPluginResize && canNextPluginResize) - return; - // otherwise, resize it and place it to the old window center. - var oldCenterX = window.Left + window.Width / 2; - var oldCenterY = window.Top + window.Height / 2; + var oldCenterX = Left + Width / 2; + var oldCenterY = Top + Height / 2; var newLeft = oldCenterX - size.Width / 2; var newTop = oldCenterY - size.Height / 2; @@ -124,7 +120,7 @@ namespace QuickLook newLeft = newLeft + size.Width > screen.Right ? screen.Right - size.Width : newLeft; // right newTop = newTop + size.Height > screen.Bottom ? screen.Bottom - size.Height : newTop; // bottom - window.MoveWindow(newLeft, newTop, size.Width, size.Height); + this.MoveWindow(newLeft, newTop, size.Width, size.Height); } internal void UnloadPlugin() @@ -181,7 +177,14 @@ namespace QuickLook (ContextObject.TitlebarOverlap ? 0 : windowCaptionContainer.Height); var newWidth = ContextObject.PreferredSize.Width + margin; - ResizeAndCenter(this, new Size(newWidth, newHeight), _canOldPluginResize, ContextObject.CanResize); + var newSize = new Size(newWidth, newHeight); + // if use has adjusted the window size, keep it + if (_customWindowSize != Size.Empty) + newSize = _customWindowSize; + else + _ignoreNextWindowSizeChange = true; + + ResizeAndCenter(newSize, _canOldPluginResize, ContextObject.CanResize); if (Visibility != Visibility.Visible) Show(); @@ -251,6 +254,10 @@ namespace QuickLook internal void BeginHide() { + // reset custom window size + _customWindowSize = Size.Empty; + _ignoreNextWindowSizeChange = true; + UnloadPlugin(); // if the this window is hidden in Max state, new show() will results in failure: diff --git a/QuickLook/ViewerWindow.xaml b/QuickLook/ViewerWindow.xaml index d39120b..e88db94 100644 --- a/QuickLook/ViewerWindow.xaml +++ b/QuickLook/ViewerWindow.xaml @@ -20,48 +20,37 @@ - + - - - - - - - - + - - - - - - - + + VerticalAlignment="Top" + ZIndex="100"> + Storyboard.Target="{Binding Source={x:Reference windowCaptionContainer}}" + Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.05" /> + Storyboard.Target="{Binding Source={x:Reference windowCaptionContainer}}" + Storyboard.TargetProperty="Opacity"> @@ -71,24 +60,24 @@ + BlurredElement="{Binding ElementName=containerPanel}" + ColorOverlayVisibility="{Binding ContextObject.TitlebarColourVisibility, ElementName=mainWindow, Converter={StaticResource BooleanToVisibilityConverter}}" + GlassVisibility="{Binding ContextObject.TitlebarBlurVisibility, ElementName=mainWindow, Converter={StaticResource BooleanToVisibilityConverter}}" + NoiseVisibility="Visible" />