Files
pipelines/app/lib/ui/layout/app_overflow_menu.dart
T
alan 046de9db4d Use proper phone sheets for chat and Files menus
On phones the chat's More menu, chat list rows and Files entries open a
Material bottom sheet headed by the chat or file, with full-width icon
rows and destructive actions last in the error colour; desktop menus
show icons, a minimum width and destructive actions after a divider.
The chat menu drops Run command and Open chats, keeps a single terminal
entry and lists Close pane last.
2026-11-13 01:14:12 +00:00

184 lines
5.4 KiB
Dart

import 'package:flutter/material.dart';
import '../adaptive/app_action_sheet.dart';
import '../adaptive/app_popups.dart';
/// One choice in an [AppOverflowMenu].
class AppMenuAction<T> {
const AppMenuAction({
required this.value,
required this.label,
this.icon,
this.key,
this.destructive = false,
this.enabled = true,
});
final T value;
final String label;
/// The action's glyph, beside its label in the menu and the phone sheet.
final IconData? icon;
/// Placed on the menu item so tests can find the same item.
final Key? key;
/// A disabled item stays listed but cannot be chosen.
final bool enabled;
/// Listed last, after a divider, in the theme's error colour.
final bool destructive;
}
/// The app's one row/header overflow control: the host's "more" icon that
/// opens the row's secondary actions.
///
/// On a wide app it is a menu anchored to its button, with each action's icon;
/// on a phone it opens a bottom sheet headed by [sheetTitle], with full-width
/// rows (see [showAppActionSheet]). Destructive actions come last either way.
class AppOverflowMenu<T> extends StatelessWidget {
const AppOverflowMenu({
super.key,
this.actions = const [],
this.actionsBuilder,
required this.onSelected,
this.label = 'More actions',
this.sheetTitle,
this.sheetSubtitle,
this.enabled = true,
this.icon = const Icon(Icons.more_vert),
});
final List<AppMenuAction<T>> actions;
/// When given, supplies the choices each time the menu opens, instead of
/// [actions]; for choices that depend on state read at that moment.
final List<AppMenuAction<T>> Function()? actionsBuilder;
/// Called with the picked value once the menu has closed.
final void Function(BuildContext context, T value) onSelected;
/// Tooltip and accessible label.
final String label;
/// The phone sheet's header: what the actions apply to (a chat, a file).
final String? sheetTitle;
/// A second header line under [sheetTitle] (a file's size, a folder).
final String? sheetSubtitle;
final bool enabled;
/// The button glyph. Most callers use the default overflow icon; controls
/// whose action has a distinct meaning can provide a matching Material icon.
final Widget icon;
List<AppMenuAction<T>> _ordered() {
final all = actionsBuilder?.call() ?? actions;
return [
for (final action in all)
if (!action.destructive) action,
for (final action in all)
if (action.destructive) action,
];
}
@override
Widget build(BuildContext context) {
if (actionsBuilder == null && actions.isEmpty) {
return const SizedBox.shrink();
}
if (!usesDesktopPopups(context)) {
return IconButton(
icon: icon,
tooltip: label,
onPressed: !enabled
? null
: () => showAppActionSheet(
context,
title: sheetTitle,
subtitle: sheetSubtitle,
actions: [
for (final action in _ordered())
AppSheetAction(
key: action.key,
label: action.label,
icon: action.icon,
destructive: action.destructive,
enabled: action.enabled,
onSelected: () {
if (context.mounted) onSelected(context, action.value);
},
),
],
),
);
}
final scheme = Theme.of(context).colorScheme;
return Localizations.override(
context: context,
delegates: const [DefaultMaterialLocalizations.delegate],
child: Material(
type: MaterialType.transparency,
child: PopupMenuButton<T>(
icon: icon,
tooltip: label,
enabled: enabled,
constraints: const BoxConstraints(minWidth: 220),
onSelected: (value) => onSelected(context, value),
itemBuilder: (_) {
final ordered = _ordered();
final firstDestructive = ordered.indexWhere((a) => a.destructive);
return [
for (final (index, action) in ordered.indexed) ...[
if (index == firstDestructive && index > 0)
const PopupMenuDivider(),
PopupMenuItem<T>(
key: action.key,
value: action.value,
enabled: action.enabled,
child: _MenuRow(
label: action.label,
icon: action.icon,
color: action.destructive && action.enabled
? scheme.error
: null,
),
),
],
];
},
),
),
);
}
}
class _MenuRow extends StatelessWidget {
const _MenuRow({required this.label, this.icon, this.color});
final String label;
final IconData? icon;
final Color? color;
@override
Widget build(BuildContext context) {
final text = Text(
label,
style: color == null ? null : TextStyle(color: color),
);
if (icon == null) return text;
return Row(
children: [
Icon(
icon,
size: 20,
color: color ?? Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 12),
Flexible(child: text),
],
);
}
}