Files
pipelines/app/lib/features/terminal/terminal_window.dart
T
alan 75a61dbd8c Make the terminal's floating mode a window of the window system
Phase 3 of docs/plans/floating-windows.md. The terminal window kind reuses
TerminalHub; TerminalWindows keeps the window in step with TerminalLayout
(show opens or shows it, hide minimises it, Minimise and Close hide the
terminal). The bottom panel stays a shell panel. The first window takes the
frame and maximised state pipelines.terminal-layout.v1 saved.

The terminal's own float chrome and FloatingLayer are deleted.
2026-11-10 22:19:30 +00:00

158 lines
4.6 KiB
Dart

import 'package:flutter/material.dart';
import '../../shell/windows/window_entry.dart';
import '../../shell/windows/window_kind.dart';
import '../../shell/windows/window_manager.dart';
import 'terminal_body.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_tab_strip.dart';
const terminalWindowKind = 'terminal';
const _terminalContent = 'terminal';
/// The terminal's floating mode as a window of the app's window system. The
/// terminal has one window, for the device (not a server); its tabs and
/// shells live in the [TerminalHub], so closing the window loses nothing.
///
/// [TerminalLayout] stays the terminal's own state (shown, panel or float):
/// showing it in float mode opens or shows the window, hiding minimises it,
/// and the window's Minimise and Close hide the terminal.
class TerminalWindows {
TerminalWindows({
required this.manager,
required this.hub,
required this.layout,
});
final WindowManager manager;
final TerminalHub hub;
final TerminalLayout layout;
late final TerminalWindowKind kind = TerminalWindowKind(hub, layout);
bool _started = false;
bool _ready = false;
bool _wide = true;
bool _syncing = false;
/// Registers the kind. Call before the manager restores, and [ready]
/// after it has, so opening the window never races the saved windows.
void start() {
if (_started) return;
_started = true;
manager.kinds.register(kind);
layout.addListener(sync);
manager.addListener(_windowsChanged);
}
/// The manager has restored its saved windows.
void ready() {
_ready = true;
sync();
}
void dispose() {
if (!_started) return;
_started = false;
layout.removeListener(sync);
manager.removeListener(_windowsChanged);
manager.kinds.unregister(terminalWindowKind);
}
/// Whether the app is wide enough for the window (a narrow app shows the
/// terminal as a page instead).
set wide(bool value) {
if (_wide == value) return;
_wide = value;
sync();
}
WindowEntry? get entry => manager.find(terminalWindowKind, _terminalContent);
bool get _wanted =>
layout.visible && layout.mode == TerminalMode.float && _wide;
/// Makes the window follow the layout.
void sync() {
if (!_started || !_ready || _syncing) return;
_syncing = true;
try {
final entry = this.entry;
if (_wanted) {
if (entry == null) {
_open();
} else if (!manager.isVisible(entry) ||
manager.active?.id != entry.id) {
manager.show(entry.id);
}
} else if (entry != null) {
if (layout.mode != TerminalMode.float) {
manager.close(entry.id);
} else if (manager.isVisible(entry)) {
manager.setMinimized(entry.id, true);
}
}
} finally {
_syncing = false;
}
}
/// The first window takes the frame and maximised state the terminal's own
/// layout saved before windows existed (`pipelines.terminal-layout.v1`).
void _open() {
final legacy =
manager.lastFrame(terminalWindowKind, _terminalContent) == null;
final entry = manager.open(
terminalWindowKind,
_terminalContent,
frame: legacy ? layout.floatRect : null,
);
if (legacy && layout.maximized) manager.setMaximized(entry.id, true);
}
/// The window's Minimise or Close (or the windows' own show and hide)
/// hides the terminal.
void _windowsChanged() {
if (!_started || !_ready || _syncing || !_wanted) return;
final entry = this.entry;
if (entry == null || !manager.isVisible(entry)) layout.hide();
}
}
/// The terminal in a floating window: its tabs and the selected terminal.
class TerminalWindowKind extends WindowKind {
TerminalWindowKind(this.hub, this.layout);
final TerminalHub hub;
final TerminalLayout layout;
@override
String get key => terminalWindowKind;
@override
bool get serverScoped => false;
@override
IconData get icon => Icons.terminal;
@override
Size get minSize => terminalFloatMinSize;
@override
Rect get defaultFrame => terminalFloatDefaultRect;
@override
Listenable get changes => hub;
@override
String title(BuildContext context, WindowEntry entry) =>
hub.selected?.title ?? 'Terminal';
@override
Widget build(BuildContext context, WindowEntry entry) => ListenableBuilder(
listenable: Listenable.merge([hub, layout]),
builder: (context, _) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TerminalTabStrip(hub: hub, layout: layout, cupertino: false),
Expanded(child: TerminalBody(hub: hub, cupertino: false)),
],
),
);
}