Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix ios layout change cause the accessibility focus to jump randomly.
  • Loading branch information
chunhtai committed Aug 5, 2020
commit c6d1b80f3239608af5730b1f76fbec18475adf46
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ - (BOOL)accessibilityPerformEscape {
- (void)accessibilityElementDidBecomeFocused {
if (![self isAccessibilityBridgeAlive])
return;
[self bridge]->AccessibilityFocusDidChange([self uid]);
if ([self node].HasFlag(flutter::SemanticsFlags::kIsHidden) ||
[self node].HasFlag(flutter::SemanticsFlags::kIsHeader)) {
[self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kShowOnScreen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void DispatchSemanticsAction(int32_t id,
SemanticsActionObservation observation(id, action);
observations.push_back(observation);
}
void AccessibilityFocusDidChange(int32_t focused_id) override {}
FlutterPlatformViewsController* GetPlatformViewsController() const override { return nil; }
std::vector<SemanticsActionObservation> observations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos {
void DispatchSemanticsAction(int32_t id,
flutter::SemanticsAction action,
std::vector<uint8_t> args) override;
void AccessibilityFocusDidChange(int32_t focused_id) override;

UIView<UITextInput>* textInputView() override;

Expand All @@ -83,6 +84,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos {
FlutterViewController* view_controller_;
PlatformViewIOS* platform_view_;
FlutterPlatformViewsController* platform_views_controller_;
int32_t last_focused_semantics_object_id_;
fml::scoped_nsobject<NSMutableDictionary<NSNumber*, SemanticsObject*>> objects_;
fml::scoped_nsprotocol<FlutterBasicMessageChannel*> accessibility_channel_;
fml::WeakPtrFactory<AccessibilityBridge> weak_factory_;
Expand Down
19 changes: 15 additions & 4 deletions shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
: view_controller_(view_controller),
platform_view_(platform_view),
platform_views_controller_(platform_views_controller),
last_focused_semantics_object_id_(0),
objects_([[NSMutableDictionary alloc] init]),
weak_factory_(this),
previous_route_id_(0),
Expand All @@ -66,6 +67,10 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
return [[platform_view_->GetOwnerViewController().get().engine textInputPlugin] textInputView];
}

void AccessibilityBridge::AccessibilityFocusDidChange(int32_t focused_id) {
last_focused_semantics_object_id_ = focused_id;
}

void AccessibilityBridge::UpdateSemantics(flutter::SemanticsNodeUpdates nodes,
flutter::CustomAccessibilityActionUpdates actions) {
BOOL layoutChanged = NO;
Expand Down Expand Up @@ -188,12 +193,18 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
[lastAdded routeFocusObject]);
}
} else if (layoutChanged) {
// TODO(goderbauer): figure out which node to focus next.
ios_delegate_->PostAccessibilityNotification(UIAccessibilityLayoutChangedNotification, nil);
// Tries to refocus the previous focused semantics object to avoid random jumps.
ios_delegate_->PostAccessibilityNotification(
UIAccessibilityLayoutChangedNotification,
[objects_.get() objectForKey:@(last_focused_semantics_object_id_)]
);
}
if (scrollOccured) {
// TODO(tvolkert): provide meaningful string (e.g. "page 2 of 5")
ios_delegate_->PostAccessibilityNotification(UIAccessibilityPageScrolledNotification, @"");
// Tries to refocus the previous focused semantics object to avoid random jumps.
ios_delegate_->PostAccessibilityNotification(
UIAccessibilityPageScrolledNotification,
[objects_.get() objectForKey:@(last_focused_semantics_object_id_)]
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AccessibilityBridgeIos {
virtual void DispatchSemanticsAction(int32_t id,
flutter::SemanticsAction action,
std::vector<uint8_t> args) = 0;
virtual void AccessibilityFocusDidChange(int32_t focused_id) = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Please add a docstring, what focused_id is could be misinterpreted (it could mean the old value).

virtual FlutterPlatformViewsController* GetPlatformViewsController() const = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ - (void)testAnnouncesRouteChanges {

flutter::SemanticsNode route_node;
route_node.id = 1;
route_node.label = label;
route_node.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute) |
static_cast<int32_t>(flutter::SemanticsFlags::kNamesRoute);
route_node.label = "route";
Expand All @@ -327,6 +326,207 @@ - (void)testAnnouncesRouteChanges {
UIAccessibilityScreenChangedNotification);
}

- (void)testAnnouncesLayoutChangeWithNilIfLastFocusIsRemoved{
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
/*platform=*/thread_task_runner,
/*raster=*/thread_task_runner,
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
/*task_runners=*/runners);
id mockFlutterView = OCMClassMock([FlutterView class]);

NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
[[[NSMutableArray alloc] init] autorelease];
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
ios_delegate->on_PostAccessibilityNotification_ =
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
[accessibility_notifications addObject:@{
@"notification" : @(notification),
@"argument" : argument ? argument : [NSNull null],
}];
};
__block auto bridge =
std::make_unique<flutter::AccessibilityBridge>(/*view=*/mockFlutterView,
/*platform_view=*/platform_view.get(),
/*platform_views_controller=*/nil,
/*ios_delegate=*/std::move(ios_delegate));

flutter::CustomAccessibilityActionUpdates actions;
flutter::SemanticsNodeUpdates first_update;

flutter::SemanticsNode route_node;
route_node.id = 1;
route_node.label = "route";
first_update[route_node.id] = route_node;
flutter::SemanticsNode root_node;
root_node.id = kRootNodeId;
root_node.label = "root";
root_node.childrenInTraversalOrder = {1};
root_node.childrenInHitTestOrder = {1};
first_update[root_node.id] = root_node;
bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);

XCTAssertEqual([accessibility_notifications count], 0ul);
// Simulates the focusing on the node 1.
bridge->AccessibilityFocusDidChange(1);

flutter::SemanticsNodeUpdates second_update;
// Simulates the removal of the node 1
flutter::SemanticsNode new_root_node;
new_root_node.id = kRootNodeId;
new_root_node.label = "root";
second_update[root_node.id] = new_root_node;
bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
NSNull* focusObject = accessibility_notifications[0][@"argument"];
// The node 1 was removed, so the bridge will set the focus object to nil.
XCTAssertEqual(focusObject, [NSNull null]);
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
UIAccessibilityLayoutChangedNotification);
}

- (void)testAnnouncesLayoutChangeWithLastFocused {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
/*platform=*/thread_task_runner,
/*raster=*/thread_task_runner,
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
/*task_runners=*/runners);
id mockFlutterView = OCMClassMock([FlutterView class]);

NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
[[[NSMutableArray alloc] init] autorelease];
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
ios_delegate->on_PostAccessibilityNotification_ =
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
[accessibility_notifications addObject:@{
@"notification" : @(notification),
@"argument" : argument ? argument : [NSNull null],
}];
};
__block auto bridge =
std::make_unique<flutter::AccessibilityBridge>(/*view=*/mockFlutterView,
/*platform_view=*/platform_view.get(),
/*platform_views_controller=*/nil,
/*ios_delegate=*/std::move(ios_delegate));

flutter::CustomAccessibilityActionUpdates actions;
flutter::SemanticsNodeUpdates first_update;

flutter::SemanticsNode node_one;
node_one.id = 1;
node_one.label = "route1";
first_update[node_one.id] = node_one;
flutter::SemanticsNode node_two;
node_two.id = 2;
node_two.label = "route2";
first_update[node_two.id] = node_two;
flutter::SemanticsNode root_node;
root_node.id = kRootNodeId;
root_node.label = "root";
root_node.childrenInTraversalOrder = {1, 2};
root_node.childrenInHitTestOrder = {1, 2};
first_update[root_node.id] = root_node;
bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);

XCTAssertEqual([accessibility_notifications count], 0ul);
// Simulates the focusing on the node 1.
bridge->AccessibilityFocusDidChange(1);

flutter::SemanticsNodeUpdates second_update;
// Simulates the removal of the node 2.
flutter::SemanticsNode new_root_node;
new_root_node.id = kRootNodeId;
new_root_node.label = "root";
new_root_node.childrenInTraversalOrder = {1};
new_root_node.childrenInHitTestOrder = {1};
second_update[root_node.id] = new_root_node;
bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
SemanticsObject* focusObject = accessibility_notifications[0][@"argument"];
// Since we have focused on the node 1 right before the layout changed, the bridge should refocus
// the node 1.
XCTAssertEqual([focusObject uid], 1);
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
UIAccessibilityLayoutChangedNotification);
}

- (void)testAnnouncesScrollChangeWithLastFocused {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
/*platform=*/thread_task_runner,
/*raster=*/thread_task_runner,
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
/*task_runners=*/runners);
id mockFlutterView = OCMClassMock([FlutterView class]);

NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
[[[NSMutableArray alloc] init] autorelease];
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
ios_delegate->on_PostAccessibilityNotification_ =
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
[accessibility_notifications addObject:@{
@"notification" : @(notification),
@"argument" : argument ? argument : [NSNull null],
}];
};
__block auto bridge =
std::make_unique<flutter::AccessibilityBridge>(/*view=*/mockFlutterView,
/*platform_view=*/platform_view.get(),
/*platform_views_controller=*/nil,
/*ios_delegate=*/std::move(ios_delegate));

flutter::CustomAccessibilityActionUpdates actions;
flutter::SemanticsNodeUpdates first_update;

flutter::SemanticsNode node_one;
node_one.id = 1;
node_one.label = "route1";
node_one.scrollPosition = 0.0;
first_update[node_one.id] = node_one;
flutter::SemanticsNode root_node;
root_node.id = kRootNodeId;
root_node.label = "root";
root_node.childrenInTraversalOrder = {1};
root_node.childrenInHitTestOrder = {1};
first_update[root_node.id] = root_node;
bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);

// The first update will trigger a scroll announcement, but we are not interested in it.
[accessibility_notifications removeAllObjects];

// Simulates the focusing on the node 1.
bridge->AccessibilityFocusDidChange(1);

flutter::SemanticsNodeUpdates second_update;
// Simulates the scrolling on the node 1.
flutter::SemanticsNode new_node_one;
new_node_one.id = 1;
new_node_one.label = "route1";
new_node_one.scrollPosition = 1.0;
second_update[new_node_one.id] = new_node_one;
bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
SemanticsObject* focusObject = accessibility_notifications[0][@"argument"];
// Since we have focused on the node 1 right before the scrolling, the bridge should refocus the
// node 1.
XCTAssertEqual([focusObject uid], 1);
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
UIAccessibilityPageScrolledNotification);
}

- (void)testAnnouncesIgnoresRouteChangesWhenModal {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
Expand Down