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.
201 lines
5.9 KiB
Dart
201 lines
5.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../core/api_gateway.dart';
|
|
import '../../ui/motion/motion.dart';
|
|
import 'terminal_focus.dart';
|
|
import 'terminal_hub.dart';
|
|
import 'terminal_layout.dart';
|
|
import 'terminal_open_action.dart';
|
|
import 'terminal_page.dart';
|
|
import 'terminal_scope.dart';
|
|
import 'terminal_shortcuts.dart';
|
|
import 'terminal_stage.dart';
|
|
|
|
/// Below this width the terminal opens as a full-screen page.
|
|
const double terminalNarrowWidth = 600;
|
|
|
|
/// The app's one terminal surface around [child] (the shell): the bottom
|
|
/// panel or the floating window on a wide window, a full-screen page on a
|
|
/// narrow one. It owns the terminal's shortcuts (see
|
|
/// [terminalToggleShortcut], [terminalTabShortcut]), its show and hide
|
|
/// motion and its focus, and gives screens below it [TerminalActions]
|
|
/// through [TerminalScope]. [child] keeps its place in the tree whatever is
|
|
/// shown, so showing the terminal never rebuilds the app's state.
|
|
class TerminalSurface extends StatefulWidget {
|
|
const TerminalSurface({
|
|
super.key,
|
|
required this.hub,
|
|
required this.layout,
|
|
required this.cupertino,
|
|
required this.child,
|
|
});
|
|
|
|
final TerminalHub hub;
|
|
final TerminalLayout layout;
|
|
final bool cupertino;
|
|
final Widget child;
|
|
|
|
@override
|
|
State<TerminalSurface> createState() => _TerminalSurfaceState();
|
|
}
|
|
|
|
class _TerminalSurfaceState extends State<TerminalSurface>
|
|
with SingleTickerProviderStateMixin
|
|
implements TerminalActions {
|
|
late final AnimationController _reveal = AnimationController(
|
|
vsync: this,
|
|
duration: Motion.standard,
|
|
value: widget.layout.visible ? 1 : 0,
|
|
);
|
|
late final CurvedAnimation _curve = CurvedAnimation(
|
|
parent: _reveal,
|
|
curve: Motion.curve,
|
|
reverseCurve: Curves.easeInCubic,
|
|
);
|
|
final TerminalFocus _focus = TerminalFocus();
|
|
final TerminalPageToggle _page = TerminalPageToggle();
|
|
|
|
/// What was last acted on: shown on a wide window, the mode, whether the
|
|
/// window was minimised and the selected tab.
|
|
bool _shown = false;
|
|
TerminalMode? _mode;
|
|
bool _minimized = false;
|
|
String? _selected;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
FocusManager.instance.addEarlyKeyEventHandler(_key);
|
|
widget.layout.addListener(_layoutChanged);
|
|
widget.hub.addListener(_hubChanged);
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
// A terminal left shown before a reload reattaches (and takes the
|
|
// keyboard) at once; a window widened past the narrow width shows it.
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _layoutChanged());
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
FocusManager.instance.removeEarlyKeyEventHandler(_key);
|
|
widget.layout.removeListener(_layoutChanged);
|
|
widget.hub.removeListener(_hubChanged);
|
|
_curve.dispose();
|
|
_reveal.dispose();
|
|
_focus.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
bool get _narrow => MediaQuery.sizeOf(context).width < terminalNarrowWidth;
|
|
|
|
@override
|
|
bool get claimsTabKeys => _shown || _page.open;
|
|
|
|
void _layoutChanged() {
|
|
if (!mounted) return;
|
|
final layout = widget.layout;
|
|
final shown = layout.visible && !_narrow;
|
|
if (shown) widget.hub.activate();
|
|
_reveal.duration = Motion.of(context, Motion.standard);
|
|
if (shown != _shown) {
|
|
_shown = shown;
|
|
if (shown) {
|
|
_focus.remember();
|
|
unawaited(_reveal.forward());
|
|
_focus.focusSoon();
|
|
} else {
|
|
_focus.release();
|
|
unawaited(_reveal.reverse());
|
|
}
|
|
} else if (shown &&
|
|
(layout.mode != _mode || (_minimized && !layout.minimized))) {
|
|
_focus.focusSoon();
|
|
}
|
|
_mode = layout.mode;
|
|
_minimized = layout.minimized;
|
|
}
|
|
|
|
void _hubChanged() {
|
|
final selected = widget.hub.selected?.key;
|
|
if (selected == _selected) return;
|
|
_selected = selected;
|
|
if (_shown) _focus.focusSoon();
|
|
}
|
|
|
|
/// The shortcuts are handled before any focused widget sees the key, so
|
|
/// they work while the terminal itself or a text field has the keyboard,
|
|
/// and neither receives them.
|
|
KeyEventResult _key(KeyEvent event) {
|
|
if (!mounted) return KeyEventResult.ignored;
|
|
final keyboard = HardwareKeyboard.instance;
|
|
if (terminalToggleShortcut(event, keyboard)) {
|
|
toggle();
|
|
return KeyEventResult.handled;
|
|
}
|
|
final step = terminalTabShortcut(event, keyboard);
|
|
if (step == null || !claimsTabKeys) return KeyEventResult.ignored;
|
|
widget.hub.step(step);
|
|
if (!_page.open) _focus.focusSoon();
|
|
return KeyEventResult.handled;
|
|
}
|
|
|
|
@override
|
|
void toggle() {
|
|
if (_narrow) {
|
|
_page.open ? _page.close() : unawaited(_openPage());
|
|
return;
|
|
}
|
|
final opening = !widget.layout.visible;
|
|
widget.layout.toggle();
|
|
if (opening) unawaited(openTerminalIfNone(widget.hub));
|
|
}
|
|
|
|
@override
|
|
Future<String?> openAt(ApiGateway gateway, String cwd) async {
|
|
final machine = terminalMachineOf(gateway);
|
|
final refusal = await widget.hub.open(machineId: machine, cwd: cwd);
|
|
if (!mounted) return refusal;
|
|
if (_narrow) {
|
|
unawaited(_openPage());
|
|
} else {
|
|
widget.layout.show();
|
|
}
|
|
return refusal;
|
|
}
|
|
|
|
Future<void> _openPage() {
|
|
unawaited(openTerminalIfNone(widget.hub));
|
|
return _page.show(
|
|
context,
|
|
hub: widget.hub,
|
|
layout: widget.layout,
|
|
cupertino: widget.cupertino,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => TerminalScope(
|
|
actions: this,
|
|
child: ListenableBuilder(
|
|
listenable: Listenable.merge([widget.hub, widget.layout, _reveal]),
|
|
builder: (context, child) => TerminalStage(
|
|
hub: widget.hub,
|
|
layout: widget.layout,
|
|
cupertino: widget.cupertino,
|
|
// Kept while the hide motion runs.
|
|
present: !_narrow && (widget.layout.visible || !_reveal.isDismissed),
|
|
reveal: _curve,
|
|
focus: _focus.scope,
|
|
child: child!,
|
|
),
|
|
child: widget.child,
|
|
),
|
|
);
|
|
}
|