Files
pipelines/app/lib/features/terminal/terminal_layout.dart
T

162 lines
4.5 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, the bottom panel's height and the floating window's frame and
/// state. Every change is saved, so a reload reopens it as it was.
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;
Rect floatRect = terminalFloatDefaultRect;
bool minimized = false;
bool maximized = false;
/// 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]!,
);
}
}
minimized = json['minimized'] == true;
maximized = json['maximized'] == true;
} catch (_) {
return;
}
notifyListeners();
}
void setMode(TerminalMode value) => _change(() => mode = value);
void toggle() => _change(() {
visible = !visible;
if (visible) minimized = false;
});
void show() => _change(() {
visible = true;
minimized = false;
});
void hide() => _change(() => visible = false);
void setPanelHeight(double value, {bool save = true}) =>
_change(() => panelHeight = value, save: save);
void setFloatRect(Rect value, {bool save = true}) =>
_change(() => floatRect = value, save: save);
void setMinimized(bool value) => _change(() {
minimized = value;
if (value) maximized = false;
});
void setMaximized(bool value) => _change(() {
maximized = value;
if (value) minimized = false;
});
/// 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,
],
'minimized': minimized,
'maximized': maximized,
}),
);
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;
}