Skip to content

Commit f208569

Browse files
committed
Add button to switch to the selected window
1 parent eb43910 commit f208569

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

PiP-Tool.Native/NativeMethods.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public static class NativeMethods
6969
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
7070
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, MonitorDefaultTo dwFlags);
7171

72+
[DllImport("user32.dll", SetLastError = true)]
73+
public static extern void SwitchToThisWindow(IntPtr hWnd);
74+
75+
[DllImport("user32.dll", SetLastError = true)]
76+
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
77+
78+
[DllImport("user32.dll", SetLastError = true)]
79+
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
80+
81+
[DllImport("user32.dll", SetLastError = true)]
82+
public static extern IntPtr SetFocus(IntPtr hWnd);
83+
84+
[DllImport("user32.dll", SetLastError = true)]
85+
public static extern bool BringWindowToTop(IntPtr hWnd);
86+
87+
[DllImport("user32.dll")]
88+
public static extern bool IsZoomed(IntPtr hWnd);
89+
7290
#endregion
7391

7492
#region gdi32.dll

PiP-Tool/Resources/Strings/StringResources.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
<system:String x:Key="Quit">Quit</system:String>
88
<system:String x:Key="SelectNewWindow">Select a new window</system:String>
99
<system:String x:Key="SetVolume">Adjust Volume</system:String>
10+
<system:String x:Key="SwitchToSelectedWindow">Switch to selected window and minimize PiP"</system:String>
1011
</ResourceDictionary>

PiP-Tool/ViewModels/PiPModeViewModel.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public string Title
5858
public ICommand ClosingCommand { get; }
5959
public ICommand ChangeSelectedWindowCommand { get; }
6060
public ICommand SetVolumeCommand { get; }
61+
public ICommand SwitchToSelectedWindowCommand { get; }
6162
public ICommand MouseEnterCommand { get; }
6263
public ICommand MouseLeaveCommand { get; }
6364
public ICommand DpiChangedCommand { get; }
@@ -203,6 +204,7 @@ public PiPModeViewModel()
203204
ClosingCommand = new RelayCommand(ClosingCommandExecute);
204205
ChangeSelectedWindowCommand = new RelayCommand(ChangeSelectedWindowCommandExecute);
205206
SetVolumeCommand = new RelayCommand<object>(SetVolumeCommandExecute);
207+
SwitchToSelectedWindowCommand = new RelayCommand(SwitchToSelectedWindowCommandExecute);
206208
MouseEnterCommand = new RelayCommand<MouseEventArgs>(MouseEnterCommandExecute);
207209
MouseLeaveCommand = new RelayCommand<MouseEventArgs>(MouseLeaveCommandExecute);
208210
DpiChangedCommand = new RelayCommand(DpiChangedCommandExecute);
@@ -507,6 +509,27 @@ private double GetWindowTop(Window window)
507509
return window.Top;
508510
}
509511

512+
513+
/// <summary>
514+
/// Executed on click on set volume button. Opens <see cref="VolumeDialog"/>
515+
/// </summary>
516+
private void SwitchToSelectedWindowCommandExecute()
517+
{
518+
var thisWindow = ThisWindow();
519+
var selectedHandle = this._selectedWindow.WindowInfo.Handle;
520+
521+
NativeMethods.ShowWindow(
522+
selectedHandle,
523+
NativeMethods.IsZoomed(selectedHandle) ? ShowWindowCommands.ShowMaximized : ShowWindowCommands.Restore
524+
);
525+
NativeMethods.SetForegroundWindow(selectedHandle);
526+
NativeMethods.SetActiveWindow(selectedHandle);
527+
NativeMethods.BringWindowToTop(selectedHandle);
528+
NativeMethods.SetFocus(selectedHandle);
529+
NativeMethods.SwitchToThisWindow(selectedHandle, true);
530+
SystemCommands.MinimizeWindow(thisWindow);
531+
}
532+
510533
/// <summary>
511534
/// Executed on click on set volume button. Opens <see cref="VolumeDialog"/>
512535
/// </summary>

PiP-Tool/Views/PiPModeWindow.xaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@
5454
Orientation="Horizontal"
5555
HorizontalAlignment="Right"
5656
>
57+
<Button HorizontalAlignment="Left"
58+
x:Name="SwitchToSelectedWindowButton"
59+
ToolTip="{StaticResource SwitchToSelectedWindow}"
60+
VerticalAlignment="Top"
61+
Command="{Binding SwitchToSelectedWindowCommand}"
62+
Height="30"
63+
Width="30"
64+
BorderBrush="#FF7C7C7C"
65+
Opacity="0.5"
66+
FontFamily="/PiP-Tool;component/Assets/#Flaticon" Content="&#128468;" Foreground="White" />
5767
<Button HorizontalAlignment="Left"
5868
x:Name="VolumeButton"
5969
ToolTip="{StaticResource SetVolume}"

0 commit comments

Comments
 (0)