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
This commit is contained in:
LongCatIsLooong
2024-10-23 17:01:20 -07:00
committed by GitHub
parent 329ecc861e
commit f754c3d36e
5 changed files with 186 additions and 0 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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<id<UIFocusItem>>*)focusItemsInRect:(CGRect)rect {
NSObject* rootAccessibilityElement =
[self.accessibilityElements count] > 0 ? self.accessibilityElements[0] : nil;
return [rootAccessibilityElement isKindOfClass:[SemanticsObjectContainer class]]
? @[ [rootAccessibilityElement accessibilityElementAtIndex:0] ]
: nil;
}
- (NSArray<id<UIFocusEnvironment>>*)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

View File

@@ -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) <UIFocusItem, UIFocusItemContainer>
@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<UIFocusEnvironment>)parentFocusEnvironment {
// The root SemanticsObject node's parent is the FlutterView.
return self.parent ?: self.bridge->view();
}
- (NSArray<id<UIFocusEnvironment>>*)preferredFocusEnvironments {
return nil;
}
- (id<UIFocusItemContainer>)focusItemContainer {
return self;
}
#pragma mark - UIFocusItem Conformance
- (BOOL)canBecomeFocused {
if ((self.node.flags & static_cast<int32_t>(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<id<UIFocusItem>>*)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<SemanticsObject*>* 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<UICoordinateSpace>)coordinateSpace {
return self.bridge->view();
}
@end

View File

@@ -16,6 +16,9 @@ FLUTTER_ASSERT_ARC
const float kFloatCompareEpsilon = 0.001;
@interface SemanticsObject (UIFocusSystem) <UIFocusItem, UIFocusItemContainer>
@end
@interface TextInputSemanticsObject (Test)
- (UIView<UITextInput>*)textInputSurrogate;
@end
@@ -1152,4 +1155,55 @@ const float kFloatCompareEpsilon = 0.001;
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)testUIFocusItemConformance {
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
new flutter::testing::MockAccessibilityBridge());
fml::WeakPtr<flutter::AccessibilityBridgeIos> 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<int32_t>(flutter::SemanticsFlags::kIsHidden);
childNode.actions = static_cast<int32_t>(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<flutter::AccessibilityBridgeIos> factory(
new flutter::testing::MockAccessibilityBridge());
fml::WeakPtr<flutter::AccessibilityBridgeIos> 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<id<UIFocusItem>>* itemsInRect = [parent focusItemsInRect:CGRectMake(0, 0, 100, 100)];
XCTAssertEqual(itemsInRect.count, (unsigned long)2);
XCTAssertTrue([itemsInRect containsObject:child1]);
XCTAssertTrue([itemsInRect containsObject:child2]);
}
@end