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'; import 'terminal_window.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, this.floating, }); 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; /// The terminal's own floating window among them; without it the float /// mode shows nothing. final TerminalWindows? floating; @override State createState() => _TerminalSurfaceState(); } class _TerminalSurfaceState extends State with SingleTickerProviderStateMixin implements TerminalActions { late final AnimationController _reveal = AnimationController( vsync: this, duration: Motion.standard, value: widget.layout.visible && widget.layout.mode == TerminalMode.panel ? 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 panel shown, and /// the selected tab. bool _shown = false; bool _panelShown = 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(); widget.floating?.wide = !_narrow; // The floating window has its own motion and focus (the window // system's); this surface moves the panel and its keyboard. final panel = shown && layout.mode == TerminalMode.panel; _reveal.duration = Motion.of(context, Motion.standard); _shown = shown; if (panel != _panelShown) { _panelShown = panel; if (panel) { _focus.remember(); unawaited(_reveal.forward()); _focus.focusSoon(); } else { _focus.release(); unawaited(_reveal.reverse()); } } } void _hubChanged() { final selected = widget.hub.selected?.key; if (selected == _selected) return; _selected = selected; if (_panelShown) _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 && _panelShown) _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 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 _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, ), ); }