From f41b77f140a512d814a5a54690d94a2700393216 Mon Sep 17 00:00:00 2001 From: Sanjay Kholiya Date: Tue, 17 Nov 2026 11:33:07 +0530 Subject: [PATCH] Let accessibility press and name every button in the window --- lib/ui/compact_icon_button.dart | 83 +++++++------- lib/ui/notes_list.dart | 4 + lib/ui/tonal_button.dart | 11 +- test/button_press_test.dart | 194 ++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 41 deletions(-) create mode 100644 test/button_press_test.dart diff --git a/lib/ui/compact_icon_button.dart b/lib/ui/compact_icon_button.dart index d09499b..c0a9b40 100644 --- a/lib/ui/compact_icon_button.dart +++ b/lib/ui/compact_icon_button.dart @@ -1,5 +1,6 @@ import 'package:material_ui/material_ui.dart'; +import '../core/platform.dart'; import '../core/theme.dart'; /// The app-wide icon action used in toolbars, footers, and sidebars. @@ -39,47 +40,49 @@ class CompactIconButton extends StatelessWidget { final scheme = Theme.of(context).colorScheme; final extent = this.extent ?? AppControlMetrics.iconButtonExtent; - return Tooltip( - message: tooltip, - child: Semantics( - button: true, - selected: selected, - child: IconButton( - onPressed: onPressed, - icon: icon, - color: foregroundColor ?? palette.textSecondary, - padding: EdgeInsets.zero, - constraints: BoxConstraints.tightFor(width: extent, height: extent), - visualDensity: VisualDensity.standard, - style: ButtonStyle( - minimumSize: WidgetStatePropertyAll(Size.square(extent)), - maximumSize: WidgetStatePropertyAll(Size.square(extent)), - tapTargetSize: - tapTargetSize ?? AppControlMetrics.iconButtonTapTargetSize, - backgroundColor: WidgetStateProperty.resolveWith((states) { - if (states.contains(WidgetState.disabled)) { + return MergeSemantics( + child: Tooltip( + message: tooltip, + child: Semantics( + label: AppPlatform.isDesktop ? tooltip : null, + selected: selected, + child: IconButton( + onPressed: onPressed, + icon: icon, + color: foregroundColor ?? palette.textSecondary, + padding: EdgeInsets.zero, + constraints: BoxConstraints.tightFor(width: extent, height: extent), + visualDensity: VisualDensity.standard, + style: ButtonStyle( + minimumSize: WidgetStatePropertyAll(Size.square(extent)), + maximumSize: WidgetStatePropertyAll(Size.square(extent)), + tapTargetSize: + tapTargetSize ?? AppControlMetrics.iconButtonTapTargetSize, + backgroundColor: WidgetStateProperty.resolveWith((states) { + if (states.contains(WidgetState.disabled)) { + return Colors.transparent; + } + if (states.contains(WidgetState.pressed)) { + return scheme.primary.withValues(alpha: 0.10); + } + if (selected) return palette.selectedBackground; + if (states.contains(WidgetState.hovered) || + states.contains(WidgetState.focused)) { + return palette.hover; + } return Colors.transparent; - } - if (states.contains(WidgetState.pressed)) { - return scheme.primary.withValues(alpha: 0.10); - } - if (selected) return palette.selectedBackground; - if (states.contains(WidgetState.hovered) || - states.contains(WidgetState.focused)) { - return palette.hover; - } - return Colors.transparent; - }), - overlayColor: const WidgetStatePropertyAll(Colors.transparent), - side: WidgetStateProperty.resolveWith((states) { - if (states.contains(WidgetState.focused)) { - return BorderSide(color: palette.selectedBorder, width: 1); - } - return BorderSide.none; - }), - shape: WidgetStatePropertyAll( - RoundedRectangleBorder( - borderRadius: BorderRadius.circular(radius), + }), + overlayColor: const WidgetStatePropertyAll(Colors.transparent), + side: WidgetStateProperty.resolveWith((states) { + if (states.contains(WidgetState.focused)) { + return BorderSide(color: palette.selectedBorder, width: 1); + } + return BorderSide.none; + }), + shape: WidgetStatePropertyAll( + RoundedRectangleBorder( + borderRadius: BorderRadius.circular(radius), + ), ), ), ), diff --git a/lib/ui/notes_list.dart b/lib/ui/notes_list.dart index 8c5980a..adaf92a 100644 --- a/lib/ui/notes_list.dart +++ b/lib/ui/notes_list.dart @@ -668,9 +668,11 @@ class SeeAllLink extends StatelessWidget { Widget build(BuildContext context) { final palette = context.palette; return Semantics( + container: true, button: true, label: 'See all pinned notes', excludeSemantics: true, + onTap: onPressed, child: MouseRegion( cursor: SystemMouseCursors.click, child: GestureDetector( @@ -730,6 +732,7 @@ class NotesListControls extends StatelessWidget { selected: on, label: forCards ? 'Cards' : 'List', excludeSemantics: true, + onTap: () => onCardsChanged(forCards), child: MouseRegion( cursor: on ? MouseCursor.defer : SystemMouseCursors.click, child: GestureDetector( @@ -843,6 +846,7 @@ class _MediaFilterState extends State<_MediaFilter> { expanded: open, label: 'Media filter, $label', excludeSemantics: true, + onTap: _toggle, child: MouseRegion( cursor: SystemMouseCursors.click, child: GestureDetector( diff --git a/lib/ui/tonal_button.dart b/lib/ui/tonal_button.dart index be7dad0..cbd1266 100644 --- a/lib/ui/tonal_button.dart +++ b/lib/ui/tonal_button.dart @@ -113,6 +113,15 @@ class TonalButton extends StatelessWidget { ); final tooltip = this.tooltip; - return tooltip == null ? button : Tooltip(message: tooltip, child: button); + if (tooltip == null) return button; + if (label != null || !AppPlatform.isDesktop) { + return Tooltip(message: tooltip, child: button); + } + return MergeSemantics( + child: Tooltip( + message: tooltip, + child: Semantics(label: tooltip, child: button), + ), + ); } } diff --git a/test/button_press_test.dart b/test/button_press_test.dart new file mode 100644 index 0000000..c9c5861 --- /dev/null +++ b/test/button_press_test.dart @@ -0,0 +1,194 @@ +import 'dart:ui' show Tristate; + +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; + +import 'golden_test.dart' + show goldenNotes, goldenPrefs, pumpForGolden, tapSettings; +import 'test_fonts.dart'; + +/// VoiceOver, Voice Control and AXPress press a control through its +/// semantics tap action, never a pointer, so these press nothing else. +void main() { + setUpAll(loadTestFonts); + + Future pumpWindow( + WidgetTester tester, { + bool sidebarVisible = true, + }) async { + await pumpForGolden( + tester, + size: const Size(1440, 900), + brightness: Brightness.dark, + sidebarVisible: sidebarVisible, + ); + } + + Future press( + WidgetTester tester, + FinderBase control, + ) async { + tester.semantics.tap(control); + await tester.pumpAndSettle(); + } + + SemanticsFinder labelled(String label) => + find.semantics.byLabel(RegExp('^${RegExp.escape(label)}\$')); + + SemanticsFinder tipped(String tooltip) => find.semantics.byPredicate( + (node) => + node.getSemanticsData().flagsCollection.isButton && + node.label.startsWith(tooltip), + ); + + String? spaceTitle(WidgetTester tester) => tester + .widget(find.byKey(const ValueKey('sidebar-space-title'))) + .data; + + /// The buttons the platform is sent: a node merged into its parent is part + /// of that parent's button, and only the parent reaches VoiceOver. + Iterable buttons() => find.semantics + .byPredicate( + (node) => + !node.isMergedIntoParent && + node.getSemanticsData().flagsCollection.isButton, + ) + .evaluate() + .map((node) => node.getSemanticsData()); + + /// Every button on screen that is switched on, and has nothing to press. + List unpressable() => [ + for (final data in buttons()) + if (data.flagsCollection.isEnabled != Tristate.isFalse && + !data.hasAction(SemanticsAction.tap)) + '"${data.label}" ${data.tooltip}'.trim(), + ]; + + /// Every button on screen with no name. macOS reads a button's name from + /// its label alone, so a button named only by its tooltip is read there as + /// "button" and nothing else. + List unnamed() => [ + for (final data in buttons()) + if (data.label.trim().isEmpty) 'tooltip "${data.tooltip}"', + ]; + + /// A test with the semantics tree on, as it is while VoiceOver runs. + void testPress(String description, WidgetTesterCallback body) => + testWidgets(description, (tester) async { + final semantics = tester.ensureSemantics(); + try { + await body(tester); + } finally { + semantics.dispose(); + } + }); + + testPress('every button in the window can be pressed', (tester) async { + await pumpWindow(tester); + for (final id in [for (var i = 0; i < 5; i++) goldenNotes.create().id]) { + goldenNotes.updateBody(id, 'Pinned'); + goldenNotes.togglePinned(id); + } + await tester.pumpAndSettle(); + expect(unpressable(), isEmpty, reason: 'with the list open'); + expect(unnamed(), isEmpty, reason: 'with the list open'); + + await tester.tap(find.byKey(const ValueKey('page-more'))); + await tester.pumpAndSettle(); + expect(unpressable(), isEmpty, reason: 'with More open'); + expect(unnamed(), isEmpty, reason: 'with More open'); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const ValueKey('sidebar-media-filter'))); + await tester.pumpAndSettle(); + expect(unpressable(), isEmpty, reason: 'with the media filter open'); + expect(unnamed(), isEmpty, reason: 'with the media filter open'); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + + for (final space in ['drawings', 'archive']) { + await tester.tap(find.byKey(ValueKey('rail-$space'))); + await tester.pumpAndSettle(); + expect(unpressable(), isEmpty, reason: 'in $space'); + expect(unnamed(), isEmpty, reason: 'in $space'); + } + + await tapSettings(tester); + expect(unpressable(), isEmpty, reason: 'with Settings open'); + expect(unnamed(), isEmpty, reason: 'with Settings open'); + }); + + testPress('every button can be pressed with the list hidden', (tester) async { + await pumpWindow(tester, sidebarVisible: false); + expect(unpressable(), isEmpty); + expect(unnamed(), isEmpty); + }); + + testPress('a rail item opens its space when pressed', (tester) async { + await pumpWindow(tester); + await press(tester, labelled('Drawings')); + expect(spaceTitle(tester), 'Drawings'); + await press(tester, labelled('Notes')); + expect(spaceTitle(tester), 'My notes'); + }); + + testPress('a tonal button makes a note when pressed', (tester) async { + await pumpWindow(tester); + final before = goldenNotes.notes.length; + await press(tester, labelled('New note')); + expect(goldenNotes.notes, hasLength(before + 1)); + }); + + testPress('a keycap is read with its button, never on its own', ( + tester, + ) async { + await pumpWindow(tester); + expect(labelled('New note'), findsOne); + expect(find.semantics.byLabel(RegExp('⌘')), findsNothing); + }); + + testPress('an icon button is pressed by its name', (tester) async { + await pumpWindow(tester); + final note = goldenNotes.notes.single; + + await press(tester, tipped('Pin note')); + expect(goldenNotes.pinnedNoteIds, contains(note.id)); + + await press(tester, tipped('Hide notes')); + expect(goldenPrefs.sidebarVisible, isFalse); + await press(tester, tipped('Show notes')); + expect(goldenPrefs.sidebarVisible, isTrue); + }); + + testPress('the list controls are pressed by their names', (tester) async { + await pumpWindow(tester); + + await press(tester, labelled('List')); + expect(goldenPrefs.noteCards, isFalse); + await press(tester, labelled('Cards')); + expect(goldenPrefs.noteCards, isTrue); + + await press(tester, find.semantics.byLabel(RegExp('^Media filter'))); + expect(find.byKey(const ValueKey('media-filter-menu')), findsOneWidget); + }); + + testPress('See all is pressed by its name', (tester) async { + await pumpWindow(tester); + for (final id in [for (var i = 0; i < 5; i++) goldenNotes.create().id]) { + goldenNotes.updateBody(id, 'Pinned'); + goldenNotes.togglePinned(id); + } + await tester.pumpAndSettle(); + + await press(tester, labelled('See all pinned notes')); + expect(labelled('See all pinned notes'), findsNothing); + expect(unpressable(), isEmpty); + expect(unnamed(), isEmpty); + + await press(tester, tipped('Back to all notes')); + expect(labelled('See all pinned notes'), findsOne); + }); +}