7d1ffe8ad3
- Live terminals no longer reflow (xterm.dart 4.0.0 left the cursor on the wrong row); narrowing cuts and erases cells past the width, rows scroll into scrollback keeping the cursor row; finished output still reflows. - App-side key encoding before xterm.dart's defaults: Alt as Meta (ESC + the typed character), Ctrl symbol table, xterm modifier parameter for arrows/Home/End/PgUp/PgDn/Ins/Del/F-keys, DECCKM and DECKPAM, Alt+BS. - Settings > Appearance > Terminal: Use Option as Meta key (Macs, on). - Inside a focused terminal only Cmd+T and Ctrl+` toggle it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
4.7 KiB
Dart
156 lines
4.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:ui';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
const terminalLayoutKey = 'pipelines.terminal-layout.v1';
|
|
|
|
/// Where the terminal shows on a wide window.
|
|
enum TerminalMode {
|
|
panel('Bottom panel'),
|
|
float('Floating window');
|
|
|
|
const TerminalMode(this.label);
|
|
final String label;
|
|
}
|
|
|
|
/// The default and smallest heights of the bottom panel, and the smallest
|
|
/// floating window; layouts clamp to what the window allows.
|
|
const double terminalPanelDefaultHeight = 300;
|
|
const double terminalPanelMinHeight = 120;
|
|
const Size terminalFloatMinSize = Size(320, 200);
|
|
const Rect terminalFloatDefaultRect = Rect.fromLTWH(80, 80, 720, 420);
|
|
|
|
abstract interface class TerminalLayoutStore {
|
|
Future<String?> load();
|
|
Future<bool> save(String value);
|
|
}
|
|
|
|
class SharedTerminalLayoutStore implements TerminalLayoutStore {
|
|
const SharedTerminalLayoutStore();
|
|
@override
|
|
Future<String?> load() async {
|
|
try {
|
|
return (await SharedPreferences.getInstance()).getString(
|
|
terminalLayoutKey,
|
|
);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<bool> save(String value) async {
|
|
try {
|
|
return await (await SharedPreferences.getInstance()).setString(
|
|
terminalLayoutKey,
|
|
value,
|
|
);
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The terminal surface's device-local layout: the mode, whether it is
|
|
/// shown and the bottom panel's height. Every change is saved, so a reload
|
|
/// reopens it as it was. The floating window's frame is the window
|
|
/// system's.
|
|
class TerminalLayout extends ChangeNotifier {
|
|
TerminalLayout({TerminalLayoutStore? store})
|
|
: _store = store ?? const SharedTerminalLayoutStore();
|
|
static final instance = TerminalLayout();
|
|
final TerminalLayoutStore _store;
|
|
int _revision = 0;
|
|
|
|
TerminalMode mode = TerminalMode.panel;
|
|
bool visible = false;
|
|
double panelHeight = terminalPanelDefaultHeight;
|
|
|
|
/// The floating window's frame and maximised state as saved before the
|
|
/// window moved to the app's window system (`TerminalWindows`), which takes
|
|
/// them for its first window. They are still written back unchanged.
|
|
Rect floatRect = terminalFloatDefaultRect;
|
|
bool maximized = false;
|
|
|
|
/// Whether a Mac's Option key is Meta in the terminal (Option+B sends
|
|
/// ESC b, readline's word back) rather than typing its symbol (∫). Other
|
|
/// platforms' Alt is always Meta.
|
|
bool optionAsMeta = true;
|
|
|
|
/// Whether the last change could not be saved.
|
|
bool saveFailed = false;
|
|
|
|
Future<void> restore() async {
|
|
final revision = _revision;
|
|
final raw = await _store.load();
|
|
if (revision != _revision || raw == null) return;
|
|
try {
|
|
final json = jsonDecode(raw) as Map;
|
|
mode = TerminalMode.values.asNameMap()[json['mode']] ?? mode;
|
|
visible = json['visible'] == true;
|
|
panelHeight = _finite(json['panel_height']) ?? panelHeight;
|
|
final rect = json['float'];
|
|
if (rect is List && rect.length == 4) {
|
|
final values = rect.map(_finite).toList();
|
|
if (!values.contains(null)) {
|
|
floatRect = Rect.fromLTWH(
|
|
values[0]!,
|
|
values[1]!,
|
|
values[2]!,
|
|
values[3]!,
|
|
);
|
|
}
|
|
}
|
|
maximized = json['maximized'] == true;
|
|
optionAsMeta = json['option_as_meta'] != false;
|
|
} catch (_) {
|
|
return;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
void setMode(TerminalMode value) => _change(() => mode = value);
|
|
void setOptionAsMeta(bool value) => _change(() => optionAsMeta = value);
|
|
void toggle() => _change(() => visible = !visible);
|
|
void show() => _change(() => visible = true);
|
|
void hide() => _change(() => visible = false);
|
|
void setPanelHeight(double value, {bool save = true}) =>
|
|
_change(() => panelHeight = value, save: save);
|
|
|
|
/// Saves the current layout (a drag saves once, when it ends).
|
|
Future<void> save() async {
|
|
final revision = _revision;
|
|
final saved = await _store.save(
|
|
jsonEncode({
|
|
'mode': mode.name,
|
|
'visible': visible,
|
|
'panel_height': panelHeight,
|
|
'float': [
|
|
floatRect.left,
|
|
floatRect.top,
|
|
floatRect.width,
|
|
floatRect.height,
|
|
],
|
|
'maximized': maximized,
|
|
'option_as_meta': optionAsMeta,
|
|
}),
|
|
);
|
|
if (revision != _revision || saved != saveFailed) return;
|
|
saveFailed = !saved;
|
|
notifyListeners();
|
|
}
|
|
|
|
void _change(VoidCallback change, {bool save = true}) {
|
|
_revision++;
|
|
change();
|
|
notifyListeners();
|
|
if (save) unawaited(this.save());
|
|
}
|
|
|
|
static double? _finite(Object? value) =>
|
|
value is num && value.isFinite ? value.toDouble() : null;
|
|
}
|