30934aa71c
Alt+T, Ctrl+T and Cmd+T join Ctrl+` as early focus-manager key handlers, so they toggle the panel, window or narrow page from anywhere without reaching the shell or a text field. Opening with no terminal starts one. The shown terminal takes the keyboard (also on reload and tab changes) and hiding gives it back. The panel slides from the bottom edge as the app shrinks, the window scales and fades, both on Motion.standard and off under reduced motion. While shown, Ctrl+Tab/Ctrl+Shift+Tab move between terminal tabs instead of chat panes. The tooltip and menu list the shortcuts the platform delivers.
104 lines
3.0 KiB
Dart
104 lines
3.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// One choice of a [TerminalMenuButton].
|
|
class TerminalChoice<T> {
|
|
const TerminalChoice(this.value, this.label, {this.checked, this.key});
|
|
final T value;
|
|
final String label;
|
|
|
|
/// Non-null draws the choice as a checkable item.
|
|
final bool? checked;
|
|
final Key? key;
|
|
}
|
|
|
|
/// An icon button opening the host's menu: a Material popup menu, or a
|
|
/// Cupertino action sheet with Cancel.
|
|
class TerminalMenuButton<T> extends StatelessWidget {
|
|
const TerminalMenuButton({
|
|
super.key,
|
|
required this.cupertino,
|
|
required this.label,
|
|
required this.icon,
|
|
required this.cupertinoIcon,
|
|
required this.choices,
|
|
required this.onSelected,
|
|
this.info,
|
|
});
|
|
|
|
final bool cupertino;
|
|
final String label;
|
|
final IconData icon;
|
|
final IconData cupertinoIcon;
|
|
final List<TerminalChoice<T>> choices;
|
|
final ValueChanged<T> onSelected;
|
|
|
|
/// A note under the choices (the terminal's shortcuts), not selectable.
|
|
final String? info;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!cupertino) {
|
|
return PopupMenuButton<T>(
|
|
tooltip: label,
|
|
icon: Icon(icon),
|
|
onSelected: onSelected,
|
|
itemBuilder: (_) => [
|
|
for (final choice in choices)
|
|
choice.checked == null
|
|
? PopupMenuItem<T>(
|
|
key: choice.key,
|
|
value: choice.value,
|
|
child: Text(choice.label),
|
|
)
|
|
: CheckedPopupMenuItem<T>(
|
|
key: choice.key,
|
|
value: choice.value,
|
|
checked: choice.checked!,
|
|
child: Text(choice.label),
|
|
),
|
|
if (info case final info?) ...[
|
|
const PopupMenuDivider(),
|
|
PopupMenuItem<T>(
|
|
key: const ValueKey('terminal-menu-info'),
|
|
enabled: false,
|
|
child: Text(info),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
return CupertinoButton(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
onPressed: () => _sheet(context),
|
|
child: Icon(cupertinoIcon, semanticLabel: label),
|
|
);
|
|
}
|
|
|
|
Future<void> _sheet(BuildContext context) async {
|
|
final picked = await showCupertinoModalPopup<T>(
|
|
context: context,
|
|
builder: (sheet) => CupertinoActionSheet(
|
|
title: Text(label),
|
|
message: info == null ? null : Text(info!),
|
|
actions: [
|
|
for (final choice in choices)
|
|
CupertinoActionSheetAction(
|
|
key: choice.key,
|
|
isDefaultAction: choice.checked ?? false,
|
|
onPressed: () => Navigator.of(sheet).pop(choice.value),
|
|
child: Text(
|
|
choice.checked ?? false ? '${choice.label} ✓' : choice.label,
|
|
),
|
|
),
|
|
],
|
|
cancelButton: CupertinoActionSheetAction(
|
|
onPressed: () => Navigator.of(sheet).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
),
|
|
);
|
|
if (picked != null) onSelected(picked);
|
|
}
|
|
}
|