Skip to content

Commit f976562

Browse files
committed
Fix message action accessibility lifecycle
Signed-off-by: kenny lopez <[email protected]>
1 parent e87ee8a commit f976562

4 files changed

Lines changed: 195 additions & 37 deletions

File tree

‎mobile/ios/Runner/NativeMessageActionSurface.swift‎

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ struct NativeMessageActionDefinition {
3434
}
3535

3636
enum NativeMessageActionSurfaceLayout {
37-
static let rowHeight: CGFloat = 48
37+
static let minimumRowHeight: CGFloat = 48
38+
static let rowVerticalPadding: CGFloat = 4
3839
static let separatorHeight: CGFloat = 0.5
3940
static let verticalInset: CGFloat = 4
4041
static let horizontalInset: CGFloat = 16
@@ -62,11 +63,31 @@ enum NativeMessageActionSurfaceLayout {
6263
max(0, populatedGroups(actions: actions).count - 1)
6364
}
6465

66+
static func rowHeight(
67+
minimumHeight: CGFloat = minimumRowHeight,
68+
compatibleWith traitCollection: UITraitCollection? = nil
69+
) -> CGFloat {
70+
let labelHeight = UIFont.preferredFont(
71+
forTextStyle: .body,
72+
compatibleWith: traitCollection
73+
).lineHeight
74+
return max(
75+
minimumHeight,
76+
ceil(labelHeight + (rowVerticalPadding * 2))
77+
)
78+
}
79+
6580
static func preferredHeight(
66-
actions: [NativeMessageActionDefinition]
81+
actions: [NativeMessageActionDefinition],
82+
minimumRowHeight: CGFloat = minimumRowHeight,
83+
compatibleWith traitCollection: UITraitCollection? = nil
6784
) -> CGFloat {
68-
(verticalInset * 2)
69-
+ (CGFloat(actions.count) * rowHeight)
85+
let resolvedRowHeight = rowHeight(
86+
minimumHeight: minimumRowHeight,
87+
compatibleWith: traitCollection
88+
)
89+
return (verticalInset * 2)
90+
+ (CGFloat(actions.count) * resolvedRowHeight)
7091
+ (CGFloat(separatorCount(actions: actions)) * separatorHeight)
7192
}
7293
}
@@ -110,6 +131,8 @@ final class NativeMessageActionRowControl: UIControl {
110131
definition: NativeMessageActionDefinition,
111132
foregroundColor: UIColor,
112133
destructiveColor: UIColor,
134+
minimumHeight: CGFloat = NativeMessageActionSurfaceLayout.minimumRowHeight,
135+
compatibleWith traitCollection: UITraitCollection? = nil,
113136
onSelected: @escaping () -> Void
114137
) {
115138
actionImageView = UIImageView(image: UIImage(systemName: definition.symbol))
@@ -121,11 +144,17 @@ final class NativeMessageActionRowControl: UIControl {
121144

122145
actionTitleLabel.text = definition.title
123146
actionTitleLabel.textColor = color
124-
actionTitleLabel.font = UIFont.preferredFont(forTextStyle: .body)
147+
actionTitleLabel.font = UIFont.preferredFont(
148+
forTextStyle: .body,
149+
compatibleWith: traitCollection
150+
)
125151
actionTitleLabel.adjustsFontForContentSizeCategory = true
126-
actionTitleLabel.numberOfLines = 1
127-
actionTitleLabel.adjustsFontSizeToFitWidth = true
128-
actionTitleLabel.minimumScaleFactor = 0.8
152+
actionTitleLabel.numberOfLines = 0
153+
actionTitleLabel.lineBreakMode = .byWordWrapping
154+
actionTitleLabel.setContentCompressionResistancePriority(
155+
.required,
156+
for: .vertical
157+
)
129158

130159
let iconColumn = UIView()
131160
iconColumn.translatesAutoresizingMaskIntoConstraints = false
@@ -159,9 +188,20 @@ final class NativeMessageActionRowControl: UIControl {
159188
equalTo: trailingAnchor,
160189
constant: -NativeMessageActionSurfaceLayout.horizontalInset
161190
),
191+
actionTitleLabel.topAnchor.constraint(
192+
greaterThanOrEqualTo: topAnchor,
193+
constant: NativeMessageActionSurfaceLayout.rowVerticalPadding
194+
),
195+
actionTitleLabel.bottomAnchor.constraint(
196+
lessThanOrEqualTo: bottomAnchor,
197+
constant: -NativeMessageActionSurfaceLayout.rowVerticalPadding
198+
),
162199
actionTitleLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
163200
heightAnchor.constraint(
164-
greaterThanOrEqualToConstant: NativeMessageActionSurfaceLayout.rowHeight
201+
greaterThanOrEqualToConstant: NativeMessageActionSurfaceLayout.rowHeight(
202+
minimumHeight: minimumHeight,
203+
compatibleWith: traitCollection
204+
)
165205
),
166206
])
167207

@@ -265,6 +305,15 @@ final class NativeMessageActionSurfacePlatformView: NSObject,
265305
let interfaceStyle = NativeMessageActionSurfaceAppearance.interfaceStyle(
266306
from: arguments?["interfaceStyle"]
267307
)
308+
var minimumRowHeight = NativeMessageActionSurfaceLayout.minimumRowHeight
309+
if let requestedRowHeight = arguments?["rowHeight"] as? NSNumber,
310+
requestedRowHeight.doubleValue.isFinite
311+
{
312+
minimumRowHeight = max(
313+
minimumRowHeight,
314+
CGFloat(requestedRowHeight.doubleValue)
315+
)
316+
}
268317
let actionArguments = arguments?["actions"] as? [[String: Any]]
269318
let actions =
270319
actionArguments?.compactMap(
@@ -317,7 +366,8 @@ final class NativeMessageActionSurfacePlatformView: NSObject,
317366
actions: actions,
318367
foregroundColor: foregroundColor,
319368
destructiveColor: destructiveColor,
320-
separatorColor: separatorColor
369+
separatorColor: separatorColor,
370+
minimumRowHeight: minimumRowHeight
321371
)
322372
}
323373

@@ -329,7 +379,8 @@ final class NativeMessageActionSurfacePlatformView: NSObject,
329379
actions: [NativeMessageActionDefinition],
330380
foregroundColor: UIColor,
331381
destructiveColor: UIColor,
332-
separatorColor: UIColor
382+
separatorColor: UIColor,
383+
minimumRowHeight: CGFloat
333384
) {
334385
let scrollView = UIScrollView()
335386
scrollView.translatesAutoresizingMaskIntoConstraints = false
@@ -377,6 +428,7 @@ final class NativeMessageActionSurfacePlatformView: NSObject,
377428
definition: definition,
378429
foregroundColor: foregroundColor,
379430
destructiveColor: destructiveColor,
431+
minimumHeight: minimumRowHeight,
380432
onSelected: { [weak self] in self?.select(definition) }
381433
)
382434
)

‎mobile/ios/RunnerTests/RunnerTests.swift‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,12 @@ class RunnerTests: XCTestCase {
431431
2
432432
)
433433
XCTAssertEqual(
434-
NativeMessageActionSurfaceLayout.preferredHeight(actions: definitions),
434+
NativeMessageActionSurfaceLayout.preferredHeight(
435+
actions: definitions,
436+
compatibleWith: UITraitCollection(
437+
preferredContentSizeCategory: .large
438+
)
439+
),
435440
153
436441
)
437442
}
@@ -463,6 +468,52 @@ class RunnerTests: XCTestCase {
463468
XCTAssertTrue(selected)
464469
}
465470

471+
@MainActor
472+
func testNativeMessageActionRowExpandsForAccessibilityTypography() throws {
473+
let traits = UITraitCollection(
474+
preferredContentSizeCategory: .accessibilityExtraExtraExtraLarge
475+
)
476+
let definition = try XCTUnwrap(
477+
NativeMessageActionDefinition(
478+
arguments: [
479+
"id": "followThread", "title": "Follow thread",
480+
"symbol": "bell", "group": "utility",
481+
]
482+
)
483+
)
484+
let row = NativeMessageActionRowControl(
485+
definition: definition,
486+
foregroundColor: .label,
487+
destructiveColor: .systemRed,
488+
compatibleWith: traits,
489+
onSelected: {}
490+
)
491+
let fittingSize = row.systemLayoutSizeFitting(
492+
CGSize(width: 288, height: UIView.layoutFittingCompressedSize.height),
493+
withHorizontalFittingPriority: .required,
494+
verticalFittingPriority: .fittingSizeLevel
495+
)
496+
row.frame = CGRect(origin: .zero, size: fittingSize)
497+
row.layoutIfNeeded()
498+
let labelFrame = row.convert(
499+
row.actionTitleLabel.bounds,
500+
from: row.actionTitleLabel
501+
)
502+
503+
XCTAssertGreaterThan(fittingSize.height, 48)
504+
XCTAssertGreaterThan(labelFrame.height, 0)
505+
XCTAssertGreaterThanOrEqual(
506+
labelFrame.minY,
507+
NativeMessageActionSurfaceLayout.rowVerticalPadding
508+
)
509+
XCTAssertLessThanOrEqual(
510+
labelFrame.maxY,
511+
fittingSize.height - NativeMessageActionSurfaceLayout.rowVerticalPadding
512+
)
513+
XCTAssertEqual(row.actionTitleLabel.numberOfLines, 0)
514+
XCTAssertFalse(row.actionTitleLabel.adjustsFontSizeToFitWidth)
515+
}
516+
466517
@MainActor
467518
func testNativeMessageActionSurfaceUsesSystemMaterial() {
468519
let effect = NativeMessageActionSurfaceAppearance.backdropEffect(

‎mobile/lib/features/channels/message_actions/message_action_popover.dart‎

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,39 @@ Future<bool> _showMessageActionsPopover({
155155
if (shouldRestoreComposerFocus) composerFocusNode!.unfocus();
156156

157157
String? selectedActionId;
158+
final dialogRoute = RawDialogRoute<String>(
159+
barrierDismissible: true,
160+
barrierLabel: 'Dismiss message actions',
161+
barrierColor: Colors.transparent,
162+
transitionDuration: reduceMotion
163+
? Duration.zero
164+
: isIos
165+
? _iosMessageActionTransitionDuration
166+
: _messageActionTransitionDuration,
167+
transitionBuilder: (context, animation, secondaryAnimation, child) =>
168+
child,
169+
pageBuilder: (dialogContext, animation, secondaryAnimation) =>
170+
_MessageActionsPopover(
171+
anchorRect: anchorRect,
172+
anchorSnapshot: snapshot,
173+
animation: animation,
174+
message: message,
175+
pageContext: context,
176+
pageRef: ref,
177+
actions: actions,
178+
useIosNativeActionSurface: useIosNativeActionSurface,
179+
),
180+
);
181+
var routePushed = false;
158182
try {
159-
selectedActionId = await showGeneralDialog<String>(
160-
context: context,
161-
barrierDismissible: true,
162-
barrierLabel: 'Dismiss message actions',
163-
barrierColor: Colors.transparent,
164-
transitionDuration: reduceMotion
165-
? Duration.zero
166-
: isIos
167-
? _iosMessageActionTransitionDuration
168-
: _messageActionTransitionDuration,
169-
transitionBuilder: (context, animation, secondaryAnimation, child) =>
170-
child,
171-
pageBuilder: (dialogContext, animation, secondaryAnimation) =>
172-
_MessageActionsPopover(
173-
anchorRect: anchorRect,
174-
anchorSnapshot: snapshot,
175-
animation: animation,
176-
message: message,
177-
pageContext: context,
178-
pageRef: ref,
179-
actions: actions,
180-
useIosNativeActionSurface: useIosNativeActionSurface,
181-
),
182-
);
183+
final popResult = Navigator.of(
184+
context,
185+
rootNavigator: true,
186+
).push(dialogRoute);
187+
routePushed = true;
188+
selectedActionId = await popResult;
183189
} finally {
190+
if (routePushed) await dialogRoute.completed;
184191
snapshot.dispose();
185192
if (context.mounted) onPopoverDismissed?.call();
186193
}
@@ -447,10 +454,12 @@ class _PopoverMessageAction {
447454

448455
class _IosNativeMessageActionSurface extends HookWidget {
449456
final List<_PopoverMessageAction> actions;
457+
final double rowHeight;
450458
final ValueChanged<String> onSelected;
451459

452460
const _IosNativeMessageActionSurface({
453461
required this.actions,
462+
required this.rowHeight,
454463
required this.onSelected,
455464
});
456465

@@ -479,6 +488,7 @@ class _IosNativeMessageActionSurface extends HookWidget {
479488
'separatorColor': context.colors.outlineVariant.toARGB32(),
480489
'errorColor': context.colors.error.toARGB32(),
481490
'interfaceStyle': context.colors.brightness.name,
491+
'rowHeight': rowHeight,
482492
},
483493
creationParamsCodec: const StandardMessageCodec(),
484494
onPlatformViewCreated: (id) => viewId.value = id,
@@ -690,6 +700,7 @@ class _MessageActionsPopover extends StatelessWidget {
690700
child: useIosNativeActionSurface
691701
? _IosNativeMessageActionSurface(
692702
actions: actions,
703+
rowHeight: menuLayout.rowHeight,
693704
onSelected: (actionId) =>
694705
Navigator.of(context).pop(actionId),
695706
)

‎mobile/test/features/channels/message_actions_test.dart‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Future<_MessageActionsPopoverHarness> _pumpMessageActionsPopover(
215215
bool disableAnimations = false,
216216
EdgeInsets viewInsets = EdgeInsets.zero,
217217
TextScaler textScaler = TextScaler.noScaling,
218+
Future<ui.Image> Function()? captureAnchorSnapshot,
218219
FocusNode? composerFocusNode,
219220
bool composerInitiallyFocused = false,
220221
Rect anchorRect = const Rect.fromLTWH(32, 260, 300, 72),
@@ -262,7 +263,8 @@ Future<_MessageActionsPopoverHarness> _pumpMessageActionsPopover(
262263
currentPubkey: 'self',
263264
isMember: true,
264265
anchorRect: anchorRect,
265-
captureAnchorSnapshot: _testMessageSnapshot,
266+
captureAnchorSnapshot:
267+
captureAnchorSnapshot ?? _testMessageSnapshot,
266268
onPopoverPresented: () => sourceHidden.value = true,
267269
onPopoverDismissed: () => sourceHidden.value = false,
268270
composerFocusNode: composerFocusNode,
@@ -882,6 +884,48 @@ void main() {
882884
await _dismissMessageActionsPopover(tester);
883885
});
884886

887+
testWidgets('keeps the snapshot alive through the reverse transition', (
888+
tester,
889+
) async {
890+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
891+
ui.Image? snapshot;
892+
try {
893+
final prefs = await _mockPrefs();
894+
await _pumpMessageActionsPopover(
895+
tester,
896+
message: _message(),
897+
prefs: prefs,
898+
captureAnchorSnapshot: () async {
899+
snapshot = await _testMessageSnapshot();
900+
return snapshot!;
901+
},
902+
);
903+
904+
final image = snapshot!;
905+
expect(image.debugDisposed, isFalse);
906+
Navigator.of(
907+
tester.element(find.byKey(const ValueKey('message-action-surface'))),
908+
).pop();
909+
await tester.pump();
910+
911+
expect(
912+
find.byKey(const ValueKey('message-action-preview')),
913+
findsOneWidget,
914+
);
915+
expect(image.debugDisposed, isFalse);
916+
await tester.pump(const Duration(milliseconds: 110));
917+
expect(image.debugDisposed, isFalse);
918+
919+
await tester.pumpAndSettle();
920+
expect(image.debugDisposed, isTrue);
921+
expect(tester.takeException(), isNull);
922+
} finally {
923+
final image = snapshot;
924+
if (image != null && !image.debugDisposed) image.dispose();
925+
debugDefaultTargetPlatformOverride = null;
926+
}
927+
});
928+
885929
testWidgets('shows parity actions for a regular message', (tester) async {
886930
final prefs = await _mockPrefs();
887931
await _pumpSheet(tester, message: _message(), prefs: prefs);

0 commit comments

Comments
 (0)