From f754c3d36e66e290696f10f1a7c851ac4d9ec2ff Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:01:20 -0700 Subject: [PATCH] Bare-bones iOS FKA implementation (flutter/engine#55964) A bare-bones implementation for iOS focus engine support, to enable basic full keyboard access (FKA) (except for scrolling which will be implemented in a different patch). Partially f1xes https://github.com/flutter/flutter/issues/76497 https://github.com/user-attachments/assets/427db87e-cc15-483a-85a1-56bf1c02c285 On iOS 15 and above, FKA, if enabled, always consumes relevant key events, so the Flutter framework can't see those key events as they won't be delivered via the `UIResponder` chain (https://developer.apple.com/documentation/uikit/uikeycommand/3780513-wantspriorityoversystembehavior). This patch provides the basic focus-related information to the iOS focus engine, derived from the information already available in the accessibility tree, so the iOS focus engine can navigate the UI hierarchy and invoke `accessibilityActivate` on the current focus when the user presses the space key. This at the moment seems to be the best option: - There doesn't seem to be a way to reliably prevent FKA from consuming the key events and that seems to be by design. - The user can remap the FKA keys in iOS system settings, but that key mapping isn't available to apps, so even if the framework can get the key events it won't be able to honor custom key maps. - When FKA is on, `-[FlutterView isAccessibilityElement]` is called without user interaction (presumably it's called when the view appears), so when the user interacts with the app using FKA, it's likely that the accessibility is already enabled, we don't have to worry detecting whether FKA is on (at least for now). Scrolling using FKA currently does not work despite `FlutterScrollableSemanticsObject` conforms to `UIFocusItemScrollableContainer`. `setContentOffset:` must be implemented using a new API that informs the framework of the new contentOffset in the scroll view. `accessibilityScroll` does not work because it scrolls too much in most cases, and it tells the framework "how much to scroll" instead of "where to scroll to" so it tends to be jumpy. ## What happens on iOS versions earlier than 15 I couldn't find iOS 14 runtime for simulators in xcode 16 so I couldn't test it. But since the key events will be delivered to the framework first regardless of whether FKA is enabled, the framework is supposed to handle keyboard focus navigation even when FKA is on. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../ci/licenses_golden/licenses_flutter | 2 + .../shell/platform/darwin/ios/BUILD.gn | 1 + .../ios/framework/Source/FlutterView.mm | 27 +++++ .../Source/SemanticsObject+UIFocusSystem.mm | 102 ++++++++++++++++++ .../framework/Source/SemanticsObjectTest.mm | 54 ++++++++++ 5 files changed, 186 insertions(+) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 13df9a1668..45e22b2e4a 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -44558,6 +44558,7 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewT ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/IOKit.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/KeyCodeMap.g.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/KeyCodeMap_Internal.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm + ../../../flutter/LICENSE @@ -47436,6 +47437,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTes FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/IOKit.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/KeyCodeMap.g.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/KeyCodeMap_Internal.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index 5e197c3158..37f6b3fe65 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -108,6 +108,7 @@ source_set("flutter_framework_source_arc") { "framework/Source/FlutterViewResponder.h", "framework/Source/KeyCodeMap.g.mm", "framework/Source/KeyCodeMap_Internal.h", + "framework/Source/SemanticsObject+UIFocusSystem.mm", "framework/Source/SemanticsObject.h", "framework/Source/SemanticsObject.mm", "framework/Source/TextInputSemanticsObject.h", diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm index 7e8e1108b1..137b1f712d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm @@ -3,6 +3,7 @@ // found in the LICENSE file. #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h" #include "flutter/fml/platform/darwin/cf_utils.h" @@ -226,4 +227,30 @@ static BOOL _forceSoftwareRendering; return NO; } +// Enables keyboard-based navigation when the user turns on +// full keyboard access (FKA), using existing accessibility information. +// +// iOS does not provide any API for monitoring or querying whether FKA is on, +// but it does call isAccessibilityElement if FKA is on, +// so the isAccessibilityElement implementation above will be called +// when the view appears and the accessibility information will most likely +// be available by the time the user starts to interact with the app using FKA. +// +// See SemanticsObject+UIFocusSystem.mm for more details. +- (NSArray>*)focusItemsInRect:(CGRect)rect { + NSObject* rootAccessibilityElement = + [self.accessibilityElements count] > 0 ? self.accessibilityElements[0] : nil; + return [rootAccessibilityElement isKindOfClass:[SemanticsObjectContainer class]] + ? @[ [rootAccessibilityElement accessibilityElementAtIndex:0] ] + : nil; +} + +- (NSArray>*)preferredFocusEnvironments { + // Occasionally we add subviews to FlutterView (text fields for example). + // These views shouldn't be directly visible to the iOS focus engine, instead + // the focus engine should only interact with the designated focus items + // (SemanticsObjects). + return nil; +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm new file mode 100644 index 0000000000..3448b92805 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm @@ -0,0 +1,102 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "SemanticsObject.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" + +FLUTTER_ASSERT_ARC + +// The SemanticsObject class conforms to UIFocusItem and UIFocusItemContainer +// protocols, so the SemanticsObject tree can also be used to represent +// interactive UI components on screen that can receive UIFocusSystem focus. +// +// Typically, physical key events received by the FlutterViewController is +// first delivered to the framework, but that stopped working for navigation keys +// since iOS 15 when full keyboard access (FKA) is on, because those events are +// consumed by the UIFocusSystem and never dispatched to the UIResponders in the +// application (see +// https://developer.apple.com/documentation/uikit/uikeycommand/3780513-wantspriorityoversystembehavior +// ). FKA relies on the iOS focus engine, to enable FKA on iOS 15+, we use +// SemanticsObject to provide the iOS focus engine with the required hierarchical +// information and geometric context. +// +// The focus engine focus is different from accessibility focus, or even the +// currentFocus of the Flutter FocusManager in the framework. On iOS 15+, FKA +// key events are dispatched to the current iOS focus engine focus (and +// translated to calls such as -[NSObject accessibilityActivate]), while most +// other key events are dispatched to the framework. +@interface SemanticsObject (UIFocusSystem) +@end + +@implementation SemanticsObject (UIFocusSystem) + +#pragma mark - UIFocusEnvironment Conformance + +- (void)setNeedsFocusUpdate { +} + +- (void)updateFocusIfNeeded { +} + +- (BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext*)context { + return YES; +} + +- (void)didUpdateFocusInContext:(UIFocusUpdateContext*)context + withAnimationCoordinator:(UIFocusAnimationCoordinator*)coordinator { +} + +- (id)parentFocusEnvironment { + // The root SemanticsObject node's parent is the FlutterView. + return self.parent ?: self.bridge->view(); +} + +- (NSArray>*)preferredFocusEnvironments { + return nil; +} + +- (id)focusItemContainer { + return self; +} + +#pragma mark - UIFocusItem Conformance + +- (BOOL)canBecomeFocused { + if ((self.node.flags & static_cast(flutter::SemanticsFlags::kIsHidden)) != 0) { + return NO; + } + // Currently only supports SemanticsObjects that handle + // -[NSObject accessibilityActivate]. + return self.node.HasAction(flutter::SemanticsAction::kTap); +} + +- (CGRect)frame { + return self.accessibilityFrame; +} + +#pragma mark - UIFocusItemContainer Conformance + +- (NSArray>*)focusItemsInRect:(CGRect)rect { + // It seems the iOS focus system relies heavily on focusItemsInRect + // (instead of preferredFocusEnvironments) for directional navigation. + // + // The order of the items seems to be important, menus and dialogs become + // unreachable via FKA if the returned children are organized + // in hit-test order. + // + // This method is only supposed to return items within the given + // rect but returning everything in the subtree seems to work fine. + NSMutableArray* reversedItems = + [[NSMutableArray alloc] initWithCapacity:self.childrenInHitTestOrder.count]; + for (NSUInteger i = 0; i < self.childrenInHitTestOrder.count; ++i) { + [reversedItems + addObject:self.childrenInHitTestOrder[self.childrenInHitTestOrder.count - 1 - i]]; + } + return reversedItems; +} + +- (id)coordinateSpace { + return self.bridge->view(); +} +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index e95078a419..d0fcaeab2b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -16,6 +16,9 @@ FLUTTER_ASSERT_ARC const float kFloatCompareEpsilon = 0.001; +@interface SemanticsObject (UIFocusSystem) +@end + @interface TextInputSemanticsObject (Test) - (UIView*)textInputSurrogate; @end @@ -1152,4 +1155,55 @@ const float kFloatCompareEpsilon = 0.001; [self waitForExpectationsWithTimeout:1 handler:nil]; } +- (void)testUIFocusItemConformance { + fml::WeakPtrFactory factory( + new flutter::testing::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* parent = [[SemanticsObject alloc] initWithBridge:bridge uid:0]; + SemanticsObject* child = [[SemanticsObject alloc] initWithBridge:bridge uid:1]; + parent.children = @[ child ]; + + // parentFocusEnvironment + XCTAssertTrue([parent.parentFocusEnvironment isKindOfClass:[UIView class]]); + XCTAssertEqual(child.parentFocusEnvironment, child.parent); + + // canBecomeFocused + flutter::SemanticsNode childNode; + childNode.flags = static_cast(flutter::SemanticsFlags::kIsHidden); + childNode.actions = static_cast(flutter::SemanticsAction::kTap); + [child setSemanticsNode:&childNode]; + XCTAssertFalse(child.canBecomeFocused); + childNode.flags = 0; + [child setSemanticsNode:&childNode]; + XCTAssertTrue(child.canBecomeFocused); + childNode.actions = 0; + [child setSemanticsNode:&childNode]; + XCTAssertFalse(child.canBecomeFocused); + + CGFloat scale = ((bridge->view().window.screen ?: UIScreen.mainScreen)).scale; + + childNode.rect = SkRect::MakeXYWH(0, 0, 100 * scale, 100 * scale); + [child setSemanticsNode:&childNode]; + flutter::SemanticsNode parentNode; + parentNode.rect = SkRect::MakeXYWH(0, 0, 200, 200); + [parent setSemanticsNode:&parentNode]; + + XCTAssertTrue(CGRectEqualToRect(child.frame, CGRectMake(0, 0, 100, 100))); +} + +- (void)testUIFocusItemContainerConformance { + fml::WeakPtrFactory factory( + new flutter::testing::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* parent = [[SemanticsObject alloc] initWithBridge:bridge uid:0]; + SemanticsObject* child1 = [[SemanticsObject alloc] initWithBridge:bridge uid:1]; + SemanticsObject* child2 = [[SemanticsObject alloc] initWithBridge:bridge uid:2]; + parent.childrenInHitTestOrder = @[ child1, child2 ]; + + // focusItemsInRect + NSArray>* itemsInRect = [parent focusItemsInRect:CGRectMake(0, 0, 100, 100)]; + XCTAssertEqual(itemsInRect.count, (unsigned long)2); + XCTAssertTrue([itemsInRect containsObject:child1]); + XCTAssertTrue([itemsInRect containsObject:child2]); +} @end