Files
pipelines/app/lib/shell/shell_list_shortcuts.dart
T
alan f7b5958d64 App: chat colours, Ctrl+1-9 opens the Nth list item, starred chats alphabetical
A chat's colour is picked from its row menu (Colour...), a long-press or
the chat information sheet. The list shows it as a leading bar, a faint
row tint and a coloured avatar; the open chat shows a faint wash under its
top bar. Shades are Cupertino's dynamic system colours and Material
swatches tuned per brightness. Changes from other devices apply live.

Ctrl+1...Ctrl+9 open the Nth item of the open tab's list (chats, runs,
pipelines) in on-screen order, also while typing. Starred chats are sorted
by name, case-insensitively, as one alphabetical group.
2026-11-03 11:05:38 +00:00

99 lines
3.0 KiB
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. One opener per tab; 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 a Ctrl+1…Ctrl+9 key event names, or null for any
/// other event. Shift, Alt and Meta must be up, so system and browser chords
/// built on the digits are left alone.
int? shellListShortcutIndex(KeyEvent event, HardwareKeyboard keyboard) {
if (event is! KeyDownEvent ||
!keyboard.isControlPressed ||
keyboard.isShiftPressed ||
keyboard.isAltPressed ||
keyboard.isMetaPressed) {
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<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;
}