Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Widget> 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(
Expand Down
58 changes: 58 additions & 0 deletions lib/feature/base/button/card_actions_menu.dart
Original file line number Diff line number Diff line change
@@ -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<CardMenuEntry> entries;

@override
Widget build(BuildContext context) {
return PopupMenuButton<void>(
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<void>(
// 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)),
],
);
}
}
15 changes: 15 additions & 0 deletions lib/feature/base/button/delete_icon_button.dart
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 2 additions & 3 deletions lib/feature/base/button/delete_model_button.dart
Original file line number Diff line number Diff line change
@@ -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<E extends DataModel> extends StatelessWidget {
Expand All @@ -24,8 +24,7 @@ class DeleteModelButton<E extends DataModel> extends StatelessWidget {

@override
Widget build(BuildContext context) {
return IconButton(
icon: deleteIcon,
return DeleteIconButton(
onPressed: () {
showDialog(
context: context,
Expand Down
101 changes: 28 additions & 73 deletions lib/feature/base/button/model_actions_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<E extends DataModel> extends StatelessWidget {
const ModelActionsMenu({
required this.value,
Expand Down Expand Up @@ -36,87 +35,43 @@ class ModelActionsMenu<E extends DataModel> extends StatelessWidget {

@override
Widget build(BuildContext context) {
return PopupMenuButton<void>(
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<void>(
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<void>(
// 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<void>(
// 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,
);
},
);
}
}
9 changes: 4 additions & 5 deletions lib/feature/item/components/item_components_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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,
),
],
Expand Down
4 changes: 2 additions & 2 deletions lib/feature/model/model_grid_card.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -60,7 +60,7 @@ class ModelGridCard<E extends DataModel> extends StatelessWidget {

return ModelCardFrame(
content: mapToDataModelItem(rawModel),
trailing: ModelCardActions(
trailing: CardActions(
color: Theme.of(context).colorScheme.onSurfaceVariant,
children: [
ModelActionsMenu<E>(
Expand Down
62 changes: 62 additions & 0 deletions test/feature/base/button/card_actions_menu_test.dart
Original file line number Diff line number Diff line change
@@ -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<void> _show(WidgetTester tester, List<CardMenuEntry> 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<void>), 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<void>(
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<void>), findsNothing);
});
}
Loading