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 load(); Future save(String value); } class SharedTerminalLayoutStore implements TerminalLayoutStore { const SharedTerminalLayoutStore(); @override Future load() async { try { return (await SharedPreferences.getInstance()).getString( terminalLayoutKey, ); } catch (_) { return null; } } @override Future 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 the last change could not be saved. bool saveFailed = false; Future 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; } catch (_) { return; } notifyListeners(); } void setMode(TerminalMode value) => _change(() => mode = 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 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, }), ); 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; }