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); /// The scrollback lengths offered, and the default. const List terminalScrollbackChoices = [ 1000, 5000, 10000, 25000, 50000, 100000, ]; const int terminalScrollbackDefault = 10000; /// The terminal's text size limits (logical pixels) and one zoom step. const double terminalFontSizeMin = 8; const double terminalFontSizeMax = 32; const double terminalFontSizeStep = 1; 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 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; /// The terminal's text size, or null for the app's default (the chat /// card's body size). Ctrl/Cmd+=, - and 0 zoom it. double? fontSize; /// Lines each terminal keeps, scrollback included. int scrollback = terminalScrollbackDefault; /// Whether a bell (BEL) flashes the terminal and dots a hidden tab. bool bell = true; /// 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; optionAsMeta = json['option_as_meta'] != false; final size = _finite(json['font_size']); fontSize = size?.clamp(terminalFontSizeMin, terminalFontSizeMax); final lines = json['scrollback']; if (lines is int && terminalScrollbackChoices.contains(lines)) { scrollback = lines; } bell = json['bell'] != false; } catch (_) { return; } notifyListeners(); } void setMode(TerminalMode value) => _change(() => mode = value); void setOptionAsMeta(bool value) => _change(() => optionAsMeta = value); void setScrollback(int value) => _change(() => scrollback = value); void setBell(bool value) => _change(() => bell = value); /// Sets the text size (null: the default), within the limits. void setFontSize(double? value) => _change( () => fontSize = value?.clamp(terminalFontSizeMin, terminalFontSizeMax), ); /// One zoom step in or out ([steps] > 0 larger) from [current], the size /// the terminal draws at now. void zoom(int steps, double current) => setFontSize( (fontSize ?? current).roundToDouble() + steps * terminalFontSizeStep, ); 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, 'option_as_meta': optionAsMeta, 'font_size': ?fontSize, 'scrollback': scrollback, 'bell': bell, }), ); 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; }