From 2a346cbce7a13947d6e3f3b34f8812da0fb9c86d Mon Sep 17 00:00:00 2001 From: HO-COOH <42881734+HO-COOH@users.noreply.github.com> Date: Wed, 12 Jun 2024 06:51:20 +0800 Subject: [PATCH] Fix: Interactive element is no longer interactive after adding it a second time (#1550) ## 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 - [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) --- .../ControlPages/TitleBarPage.xaml.cs | 43 +++++++++++-------- .../Navigation/NavigationRootPage.xaml | 4 +- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/WinUIGallery/ControlPages/TitleBarPage.xaml.cs b/WinUIGallery/ControlPages/TitleBarPage.xaml.cs index 0806e701e..f9112e44d 100644 --- a/WinUIGallery/ControlPages/TitleBarPage.xaml.cs +++ b/WinUIGallery/ControlPages/TitleBarPage.xaml.cs @@ -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; @@ -204,7 +221,11 @@ 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 @@ -212,26 +233,14 @@ private void AddInteractiveElements_Click(object sender, RoutedEventArgs 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"); } } diff --git a/WinUIGallery/Navigation/NavigationRootPage.xaml b/WinUIGallery/Navigation/NavigationRootPage.xaml index 5650a18a3..453ee5007 100644 --- a/WinUIGallery/Navigation/NavigationRootPage.xaml +++ b/WinUIGallery/Navigation/NavigationRootPage.xaml @@ -59,8 +59,8 @@ AutomationProperties.AutomationId="AppTitleBar" Canvas.ZIndex="1" IsHitTestVisible="True"> - - + +