From 5c0cb0158ae9ffba0eb2ec816f1ada7e6ede0693 Mon Sep 17 00:00:00 2001 From: Muhammad Rizqi Nur Date: Mon, 28 Mar 2022 17:03:52 +0700 Subject: [PATCH] Added minimize button and disable aero snap --- PiP-Tool/Controls/Snap.cs | 88 +++++++++++++++++++ PiP-Tool/PiP-Tool.csproj | 1 + .../Resources/Strings/StringResources.xaml | 1 + PiP-Tool/ViewModels/PiPModeViewModel.cs | 78 +++++++++++++++- PiP-Tool/Views/PiPModeWindow.xaml | 49 +++++++---- 5 files changed, 199 insertions(+), 18 deletions(-) create mode 100644 PiP-Tool/Controls/Snap.cs diff --git a/PiP-Tool/Controls/Snap.cs b/PiP-Tool/Controls/Snap.cs new file mode 100644 index 0000000..3d74bc7 --- /dev/null +++ b/PiP-Tool/Controls/Snap.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Interactivity; + +namespace PiP_Tool.Controls +{/// + /// behavior that makes a window/dialog draggable by clicking anywhere + /// on it that is not a control (ie, button) + /// + /// https://stackoverflow.com/questions/2470685/how-do-you-disable-aero-snap-in-an-application/13403067#13403067 + public class DragMoveBehavior : Behavior where T : FrameworkElement + { + protected override void OnAttached() + { + AssociatedObject.MouseLeftButtonDown += MouseDown; + base.OnAttached(); + } + + protected override void OnDetaching() + { + AssociatedObject.MouseLeftButtonDown -= MouseDown; + base.OnDetaching(); + } + + void MouseDown(object sender, EventArgs ea) => Window.GetWindow(sender as T)?.DragMove(); + } + + public class WinDragMoveBehavior : DragMoveBehavior { } + + public class UCDragMoveBehavior : DragMoveBehavior { } + + /// + /// behavior that makes a window/dialog not resizable while clicked. this prevents + /// the window from being snapped to the edge of the screen (AeroSnap). if DragMoveBehavior + /// is also used, this must be attached first. + /// + /// + public class NoSnapBehavior : Behavior where T : FrameworkElement + { + ResizeMode lastMode = ResizeMode.NoResize; + protected override void OnAttached() + { + AssociatedObject.MouseLeftButtonDown += MouseDown; + AssociatedObject.MouseLeftButtonUp += MouseUp; + base.OnAttached(); + } + + protected override void OnDetaching() + { + AssociatedObject.MouseLeftButtonDown -= MouseDown; + AssociatedObject.MouseLeftButtonUp -= MouseUp; + base.OnDetaching(); + } + + /// + /// make it so the window can be moved by dragging + /// + void MouseDown(object sender, EventArgs ea) + { + var win = Window.GetWindow(sender as T); + if (win != null && win.ResizeMode != ResizeMode.NoResize) + { + lastMode = win.ResizeMode; + win.ResizeMode = ResizeMode.NoResize; + win.UpdateLayout(); + } + } + + void MouseUp(object sender, EventArgs ea) + { + var win = Window.GetWindow(sender as T); + if (win != null && win.ResizeMode != lastMode) + { + win.ResizeMode = lastMode; + win.UpdateLayout(); + } + } + } + + public class WinNoSnapBehavior : NoSnapBehavior { } + + public class UCNoSnapBehavior : NoSnapBehavior { } +} diff --git a/PiP-Tool/PiP-Tool.csproj b/PiP-Tool/PiP-Tool.csproj index 0ce9ed9..c7e6512 100644 --- a/PiP-Tool/PiP-Tool.csproj +++ b/PiP-Tool/PiP-Tool.csproj @@ -208,6 +208,7 @@ MSBuild:Compile Designer + diff --git a/PiP-Tool/Resources/Strings/StringResources.xaml b/PiP-Tool/Resources/Strings/StringResources.xaml index e80cd4c..89480a8 100644 --- a/PiP-Tool/Resources/Strings/StringResources.xaml +++ b/PiP-Tool/Resources/Strings/StringResources.xaml @@ -9,4 +9,5 @@ Adjust volume Switch to selected window and minimize PiP Adjust PiP window opacity + Minimize this PiP window \ No newline at end of file diff --git a/PiP-Tool/ViewModels/PiPModeViewModel.cs b/PiP-Tool/ViewModels/PiPModeViewModel.cs index 0ee9c64..d01a9f6 100644 --- a/PiP-Tool/ViewModels/PiPModeViewModel.cs +++ b/PiP-Tool/ViewModels/PiPModeViewModel.cs @@ -83,8 +83,11 @@ public double BackgroundOpacity public ICommand ChangeSelectedWindowCommand { get; } public ICommand SetVolumeCommand { get; } public ICommand SwitchToSelectedWindowCommand { get; } + public ICommand MinimizeCommand { get; } public ICommand SetOpacityCommand { get; } public ICommand MouseEnterCommand { get; } + public ICommand MouseDownCommand { get; } + public ICommand MouseUpCommand { get; } public ICommand MouseLeaveCommand { get; } public ICommand DpiChangedCommand { get; } @@ -231,8 +234,11 @@ public PiPModeViewModel() ChangeSelectedWindowCommand = new RelayCommand(ChangeSelectedWindowCommandExecute); SetVolumeCommand = new RelayCommand(SetVolumeCommandExecute); SwitchToSelectedWindowCommand = new RelayCommand(SwitchToSelectedWindowCommandExecute); + MinimizeCommand = new RelayCommand(MinimizeCommandExecute); SetOpacityCommand = new RelayCommand(SetOpacityCommandExecute); MouseEnterCommand = new RelayCommand(MouseEnterCommandExecute); + MouseDownCommand = new RelayCommand(MouseDownCommandExecute); + MouseUpCommand = new RelayCommand(MouseUpCommandExecute); MouseLeaveCommand = new RelayCommand(MouseLeaveCommandExecute); DpiChangedCommand = new RelayCommand(DpiChangedCommandExecute); @@ -447,7 +453,13 @@ private void Train() /// The appropriate return value depends on the particular message. See the message documentation details for the Win32 message being handled. private IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled) { - if ((WM)msg != WM.WINDOWPOSCHANGING) + var msg2 = (WM)msg; + if (msg2 == WM.LBUTTONDOWN) + SetNoResize(); + else if (msg2 == WM.LBUTTONUP) + SetResizeGrip(); + + if (msg2 != WM.WINDOWPOSCHANGING) return IntPtr.Zero; if (_renderSizeEventDisabled) @@ -549,7 +561,7 @@ private double GetWindowTop(Window window) /// - /// Executed on click on set volume button. Opens + /// Executed on click on switch to button. /> /// private void SwitchToSelectedWindowCommandExecute() { @@ -568,6 +580,15 @@ private void SwitchToSelectedWindowCommandExecute() SystemCommands.MinimizeWindow(thisWindow); } + /// + /// Executed on click on minimize button. /> + /// + private void MinimizeCommandExecute() + { + var thisWindow = ThisWindow(); + SystemCommands.MinimizeWindow(thisWindow); + } + /// /// Executed on click on set volume button. Opens /// @@ -657,6 +678,59 @@ private void MouseEnterCommandExecute(MouseEventArgs e) e.Handled = true; } + /// + /// Executed on mouse down. Disable Resize + /// + /// Event arguments + private void MouseDownCommandExecute(MouseEventArgs e) + { + Logger.Instance.Debug("Mousedown"); + if ( + e.LeftButton == MouseButtonState.Pressed + || System.Windows.Input.Mouse.LeftButton == MouseButtonState.Pressed + ) + { + Logger.Instance.Debug("Left mouse pressed"); + SetNoResize(); + } + e.Handled = true; + } + + private void SetNoResize() + { + var thisWindow = ThisWindow(); + // this prevents win7 aerosnap + if (thisWindow.ResizeMode != System.Windows.ResizeMode.NoResize) + { + Logger.Instance.Debug("Set no resize"); + thisWindow.ResizeMode = System.Windows.ResizeMode.NoResize; + thisWindow.UpdateLayout(); + } + + } + + /// + /// Executed on mouse up. Enable Resize + /// + /// Event arguments + private void MouseUpCommandExecute(MouseEventArgs e) + { + SetResizeGrip(); + e.Handled = true; + } + + private void SetResizeGrip() + { + + var thisWindow = ThisWindow(); + if (thisWindow.ResizeMode == System.Windows.ResizeMode.NoResize) + { + // restore resize grips + thisWindow.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip; + thisWindow.UpdateLayout(); + } + } + /// /// Executed on mouse leave. Close top bar /// diff --git a/PiP-Tool/Views/PiPModeWindow.xaml b/PiP-Tool/Views/PiPModeWindow.xaml index f6e2440..7f70628 100644 --- a/PiP-Tool/Views/PiPModeWindow.xaml +++ b/PiP-Tool/Views/PiPModeWindow.xaml @@ -4,6 +4,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:viewModels="clr-namespace:PiP_Tool.ViewModels" + xmlns:controls="clr-namespace:PiP_Tool.Controls" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:command="http://www.galasoft.ch/mvvmlight" mc:Ignorable="d" @@ -36,6 +37,12 @@ + + + + + + @@ -64,46 +71,56 @@ HorizontalAlignment="Right" >