Skip to content

Commit

Permalink
Added minimize button and disable aero snap
Browse files Browse the repository at this point in the history
  • Loading branch information
R-N committed Mar 28, 2022
1 parent e6e5010 commit 5c0cb01
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 18 deletions.
88 changes: 88 additions & 0 deletions PiP-Tool/Controls/Snap.cs
Original file line number Diff line number Diff line change
@@ -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
{/// <summary>
/// behavior that makes a window/dialog draggable by clicking anywhere
/// on it that is not a control (ie, button)
/// </summary>
/// https://stackoverflow.com/questions/2470685/how-do-you-disable-aero-snap-in-an-application/13403067#13403067
public class DragMoveBehavior<T> : Behavior<T> 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<Window> { }

public class UCDragMoveBehavior : DragMoveBehavior<UserControl> { }

/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T"></typeparam>
public class NoSnapBehavior<T> : Behavior<T> 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();
}

/// <summary>
/// make it so the window can be moved by dragging
/// </summary>
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<Window> { }

public class UCNoSnapBehavior : NoSnapBehavior<UserControl> { }
}
1 change: 1 addition & 0 deletions PiP-Tool/PiP-Tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Controls\Snap.cs" />
<Compile Include="DataModel\AudioControl.cs" />
<Compile Include="DataModel\AudioControls.cs" />
<Compile Include="Controls\MoveThumb.cs" />
Expand Down
1 change: 1 addition & 0 deletions PiP-Tool/Resources/Strings/StringResources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<system:String x:Key="SetVolume">Adjust volume</system:String>
<system:String x:Key="SwitchToSelectedWindow">Switch to selected window and minimize PiP</system:String>
<system:String x:Key="SetOpacity">Adjust PiP window opacity</system:String>
<system:String x:Key="Minimize">Minimize this PiP window</system:String>
</ResourceDictionary>
78 changes: 76 additions & 2 deletions PiP-Tool/ViewModels/PiPModeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -231,8 +234,11 @@ public PiPModeViewModel()
ChangeSelectedWindowCommand = new RelayCommand(ChangeSelectedWindowCommandExecute);
SetVolumeCommand = new RelayCommand<object>(SetVolumeCommandExecute);
SwitchToSelectedWindowCommand = new RelayCommand(SwitchToSelectedWindowCommandExecute);
MinimizeCommand = new RelayCommand(MinimizeCommandExecute);
SetOpacityCommand = new RelayCommand<object>(SetOpacityCommandExecute);
MouseEnterCommand = new RelayCommand<MouseEventArgs>(MouseEnterCommandExecute);
MouseDownCommand = new RelayCommand<MouseEventArgs>(MouseDownCommandExecute);
MouseUpCommand = new RelayCommand<MouseEventArgs>(MouseUpCommandExecute);
MouseLeaveCommand = new RelayCommand<MouseEventArgs>(MouseLeaveCommandExecute);
DpiChangedCommand = new RelayCommand(DpiChangedCommandExecute);

Expand Down Expand Up @@ -447,7 +453,13 @@ private void Train()
/// <returns>The appropriate return value depends on the particular message. See the message documentation details for the Win32 message being handled.</returns>
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)
Expand Down Expand Up @@ -549,7 +561,7 @@ private double GetWindowTop(Window window)


/// <summary>
/// Executed on click on set volume button. Opens <see cref="VolumeDialog"/>
/// Executed on click on switch to button. />
/// </summary>
private void SwitchToSelectedWindowCommandExecute()
{
Expand All @@ -568,6 +580,15 @@ private void SwitchToSelectedWindowCommandExecute()
SystemCommands.MinimizeWindow(thisWindow);
}

/// <summary>
/// Executed on click on minimize button. />
/// </summary>
private void MinimizeCommandExecute()
{
var thisWindow = ThisWindow();
SystemCommands.MinimizeWindow(thisWindow);
}

/// <summary>
/// Executed on click on set volume button. Opens <see cref="VolumeDialog"/>
/// </summary>
Expand Down Expand Up @@ -657,6 +678,59 @@ private void MouseEnterCommandExecute(MouseEventArgs e)
e.Handled = true;
}

/// <summary>
/// Executed on mouse down. Disable Resize
/// </summary>
/// <param name="e">Event arguments</param>
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();
}

}

/// <summary>
/// Executed on mouse up. Enable Resize
/// </summary>
/// <param name="e">Event arguments</param>
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();
}
}

/// <summary>
/// Executed on mouse leave. Close top bar
/// </summary>
Expand Down
49 changes: 33 additions & 16 deletions PiP-Tool/Views/PiPModeWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -36,6 +37,12 @@
<i:EventTrigger EventName="MouseEnter">
<command:EventToCommand Command="{Binding MouseEnterCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseDown">
<command:EventToCommand Command="{Binding MouseDownCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp">
<command:EventToCommand Command="{Binding MouseUpCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<command:EventToCommand Command="{Binding MouseLeaveCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
Expand Down Expand Up @@ -64,46 +71,56 @@
HorizontalAlignment="Right"
>
<Button HorizontalAlignment="Left"
x:Name="OpacityButton"
ToolTip="{StaticResource SetOpacity}"
ToolTip="{StaticResource SelectNewWindow}"
VerticalAlignment="Top"
Command="{Binding SetOpacityCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Command="{Binding ChangeSelectedWindowCommand}"
Height="30"
Width="30"
BorderBrush="#FF7C7C7C"
Opacity="0.5"
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#128065;" Foreground="White" />
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#xF103;" Foreground="White" />
<Button HorizontalAlignment="Left"
x:Name="SwitchToSelectedWindowButton"
ToolTip="{StaticResource SwitchToSelectedWindow}"
x:Name="VolumeButton"
ToolTip="{StaticResource SetVolume}"
VerticalAlignment="Top"
Command="{Binding SwitchToSelectedWindowCommand}"
Command="{Binding SetVolumeCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Height="30"
Width="30"
BorderBrush="#FF7C7C7C"
Opacity="0.5"
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#128468;" Foreground="White" />
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#x1F50A;" Foreground="White" />
<Button HorizontalAlignment="Left"
x:Name="VolumeButton"
ToolTip="{StaticResource SetVolume}"
x:Name="OpacityButton"
ToolTip="{StaticResource SetOpacity}"
VerticalAlignment="Top"
Command="{Binding SetVolumeCommand}"
Command="{Binding SetOpacityCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Height="30"
Width="30"
BorderBrush="#FF7C7C7C"
Opacity="0.5"
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#x1F50A;" Foreground="White" />
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#128065;" Foreground="White" />
<Button HorizontalAlignment="Left"
ToolTip="{StaticResource SelectNewWindow}"
x:Name="MinimizeButton"
ToolTip="{StaticResource Minimize}"
VerticalAlignment="Top"
Command="{Binding ChangeSelectedWindowCommand}"
Command="{Binding MinimizeCommand}"
Height="30"
Width="30"
BorderBrush="#FF7C7C7C"
Opacity="0.5"
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#xF103;" Foreground="White" />
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#128469;" Foreground="White" />
<Button HorizontalAlignment="Left"
x:Name="SwitchToSelectedWindowButton"
ToolTip="{StaticResource SwitchToSelectedWindow}"
VerticalAlignment="Top"
Command="{Binding SwitchToSelectedWindowCommand}"
Height="30"
Width="30"
BorderBrush="#FF7C7C7C"
Opacity="0.5"
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#128468;" Foreground="White" />
<Button HorizontalAlignment="Left"
ToolTip="{StaticResource Quit}"
VerticalAlignment="Top"
Expand Down

0 comments on commit 5c0cb01

Please sign in to comment.