Skip to content

Commit

Permalink
Merge pull request #507 from ghost1372/Fix-TitleBar2
Browse files Browse the repository at this point in the history
Fix titlebar drag regions
  • Loading branch information
niels9001 authored Nov 6, 2023
2 parents b21d4c3 + f748819 commit 489cf3a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
59 changes: 29 additions & 30 deletions components/TitleBar/src/TitleBar.WASDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.UI;
using Microsoft.UI.Windowing;
using static CommunityToolkit.WinUI.Controls.NativeMethods;
using WinRT.Interop;

namespace CommunityToolkit.WinUI.Controls;

Expand All @@ -18,6 +17,7 @@ namespace CommunityToolkit.WinUI.Controls;
[TemplatePart(Name = nameof(PART_FooterColumn), Type = typeof(ColumnDefinition))]
[TemplatePart(Name = nameof(PART_RightDragColumn), Type = typeof(ColumnDefinition))]
[TemplatePart(Name = nameof(PART_TitleHolder), Type = typeof(StackPanel))]
[TemplatePart(Name = nameof(PART_RootGrid), Type = typeof(Grid))]

public partial class TitleBar : Control
{
Expand All @@ -29,6 +29,7 @@ public partial class TitleBar : Control
ColumnDefinition? PART_FooterColumn;
ColumnDefinition? PART_RightDragColumn;
StackPanel? PART_TitleHolder;
Grid? PART_RootGrid;

private void SetWASDKTitleBar()
{
Expand Down Expand Up @@ -62,6 +63,7 @@ private void SetWASDKTitleBar()
PART_RightDragColumn = GetTemplateChild(nameof(PART_RightDragColumn)) as ColumnDefinition;
PART_FooterColumn = GetTemplateChild(nameof(PART_FooterColumn)) as ColumnDefinition;
PART_TitleHolder = GetTemplateChild(nameof(PART_TitleHolder)) as StackPanel;
PART_RootGrid = GetTemplateChild(nameof(PART_RootGrid)) as Grid;

// Get caption button occlusion information.
int CaptionButtonOcclusionWidthRight = Window.AppWindow.TitleBar.RightInset;
Expand Down Expand Up @@ -136,44 +138,41 @@ private void SetDragRegionForCustomTitleBar()
PART_RightPaddingColumn.Width = new GridLength(Window.AppWindow.TitleBar.RightInset / scaleAdjustment);
PART_LeftPaddingColumn.Width = new GridLength(Window.AppWindow.TitleBar.LeftInset / scaleAdjustment);

List<Windows.Graphics.RectInt32> dragRectsList = new();

Windows.Graphics.RectInt32 dragRectL;
dragRectL.X = (int)((PART_LeftPaddingColumn.ActualWidth
+ PART_ButtonsHolderColumn!.ActualWidth)
* scaleAdjustment);
dragRectL.Y = 0;
dragRectL.Height = (int)(this.ActualHeight * scaleAdjustment);
dragRectL.Width = (int)((PART_IconColumn!.ActualWidth
+ PART_TitleColumn!.ActualWidth
+ PART_LeftDragColumn!.ActualWidth)
* scaleAdjustment);
dragRectsList.Add(dragRectL);

Windows.Graphics.RectInt32 dragRectR;
dragRectR.X = (int)((PART_LeftPaddingColumn.ActualWidth
+ PART_IconColumn.ActualWidth
+ PART_ButtonsHolderColumn!.ActualWidth
+ PART_TitleHolder!.ActualWidth
+ PART_LeftDragColumn.ActualWidth
+ PART_ContentColumn!.ActualWidth)
var height = (int)(this.ActualHeight * scaleAdjustment);
Windows.Graphics.RectInt32 rect1 = new(0, 0, 0, height);
Windows.Graphics.RectInt32 rect2 = new(0, 0, 0, height);
Windows.Graphics.RectInt32 rect3 = new(0, 0, 0, height);
Windows.Graphics.RectInt32 rect4 = new(0, 0, 0, height);

rect1.X = 0;
rect1.Width = (int)((PART_RootGrid.Padding.Left

Check failure on line 148 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.

Check failure on line 148 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
+ PART_LeftPaddingColumn.ActualWidth)
* scaleAdjustment);
dragRectR.Y = 0;
dragRectR.Height = (int)(this.ActualHeight * scaleAdjustment);
dragRectR.Width = (int)(PART_RightDragColumn!.ActualWidth * scaleAdjustment);
dragRectsList.Add(dragRectR);

Windows.Graphics.RectInt32[] dragRects = dragRectsList.ToArray();
rect2.X = rect1.X + rect1.Width + (int)((PART_ButtonsHolderColumn.ActualWidth) * scaleAdjustment);

Check failure on line 152 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.

Check failure on line 152 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
rect2.Width = (int)((PART_IconColumn.ActualWidth

Check failure on line 153 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
+ PART_TitleColumn.ActualWidth

Check failure on line 154 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
+ PART_LeftDragColumn.ActualWidth)

Check failure on line 155 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
* scaleAdjustment);

rect3.X = rect2.X + rect2.Width + (int)(PART_ContentColumn.ActualWidth * scaleAdjustment);

Check failure on line 158 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
rect3.Width = (int)(PART_RightDragColumn.ActualWidth * scaleAdjustment);

Check failure on line 159 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.

rect4.X = rect3.X + rect3.Width + (int)((PART_FooterColumn.ActualWidth

Check failure on line 161 in components/TitleBar/src/TitleBar.WASDK.cs

View workflow job for this annotation

GitHub Actions / build (WinUI3)

Dereference of a possibly null reference.
+ PART_RightPaddingColumn.ActualWidth
+ PART_RootGrid.Padding.Right)
* scaleAdjustment);
rect4.Width = (int)(PART_RightPaddingColumn.ActualWidth * scaleAdjustment);

Windows.Graphics.RectInt32[] dragRects = new[] { rect1, rect2, rect3, rect4 };

Window.AppWindow.TitleBar.SetDragRectangles(dragRects);
}
}

private double GetScaleAdjustment()
{
IntPtr hWnd = WindowNative.GetWindowHandle(this.Window);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
DisplayArea displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary);
DisplayArea displayArea = DisplayArea.GetFromWindowId(this.Window.AppWindow.Id, DisplayAreaFallback.Primary);
IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId);

// Get DPI.
Expand Down
2 changes: 1 addition & 1 deletion components/TitleBar/src/TitleBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ControlTemplate TargetType="local:TitleBar">
<Grid x:Name="PART_RootGrid"
Height="{TemplateBinding MinHeight}"
Padding="4,0,4,0"
Padding="4,0,0,0"
VerticalAlignment="Stretch"
Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
Expand Down

0 comments on commit 489cf3a

Please sign in to comment.