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

Fix: Interactive element is no longer interactive after adding it a second time #1550

Merged
merged 4 commits into from
Jun 11, 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
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