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.
52 lines
1.6 KiB
Dart
52 lines
1.6 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
/// The terminal surface's keyboard focus: the selected terminal takes it
|
|
/// whenever the terminal shows (or its tab changes), and hiding gives it back
|
|
/// to what had it before (the chat composer, a field), else to the screen.
|
|
class TerminalFocus {
|
|
/// The scope around the selected terminal (see `TerminalBody.focus`). Its
|
|
/// emulator autofocuses inside it, so focusing the scope focuses it.
|
|
final FocusScopeNode scope = FocusScopeNode(debugLabel: 'terminal');
|
|
FocusNode? _previous;
|
|
|
|
bool _inside(FocusNode node) =>
|
|
identical(node, scope) || node.ancestors.contains(scope);
|
|
|
|
/// Notes what has the keyboard now, before the terminal takes it.
|
|
void remember() {
|
|
final current = FocusManager.instance.primaryFocus;
|
|
if (current == null || _inside(current)) return;
|
|
_previous = identical(current, FocusManager.instance.rootScope)
|
|
? null
|
|
: current;
|
|
}
|
|
|
|
/// Focuses the selected terminal once this frame has built it.
|
|
void focusSoon() {
|
|
WidgetsBinding.instance
|
|
..addPostFrameCallback((_) {
|
|
if (scope.context != null && scope.canRequestFocus) {
|
|
scope.requestFocus();
|
|
}
|
|
})
|
|
..ensureVisualUpdate();
|
|
}
|
|
|
|
/// Gives the keyboard back to what had it before the terminal showed, if
|
|
/// the terminal has it now.
|
|
void release() {
|
|
final previous = _previous;
|
|
_previous = null;
|
|
if (!scope.hasFocus) return;
|
|
if (previous != null &&
|
|
previous.context != null &&
|
|
previous.canRequestFocus) {
|
|
previous.requestFocus();
|
|
} else {
|
|
scope.unfocus();
|
|
}
|
|
}
|
|
|
|
void dispose() => scope.dispose();
|
|
}
|