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

Commit 59dba26

Browse files
committed
Started ignoring route change names when presenting modal view
controllers and added a test that exercises announcing route changes.
1 parent f3a38f0 commit 59dba26

File tree

5 files changed

+142
-8
lines changed

5 files changed

+142
-8
lines changed

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@interface FlutterViewController () <FlutterBinaryMessenger, UIScrollViewDelegate>
4040
@property(nonatomic, readwrite, getter=isDisplayingFlutterUI) BOOL displayingFlutterUI;
4141
@property(nonatomic, assign) BOOL isHomeIndicatorHidden;
42+
@property(nonatomic, assign) BOOL isPresentingViewControllerAnimating;
4243
@end
4344

4445
// The following conditional compilation defines an API 13 concept on earlier API targets so that
@@ -1240,4 +1241,22 @@ - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
12401241
return [_engine.get() valuePublishedByPlugin:pluginKey];
12411242
}
12421243

1244+
- (void)presentViewController:(UIViewController*)viewControllerToPresent
1245+
animated:(BOOL)flag
1246+
completion:(void (^)(void))completion {
1247+
self.isPresentingViewControllerAnimating = YES;
1248+
[super presentViewController:viewControllerToPresent
1249+
animated:flag
1250+
completion:^{
1251+
self.isPresentingViewControllerAnimating = NO;
1252+
if (completion) {
1253+
completion();
1254+
}
1255+
}];
1256+
}
1257+
1258+
- (BOOL)isPresentingViewController {
1259+
return self.presentedViewController != nil || self.isPresentingViewControllerAnimating;
1260+
}
1261+
12431262
@end

shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern NSNotificationName const FlutterViewControllerShowHomeIndicator;
2323

2424
@interface FlutterViewController ()
2525

26+
@property(nonatomic, readonly) BOOL isPresentingViewController;
2627
- (fml::WeakPtr<FlutterViewController>)getWeakPtr;
2728
- (flutter::FlutterPlatformViewsController*)platformViewsController;
2829

shell/platform/darwin/ios/framework/Source/accessibility_bridge.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,19 @@ class PlatformViewIOS;
3636
*/
3737
class AccessibilityBridge final : public AccessibilityBridgeIos {
3838
public:
39+
/** Delegate for handling iOS operations. */
40+
class IosDelegate {
41+
public:
42+
virtual ~IosDelegate() = default;
43+
virtual bool IsFluterViewControllerPresentingModalViewController(UIView* view) = 0;
44+
virtual void PostAccessibilityNotification(UIAccessibilityNotifications notification,
45+
id argument) = 0;
46+
};
47+
3948
AccessibilityBridge(UIView* view,
4049
PlatformViewIOS* platform_view,
41-
FlutterPlatformViewsController* platform_views_controller);
50+
FlutterPlatformViewsController* platform_views_controller,
51+
std::unique_ptr<IosDelegate> ios_delegate = nullptr);
4252
~AccessibilityBridge();
4353

4454
void UpdateSemantics(flutter::SemanticsNodeUpdates nodes,
@@ -75,6 +85,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos {
7585
int32_t previous_route_id_;
7686
std::unordered_map<int32_t, flutter::CustomAccessibilityAction> actions_;
7787
std::vector<int32_t> previous_routes_;
88+
std::unique_ptr<IosDelegate> ios_delegate_;
7889

7990
FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridge);
8091
};

shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h"
66
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
7+
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
78
#import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.h"
89

910
#import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
@@ -13,17 +14,50 @@
1314
FLUTTER_ASSERT_NOT_ARC
1415

1516
namespace flutter {
17+
namespace {
18+
19+
FlutterViewController* GetFlutterViewControllerForView(UIView* view) {
20+
id nextResponder = [view nextResponder];
21+
if ([nextResponder isKindOfClass:[FlutterViewController class]]) {
22+
return nextResponder;
23+
} else if ([nextResponder isKindOfClass:[UIView class]]) {
24+
return GetFlutterViewControllerForView(nextResponder);
25+
} else {
26+
return nil;
27+
}
28+
}
29+
30+
class DefaultIosDelegate : public AccessibilityBridge::IosDelegate {
31+
public:
32+
bool IsFluterViewControllerPresentingModalViewController(UIView* view) override {
33+
FlutterViewController* viewController = GetFlutterViewControllerForView(view);
34+
if (viewController) {
35+
return viewController.isPresentingViewController;
36+
} else {
37+
return false;
38+
}
39+
}
40+
41+
void PostAccessibilityNotification(UIAccessibilityNotifications notification,
42+
id argument) override {
43+
UIAccessibilityPostNotification(notification, argument);
44+
}
45+
};
46+
} // namespace
1647

1748
AccessibilityBridge::AccessibilityBridge(UIView* view,
1849
PlatformViewIOS* platform_view,
19-
FlutterPlatformViewsController* platform_views_controller)
50+
FlutterPlatformViewsController* platform_views_controller,
51+
std::unique_ptr<IosDelegate> ios_delegate)
2052
: view_(view),
2153
platform_view_(platform_view),
2254
platform_views_controller_(platform_views_controller),
2355
objects_([[NSMutableDictionary alloc] init]),
2456
weak_factory_(this),
2557
previous_route_id_(0),
26-
previous_routes_({}) {
58+
previous_routes_({}),
59+
ios_delegate_(ios_delegate ? std::move(ios_delegate)
60+
: std::make_unique<DefaultIosDelegate>()) {
2761
accessibility_channel_.reset([[FlutterBasicMessageChannel alloc]
2862
initWithName:@"flutter/accessibility"
2963
binaryMessenger:platform_view->GetOwnerViewController().get().engine.binaryMessenger
@@ -137,15 +171,18 @@
137171

138172
layoutChanged = layoutChanged || [doomed_uids count] > 0;
139173
if (routeChanged) {
140-
NSString* routeName = [lastAdded routeName];
141-
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, routeName);
174+
if (!ios_delegate_->IsFluterViewControllerPresentingModalViewController(view_)) {
175+
NSString* routeName = [lastAdded routeName];
176+
ios_delegate_->PostAccessibilityNotification(UIAccessibilityScreenChangedNotification,
177+
routeName);
178+
}
142179
} else if (layoutChanged) {
143180
// TODO(goderbauer): figure out which node to focus next.
144-
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
181+
ios_delegate_->PostAccessibilityNotification(UIAccessibilityLayoutChangedNotification, nil);
145182
}
146183
if (scrollOccured) {
147184
// TODO(tvolkert): provide meaningful string (e.g. "page 2 of 5")
148-
UIAccessibilityPostNotification(UIAccessibilityPageScrolledNotification, @"");
185+
ios_delegate_->PostAccessibilityNotification(UIAccessibilityPageScrolledNotification, @"");
149186
}
150187
}
151188

@@ -233,7 +270,7 @@ static bool DidFlagChange(const flutter::SemanticsNode& oldNode,
233270
NSString* type = annotatedEvent[@"type"];
234271
if ([type isEqualToString:@"announce"]) {
235272
NSString* message = annotatedEvent[@"data"][@"message"];
236-
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, message);
273+
ios_delegate_->PostAccessibilityNotification(UIAccessibilityAnnouncementNotification, message);
237274
}
238275
}
239276

shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
8686
void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
8787
void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
8888
};
89+
90+
class MockIosDelegate : public AccessibilityBridge::IosDelegate {
91+
public:
92+
void PostAccessibilityNotification(UIAccessibilityNotifications notification,
93+
id argument) override {
94+
if (on_PostAccessibilityNotification_) {
95+
on_PostAccessibilityNotification_(notification, argument);
96+
}
97+
}
98+
std::function<void(UIAccessibilityNotifications, id)> on_PostAccessibilityNotification_;
99+
};
89100
} // namespace
90101
} // namespace flutter
91102

@@ -238,4 +249,59 @@ - (void)testSemanticsDeallocated {
238249
XCTAssertNil(gMockPlatformView);
239250
}
240251

252+
- (void)testAnnouncesRouteChanges {
253+
flutter::MockDelegate mock_delegate;
254+
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
255+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
256+
/*platform=*/thread_task_runner,
257+
/*raster=*/thread_task_runner,
258+
/*ui=*/thread_task_runner,
259+
/*io=*/thread_task_runner);
260+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
261+
/*delegate=*/mock_delegate,
262+
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
263+
/*task_runners=*/runners);
264+
id mockFlutterView = OCMClassMock([FlutterView class]);
265+
std::string label = "some label";
266+
267+
NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
268+
[[[NSMutableArray alloc] init] autorelease];
269+
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
270+
ios_delegate->on_PostAccessibilityNotification_ =
271+
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
272+
[accessibility_notifications addObject:@{
273+
@"notification" : @(notification),
274+
@"argument" : argument ? argument : [NSNull null],
275+
}];
276+
};
277+
__block auto bridge =
278+
std::make_unique<flutter::AccessibilityBridge>(/*view=*/mockFlutterView,
279+
/*platform_view=*/platform_view.get(),
280+
/*platform_views_controller=*/nil,
281+
/*ios_delegate=*/std::move(ios_delegate));
282+
283+
flutter::CustomAccessibilityActionUpdates actions;
284+
flutter::SemanticsNodeUpdates nodes;
285+
286+
flutter::SemanticsNode route_node;
287+
route_node.id = 1;
288+
route_node.label = label;
289+
route_node.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute) |
290+
static_cast<int32_t>(flutter::SemanticsFlags::kNamesRoute);
291+
route_node.label = "route";
292+
nodes[route_node.id] = route_node;
293+
flutter::SemanticsNode root_node;
294+
root_node.id = kRootNodeId;
295+
root_node.label = label;
296+
root_node.childrenInTraversalOrder = {1};
297+
root_node.childrenInHitTestOrder = {1};
298+
nodes[root_node.id] = root_node;
299+
bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
300+
301+
XCTAssertEqual([accessibility_notifications count], 1ul);
302+
XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], @"route");
303+
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
304+
UIAccessibilityScreenChangedNotification);
305+
}
306+
241307
@end

0 commit comments

Comments
 (0)