macOS: Update platform node when AXNodeData role changes (flutter/engine#54364)

Fixes https://github.com/flutter/flutter/issues/151428

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
This commit is contained in:
Matej Knopp
2024-08-06 21:51:36 +02:00
committed by GitHub
parent 31f50610c0
commit e3794a82e0
9 changed files with 121 additions and 4 deletions

View File

@@ -163,6 +163,17 @@ void AccessibilityBridge::OnRoleChanged(ui::AXTree* tree,
ax::mojom::Role old_role,
ax::mojom::Role new_role) {}
void AccessibilityBridge::OnNodeDataChanged(
ui::AXTree* tree,
const ui::AXNodeData& old_node_data,
const ui::AXNodeData& new_node_data) {
auto platform_view =
GetFlutterPlatformNodeDelegateFromID(new_node_data.id).lock();
if (platform_view) {
platform_view->NodeDataChanged(old_node_data, new_node_data);
}
}
void AccessibilityBridge::OnNodeCreated(ui::AXTree* tree, ui::AXNode* node) {
BASE_DCHECK(node);
id_wrapper_map_[node->id()] = CreateFlutterPlatformNodeDelegate();

View File

@@ -260,6 +260,11 @@ class AccessibilityBridge
ax::mojom::Role old_role,
ax::mojom::Role new_role) override;
// |AXTreeObserver|
void OnNodeDataChanged(ui::AXTree* tree,
const ui::AXNodeData& old_node_data,
const ui::AXNodeData& new_node_data) override;
// |AXTreeObserver|
void OnAtomicUpdateFinished(
ui::AXTree* tree,

View File

@@ -139,6 +139,12 @@ class FlutterPlatformNodeDelegate : public ui::AXPlatformNodeDelegateBase {
/// Subclasses must call super.
virtual void Init(std::weak_ptr<OwnerBridge> bridge, ui::AXNode* node);
//------------------------------------------------------------------------------
// @brief Called when node was updated. Subclasses can override this
// to update platform nodes.
virtual void NodeDataChanged(const ui::AXNodeData& old_node_data,
const ui::AXNodeData& new_node_data) {}
//------------------------------------------------------------------------------
/// @brief Gets the underlying ax node for this platform node delegate.
ui::AXNode* GetAXNode() const;

View File

@@ -165,11 +165,11 @@ AccessibilityBridgeMac::MacOSEventsFromAXEvent(ui::AXEventGenerator::Event event
// If it is a text field, the value change notifications are handled by
// the FlutterTextField directly. Only need to make sure it is the
// first responder.
FlutterTextField* native_text_field =
(FlutterTextField*)mac_platform_node_delegate->GetNativeViewAccessible();
id native_text_field = mac_platform_node_delegate->GetNativeViewAccessible();
FML_DCHECK([native_text_field isKindOfClass:FlutterTextField.class]);
id focused = mac_platform_node_delegate->GetFocus();
if (!focused || native_text_field == focused) {
[native_text_field startEditing];
[(FlutterTextField*)native_text_field startEditing];
}
break;
}

View File

@@ -27,6 +27,9 @@ class FlutterPlatformNodeDelegateMac : public FlutterPlatformNodeDelegate {
void Init(std::weak_ptr<OwnerBridge> bridge, ui::AXNode* node) override;
void NodeDataChanged(const ui::AXNodeData& old_node_data,
const ui::AXNodeData& new_node_data) override;
//---------------------------------------------------------------------------
/// @brief Gets the live region text of this node in UTF-8 format. This
/// is useful to determine the changes in between semantics

View File

@@ -35,6 +35,17 @@ void FlutterPlatformNodeDelegateMac::Init(std::weak_ptr<OwnerBridge> bridge, ui:
NSCAssert(ax_platform_node_, @"Failed to create platform node.");
}
void FlutterPlatformNodeDelegateMac::NodeDataChanged(const ui::AXNodeData& old_node_data,
const ui::AXNodeData& new_node_data) {
if (old_node_data.IsTextField() && !new_node_data.IsTextField()) {
ax_platform_node_->Destroy();
ax_platform_node_ = ui::AXPlatformNode::Create(this);
} else if (!old_node_data.IsTextField() && new_node_data.IsTextField()) {
ax_platform_node_->Destroy();
ax_platform_node_ = new FlutterTextPlatformNode(this, view_controller_);
}
}
FlutterPlatformNodeDelegateMac::~FlutterPlatformNodeDelegateMac() {
// Destroy() also calls delete on itself.
ax_platform_node_->Destroy();

View File

@@ -296,4 +296,81 @@ TEST(FlutterPlatformNodeDelegateMac, TextFieldUsesFlutterTextField) {
EXPECT_EQ([native_text_field.stringValue isEqualToString:@"textfield"], YES);
}
TEST(FlutterPlatformNodeDelegateMac, ChangingFlagsUpdatesNativeViewAccessible) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
[viewController loadView];
// Creates a NSWindow so that the native text field can become first responder.
NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
window.contentView = viewController.view;
engine.semanticsEnabled = YES;
auto bridge = viewController.accessibilityBridge.lock();
// Initialize ax node data.
FlutterSemanticsNode2 root;
root.id = 0;
root.flags = static_cast<FlutterSemanticsFlag>(0);
root.actions = static_cast<FlutterSemanticsAction>(0);
root.label = "root";
root.hint = "";
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
root.custom_accessibility_actions_count = 0;
root.rect = {0, 0, 100, 100}; // LTRB
root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
bridge->AddFlutterSemanticsNodeUpdate(root);
double rectSize = 50;
double transformFactor = 0.5;
FlutterSemanticsNode2 child1;
child1.id = 1;
child1.flags = static_cast<FlutterSemanticsFlag>(0);
child1.actions = static_cast<FlutterSemanticsAction>(0);
child1.label = "";
child1.hint = "";
child1.value = "textfield";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.text_selection_base = -1;
child1.text_selection_extent = -1;
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
child1.rect = {0, 0, rectSize, rectSize}; // LTRB
child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
bridge->AddFlutterSemanticsNodeUpdate(child1);
bridge->CommitUpdates();
auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
// Verify the accessibility attribute matches.
id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
EXPECT_TRUE([[native_accessibility className] isEqualToString:@"AXPlatformNodeCocoa"]);
// Converting child to text field should produce `FlutterTextField` native view accessible.
child1.flags = FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField;
bridge->AddFlutterSemanticsNodeUpdate(child1);
bridge->CommitUpdates();
native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
EXPECT_TRUE([native_accessibility isKindOfClass:[FlutterTextField class]]);
child1.flags = static_cast<FlutterSemanticsFlag>(0);
bridge->AddFlutterSemanticsNodeUpdate(child1);
bridge->CommitUpdates();
native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
EXPECT_TRUE([[native_accessibility className] isEqualToString:@"AXPlatformNodeCocoa"]);
}
} // namespace flutter::testing

View File

@@ -190,6 +190,9 @@ NSRect FlutterTextPlatformNode::GetFrame() {
FlutterPlatformNodeDelegate* delegate = static_cast<FlutterPlatformNodeDelegate*>(GetDelegate());
bool offscreen;
auto bridge_ptr = delegate->GetOwnerBridge().lock();
if (!bridge_ptr) {
return NSZeroRect;
}
gfx::RectF bounds = bridge_ptr->RelativeToGlobalBounds(delegate->GetAXNode(), offscreen, true);
// Converts to NSRect to use NSView rect conversion.

View File

@@ -519,7 +519,8 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
}
- (void)updateSemantics:(const FlutterSemanticsUpdate2*)update {
NSAssert(_engine.semanticsEnabled, @"Semantics must be enabled.");
// Semantics will be disabled when unfocusing application but the updateSemantics:
// callback is received in next run loop turn.
if (!_engine.semanticsEnabled) {
return;
}