diff --git a/lib/feature/model/model_card_actions.dart b/lib/feature/base/button/card_actions.dart similarity index 66% rename from lib/feature/model/model_card_actions.dart rename to lib/feature/base/button/card_actions.dart index b27fc241..1398652a 100644 --- a/lib/feature/model/model_card_actions.dart +++ b/lib/feature/base/button/card_actions.dart @@ -1,28 +1,34 @@ import 'package:material_ui/material_ui.dart'; -/// A row of icon-button actions (delete, and more to come) shown in a -/// [ModelGridCard]'s header. Each child wraps a plain IconButton. Content +/// A row of compact icon-button actions in the header of a card, e.g. a +/// `CardActionsMenu` and a `DeleteIconButton` on a model grid card. Each +/// child wraps a plain IconButton. Content /// below the title (e.g. a key badge) means the header can be taller than /// one line, so the row is top-aligned against it rather than centered /// against the whole block. 30x30 keeps each button's box close to /// titleMedium's line-box height (~24px), but Text's font leading and /// IconButton's own centering don't share the same top inset, so a small -/// manual offset closes the remaining gap between the title's glyphs and +/// manual [offset] closes the remaining gap between the title's glyphs and /// the icons. -class ModelCardActions extends StatelessWidget { - const ModelCardActions({ +class CardActions extends StatelessWidget { + const CardActions({ required this.color, required this.children, + this.offset = const Offset(0, -6), super.key, }); final Color color; final List children; + /// Moves the row against the title, [Offset.zero] next to a smaller text + /// the row is centered on. + final Offset offset; + @override Widget build(BuildContext context) { return Transform.translate( - offset: const Offset(0, -6), + offset: offset, child: IconButtonTheme( data: IconButtonThemeData( style: IconButton.styleFrom( diff --git a/lib/feature/base/button/card_actions_menu.dart b/lib/feature/base/button/card_actions_menu.dart new file mode 100644 index 00000000..3a2c3a0a --- /dev/null +++ b/lib/feature/base/button/card_actions_menu.dart @@ -0,0 +1,58 @@ +import 'package:material_ui/material_ui.dart'; +import 'package:stelaris/util/constants.dart'; +import 'package:stelaris/util/l10n_ext.dart'; + +/// An entry of a [CardActionsMenu]. +typedef CardMenuEntry = ({IconData icon, String label, VoidCallback onTap}); + +/// The 3-dot action menu on a card, with one item per entry of [entries]. +class CardActionsMenu extends StatelessWidget { + const CardActionsMenu({required this.entries, super.key}); + + final List entries; + + @override + Widget build(BuildContext context) { + return PopupMenuButton( + icon: const Icon(Icons.more_vert), + // PopupMenuButton doesn't read the ambient IconButtonTheme the way a + // plain IconButton does (e.g. DeleteIconButton) — without this it + // falls back to the Material default (24), visibly larger than the + // 18 the surrounding IconButtonTheme sets for the rest of the header. + iconSize: 18, + padding: EdgeInsets.zero, + tooltip: context.l10n.tooltip_more_actions, + itemBuilder: (context) => [ + for (final entry in entries) + PopupMenuItem( + // PopupMenuItem.onTap fires before its route is popped, so a + // dialog it opens must wait a beat or the still-closing menu + // route would dismiss it right away. + onTap: () => Future.microtask(entry.onTap), + child: _MenuItemContent(icon: entry.icon, label: entry.label), + ), + ], + ); + } +} + +class _MenuItemContent extends StatelessWidget { + const _MenuItemContent({required this.icon, required this.label}); + + final IconData icon; + final String label; + + @override + Widget build(BuildContext context) { + final colorScheme = Theme.of(context).colorScheme; + + return Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon(icon, size: 18, color: colorScheme.onSurfaceVariant), + horizontalSpacing10, + Flexible(child: Text(label)), + ], + ); + } +} diff --git a/lib/feature/base/button/delete_icon_button.dart b/lib/feature/base/button/delete_icon_button.dart new file mode 100644 index 00000000..271997bb --- /dev/null +++ b/lib/feature/base/button/delete_icon_button.dart @@ -0,0 +1,15 @@ +import 'package:material_ui/material_ui.dart'; +import 'package:stelaris/util/constants.dart'; + +/// The red delete button of a card. What it deletes and how it asks first +/// is up to [onPressed]. +class DeleteIconButton extends StatelessWidget { + const DeleteIconButton({required this.onPressed, this.tooltip, super.key}); + + final VoidCallback? onPressed; + final String? tooltip; + + @override + Widget build(BuildContext context) => + IconButton(tooltip: tooltip, icon: deleteIcon, onPressed: onPressed); +} diff --git a/lib/feature/base/button/delete_model_button.dart b/lib/feature/base/button/delete_model_button.dart index 2539cd34..c1b5118f 100644 --- a/lib/feature/base/button/delete_model_button.dart +++ b/lib/feature/base/button/delete_model_button.dart @@ -1,7 +1,7 @@ import 'package:material_ui/material_ui.dart'; import 'package:stelaris_models/stelaris_models.dart'; import 'package:stelaris/feature/dialogs/model_delete_dialog.dart'; -import 'package:stelaris/util/constants.dart'; +import 'package:stelaris/feature/base/button/delete_icon_button.dart'; import 'package:stelaris/util/typedefs.dart'; class DeleteModelButton extends StatelessWidget { @@ -24,8 +24,7 @@ class DeleteModelButton extends StatelessWidget { @override Widget build(BuildContext context) { - return IconButton( - icon: deleteIcon, + return DeleteIconButton( onPressed: () { showDialog( context: context, diff --git a/lib/feature/base/button/model_actions_menu.dart b/lib/feature/base/button/model_actions_menu.dart index cde2ea2e..77668177 100644 --- a/lib/feature/base/button/model_actions_menu.dart +++ b/lib/feature/base/button/model_actions_menu.dart @@ -2,12 +2,11 @@ import 'package:material_ui/material_ui.dart'; import 'package:stelaris_models/stelaris_models.dart'; import 'package:stelaris/feature/dialogs/model_info_dialog.dart'; import 'package:stelaris/feature/model/model_page.dart'; -import 'package:stelaris/util/constants.dart'; +import 'package:stelaris/feature/base/button/card_actions_menu.dart'; import 'package:stelaris/util/l10n_ext.dart'; -/// The 3-dot action menu on a model card: "Info", plus "Copy" when +/// The [CardActionsMenu] of a model card: "Info", plus "Copy" when /// [onCopy] is given and "Edit notes" when [onEditNotes] is given. -/// Further actions can be added as more [PopupMenuItem]s. class ModelActionsMenu extends StatelessWidget { const ModelActionsMenu({ required this.value, @@ -36,87 +35,43 @@ class ModelActionsMenu extends StatelessWidget { @override Widget build(BuildContext context) { - return PopupMenuButton( - icon: const Icon(Icons.more_vert), - // PopupMenuButton doesn't read the ambient IconButtonTheme the way a - // plain IconButton does (e.g. DeleteModelButton) — without this it - // falls back to the Material default (24), visibly larger than the - // 18 the surrounding IconButtonTheme sets for the rest of the header. - iconSize: 18, - padding: EdgeInsets.zero, - tooltip: context.l10n.tooltip_more_actions, - itemBuilder: (context) => [ - PopupMenuItem( + final l10n = context.l10n; + return CardActionsMenu( + entries: [ + ( + icon: Icons.info_outline, + label: l10n.menu_item_info, onTap: () => _openInfoDialog(context), - child: _MenuItemContent( - icon: Icons.info_outline, - label: context.l10n.menu_item_info, - ), ), if (onCopy case final onCopy?) - PopupMenuItem( - // Like the info dialog: wait for the menu route to pop first. - onTap: () => Future.microtask(onCopy), - child: _MenuItemContent( - icon: Icons.copy_outlined, - label: context.l10n.menu_item_copy, - ), + ( + icon: Icons.copy_outlined, + label: l10n.menu_item_copy, + onTap: onCopy, ), if (onEditNotes case final onEditNotes?) - PopupMenuItem( - // Like the info dialog: wait for the menu route to pop first. - onTap: () => Future.microtask(onEditNotes), - child: _MenuItemContent( - icon: Icons.sticky_note_2_outlined, - label: context.l10n.menu_item_edit_notes, - ), + ( + icon: Icons.sticky_note_2_outlined, + label: l10n.menu_item_edit_notes, + onTap: onEditNotes, ), ], ); } void _openInfoDialog(BuildContext context) { - final name = nameSelector(value); - final key = keySelector(value); - - // PopupMenuItem.onTap fires before its route is popped, so the dialog - // must wait a beat or it would be shown (and immediately dismissed by) - // the still-closing menu route. - Future.microtask(() { - if (!context.mounted) return; - showDialog( - context: context, - builder: (context) { - return ModelInfoDialog( - name: name, - namespacedKey: '$projectKey:$key', - id: value.id, - creationDate: value.creationDate, - modificationDate: value.modificationDate, - ); - }, - ); - }); - } -} - -class _MenuItemContent extends StatelessWidget { - const _MenuItemContent({required this.icon, required this.label}); - - final IconData icon; - final String label; - - @override - Widget build(BuildContext context) { - final colorScheme = Theme.of(context).colorScheme; - - return Row( - mainAxisSize: MainAxisSize.min, - children: [ - Icon(icon, size: 18, color: colorScheme.onSurfaceVariant), - horizontalSpacing10, - Text(label), - ], + if (!context.mounted) return; + showDialog( + context: context, + builder: (context) { + return ModelInfoDialog( + name: nameSelector(value), + namespacedKey: '$projectKey:${keySelector(value)}', + id: value.id, + creationDate: value.creationDate, + modificationDate: value.modificationDate, + ); + }, ); } } diff --git a/lib/feature/item/components/item_components_page.dart b/lib/feature/item/components/item_components_page.dart index fe0ce8ca..4242d481 100644 --- a/lib/feature/item/components/item_components_page.dart +++ b/lib/feature/item/components/item_components_page.dart @@ -2,6 +2,8 @@ import 'package:async_redux/async_redux.dart'; import 'package:material_ui/material_ui.dart'; import 'package:stelaris/api/state/actions/item/item_component_actions.dart'; import 'package:stelaris/api/state/app_state.dart'; +import 'package:stelaris/feature/base/button/card_actions.dart'; +import 'package:stelaris/feature/base/button/delete_icon_button.dart'; import 'package:stelaris/feature/dialogs/delete_dialog.dart'; import 'package:stelaris/feature/base/empty_data_widget.dart'; import 'package:stelaris/feature/base/page_header.dart'; @@ -13,8 +15,6 @@ import 'package:stelaris/feature/item/components/item_model_preview_slot.dart'; import 'package:stelaris/feature/item/components/schema/schema.dart'; import 'package:stelaris/feature/item/components/stelaris_components.dart'; import 'package:stelaris/feature/item/item_tab_loader.dart'; -import 'package:stelaris/feature/model/model_card_actions.dart'; -import 'package:stelaris/util/constants.dart'; import 'package:stelaris/util/l10n_ext.dart'; import 'package:stelaris_models/stelaris_models.dart'; import 'package:vulpes_data/component.dart'; @@ -412,13 +412,12 @@ class _ComponentCard extends StatelessWidget { // A tap on the card edits, so only removing needs a // button, the same compact one as on a model grid card. if (onRemove != null) - ModelCardActions( + CardActions( color: colorScheme.onSurfaceVariant, children: [ - IconButton( + DeleteIconButton( key: const Key('component_card_remove'), tooltip: context.l10n.component_delete_tooltip, - icon: deleteIcon, onPressed: onRemove, ), ], diff --git a/lib/feature/model/model_grid_card.dart b/lib/feature/model/model_grid_card.dart index fa13af32..6e0784b3 100644 --- a/lib/feature/model/model_grid_card.dart +++ b/lib/feature/model/model_grid_card.dart @@ -1,4 +1,5 @@ import 'package:material_ui/material_ui.dart'; +import 'package:stelaris/feature/base/button/card_actions.dart'; import 'package:stelaris_models/stelaris_models.dart'; import 'package:stelaris/feature/base/button/delete_model_button.dart'; import 'package:stelaris/feature/base/button/model_actions_menu.dart'; @@ -10,7 +11,6 @@ import 'package:stelaris/util/notes.dart'; import 'package:stelaris/util/relative_time.dart'; import 'package:stelaris/util/typedefs.dart'; -import 'model_card_actions.dart'; import 'model_card_frame.dart'; import 'model_page.dart'; @@ -60,7 +60,7 @@ class ModelGridCard extends StatelessWidget { return ModelCardFrame( content: mapToDataModelItem(rawModel), - trailing: ModelCardActions( + trailing: CardActions( color: Theme.of(context).colorScheme.onSurfaceVariant, children: [ ModelActionsMenu( diff --git a/test/feature/base/button/card_actions_menu_test.dart b/test/feature/base/button/card_actions_menu_test.dart new file mode 100644 index 00000000..3712f920 --- /dev/null +++ b/test/feature/base/button/card_actions_menu_test.dart @@ -0,0 +1,62 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:stelaris/feature/base/button/card_actions_menu.dart'; +import 'package:stelaris/l10n/app_localizations.dart'; + +Future _show(WidgetTester tester, List entries) => + tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold(body: CardActionsMenu(entries: entries)), + ), + ); + +void main() { + testWidgets('lists one item per entry', (tester) async { + await _show(tester, [ + (icon: Icons.copy, label: 'Copy', onTap: () {}), + (icon: Icons.delete, label: 'Remove', onTap: () {}), + ]); + + await tester.tap(find.byTooltip('More actions')); + await tester.pumpAndSettle(); + + expect(find.byType(PopupMenuItem), findsNWidgets(2)); + expect(find.text('Copy'), findsOneWidget); + expect(find.text('Remove'), findsOneWidget); + }); + + testWidgets('keeps a dialog an entry opens', (tester) async { + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold( + body: Builder( + builder: (context) => CardActionsMenu( + entries: [ + ( + icon: Icons.info_outline, + label: 'Info', + onTap: () => showDialog( + context: context, + builder: (_) => const AlertDialog(content: Text('Opened')), + ), + ), + ], + ), + ), + ), + ), + ); + + await tester.tap(find.byTooltip('More actions')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Info')); + await tester.pumpAndSettle(); + + expect(find.text('Opened'), findsOneWidget); + expect(find.byType(PopupMenuItem), findsNothing); + }); +}