Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit fd5a96e

Browse files
Limit selection change to focused node on Windows (#38634)
* Limit selection change to focused node on Windows * Focus fix * Test document selection change * Comment * Formatting * Update shell/platform/windows/accessibility_bridge_windows.cc Co-authored-by: Loïc Sharma <[email protected]> Co-authored-by: Loïc Sharma <[email protected]>
1 parent 03609b4 commit fd5a96e

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

shell/platform/common/accessibility_bridge.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,12 @@ void AccessibilityBridge::SetTooltipFromFlutterUpdate(
527527
void AccessibilityBridge::SetTreeData(const SemanticsNode& node,
528528
ui::AXTreeUpdate& tree_update) {
529529
FlutterSemanticsFlag flags = node.flags;
530-
// Set selection if:
530+
// Set selection of the focused node if:
531531
// 1. this text field has a valid selection
532532
// 2. this text field doesn't have a valid selection but had selection stored
533533
// in the tree.
534-
if (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField) {
534+
if (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField &&
535+
flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsFocused) {
535536
if (node.text_selection_base != -1) {
536537
tree_update.tree_data.sel_anchor_object_id = node.id;
537538
tree_update.tree_data.sel_anchor_offset = node.text_selection_base;

shell/platform/common/accessibility_bridge_unittests.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ TEST(AccessibilityBridgeTest, canHandleSelectionChangeCorrectly) {
152152
std::shared_ptr<TestAccessibilityBridge> bridge =
153153
std::make_shared<TestAccessibilityBridge>();
154154
FlutterSemanticsNode root = CreateSemanticsNode(0, "root");
155-
root.flags = FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField;
155+
root.flags = static_cast<FlutterSemanticsFlag>(
156+
FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField |
157+
FlutterSemanticsFlag::kFlutterSemanticsFlagIsFocused);
156158
bridge->AddFlutterSemanticsNodeUpdate(&root);
157159
bridge->CommitUpdates();
158160

shell/platform/windows/accessibility_bridge_windows.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,23 @@ void AccessibilityBridgeWindows::OnAccessibilityEvent(
4141
DispatchWinAccessibilityEvent(win_delegate,
4242
ax::mojom::Event::kChildrenChanged);
4343
break;
44-
case ui::AXEventGenerator::Event::DOCUMENT_SELECTION_CHANGED:
44+
case ui::AXEventGenerator::Event::DOCUMENT_SELECTION_CHANGED: {
45+
// An event indicating a change in document selection should be fired
46+
// only for the focused node whose selection has changed. If a valid
47+
// caret and selection exist in the app tree, they must both be within
48+
// the focus node.
49+
ui::AXNode::AXID focus_id = GetAXTreeData().sel_focus_object_id;
50+
auto focus_delegate =
51+
GetFlutterPlatformNodeDelegateFromID(focus_id).lock();
52+
if (!focus_delegate) {
53+
win_delegate =
54+
std::static_pointer_cast<FlutterPlatformNodeDelegateWindows>(
55+
focus_delegate);
56+
}
4557
DispatchWinAccessibilityEvent(
4658
win_delegate, ax::mojom::Event::kDocumentSelectionChanged);
4759
break;
60+
}
4861
case ui::AXEventGenerator::Event::FOCUS_CHANGED:
4962
DispatchWinAccessibilityEvent(win_delegate, ax::mojom::Event::kFocus);
5063
SetFocus(win_delegate);

shell/platform/windows/accessibility_bridge_windows_unittests.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,11 @@ TEST(AccessibilityBridgeWindows, OnAccessibilityStateChanged) {
338338
ax::mojom::Event::kStateChanged);
339339
}
340340

341+
TEST(AccessibilityBridgeWindows, OnDocumentSelectionChanged) {
342+
ExpectWinEventFromAXEvent(
343+
1, ui::AXEventGenerator::Event::DOCUMENT_SELECTION_CHANGED,
344+
ax::mojom::Event::kDocumentSelectionChanged);
345+
}
346+
341347
} // namespace testing
342348
} // namespace flutter

0 commit comments

Comments
 (0)