070f9e205e
While shells are running and the full-screen terminal is closed, a small floating button under the right end of the top bar reopens it, with a count when more than one shell runs. The surface's stack is always present so the app never remounts when the shortcut comes or goes.
231 lines
6.9 KiB
Dart
231 lines
6.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_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,
|
|
});
|
|
|
|
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)) {
|
|
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(() {});
|
|
}
|
|
|
|
/// Running shells on a phone with the terminal page closed get a small
|
|
/// shortcut below the top bar's right end. 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 = _narrow && running > 0 && !_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,
|
|
child: _withShortcut(child!),
|
|
),
|
|
child: widget.child,
|
|
),
|
|
);
|
|
}
|