Limit selection change to focused node on Windows (flutter/engine#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 <737941+loic-sharma@users.noreply.github.com>

Co-authored-by: Loïc Sharma <737941+loic-sharma@users.noreply.github.com>
This commit is contained in:
yaakovschectman
2023-01-06 16:36:11 -05:00
committed by GitHub
parent 383ce52b0d
commit c2d59665dd
4 changed files with 26 additions and 4 deletions

View File

@@ -527,11 +527,12 @@ void AccessibilityBridge::SetTooltipFromFlutterUpdate(
void AccessibilityBridge::SetTreeData(const SemanticsNode& node,
ui::AXTreeUpdate& tree_update) {
FlutterSemanticsFlag flags = node.flags;
// Set selection if:
// Set selection of the focused node if:
// 1. this text field has a valid selection
// 2. this text field doesn't have a valid selection but had selection stored
// in the tree.
if (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField) {
if (flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField &&
flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsFocused) {
if (node.text_selection_base != -1) {
tree_update.tree_data.sel_anchor_object_id = node.id;
tree_update.tree_data.sel_anchor_offset = node.text_selection_base;

View File

@@ -152,7 +152,9 @@ TEST(AccessibilityBridgeTest, canHandleSelectionChangeCorrectly) {
std::shared_ptr<TestAccessibilityBridge> bridge =
std::make_shared<TestAccessibilityBridge>();
FlutterSemanticsNode root = CreateSemanticsNode(0, "root");
root.flags = FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField;
root.flags = static_cast<FlutterSemanticsFlag>(
FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField |
FlutterSemanticsFlag::kFlutterSemanticsFlagIsFocused);
bridge->AddFlutterSemanticsNodeUpdate(&root);
bridge->CommitUpdates();

View File

@@ -41,10 +41,23 @@ void AccessibilityBridgeWindows::OnAccessibilityEvent(
DispatchWinAccessibilityEvent(win_delegate,
ax::mojom::Event::kChildrenChanged);
break;
case ui::AXEventGenerator::Event::DOCUMENT_SELECTION_CHANGED:
case ui::AXEventGenerator::Event::DOCUMENT_SELECTION_CHANGED: {
// An event indicating a change in document selection should be fired
// only for the focused node whose selection has changed. If a valid
// caret and selection exist in the app tree, they must both be within
// the focus node.
ui::AXNode::AXID focus_id = GetAXTreeData().sel_focus_object_id;
auto focus_delegate =
GetFlutterPlatformNodeDelegateFromID(focus_id).lock();
if (!focus_delegate) {
win_delegate =
std::static_pointer_cast<FlutterPlatformNodeDelegateWindows>(
focus_delegate);
}
DispatchWinAccessibilityEvent(
win_delegate, ax::mojom::Event::kDocumentSelectionChanged);
break;
}
case ui::AXEventGenerator::Event::FOCUS_CHANGED:
DispatchWinAccessibilityEvent(win_delegate, ax::mojom::Event::kFocus);
SetFocus(win_delegate);

View File

@@ -338,5 +338,11 @@ TEST(AccessibilityBridgeWindows, OnAccessibilityStateChanged) {
ax::mojom::Event::kStateChanged);
}
TEST(AccessibilityBridgeWindows, OnDocumentSelectionChanged) {
ExpectWinEventFromAXEvent(
1, ui::AXEventGenerator::Event::DOCUMENT_SELECTION_CHANGED,
ax::mojom::Event::kDocumentSelectionChanged);
}
} // namespace testing
} // namespace flutter