Skip to content

Commit 15875ed

Browse files
authored
Fix: Interactive element is no longer interactive after adding it a second 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)
1 parent 0af9840 commit 15875ed

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

WinUIGallery/ControlPages/TitleBarPage.xaml.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ private void defaultTitleBar_Click(object sender, RoutedEventArgs e)
192192
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
193193
}
194194

195+
private void setTxtBoxAsPasthrough(FrameworkElement txtBoxNonClientArea)
196+
{
197+
GeneralTransform transformTxtBox = txtBoxNonClientArea.TransformToVisual(null);
198+
Rect bounds = transformTxtBox.TransformBounds(new Rect(0, 0, txtBoxNonClientArea.ActualWidth, txtBoxNonClientArea.ActualHeight));
199+
200+
var scale = WindowHelper.GetRasterizationScaleForElement(this);
201+
202+
var transparentRect = new Windows.Graphics.RectInt32(
203+
_X: (int)Math.Round(bounds.X * scale),
204+
_Y: (int)Math.Round(bounds.Y * scale),
205+
_Width: (int)Math.Round(bounds.Width * scale),
206+
_Height: (int)Math.Round(bounds.Height * scale)
207+
);
208+
var rectArr = new Windows.Graphics.RectInt32[] { transparentRect };
209+
SetClickThruRegions(rectArr);
210+
}
211+
195212
private void AddInteractiveElements_Click(object sender, RoutedEventArgs e)
196213
{
197214
var txtBoxNonClientArea = UIHelper.FindElementByName(sender as UIElement, "AppTitleBarTextBox") as FrameworkElement;
@@ -204,34 +221,26 @@ private void AddInteractiveElements_Click(object sender, RoutedEventArgs e)
204221
{
205222
addInteractiveElements.Content = "Remove interactive control from titlebar";
206223
txtBoxNonClientArea.Visibility = Visibility.Visible;
207-
if (!sizeChangedEventHandlerAdded)
224+
if (sizeChangedEventHandlerAdded)
225+
{
226+
setTxtBoxAsPasthrough(txtBoxNonClientArea);
227+
}
228+
else
208229
{
209230
sizeChangedEventHandlerAdded = true;
210231
// run this code when textbox has been made visible and its actual width and height has been calculated
211232
txtBoxNonClientArea.SizeChanged += (object sender, SizeChangedEventArgs e) =>
212233
{
213234
if (txtBoxNonClientArea.Visibility != Visibility.Collapsed)
214235
{
215-
GeneralTransform transformTxtBox = txtBoxNonClientArea.TransformToVisual(null);
216-
Rect bounds = transformTxtBox.TransformBounds(new Rect(0, 0, txtBoxNonClientArea.ActualWidth, txtBoxNonClientArea.ActualHeight));
217-
218-
var scale = WindowHelper.GetRasterizationScaleForElement(this);
219-
220-
var transparentRect = new Windows.Graphics.RectInt32(
221-
_X: (int)Math.Round(bounds.X * scale),
222-
_Y: (int)Math.Round(bounds.Y * scale),
223-
_Width: (int)Math.Round(bounds.Width * scale),
224-
_Height: (int)Math.Round(bounds.Height * scale)
225-
);
226-
var rectArr = new Windows.Graphics.RectInt32[] { transparentRect };
227-
SetClickThruRegions(rectArr);
236+
setTxtBoxAsPasthrough(txtBoxNonClientArea);
228237
}
229238
};
230239
}
231-
txtBoxNonClientArea.Width += 1; //to trigger size changed event
240+
241+
// announce visual change to automation
242+
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
232243
}
233-
// announce visual change to automation
234-
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
235244
}
236245

237246
}

WinUIGallery/Navigation/NavigationRootPage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
AutomationProperties.AutomationId="AppTitleBar"
6060
Canvas.ZIndex="1"
6161
IsHitTestVisible="True">
62-
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
63-
<Image Width="18" Source="ms-appx:///Assets/Tiles/TitlebarLogo.png" />
62+
<StackPanel VerticalAlignment="Stretch" Orientation="Horizontal">
63+
<Image Width="18" VerticalAlignment="Center" Source="ms-appx:///Assets/Tiles/TitlebarLogo.png" />
6464
<TextBlock
6565
x:Name="AppTitle"
6666
Margin="12,0,0,0"

0 commit comments

Comments
 (0)