import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'destination.dart'; import 'shell_bar.dart'; /// Opens the Nth item of a tab's list: what each tab's list screen published /// for Ctrl+1…Ctrl+9 (also Command on macOS). One opener per tab; /// the latest mounted list wins. class ShellListShortcuts { final Map _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, }) { final control = keyboard.isControlPressed && !keyboard.isMetaPressed; final command = (platform ?? defaultTargetPlatform) == TargetPlatform.macOS && keyboard.isMetaPressed && !keyboard.isControlPressed; if (event is! KeyDownEvent || !(control || command) || keyboard.isShiftPressed || keyboard.isAltPressed) { return null; } final index = _digits.indexOf(event.logicalKey); return index < 0 ? null : index; } 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's Ctrl+1…Ctrl+9 open it from anywhere in the tab (also while a text /// field has focus). [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 createState() => _ShellListShortcutState(); } class _ShellListShortcutState extends State { 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; }