Files
pipelines/app/lib/features/terminal/terminal_surface.dart
T
alan a99f7014f5 Add the shell window host with layering acceptance tests
Phase 1.3 and 1.4 of docs/plans/floating-windows.md. Windows are plain
children of the terminal stage's stack in the shell page, outside every
destination, so popups opened from one are above all of them. Each window
has its own page navigator: pushed pages open inside the window, and popup
routes go to the root navigator. One WindowFrame chrome (move, resize,
minimise, maximise, close), a focus scope per window, a ScaffoldMessenger
per window.

No feature uses it yet. The capture fixture opens probe windows with
?windows=N, and window_layering_test.dart covers every popup type from a
window over Chat and over another destination.
2026-11-10 20:48:31 +00:00

242 lines
7.3 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_shortcut_bubble.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,
this.shortcutHere = true,
this.windows,
});
final TerminalHub hub;
final TerminalLayout layout;
final bool cupertino;
final Widget child;
/// Whether the current page offers the phone shortcut to reopen the
/// terminal (the app shows it on Chat only).
final bool shortcutHere;
/// The app's floating windows (see [TerminalStage.windows]).
final Widget? windows;
@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)) {
if (event is KeyDownEvent) 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() async {
unawaited(openTerminalIfNone(widget.hub));
setState(() {});
await _page.show(
context,
hub: widget.hub,
layout: widget.layout,
cupertino: widget.cupertino,
);
if (mounted) setState(() {});
}
/// On a phone, the Chat page offers a small shortcut below the top bar's
/// right end that opens the terminal (starting one at its login prompt when
/// none is open). The stack is always there so the
/// app below never remounts when the shortcut comes or goes.
Widget _withShortcut(Widget child) {
final running = widget.hub.tabs.where((tab) => tab.info.running).length;
final show = widget.shortcutHere && _narrow && !_page.open;
return Stack(
fit: StackFit.passthrough,
children: [
child,
if (show)
PositionedDirectional(
end: 12,
top: TerminalShortcutBubble.topOf(
context,
cupertino: widget.cupertino,
),
child: TerminalShortcutBubble(
running: running,
cupertino: widget.cupertino,
onOpen: () => unawaited(_openPage()),
),
),
],
);
}
@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,
windows: widget.windows,
child: _withShortcut(child!),
),
child: widget.child,
),
);
}