diff --git a/lib/ui/compact_icon_button.dart b/lib/ui/compact_icon_button.dart index 048351f..f4f3981 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. @@ -44,49 +45,51 @@ 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 selectedBackground ?? 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 selectedBackground ?? 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/draw/drawing_canvas.dart b/lib/ui/draw/drawing_canvas.dart index 106e69a..35aea85 100644 --- a/lib/ui/draw/drawing_canvas.dart +++ b/lib/ui/draw/drawing_canvas.dart @@ -1093,45 +1093,50 @@ class DrawingCanvasState extends State { if (!widget.readOnly) ...[ Tooltip( message: 'Colour and size', - child: InkWell( - key: const ValueKey('draw-style'), - onTap: () => setState(() => _styleOpen = !_styleOpen), - borderRadius: BorderRadius.circular(9), - child: Container( - height: extent, - padding: const EdgeInsets.symmetric(horizontal: 8), - decoration: _styleOpen - ? BoxDecoration( - color: palette.selectedBackground, - borderRadius: BorderRadius.circular(9), - ) - : null, - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Container( - width: 14, - height: 14, - decoration: BoxDecoration( - color: _color == null - ? palette.textPrimary - : Color(_color!), - shape: BoxShape.circle, - border: Border.all( - color: palette.separator, - width: 1.5, - strokeAlign: BorderSide.strokeAlignOutside, + child: Semantics( + button: true, + expanded: _styleOpen, + label: AppPlatform.isDesktop ? 'Colour and size' : null, + child: InkWell( + key: const ValueKey('draw-style'), + onTap: () => setState(() => _styleOpen = !_styleOpen), + borderRadius: BorderRadius.circular(9), + child: Container( + height: extent, + padding: const EdgeInsets.symmetric(horizontal: 8), + decoration: _styleOpen + ? BoxDecoration( + color: palette.selectedBackground, + borderRadius: BorderRadius.circular(9), + ) + : null, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Container( + width: 14, + height: 14, + decoration: BoxDecoration( + color: _color == null + ? palette.textPrimary + : Color(_color!), + shape: BoxShape.circle, + border: Border.all( + color: palette.separator, + width: 1.5, + strokeAlign: BorderSide.strokeAlignOutside, + ), ), ), - ), - const SizedBox(width: 6), - KapyIcon( - KapyIcons.chevronDownRounded, - size: 12, - strokeWidth: 2, - color: palette.textSecondary, - ), - ], + const SizedBox(width: 6), + KapyIcon( + KapyIcons.chevronDownRounded, + size: 12, + strokeWidth: 2, + color: palette.textSecondary, + ), + ], + ), ), ), ), @@ -1298,31 +1303,36 @@ class _StylePanel extends StatelessWidget { for (final (name, option) in drawColors) Tooltip( message: name, - child: InkWell( - key: ValueKey('draw-color-${option ?? 'ink'}'), - customBorder: const CircleBorder(), - onTap: () => onColor(option), - child: Container( - width: 30, - height: 30, - alignment: Alignment.center, - decoration: option == color - ? BoxDecoration( - shape: BoxShape.circle, - border: Border.all( - color: palette.selectedBorder, - width: 2, - ), - ) - : null, + child: Semantics( + button: true, + selected: option == color, + label: AppPlatform.isDesktop ? name : null, + child: InkWell( + key: ValueKey('draw-color-${option ?? 'ink'}'), + customBorder: const CircleBorder(), + onTap: () => onColor(option), child: Container( - width: 20, - height: 20, - decoration: BoxDecoration( - color: option == null - ? palette.textPrimary - : Color(option), - shape: BoxShape.circle, + width: 30, + height: 30, + alignment: Alignment.center, + decoration: option == color + ? BoxDecoration( + shape: BoxShape.circle, + border: Border.all( + color: palette.selectedBorder, + width: 2, + ), + ) + : null, + child: Container( + width: 20, + height: 20, + decoration: BoxDecoration( + color: option == null + ? palette.textPrimary + : Color(option), + shape: BoxShape.circle, + ), ), ), ), diff --git a/lib/ui/notes_list.dart b/lib/ui/notes_list.dart index 9049ec6..eeb3741 100644 --- a/lib/ui/notes_list.dart +++ b/lib/ui/notes_list.dart @@ -987,9 +987,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( @@ -1051,6 +1053,7 @@ class NotesListControls extends StatelessWidget { selected: on, label: forCards ? 'Cards' : 'List', excludeSemantics: true, + onTap: on ? null : () => onCardsChanged(forCards), child: MouseRegion( cursor: on ? MouseCursor.defer : SystemMouseCursors.click, child: GestureDetector( @@ -1168,6 +1171,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..84b19d0 --- /dev/null +++ b/test/button_press_test.dart @@ -0,0 +1,215 @@ +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, + /// other than the chosen option of a switch, which pressing would not + /// change. + List unpressable() => [ + for (final data in buttons()) + if (data.flagsCollection.isEnabled != Tristate.isFalse && + data.flagsCollection.isSelected != Tristate.isTrue && + !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 node + in find.semantics + .byPredicate( + (node) => + !node.isMergedIntoParent && + node.getSemanticsData().hasAction(SemanticsAction.tap), + ) + .evaluate()) + if (node.getSemanticsData() case final data + when data.label.trim().isEmpty && !data.flagsCollection.isTextField) + 'tooltip "${data.tooltip}" value "${data.value}"', + ]; + + /// 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(); + + await tester.tap(find.byKey(const ValueKey('rail-drawings'))); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const ValueKey('sidebar-new-note'))); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const ValueKey('draw-style'))); + await tester.pumpAndSettle(); + expect(unpressable(), isEmpty, reason: 'on a drawing'); + expect(unnamed(), isEmpty, reason: 'on a drawing'); + + 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); + }); +}