Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Input;
using Windows.Storage.Streams;
using Windows.System;

namespace Microsoft.Maui.Controls.Platform
{
Expand Down Expand Up @@ -400,6 +401,64 @@ void ClearContainerEventHandlers()
}
}
}

// Reset tab stop state when clearing gesture handlers for Border
UpdateContentPanelIsTapStop(false);
}

// Sets or resets the IsTabStop property on ContentPanel (Border) when gesture recognizers are added or removed for border.
// This allows Border to be focusable when it has TapGestureRecognizers for keyboard accessibility.
void UpdateContentPanelIsTapStop(bool canAllowTabStop)
{
if (_control is ContentPanel contentPanel && contentPanel.CrossPlatformLayout is IBorderView)
{
if (canAllowTabStop && !contentPanel.IsTabStop)
{
contentPanel.IsTabStop = true;
contentPanel.KeyDown += ContentPanelOnKeyDown;
}
else if (!canAllowTabStop && contentPanel.IsTabStop)
{
contentPanel.IsTabStop = false;
contentPanel.KeyDown -= ContentPanelOnKeyDown;
}
}
}

// Handle Enter and Space key presses to trigger TapGestureRecognizers on Border for keyboard accessibility
// This is only applicable when the Border has TapGestureRecognizers attached
void ContentPanelOnKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter || e.Key == VirtualKey.Space)
{
if (Element is View view)
{
IEnumerable<TapGestureRecognizer> tapGestures = view.GestureRecognizers.GetGesturesFor<TapGestureRecognizer>(recognizer =>
recognizer.NumberOfTapsRequired == 1 &&
(recognizer.Buttons & ButtonsMask.Primary) == ButtonsMask.Primary);

if (tapGestures is null)
{
return;
}

bool handled = false;

foreach (var recognizer in tapGestures)
{
recognizer.SendTapped(view, (relativeTo) =>
{
return new Point(view.X, view.Y);
});
handled = true;
}

if (handled)
{
e.Handled = true;
}
}
}
}

protected virtual void Dispose(bool disposing)
Expand Down Expand Up @@ -434,6 +493,9 @@ protected virtual void Dispose(bool disposing)
{
_control.DoubleTapped -= HandleDoubleTapped;
}

// Reset tab stop state when clearing gesture handlers for border layouts
UpdateContentPanelIsTapStop(false);
}

Control = null;
Expand Down Expand Up @@ -973,6 +1035,9 @@ void UpdatingGestureRecognizers()
}

_container.RightTapped += OnTap;

// Set tab stop state when adding gesture handlers for border layouts
UpdateContentPanelIsTapStop(true);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,38 @@ await InvokeOnMainThreadAsync(() =>
});
}

[Theory(DisplayName = "Border ContentPanel IsTabStop reflects TapGestureRecognizer state")]
[InlineData(false)]
[InlineData(true)]
public async Task ContentPanelIsTabStopReflectsTapGestureRecognizer(bool hasTapGestureRecognizer)
{
SetupBuilder();

var border = new Border()
{
Content = new Label
{
Text = "Focusable Border"
},
StrokeShape = new Rectangle(),
WidthRequest = 300,
HeightRequest = 100
};

if (hasTapGestureRecognizer)
{
border.GestureRecognizers.Add(new TapGestureRecognizer());
}

await AttachAndRun(border, handler =>
{
var contentPanel = GetNativeBorder(handler as BorderHandler);
Assert.Equal(hasTapGestureRecognizer, contentPanel.IsTabStop);
});
}

ContentPanel GetNativeBorder(BorderHandler borderHandler) =>
borderHandler.PlatformView;

}
}
}
43 changes: 43 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue27627.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 27627, "[Windows] Border Automation Peer", PlatformAffected.UWP)]
public partial class Issue27627 : ContentPage
{
public Issue27627()
{
var grid = new Grid();

var border = new Border
{
AutomationId = "TestBorder",
VerticalOptions = LayoutOptions.Center,
HeightRequest = 100,
Padding = new Thickness(10),
Stroke = Colors.Red
};

var label = new Label
{
Text = "Welcome to Maui!",
AutomationId = "TestLabel",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};

var nestedBorder = new Border
{
Stroke = Colors.Blue,
AutomationId = "NestedBorder",
StrokeThickness = 2,
Padding = new Thickness(10),
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
};

nestedBorder.Content = label;
border.Content = nestedBorder;

grid.Children.Add(border);
Content = grid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue27627 : _IssuesUITest
{
public override string Issue => "[Windows] Border Automation Peer";

public Issue27627(TestDevice device)
: base(device)
{ }

[Test]
[Category(UITestCategories.Border)]
public void VerifyBorderAutomationPeer()
{
// Check whether the parent Border is found or not
App.WaitForElement("TestBorder");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before you set the automation peer, is this not possible? I mean, this test appears to be looking for a element with this ID, so I just want to check if the test would fail without this new peer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the test failed without the new peer, and it passed after adding it. I have attached the output images for your reference.

Without this peer With this peer


// Check whether the nested Border is found or not
App.WaitForElement("NestedBorder");

// Check whether the Label inside the nested Border is found or not
App.WaitForElement("TestLabel");
}
}
}
11 changes: 11 additions & 0 deletions src/Core/src/Platform/Windows/ContentPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.UI.Xaml.Automation.Peers;

namespace Microsoft.Maui.Platform
{
Expand Down Expand Up @@ -65,6 +66,16 @@ internal FrameworkElement? Content
return size;
}

protected override AutomationPeer OnCreateAutomationPeer()
{
if (CrossPlatformLayout is IBorderView)
{
return new MauiBorderAutomationPeer(this);
}

return base.OnCreateAutomationPeer();
}

public ContentPanel()
{
_borderPath = new Path();
Expand Down
46 changes: 46 additions & 0 deletions src/Core/src/Platform/Windows/MauiBorderAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Platform
{
// TODO: Make this class public in .NET11.0. Issue Link: https://github.com/dotnet/maui/issues/30205
internal partial class MauiBorderAutomationPeer : FrameworkElementAutomationPeer
{
internal MauiBorderAutomationPeer(Panel owner) : base(owner) { }

// Control Type: Returns "Pane" as the control type for the border automation peer
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Pane;
}

// Class Name: Returns "Panel" as the class name for the border automation peer
protected override string GetClassNameCore()
{
return nameof(Panel);
}

// Keyboard Focusable: Allows border to receive keyboard focus only when it has gesture recognizers (IsTabStop = true)
protected override bool IsKeyboardFocusableCore() => (Owner as ContentPanel)?.IsTabStop == true;

// Control View: Expose only when the Border is interactive (has tap gestures) or has an explicit AutomationId
protected override bool IsControlElementCore() => IsInteractiveOrNamed();

// Content View: Expose only when the Border is interactive (has tap gestures) or has an explicit AutomationId
protected override bool IsContentElementCore() => IsInteractiveOrNamed();

// Gates automation exposure: Border appears in the control/content view only when it is interactive
// (IsTabStop == true, set by GesturePlatformManager when tap gestures are present) or explicitly
// named via AutomationId (opted-in for automation visibility by the developer).
bool IsInteractiveOrNamed()
{
if (Owner is not ContentPanel contentPanel)
{
return false;
}

return contentPanel.IsTabStop || !string.IsNullOrEmpty(AutomationProperties.GetAutomationId(contentPanel));
}
}
}
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
override Microsoft.Maui.Platform.ContentPanel.OnCreateAutomationPeer() -> Microsoft.UI.Xaml.Automation.Peers.AutomationPeer!
Loading