import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../core/api_gateway.dart'; import 'terminal_float.dart'; import 'terminal_hub.dart'; import 'terminal_layout.dart'; import 'terminal_page.dart'; import 'terminal_panel.dart'; import 'terminal_scope.dart'; /// Below this width the terminal opens as a full-screen page. const double terminalNarrowWidth = 600; /// Whether [event] is the terminal's toggle shortcut, Ctrl+` on every /// platform (Cmd+` switches windows on macOS). bool terminalToggleShortcut(KeyEvent event, HardwareKeyboard keyboard) => event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.backquote && keyboard.isControlPressed && !keyboard.isMetaPressed && !keyboard.isAltPressed; /// 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 toggle shortcut 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 createState() => _TerminalSurfaceState(); } class _TerminalSurfaceState extends State implements TerminalActions { bool _pageOpen = false; @override void initState() { super.initState(); HardwareKeyboard.instance.addHandler(_key); widget.layout.addListener(_layoutChanged); // A terminal left shown before a reload reattaches at once. WidgetsBinding.instance.addPostFrameCallback((_) => _layoutChanged()); } @override void dispose() { HardwareKeyboard.instance.removeHandler(_key); widget.layout.removeListener(_layoutChanged); super.dispose(); } void _layoutChanged() { if (mounted && widget.layout.visible && !_narrow) widget.hub.activate(); } bool get _narrow => MediaQuery.sizeOf(context).width < terminalNarrowWidth; /// The shortcut is handled before the focused widget, so it toggles even /// while the terminal itself has the keyboard. bool _key(KeyEvent event) { if (!mounted || !terminalToggleShortcut(event, HardwareKeyboard.instance)) { return false; } toggle(); return true; } @override void toggle() { if (_narrow) { unawaited(_openPage()); } else { widget.layout.toggle(); } } @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 { widget.hub.activate(); if (_pageOpen) return; _pageOpen = true; try { await openTerminalPage( context, hub: widget.hub, layout: widget.layout, cupertino: widget.cupertino, ); } finally { _pageOpen = false; } } @override Widget build(BuildContext context) => TerminalScope( actions: this, child: ListenableBuilder( listenable: Listenable.merge([widget.hub, widget.layout]), builder: (context, child) => LayoutBuilder( builder: (context, constraints) { final layout = widget.layout; final shown = layout.visible && !_narrow; final panel = shown && layout.mode == TerminalMode.panel; final float = shown && layout.mode == TerminalMode.float; final height = panel ? terminalPanelHeight(layout.panelHeight, constraints.maxHeight) : 0.0; return Stack( children: [ Positioned.fill( bottom: height, // The app above a docked panel has no system inset below it. child: MediaQuery.removePadding( context: context, removeBottom: panel, child: child!, ), ), if (panel) Positioned( left: 0, right: 0, bottom: 0, height: height, child: SafeArea( top: false, child: TerminalPanel( key: const ValueKey('terminal-panel'), hub: widget.hub, layout: layout, cupertino: widget.cupertino, available: constraints.maxHeight, ), ), ), if (float) TerminalFloat( key: const ValueKey('terminal-float'), hub: widget.hub, layout: layout, cupertino: widget.cupertino, bounds: constraints.biggest, ), ], ); }, ), child: widget.child, ), ); }