16afda3634
AppMenuAnchor is the app's one MenuAnchor: root overlay, takes the keyboard while open (Escape and arrows work wherever focus was) and gives it back. A window no longer takes the keyboard back from a popup its own press opened. In a window, Files opens as a page of that window and Chat details is in the More menu.
183 lines
5.5 KiB
Dart
183 lines
5.5 KiB
Dart
import 'app_menu_anchor.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// The key of a [SegmentedBar]'s More button, shown when its actions do not
|
|
/// all fit.
|
|
const segmentedMoreKey = ValueKey<String>('segmented.more');
|
|
|
|
/// One choice under a [SegmentedOverflowEntry] (a sort order): a submenu on
|
|
/// Material, a second action sheet on Cupertino.
|
|
class SegmentedMenuChoice {
|
|
const SegmentedMenuChoice({
|
|
required this.label,
|
|
required this.onSelected,
|
|
this.selected = false,
|
|
});
|
|
|
|
final String label;
|
|
final VoidCallback onSelected;
|
|
final bool selected;
|
|
}
|
|
|
|
/// How a bar action reads in the bar's More menu once a narrow bar moves it
|
|
/// there: its label and icon, and what choosing it does.
|
|
class SegmentedOverflowEntry {
|
|
const SegmentedOverflowEntry({
|
|
required this.label,
|
|
required this.icon,
|
|
this.onPressed,
|
|
this.choices = const [],
|
|
this.shortcut,
|
|
});
|
|
|
|
final String label;
|
|
final IconData icon;
|
|
|
|
/// What choosing the entry does; null (with no [choices]) disables it.
|
|
final VoidCallback? onPressed;
|
|
|
|
/// Choices offered under the entry instead of [onPressed].
|
|
final List<SegmentedMenuChoice> choices;
|
|
|
|
/// The keyboard shortcut the Material menu lists beside the entry. It is
|
|
/// shown only; the owner handles the keys itself.
|
|
final MenuSerializableShortcut? shortcut;
|
|
}
|
|
|
|
/// A bar action the bar may move into its More menu. An action that is not
|
|
/// [SegmentedOverflowable] always stays in the bar.
|
|
abstract interface class SegmentedOverflowable {
|
|
SegmentedOverflowEntry get overflowEntry;
|
|
}
|
|
|
|
/// Marks any bar action as [SegmentedOverflowable]: it draws [child] in the
|
|
/// bar and [overflowEntry] in the More menu.
|
|
class SegmentedOverflowAction extends StatelessWidget
|
|
implements SegmentedOverflowable {
|
|
const SegmentedOverflowAction({
|
|
super.key,
|
|
required this.overflowEntry,
|
|
required this.child,
|
|
});
|
|
|
|
@override
|
|
final SegmentedOverflowEntry overflowEntry;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => child;
|
|
}
|
|
|
|
/// The bar's More button: the host's overflow icon at the platform's touch
|
|
/// target, opening a Material menu or a Cupertino action sheet of [entries].
|
|
class SegmentedMoreButton extends StatelessWidget {
|
|
const SegmentedMoreButton({
|
|
super.key,
|
|
required this.cupertino,
|
|
required this.entries,
|
|
});
|
|
|
|
final bool cupertino;
|
|
final List<SegmentedOverflowEntry> entries;
|
|
|
|
static const String label = 'More actions';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (cupertino) {
|
|
return Semantics(
|
|
button: true,
|
|
label: label,
|
|
excludeSemantics: true,
|
|
child: CupertinoButton(
|
|
key: segmentedMoreKey,
|
|
padding: EdgeInsets.zero,
|
|
minimumSize: const Size.square(kMinInteractiveDimensionCupertino),
|
|
onPressed: () => _showSheet(context),
|
|
child: const Icon(CupertinoIcons.ellipsis),
|
|
),
|
|
);
|
|
}
|
|
return AppMenuAnchor(
|
|
menuChildren: [
|
|
for (final entry in entries)
|
|
if (entry.choices.isEmpty)
|
|
MenuItemButton(
|
|
leadingIcon: Icon(entry.icon),
|
|
onPressed: entry.onPressed,
|
|
shortcut: entry.shortcut,
|
|
child: Text(entry.label),
|
|
)
|
|
else
|
|
SubmenuButton(
|
|
leadingIcon: Icon(entry.icon),
|
|
menuChildren: [
|
|
for (final choice in entry.choices)
|
|
MenuItemButton(
|
|
leadingIcon: Icon(
|
|
Icons.check,
|
|
color: choice.selected ? null : Colors.transparent,
|
|
),
|
|
onPressed: choice.onSelected,
|
|
child: Semantics(
|
|
selected: choice.selected,
|
|
child: Text(choice.label),
|
|
),
|
|
),
|
|
],
|
|
child: Text(entry.label),
|
|
),
|
|
],
|
|
builder: (context, menu, _) => IconButton(
|
|
key: segmentedMoreKey,
|
|
tooltip: label,
|
|
onPressed: () => menu.isOpen ? menu.close() : menu.open(),
|
|
icon: const Icon(Icons.more_vert),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _showSheet(BuildContext context) async {
|
|
final picked = await _sheet<SegmentedOverflowEntry>(context, [
|
|
for (final entry in entries)
|
|
if (entry.onPressed != null || entry.choices.isNotEmpty)
|
|
(entry.label, false, entry),
|
|
]);
|
|
if (picked == null || !context.mounted) return;
|
|
if (picked.choices.isEmpty) {
|
|
picked.onPressed?.call();
|
|
return;
|
|
}
|
|
final choice = await _sheet<SegmentedMenuChoice>(context, [
|
|
for (final choice in picked.choices)
|
|
(choice.label, choice.selected, choice),
|
|
], title: picked.label);
|
|
choice?.onSelected();
|
|
}
|
|
|
|
Future<T?> _sheet<T>(
|
|
BuildContext context,
|
|
List<(String, bool, T)> items, {
|
|
String? title,
|
|
}) => showCupertinoModalPopup<T>(
|
|
context: context,
|
|
builder: (sheet) => CupertinoActionSheet(
|
|
title: title == null ? null : Text(title),
|
|
actions: [
|
|
for (final (label, selected, value) in items)
|
|
CupertinoActionSheetAction(
|
|
// The current choice is the sheet's default (bold) action.
|
|
isDefaultAction: selected,
|
|
onPressed: () => Navigator.of(sheet).pop(value),
|
|
child: Semantics(selected: selected, child: Text(label)),
|
|
),
|
|
],
|
|
cancelButton: CupertinoActionSheetAction(
|
|
onPressed: () => Navigator.of(sheet).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
),
|
|
);
|
|
}
|