Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PI: Move SnapHelper out of Vertical Bar window and fix snapping bugs. #3094

Merged
merged 17 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions tools/PI/DevHome.PI/BarWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using DevHome.PI.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.Graphics;
using Windows.Win32.Foundation;
using WinUIEx;

Expand Down Expand Up @@ -42,6 +43,8 @@ internal HWND CurrentHwnd

public void Close()
{
UnsnapBarWindow();

_horizontalWindow.Close();
_verticalWindow.Close();

Expand All @@ -53,21 +56,6 @@ public void Close()

public Frame GetFrame() => _horizontalWindow.GetFrame();

public IntPtr CurrentWindowHandle
{
get
{
if (_horizontalWindow.Visible)
{
return DevHome.Common.Extensions.WindowExExtensions.GetWindowHandle(_horizontalWindow);
}
else
{
return DevHome.Common.Extensions.WindowExExtensions.GetWindowHandle(_verticalWindow);
}
}
}

internal void SetRequestedTheme(ElementTheme theme)
{
_horizontalWindow.SetRequestedTheme(theme);
Expand Down Expand Up @@ -141,4 +129,24 @@ public void RotateBar()
_horizontalWindow.Show();
}
}

public void UpdateBarWindowPosition(PointInt32 position)
zadesai marked this conversation as resolved.
Show resolved Hide resolved
{
_viewModel.WindowPosition = position;
}

public void ResetBarWindowOnTop()
{
_viewModel.ResetBarWindowOnTop();
}

public void UnsnapBarWindow()
{
_viewModel.UnsnapBarWindow();
}

public bool IsBarSnappedToWindow()
{
return _viewModel.IsSnapped;
}
}
12 changes: 6 additions & 6 deletions tools/PI/DevHome.PI/BarWindowHorizontal.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
</Grid.ColumnDefinitions>

<TextBlock x:Uid="Title" FontSize="10" Margin="10,0,0,0" HorizontalAlignment="Left" Grid.Column="1" VerticalAlignment="Center"/>
<StackPanel x:Name="ChromeButtonPanel" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0, 0, 10, 0" Background="Transparent" Grid.Column="1">
<StackPanel x:Name="ChromeButtonPanel" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0, 0, 10, 0" Background="Transparent" Grid.Column="1" SizeChanged="{x:Bind SetRegionsForTitleBar}">
<Button
x:Uid="SnapButton"
x:Uid="SnapButton" Visibility="{x:Bind _viewModel.AppBarVisibility, Mode=OneWay}"
x:Name="SnapButton" Style="{StaticResource ChromeButton}" FontSize="11"
Click="{x:Bind _viewModel.PerformSnapCommand}" HorizontalAlignment="Left" IsEnabled="{x:Bind _viewModel.IsSnappingEnabled, Mode=OneWay}">
Command="{x:Bind _viewModel.ToggleSnapCommand}" HorizontalAlignment="Left" IsEnabled="{x:Bind _viewModel.IsSnappingEnabled, Mode=OneWay}">
<TextBlock Text="{x:Bind _viewModel.CurrentSnapButtonText, Mode=OneWay}"/>
</Button>
</StackPanel>
Expand All @@ -60,7 +60,7 @@
</Style>
</StackPanel.Resources>

<controls:ProcessSelectionButton x:Uid="ProcessChooserButton" x:Name="ProcessChooserButton" Click="{x:Bind _viewModel.ProcessChooserCommand}" HorizontalAlignment="Center" Margin="0">
<controls:ProcessSelectionButton x:Uid="ProcessChooserButton" x:Name="ProcessChooserButton" Command="{x:Bind _viewModel.ProcessChooserCommand}" HorizontalAlignment="Center" Margin="0">
<TextBlock Text="&#xecaa;" Margin="0" />
</controls:ProcessSelectionButton>

Expand All @@ -70,10 +70,10 @@
<TextBlock x:Uid="AppCPUUsage" Text="{x:Bind _viewModel.AppCpuUsage, Mode=OneWay}" FontFamily="Segoe UI" FontSize="10" Margin="5" VerticalAlignment="Center" Visibility="{x:Bind _viewModel.AppBarVisibility, Mode=OneWay}"/>

<!-- Per System controls -->
<Button x:Uid="SwitchLayoutButton" x:Name="SwitchLayoutButton" HorizontalAlignment="Center" Click="{x:Bind _viewModel.SwitchLayoutCommand}">
<Button x:Uid="SwitchLayoutButton" x:Name="SwitchLayoutButton" HorizontalAlignment="Center" Command="{x:Bind _viewModel.SwitchLayoutCommand}">
<TextBlock Text="&#xe8b4;"/>
</Button>
<Button x:Uid="SwitchToLargeLayoutButton" Click="{x:Bind _viewModel.ShowBigWindowCommand}" HorizontalAlignment="Center">
<Button x:Uid="SwitchToLargeLayoutButton" Command="{x:Bind _viewModel.ToggleExpandedContentVisibilityCommand}" HorizontalAlignment="Center">
<TextBlock Text="&#xe8a1;"/>
</Button>

Expand Down
13 changes: 7 additions & 6 deletions tools/PI/DevHome.PI/BarWindowVertical.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
mc:Ignorable="d"
Title="" MinHeight="700" MinWidth="70" MaxWidth="70" Width="70" Height="700"
TaskBarIcon="Images/pi.ico" IsTitleBarVisible="False"
Closed="WindowEx_Closed" >

IsAlwaysOnTop="{x:Bind _viewModel.IsAlwaysOnTop, Mode=OneWay}"
Closed="WindowEx_Closed">
<!-- TODO Make DesktopAcrylicBackdrop/MicaBackdrop and AcrylicBackgroundFillColorBaseBrush user-configurable.-->
<Window.SystemBackdrop>
<MicaBackdrop/>
Expand All @@ -30,11 +30,12 @@
<TextBlock x:Name="GripTextBlock" Text="&#xe76f;&#xe76f;" FontSize="10" Margin="0" HorizontalAlignment="Center" FontFamily="Segoe Fluent Icons" PointerPressed="Window_PointerPressed" PointerMoved="Window_PointerMoved" PointerReleased="Window_PointerReleased"/>
<Button
x:Uid="SnapButton"
Visibility="{x:Bind _viewModel.AppBarVisibility, Mode=OneWay}"
Margin="0,20, 0, 0"
Style="{StaticResource ChromeButton}"
HorizontalAlignment="Center"
IsEnabled="{x:Bind _viewModel.IsSnappingEnabled, Mode=OneWay}"
Click="{x:Bind _viewModel.PerformSnapCommand}" >
Command="{x:Bind _viewModel.ToggleSnapCommand}" >
<TextBlock Text="{x:Bind _viewModel.CurrentSnapButtonText, Mode=OneWay}"/>
</Button>
<Button
Expand Down Expand Up @@ -65,7 +66,7 @@
</Style>
</StackPanel.Resources>

<controls:ProcessSelectionButton x:Uid="ProcessChooserButton" x:Name="ProcessChooserButton" Click="{x:Bind _viewModel.ProcessChooserCommand}" HorizontalAlignment="Center" Margin="0">
<controls:ProcessSelectionButton x:Uid="ProcessChooserButton" x:Name="ProcessChooserButton" Command="{x:Bind _viewModel.ProcessChooserCommand}" HorizontalAlignment="Center" Margin="0">
<TextBlock Text="&#xecaa;" Margin="0" />
</controls:ProcessSelectionButton>

Expand All @@ -75,10 +76,10 @@
<TextBlock x:Uid="AppCPUUsage" Text="{x:Bind _viewModel.AppCpuUsage, Mode=OneWay}" FontFamily="Segoe UI" FontSize="10" Margin="5" HorizontalAlignment="Center" Visibility="{x:Bind _viewModel.AppBarVisibility, Mode=OneWay}"/>

<!-- Per System controls -->
<Button x:Uid="SwitchLayoutButton" x:Name="SwitchLayoutButton" Click="{x:Bind _viewModel.SwitchLayoutCommand}" HorizontalAlignment="Center">
<Button x:Uid="SwitchLayoutButton" x:Name="SwitchLayoutButton" Command="{x:Bind _viewModel.SwitchLayoutCommand}" HorizontalAlignment="Center">
<TextBlock Text="&#xe8b4;"/>
</Button>
<Button x:Uid="SwitchToLargeLayoutButton" Click="{x:Bind _viewModel.ShowBigWindowCommand}" HorizontalAlignment="Center">
<Button x:Uid="SwitchToLargeLayoutButton" Command="{x:Bind _viewModel.ToggleExpandedContentVisibilityCommand}" HorizontalAlignment="Center">
<TextBlock Text="&#xe8a1;"/>
</Button>

Expand Down
138 changes: 8 additions & 130 deletions tools/PI/DevHome.PI/BarWindowVertical.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@ public partial class BarWindowVertical : WindowEx
private int _appWindowPosY; // = 0;
private bool isWindowMoving; // = false;
private bool isClosing;
private const int UnsnapGap = 9;

private readonly WINEVENTPROC _winPositionEventDelegate;
private readonly WINEVENTPROC _winFocusEventDelegate;

private Button? _selectedExternalToolButton;

private HWINEVENTHOOK _positionEventHook;
private HWINEVENTHOOK _focusEventHook;

internal HWND ThisHwnd { get; private set; }

public Microsoft.UI.Dispatching.DispatcherQueue TheDispatcher
Expand All @@ -65,23 +58,6 @@ public BarWindowVertical(BarWindowViewModel model)

InitializeComponent();
_viewModel.PropertyChanged += ViewModel_PropertyChanged;
_winPositionEventDelegate = new(WinPositionEventProc);
_winFocusEventDelegate = new(WinFocusEventProc);
}

private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(BarWindowViewModel.IsSnapped))
{
if (_viewModel.IsSnapped)
{
Snap();
}
else
{
Unsnap();
}
}
}

private void MainPanel_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -96,6 +72,14 @@ private void MainPanel_Loaded(object sender, RoutedEventArgs e)
Width = 70;
}

private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (string.Equals(e.PropertyName, nameof(BarWindowViewModel.WindowPosition), StringComparison.OrdinalIgnoreCase))
{
this.Move(_viewModel.WindowPosition.X, _viewModel.WindowPosition.Y);
}
}

private void ExternalToolsMenu_Opening(object sender, object e)
{
// Cancel the opening of the menu if there are no items.
Expand Down Expand Up @@ -130,18 +114,6 @@ private void ExternalToolButton_Click(object sender, RoutedEventArgs e)

private void WindowEx_Closed(object sender, WindowEventArgs args)
{
if (_positionEventHook != IntPtr.Zero)
{
PInvoke.UnhookWinEvent(_positionEventHook);
_positionEventHook = HWINEVENTHOOK.Null;
}

if (_focusEventHook != HWINEVENTHOOK.Null)
{
PInvoke.UnhookWinEvent(_focusEventHook);
_focusEventHook = HWINEVENTHOOK.Null;
}

if (!isClosing)
{
isClosing = true;
Expand Down Expand Up @@ -180,100 +152,6 @@ internal void SetRequestedTheme(ElementTheme theme)
}
}

private void WinPositionEventProc(HWINEVENTHOOK hWinEventHook, uint eventType, HWND hwnd, int idObject, int idChild, uint idEventThread, uint dwmsEventTime)
{
// Filter out events for non-main windows.
if (idObject != 0 || idChild != 0)
{
return;
}

if (hwnd == TargetAppData.Instance.HWnd)
{
if (eventType == PInvoke.EVENT_OBJECT_LOCATIONCHANGE)
{
if (_viewModel.IsSnapped)
{
// If the window has been maximized, un-snap the bar window and free-float it.
if (PInvoke.IsZoomed(TargetAppData.Instance.HWnd))
{
_viewModel.IsSnapped = false;
}
else
{
// Reposition the window to match the moved/resized/minimized/restored target window.
// If the target window was maximized and has now been restored, we want
// to resnap to it, but not do all the other work we do when we resnap
// to a new window.
SnapToWindow();
}
}
}

// If the window we're watching closes, we unsnap
if (eventType == PInvoke.EVENT_OBJECT_DESTROY)
{
Unsnap();
}
}
}

private void WinFocusEventProc(HWINEVENTHOOK hWinEventHook, uint eventType, HWND hwnd, int idObject, int idChild, uint idEventThread, uint dwmsEventTime)
{
// If we're snapped to a target window, and that window loses and then regains focus,
// we need to bring our window to the front also, to be in-sync. Otherwise, we can
// end up with the target in the foreground, but our window partially obscured.
if (hwnd == TargetAppData.Instance.HWnd && _viewModel.IsSnapped)
{
this.SetIsAlwaysOnTop(true);
this.SetIsAlwaysOnTop(false);
return;
}
}

private void Snap()
{
Debug.Assert(_positionEventHook == HWINEVENTHOOK.Null, "Hook should be cleared");
Debug.Assert(_focusEventHook == HWINEVENTHOOK.Null, "Hook should be cleared");

_positionEventHook = WatchWindowPositionEvents(_winPositionEventDelegate, (uint)TargetAppData.Instance.ProcessId);
_focusEventHook = WatchWindowFocusEvents(_winFocusEventDelegate, (uint)TargetAppData.Instance.ProcessId);

SnapToWindow();
}

private void Unsnap()
{
// Set a gap from the associated app window to provide positive feedback.
this.MoveAndResize(
AppWindow.Position.X + UnsnapGap,
AppWindow.Position.Y,
AppWindow.Size.Width,
AppWindow.Size.Height);

if (_positionEventHook != HWINEVENTHOOK.Null)
{
PInvoke.UnhookWinEvent(_positionEventHook);
_positionEventHook = HWINEVENTHOOK.Null;
}

if (_focusEventHook != HWINEVENTHOOK.Null)
{
PInvoke.UnhookWinEvent(_focusEventHook);
_focusEventHook = HWINEVENTHOOK.Null;
}
}

private void SnapToWindow()
{
Debug.Assert(_viewModel.IsSnapped, "We're not snapped!");

WindowHelper.SnapToWindow(TargetAppData.Instance.HWnd, ThisHwnd, AppWindow.Size);

this.SetIsAlwaysOnTop(true);
this.SetIsAlwaysOnTop(false);
}

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
var primaryWindow = Application.Current.GetService<PrimaryWindow>();
Expand Down
Loading
Loading