This commit is contained in:
Tong Mu
2023-01-30 17:00:56 -08:00
committed by GitHub
parent 5c3acfecc6
commit 9d1b539c4a
10 changed files with 139 additions and 97 deletions

View File

@@ -23,7 +23,7 @@
* Some single-view APIs will eventually be replaced by their multi-view
* variant. During the deprecation period, the single-view APIs will coexist with
* and work with the multi-view APIs as if the other views don't exist. For
* backward compatibility, single-view APIs will always operate the view with
* backward compatibility, single-view APIs will always operate on the view with
* this ID. Also, the first view assigned to the engine will also have this ID.
*/
extern const uint64_t kFlutterDefaultViewId;

View File

@@ -32,17 +32,15 @@ class AccessibilityBridgeMacSpy : public AccessibilityBridgeMac {
} // namespace
} // namespace flutter::testing
@interface AccessibilityBridgeTestEngine : FlutterEngine
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)
createAccessibilityBridge:(nonnull FlutterEngine*)engine
viewController:(nonnull FlutterViewController*)viewController;
@interface AccessibilityBridgeTestViewController : FlutterViewController
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
(nonnull FlutterEngine*)engine;
@end
@implementation AccessibilityBridgeTestEngine
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)
createAccessibilityBridge:(nonnull FlutterEngine*)engine
viewController:(nonnull FlutterViewController*)viewController {
return std::make_shared<flutter::testing::AccessibilityBridgeMacSpy>(engine, viewController);
@implementation AccessibilityBridgeTestViewController
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
(nonnull FlutterEngine*)engine {
return std::make_shared<flutter::testing::AccessibilityBridgeMacSpy>(engine, self);
}
@end
@@ -56,10 +54,7 @@ FlutterViewController* CreateTestViewController() {
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
FlutterEngine* engine = [[AccessibilityBridgeTestEngine alloc] initWithName:@"test"
project:project
allowHeadlessExecution:true];
return [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
return [[AccessibilityBridgeTestViewController alloc] initWithProject:project];
}
} // namespace
@@ -76,8 +71,8 @@ TEST(AccessibilityBridgeMacTest, sendsAccessibilityCreateNotificationToWindowOfF
// Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
// can query semantics information from.
engine.semanticsEnabled = YES;
auto bridge =
std::reinterpret_pointer_cast<AccessibilityBridgeMacSpy>(engine.accessibilityBridge.lock());
auto bridge = std::reinterpret_pointer_cast<AccessibilityBridgeMacSpy>(
viewController.accessibilityBridge.lock());
FlutterSemanticsNode root;
root.id = 0;
root.flags = static_cast<FlutterSemanticsFlag>(0);
@@ -124,8 +119,8 @@ TEST(AccessibilityBridgeMacTest, doesNotSendAccessibilityCreateNotificationWhenH
// Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
// can query semantics information from.
engine.semanticsEnabled = YES;
auto bridge =
std::reinterpret_pointer_cast<AccessibilityBridgeMacSpy>(engine.accessibilityBridge.lock());
auto bridge = std::reinterpret_pointer_cast<AccessibilityBridgeMacSpy>(
viewController.accessibilityBridge.lock());
FlutterSemanticsNode root;
root.id = 0;
root.flags = static_cast<FlutterSemanticsFlag>(0);
@@ -171,8 +166,8 @@ TEST(AccessibilityBridgeMacTest, doesNotSendAccessibilityCreateNotificationWhenN
// Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
// can query semantics information from.
engine.semanticsEnabled = YES;
auto bridge =
std::reinterpret_pointer_cast<AccessibilityBridgeMacSpy>(engine.accessibilityBridge.lock());
auto bridge = std::reinterpret_pointer_cast<AccessibilityBridgeMacSpy>(
viewController.accessibilityBridge.lock());
FlutterSemanticsNode root;
root.id = 0;
root.flags = static_cast<FlutterSemanticsFlag>(0);

View File

@@ -194,9 +194,6 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
// The embedding-API-level engine object.
FLUTTER_API_SYMBOL(FlutterEngine) _engine;
// The private member for accessibility.
std::shared_ptr<flutter::AccessibilityBridgeMac> _bridge;
// The project being run by this engine.
FlutterDartProject* _project;
@@ -315,7 +312,7 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
flutterArguments.update_semantics_callback = [](const FlutterSemanticsUpdate* update,
void* user_data) {
FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
[engine updateSemantics:update];
[engine.viewController updateSemantics:update];
};
flutterArguments.custom_dart_entrypoint = entrypoint.UTF8String;
flutterArguments.shutdown_dart_vm_when_done = true;
@@ -543,10 +540,6 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
return _embedderAPI;
}
- (std::weak_ptr<flutter::AccessibilityBridgeMac>)accessibilityBridge {
return _bridge;
}
- (nonnull NSString*)executableName {
return [[[NSProcessInfo processInfo] arguments] firstObject] ?: @"Flutter";
}
@@ -586,24 +579,10 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
return;
}
_semanticsEnabled = enabled;
// Remove the accessibility children from flutter view before reseting the bridge.
if (!_semanticsEnabled && self.viewController.viewLoaded) {
self.viewController.flutterView.accessibilityChildren = nil;
}
if (!_semanticsEnabled && _bridge) {
_bridge.reset();
} else if (_semanticsEnabled && !_bridge) {
_bridge = [self createAccessibilityBridge:self viewController:self.viewController];
}
[_viewController notifySemanticsEnabledChanged];
_embedderAPI.UpdateSemanticsEnabled(_engine, _semanticsEnabled);
}
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)
createAccessibilityBridge:(nonnull FlutterEngine*)engine
viewController:(nonnull FlutterViewController*)viewController {
return std::make_shared<flutter::AccessibilityBridgeMac>(engine, _viewController);
}
- (void)dispatchSemanticsAction:(FlutterSemanticsAction)action
toTarget:(uint16_t)target
withData:(fml::MallocMapping)data {
@@ -939,36 +918,6 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
return _embedderAPI.UnregisterExternalTexture(_engine, textureID) == kSuccess;
}
- (void)updateSemantics:(const FlutterSemanticsUpdate*)update {
NSAssert(_bridge, @"The accessibility bridge must be initialized.");
for (size_t i = 0; i < update->nodes_count; i++) {
const FlutterSemanticsNode* node = &update->nodes[i];
_bridge->AddFlutterSemanticsNodeUpdate(node);
}
for (size_t i = 0; i < update->custom_actions_count; i++) {
const FlutterSemanticsCustomAction* action = &update->custom_actions[i];
_bridge->AddFlutterSemanticsCustomActionUpdate(action);
}
_bridge->CommitUpdates();
// Accessibility tree can only be used when the view is loaded.
if (!self.viewController.viewLoaded) {
return;
}
// Attaches the accessibility root to the flutter view.
auto root = _bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
if (root) {
if ([self.viewController.flutterView.accessibilityChildren count] == 0) {
NSAccessibilityElement* native_root = root->GetNativeViewAccessible();
self.viewController.flutterView.accessibilityChildren = @[ native_root ];
}
} else {
self.viewController.flutterView.accessibilityChildren = nil;
}
}
#pragma mark - Task runner integration
- (void)runTaskOnEmbedder:(FlutterTask)task {

View File

@@ -348,6 +348,31 @@ TEST_F(FlutterEngineTest, CanToggleAccessibilityWhenHeadless) {
EXPECT_EQ(engine.viewController, nil);
}
TEST_F(FlutterEngineTest, ProducesAccessibilityTreeWhenAddingViews) {
FlutterEngine* engine = GetFlutterEngine();
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
// Enable the semantics without attaching a view controller.
bool enabled_called = false;
engine.embedderAPI.UpdateSemanticsEnabled =
MOCK_ENGINE_PROC(UpdateSemanticsEnabled, ([&enabled_called](auto engine, bool enabled) {
enabled_called = enabled;
return kSuccess;
}));
engine.semanticsEnabled = YES;
EXPECT_TRUE(enabled_called);
EXPECT_EQ(engine.viewController, nil);
// Assign the view controller after enabling semantics
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
engine.viewController = viewController;
EXPECT_NE(viewController.accessibilityBridge.lock(), nullptr);
}
TEST_F(FlutterEngineTest, NativeCallbacks) {
fml::AutoResetWaitableEvent latch;
bool latch_called = false;

View File

@@ -31,8 +31,6 @@
*/
@property(nonatomic) FlutterEngineProcTable& embedderAPI;
@property(nonatomic, readonly) std::weak_ptr<flutter::AccessibilityBridgeMac> accessibilityBridge;
/**
* True if the semantics is enabled. The Flutter framework starts sending
* semantics update through the embedder as soon as it is set to YES.
@@ -94,14 +92,3 @@
withData:(fml::MallocMapping)data;
@end
@interface FlutterEngine (TestMethods)
/* Creates an accessibility bridge with the provided parameters.
*
* By default this method calls AccessibilityBridgeMac's initializer. Exposing
* this method allows unit tests to override in order to capture information.
*/
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)
createAccessibilityBridge:(nonnull FlutterEngine*)engine
viewController:(nonnull FlutterViewController*)viewController;
@end

View File

@@ -33,7 +33,7 @@ TEST(FlutterPlatformNodeDelegateMac, Basics) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
// Initialize ax node data.
FlutterSemanticsNode root;
root.id = 0;
@@ -69,7 +69,7 @@ TEST(FlutterPlatformNodeDelegateMac, SelectableTextHasCorrectSemantics) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
// Initialize ax node data.
FlutterSemanticsNode root;
root.id = 0;
@@ -111,7 +111,7 @@ TEST(FlutterPlatformNodeDelegateMac, SelectableTextWithoutSelectionReturnZeroRan
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
// Initialize ax node data.
FlutterSemanticsNode root;
root.id = 0;
@@ -158,7 +158,7 @@ TEST(FlutterPlatformNodeDelegateMac, CanPerformAction) {
window.contentView = viewController.view;
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
// Initialize ax node data.
FlutterSemanticsNode root;
root.id = 0;
@@ -233,7 +233,7 @@ TEST(FlutterPlatformNodeDelegateMac, TextFieldUsesFlutterTextField) {
window.contentView = viewController.view;
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
// Initialize ax node data.
FlutterSemanticsNode root;
root.id = 0;

View File

@@ -1414,14 +1414,14 @@ TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) {
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
FlutterPlatformNodeDelegateMac delegate(bridge, viewController);
ui::AXTree tree;
ui::AXNode ax_node(&tree, nullptr, 0, 0);
ui::AXNodeData node_data;
node_data.SetValue("initial text");
ax_node.SetData(node_data);
delegate.Init(engine.accessibilityBridge, &ax_node);
delegate.Init(viewController.accessibilityBridge, &ax_node);
{
FlutterTextPlatformNode text_platform_node(&delegate, viewController);
@@ -1480,14 +1480,14 @@ TEST(FlutterTextInputPluginTest, CanNotBecomeResponderIfNoViewController) {
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
FlutterPlatformNodeDelegateMac delegate(bridge, viewController);
ui::AXTree tree;
ui::AXNode ax_node(&tree, nullptr, 0, 0);
ui::AXNodeData node_data;
node_data.SetValue("initial text");
ax_node.SetData(node_data);
delegate.Init(engine.accessibilityBridge, &ax_node);
delegate.Init(viewController.accessibilityBridge, &ax_node);
FlutterTextPlatformNode text_platform_node(&delegate, viewController);
FlutterTextField* textField = text_platform_node.GetNativeViewAccessible();

View File

@@ -41,14 +41,14 @@ TEST(FlutterTextInputSemanticsObjectTest, DoesInitialize) {
engine.semanticsEnabled = YES;
auto bridge = engine.accessibilityBridge.lock();
auto bridge = viewController.accessibilityBridge.lock();
FlutterPlatformNodeDelegateMac delegate(bridge, viewController);
ui::AXTree tree;
ui::AXNode ax_node(&tree, nullptr, 0, 0);
ui::AXNodeData node_data;
node_data.SetValue("initial text");
ax_node.SetData(node_data);
delegate.Init(engine.accessibilityBridge, &ax_node);
delegate.Init(viewController.accessibilityBridge, &ax_node);
// Verify that a FlutterTextField is attached to the view.
FlutterTextPlatformNode text_platform_node(&delegate, viewController);
id native_accessibility = text_platform_node.GetNativeViewAccessible();

View File

@@ -287,11 +287,14 @@ void OnKeyboardLayoutChanged(CFNotificationCenterRef center,
// The project to run in this controller's engine.
FlutterDartProject* _project;
std::shared_ptr<flutter::AccessibilityBridgeMac> _bridge;
uint64_t _id;
}
@dynamic id;
@dynamic view;
@dynamic accessibilityBridge;
/**
* Performs initialization that's common between the different init paths.
@@ -314,6 +317,7 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
controller->_mouseTrackingMode = FlutterMouseTrackingModeInKeyWindow;
controller->_textInputPlugin = [[FlutterTextInputPlugin alloc] initWithViewController:controller];
[controller initializeKeyboard];
[controller notifySemanticsEnabledChanged];
// macOS fires this message when changing IMEs.
CFNotificationCenterRef cfCenter = CFNotificationCenterGetDistributedCenter();
__weak FlutterViewController* weakSelf = controller;
@@ -437,6 +441,26 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
[self initializeKeyboard];
}
- (void)notifySemanticsEnabledChanged {
BOOL mySemanticsEnabled = !!_bridge;
BOOL newSemanticsEnabled = _engine.semanticsEnabled;
if (newSemanticsEnabled == mySemanticsEnabled) {
return;
}
if (newSemanticsEnabled) {
_bridge = [self createAccessibilityBridgeWithEngine:_engine];
} else {
// Remove the accessibility children from flutter view before resetting the bridge.
_flutterView.accessibilityChildren = nil;
_bridge.reset();
}
NSAssert(newSemanticsEnabled == !!_bridge, @"Failed to update semantics for the view.");
}
- (std::weak_ptr<flutter::AccessibilityBridgeMac>)accessibilityBridge {
return _bridge;
}
- (void)attachToEngine:(nonnull FlutterEngine*)engine withId:(uint64_t)viewId {
NSAssert(_engine == nil, @"Already attached to an engine %@.", _engine);
_engine = engine;
@@ -452,6 +476,39 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
return _engine != nil;
}
- (void)updateSemantics:(const FlutterSemanticsUpdate*)update {
NSAssert(_engine.semanticsEnabled, @"Semantics must be enabled.");
if (!_engine.semanticsEnabled) {
return;
}
for (size_t i = 0; i < update->nodes_count; i++) {
const FlutterSemanticsNode* node = &update->nodes[i];
_bridge->AddFlutterSemanticsNodeUpdate(node);
}
for (size_t i = 0; i < update->custom_actions_count; i++) {
const FlutterSemanticsCustomAction* action = &update->custom_actions[i];
_bridge->AddFlutterSemanticsCustomActionUpdate(action);
}
_bridge->CommitUpdates();
// Accessibility tree can only be used when the view is loaded.
if (!self.viewLoaded) {
return;
}
// Attaches the accessibility root to the flutter view.
auto root = _bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
if (root) {
if ([self.flutterView.accessibilityChildren count] == 0) {
NSAccessibilityElement* native_root = root->GetNativeViewAccessible();
self.flutterView.accessibilityChildren = @[ native_root ];
}
} else {
self.flutterView.accessibilityChildren = nil;
}
}
#pragma mark - Private methods
- (BOOL)launchEngine {
@@ -713,6 +770,11 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
}
}
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
(nonnull FlutterEngine*)engine {
return std::make_shared<flutter::AccessibilityBridgeMac>(engine, self);
}
- (nonnull FlutterView*)createFlutterViewWithMTLDevice:(id<MTLDevice>)device
commandQueue:(id<MTLCommandQueue>)commandQueue {
return [[FlutterView alloc] initWithMTLDevice:device

View File

@@ -4,6 +4,8 @@
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h"
#include <memory>
#import "flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMac.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardViewDelegate.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h"
@@ -19,6 +21,8 @@
*/
@property(nonatomic, readonly, nonnull) FlutterTextInputPlugin* textInputPlugin;
@property(nonatomic, readonly) std::weak_ptr<flutter::AccessibilityBridgeMac> accessibilityBridge;
/**
* Returns YES if provided event is being currently redispatched by keyboard manager.
*/
@@ -38,12 +42,32 @@
*/
- (void)detachFromEngine;
/**
* Called by the associated FlutterEngine when FlutterEngine#semanticsEnabled
* has changed.
*/
- (void)notifySemanticsEnabledChanged;
/**
* Notify from the framework that the semantics for this view needs to be
* updated.
*/
- (void)updateSemantics:(nonnull const FlutterSemanticsUpdate*)update;
@end
// Private methods made visible for testing
@interface FlutterViewController (TestMethods)
- (void)onAccessibilityStatusChanged:(BOOL)enabled;
/* Creates an accessibility bridge with the provided parameters.
*
* By default this method calls AccessibilityBridgeMac's initializer. Exposing
* this method allows unit tests to override.
*/
- (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
(nonnull FlutterEngine*)engine;
- (nonnull FlutterView*)createFlutterViewWithMTLDevice:(nonnull id<MTLDevice>)device
commandQueue:(nonnull id<MTLCommandQueue>)commandQueue;