125 lines
3.8 KiB
Dart
125 lines
3.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'destination.dart';
|
|
import 'shell_bar.dart';
|
|
|
|
/// List openers registered by destination. The shell invokes only Chat for
|
|
/// Ctrl+1…Ctrl+9 (also Command on macOS); Pipelines and Runs have no chord.
|
|
/// One opener per destination; the latest mounted list wins.
|
|
class ShellListShortcuts {
|
|
final Map<AppDestination, (Object, bool Function(int index))> _openers = {};
|
|
|
|
/// Opens the item at [index] (0-based, on-screen order) of [destination]'s
|
|
/// list; false when the tab publishes no list or has no such item.
|
|
bool open(AppDestination destination, int index) =>
|
|
_openers[destination]?.$2(index) ?? false;
|
|
|
|
void _set(
|
|
AppDestination destination,
|
|
Object owner,
|
|
bool Function(int) open,
|
|
) => _openers[destination] = (owner, open);
|
|
|
|
void _clear(AppDestination destination, Object owner) {
|
|
if (_openers[destination]?.$1 == owner) _openers.remove(destination);
|
|
}
|
|
}
|
|
|
|
/// The 0-based list index named by Ctrl+1…9, or Command+1…9 on macOS.
|
|
/// Extra modifiers and non-digit events pass through.
|
|
int? shellListShortcutIndex(
|
|
KeyEvent event,
|
|
HardwareKeyboard keyboard, {
|
|
TargetPlatform? platform,
|
|
}) {
|
|
if (event is! KeyDownEvent ||
|
|
!shellPrimaryModifier(keyboard, platform: platform) ||
|
|
keyboard.isShiftPressed ||
|
|
keyboard.isAltPressed) {
|
|
return null;
|
|
}
|
|
final index = _digits.indexOf(event.logicalKey);
|
|
return index < 0 ? null : index;
|
|
}
|
|
|
|
/// Ctrl on any host, or Command on the actual macOS host. Theme overrides do
|
|
/// not change keyboard conventions in a browser.
|
|
bool shellPrimaryModifier(
|
|
HardwareKeyboard keyboard, {
|
|
TargetPlatform? platform,
|
|
}) =>
|
|
(keyboard.isControlPressed && !keyboard.isMetaPressed) ||
|
|
((platform ?? defaultTargetPlatform) == TargetPlatform.macOS &&
|
|
keyboard.isMetaPressed &&
|
|
!keyboard.isControlPressed);
|
|
|
|
bool shellSearchShortcut(
|
|
KeyEvent event,
|
|
HardwareKeyboard keyboard, {
|
|
TargetPlatform? platform,
|
|
}) =>
|
|
event is KeyDownEvent &&
|
|
event.logicalKey == LogicalKeyboardKey.slash &&
|
|
shellPrimaryModifier(keyboard, platform: platform) &&
|
|
!keyboard.isAltPressed &&
|
|
!keyboard.isShiftPressed;
|
|
|
|
const _digits = [
|
|
LogicalKeyboardKey.digit1,
|
|
LogicalKeyboardKey.digit2,
|
|
LogicalKeyboardKey.digit3,
|
|
LogicalKeyboardKey.digit4,
|
|
LogicalKeyboardKey.digit5,
|
|
LogicalKeyboardKey.digit6,
|
|
LogicalKeyboardKey.digit7,
|
|
LogicalKeyboardKey.digit8,
|
|
LogicalKeyboardKey.digit9,
|
|
];
|
|
|
|
/// Publishes, while mounted, how its tab's list opens its Nth item, so the
|
|
/// shell can open an item without coupling to its screen (also while a text
|
|
/// field has focus). Keyboard dispatch currently invokes only Chat. [onOpenIndex] reads the list at key time, in on-screen
|
|
/// order, and reports whether an item was opened. Outside a shell it does
|
|
/// nothing.
|
|
class ShellListShortcut extends StatefulWidget {
|
|
const ShellListShortcut({
|
|
super.key,
|
|
required this.onOpenIndex,
|
|
required this.child,
|
|
});
|
|
|
|
final bool Function(int index) onOpenIndex;
|
|
final Widget child;
|
|
|
|
@override
|
|
State<ShellListShortcut> createState() => _ShellListShortcutState();
|
|
}
|
|
|
|
class _ShellListShortcutState extends State<ShellListShortcut> {
|
|
final Object _owner = Object();
|
|
ShellBarScope? _scope;
|
|
|
|
bool _open(int index) => mounted && widget.onOpenIndex(index);
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
final scope = ShellBarScope.maybeOf(context);
|
|
if (scope == _scope) return;
|
|
_scope?.registry.lists._clear(_scope!.destination, _owner);
|
|
_scope = scope;
|
|
scope?.registry.lists._set(scope.destination, _owner, _open);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scope?.registry.lists._clear(_scope!.destination, _owner);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => widget.child;
|
|
}
|