Terminal surface: lazy activation, settings mode choice, open in terminal, tests

This commit is contained in:
2026-11-09 21:55:29 +00:00
parent acd160b49e
commit e79a120ac7
7 changed files with 633 additions and 6 deletions
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'terminal_body.dart';
@@ -153,6 +154,8 @@ class TerminalFloat extends StatelessWidget {
child: GestureDetector(
key: const ValueKey('terminal-float-title'),
behavior: HitTestBehavior.opaque,
// The window follows the pointer from where it went down.
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => _move(details.delta),
onPanEnd: (_) => unawaited(layout.save()),
onDoubleTap: () => layout.setMaximized(!layout.maximized),
@@ -212,6 +215,7 @@ class TerminalFloat extends StatelessWidget {
child: GestureDetector(
key: const ValueKey('terminal-float-resize'),
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => _resize(details.delta),
onPanEnd: (_) => unawaited(layout.save()),
child: Padding(
+18 -5
View File
@@ -16,13 +16,11 @@ import 'terminal_tab.dart';
/// restart) its terminals are listed, new ones get a tab, and every tab
/// resumes its output from the last byte it holds.
class TerminalHub extends ChangeNotifier {
TerminalHub(this.machines) {
machines.addListener(_machinesChanged);
_machinesChanged();
}
TerminalHub(this.machines);
final TerminalMachines machines;
final Map<String, _Link> _links = {};
bool _active = false;
final List<TerminalTab> _tabs = [];
String? _selected;
bool _disposed = false;
@@ -43,6 +41,20 @@ class TerminalHub extends ChangeNotifier {
connected.length > 1 ||
_tabs.map((tab) => tab.machineId).toSet().length > 1;
/// Starts following the machines: their terminals are listed now and on
/// every reconnect. The surface calls it the first time the terminal is
/// shown, so an app that never shows it sends no terminal requests (each
/// connection sends one request at a time; the first screen's read goes
/// first).
void activate() {
if (_active || _disposed) return;
_active = true;
machines.addListener(_machinesChanged);
_machinesChanged();
}
bool get active => _active;
void select(TerminalTab tab) {
if (_selected == tab.key) return;
_selected = tab.key;
@@ -56,6 +68,7 @@ class TerminalHub extends ChangeNotifier {
/// Opens a shell on [machineId] (the first connected machine when null)
/// in [cwd] (home when empty) and selects its tab. Returns the refusal.
Future<String?> open({String? machineId, String cwd = ''}) async {
activate();
final machine = machineId == null
? connected.firstOrNull
: machineFor(machineId);
@@ -209,7 +222,7 @@ class TerminalHub extends ChangeNotifier {
@override
void dispose() {
_disposed = true;
machines.removeListener(_machinesChanged);
if (_active) machines.removeListener(_machinesChanged);
for (final link in _links.values) {
link.cancel();
}
@@ -62,6 +62,9 @@ class _TerminalPanelState extends State<TerminalPanel> {
save: save,
);
String _describe(double height) =>
'${terminalPanelHeight(height, widget.available).round()} pixels';
KeyEventResult _key(FocusNode _, KeyEvent event) {
if (event is! KeyDownEvent && event is! KeyRepeatEvent) {
return KeyEventResult.ignored;
@@ -84,7 +87,9 @@ class _TerminalPanelState extends State<TerminalPanel> {
label: 'Terminal height',
hint: 'Drag, or use the arrow keys, to resize the terminal',
slider: true,
value: '${height.round()} pixels',
value: _describe(height),
increasedValue: _describe(height + terminalPanelKeyStep),
decreasedValue: _describe(height - terminalPanelKeyStep),
onIncrease: () => _resize(height + terminalPanelKeyStep),
onDecrease: () => _resize(height - terminalPanelKeyStep),
child: Focus(
@@ -55,14 +55,22 @@ class _TerminalSurfaceState extends State<TerminalSurface>
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
@@ -98,6 +106,7 @@ class _TerminalSurfaceState extends State<TerminalSurface>
}
Future<void> _openPage() async {
widget.hub.activate();
if (_pageOpen) return;
_pageOpen = true;
try {