Skip to content

Commit

Permalink
Fix: Interactive element is no longer interactive after adding it a s…
Browse files Browse the repository at this point in the history
…econd time (#1550)

<!--- Provide a general summary of your changes in the Title above -->

## Description
- `AppTitleBarTextBox` 's `Width` is not set, so `Width +=1 ` does
nothing
- The `SizeChanged` event handler is called **once**, because of
`Visibility` changed, and it was put in a `StackPanel` that's vertically
constrained, so the size change will occur once, not because `Width +=
1`. Consecutive adding it to the title bar no longer triggers the event
handler, therefore the issue
- And `Width += 1` is a dirty hack anyway, does not need that.

## Motivation and Context
Close #1470

## How Has This Been Tested?
Manual.

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
HO-COOH committed Jun 11, 2024
1 parent c8bc86d commit 2a346cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
43 changes: 26 additions & 17 deletions WinUIGallery/ControlPages/TitleBarPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ private void defaultTitleBar_Click(object sender, RoutedEventArgs e)
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
}

private void setTxtBoxAsPasthrough(FrameworkElement txtBoxNonClientArea)
{
GeneralTransform transformTxtBox = txtBoxNonClientArea.TransformToVisual(null);
Rect bounds = transformTxtBox.TransformBounds(new Rect(0, 0, txtBoxNonClientArea.ActualWidth, txtBoxNonClientArea.ActualHeight));

var scale = WindowHelper.GetRasterizationScaleForElement(this);

var transparentRect = new Windows.Graphics.RectInt32(
_X: (int)Math.Round(bounds.X * scale),
_Y: (int)Math.Round(bounds.Y * scale),
_Width: (int)Math.Round(bounds.Width * scale),
_Height: (int)Math.Round(bounds.Height * scale)
);
var rectArr = new Windows.Graphics.RectInt32[] { transparentRect };
SetClickThruRegions(rectArr);
}

private void AddInteractiveElements_Click(object sender, RoutedEventArgs e)
{
var txtBoxNonClientArea = UIHelper.FindElementByName(sender as UIElement, "AppTitleBarTextBox") as FrameworkElement;
Expand All @@ -204,34 +221,26 @@ private void AddInteractiveElements_Click(object sender, RoutedEventArgs e)
{
addInteractiveElements.Content = "Remove interactive control from titlebar";
txtBoxNonClientArea.Visibility = Visibility.Visible;
if (!sizeChangedEventHandlerAdded)
if (sizeChangedEventHandlerAdded)
{
setTxtBoxAsPasthrough(txtBoxNonClientArea);
}
else
{
sizeChangedEventHandlerAdded = true;
// run this code when textbox has been made visible and its actual width and height has been calculated
txtBoxNonClientArea.SizeChanged += (object sender, SizeChangedEventArgs e) =>
{
if (txtBoxNonClientArea.Visibility != Visibility.Collapsed)
{
GeneralTransform transformTxtBox = txtBoxNonClientArea.TransformToVisual(null);
Rect bounds = transformTxtBox.TransformBounds(new Rect(0, 0, txtBoxNonClientArea.ActualWidth, txtBoxNonClientArea.ActualHeight));
var scale = WindowHelper.GetRasterizationScaleForElement(this);
var transparentRect = new Windows.Graphics.RectInt32(
_X: (int)Math.Round(bounds.X * scale),
_Y: (int)Math.Round(bounds.Y * scale),
_Width: (int)Math.Round(bounds.Width * scale),
_Height: (int)Math.Round(bounds.Height * scale)
);
var rectArr = new Windows.Graphics.RectInt32[] { transparentRect };
SetClickThruRegions(rectArr);
setTxtBoxAsPasthrough(txtBoxNonClientArea);
}
};
}
txtBoxNonClientArea.Width += 1; //to trigger size changed event

// announce visual change to automation
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
}
// announce visual change to automation
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
}

}
Expand Down
4 changes: 2 additions & 2 deletions WinUIGallery/Navigation/NavigationRootPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
AutomationProperties.AutomationId="AppTitleBar"
Canvas.ZIndex="1"
IsHitTestVisible="True">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<Image Width="18" Source="ms-appx:///Assets/Tiles/TitlebarLogo.png" />
<StackPanel VerticalAlignment="Stretch" Orientation="Horizontal">
<Image Width="18" VerticalAlignment="Center" Source="ms-appx:///Assets/Tiles/TitlebarLogo.png" />
<TextBlock
x:Name="AppTitle"
Margin="12,0,0,0"
Expand Down

0 comments on commit 2a346cb

Please sign in to comment.