Merge main

This commit is contained in:
2026-11-10 22:42:42 +00:00
17 changed files with 535 additions and 598 deletions
@@ -1,220 +0,0 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'terminal_body.dart';
import 'terminal_float_frame.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_tab_strip.dart';
export 'terminal_float_frame.dart' show fitTerminalFloat, terminalFloatMargin;
/// The terminal as a window over the app: moved by its title bar, resized
/// from its lower right corner, minimised to its title bar or maximised to
/// the app. Its frame is kept in [layout]. It grows and fades in from its
/// place as [reveal] runs forward, and shrinks and fades out in reverse.
class TerminalFloat extends StatelessWidget {
const TerminalFloat({
super.key,
required this.hub,
required this.layout,
required this.cupertino,
required this.bounds,
this.reveal = kAlwaysCompleteAnimation,
this.focus,
this.hiding = false,
});
final TerminalHub hub;
final TerminalLayout layout;
final bool cupertino;
final Size bounds;
final Animation<double> reveal;
/// The terminal's focus scope (see [TerminalBody.focus]).
final FocusScopeNode? focus;
/// While the window hides it takes no input.
final bool hiding;
/// The scale the window grows from as it shows.
static const double revealScale = 0.92;
/// Where the window is drawn now.
Rect get frame => layout.maximized
? (Offset.zero & bounds).deflate(terminalFloatMargin)
: fitTerminalFloat(layout.floatRect, bounds);
void _move(Offset delta) {
if (layout.maximized) return;
layout.setFloatRect(frame.shift(delta), save: false);
}
void _resize(Offset delta) {
final rect = frame;
layout.setFloatRect(
fitTerminalFloat(
Rect.fromLTWH(
rect.left,
rect.top,
rect.width + delta.dx,
rect.height + delta.dy,
),
bounds,
),
save: false,
);
}
@override
Widget build(BuildContext context) {
final rect = frame;
final title = _titleBar(context);
final Widget window;
if (layout.minimized) {
window = SizedBox(width: rect.width, child: title);
} else {
window = SizedBox.fromSize(
size: rect.size,
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
title,
TerminalTabStrip(
hub: hub,
layout: layout,
cupertino: cupertino,
),
Expanded(
child: TerminalBody(
hub: hub,
cupertino: cupertino,
focus: focus,
),
),
],
),
if (!layout.maximized)
PositionedDirectional(end: 0, bottom: 0, child: _corner()),
],
),
);
}
return Positioned(
left: rect.left,
top: rect.top,
child: IgnorePointer(
ignoring: hiding,
child: ExcludeFocus(
excluding: hiding,
child: FadeTransition(
opacity: reveal,
child: ScaleTransition(
scale: Tween<double>(begin: revealScale, end: 1).animate(reveal),
child: Semantics(
container: true,
label: 'Terminal window',
child: terminalFloatChrome(context, cupertino, window),
),
),
),
),
),
);
}
Widget _titleBar(BuildContext context) {
final label = Text(
hub.selected?.title ?? 'Terminal',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: cupertino
? CupertinoTheme.of(context).textTheme.navTitleTextStyle
: Theme.of(context).textTheme.titleSmall,
);
final drag = MouseRegion(
cursor: layout.maximized
? SystemMouseCursors.basic
: SystemMouseCursors.move,
child: GestureDetector(
key: const ValueKey('terminal-float-title'),
behavior: HitTestBehavior.opaque,
// The window follows the pointer from where it went down.
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => _move(details.delta),
onPanEnd: (_) => unawaited(layout.save()),
onDoubleTap: () => layout.setMaximized(!layout.maximized),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 12),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: label,
),
),
),
);
return Row(
children: [
Expanded(child: drag),
_button(
layout.minimized ? 'Restore' : 'Minimise',
layout.minimized ? Icons.expand_less : Icons.minimize,
layout.minimized ? CupertinoIcons.chevron_up : CupertinoIcons.minus,
() => layout.setMinimized(!layout.minimized),
),
_button(
layout.maximized ? 'Restore size' : 'Maximise',
layout.maximized ? Icons.fullscreen_exit : Icons.fullscreen,
layout.maximized
? CupertinoIcons.fullscreen_exit
: CupertinoIcons.fullscreen,
() => layout.setMaximized(!layout.maximized),
),
_button(
'Hide terminal',
Icons.close,
CupertinoIcons.xmark,
layout.hide,
),
],
);
}
Widget _button(
String label,
IconData material,
IconData cupertinoIcon,
VoidCallback onPressed,
) => cupertino
? CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 8),
onPressed: onPressed,
child: Icon(cupertinoIcon, semanticLabel: label),
)
: IconButton(tooltip: label, onPressed: onPressed, icon: Icon(material));
Widget _corner() => Semantics(
label: 'Resize terminal window',
child: MouseRegion(
cursor: SystemMouseCursors.resizeDownRight,
child: GestureDetector(
key: const ValueKey('terminal-float-resize'),
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => _resize(details.delta),
onPanEnd: (_) => unawaited(layout.save()),
child: Padding(
padding: const EdgeInsets.all(4),
child: Icon(
cupertino ? CupertinoIcons.arrow_down_right : Icons.south_east,
),
),
),
),
);
}
@@ -1,47 +0,0 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'terminal_layout.dart';
/// Margin a maximised window keeps from the edges of the app.
const double terminalFloatMargin = 8;
/// [rect] moved and shrunk to fit inside [bounds], at least the minimum
/// window size where the bounds allow it.
Rect fitTerminalFloat(Rect rect, Size bounds) {
final width = rect.width
.clamp(terminalFloatMinSize.width, double.infinity)
.clamp(0.0, bounds.width);
final height = rect.height
.clamp(terminalFloatMinSize.height, double.infinity)
.clamp(0.0, bounds.height);
final left = rect.left.clamp(0.0, bounds.width - width);
final top = rect.top.clamp(0.0, bounds.height - height);
return Rect.fromLTWH(left, top, width, height);
}
/// The window's card: a Material surface with elevation, or a Cupertino
/// rounded box with a hairline and shadow.
Widget terminalFloatChrome(BuildContext context, bool cupertino, Widget child) {
if (!cupertino) {
return Material(
elevation: 8,
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.circular(12),
color: Theme.of(context).colorScheme.surfaceContainer,
child: child,
);
}
return DecoratedBox(
decoration: BoxDecoration(
color: CupertinoColors.secondarySystemBackground.resolveFrom(context),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: CupertinoColors.separator.resolveFrom(context)),
boxShadow: const [BoxShadow(blurRadius: 16, color: Color(0x33000000))],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Material(type: MaterialType.transparency, child: child),
),
);
}
+9 -23
View File
@@ -55,8 +55,9 @@ class SharedTerminalLayoutStore implements TerminalLayoutStore {
}
/// 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.
/// 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();
@@ -67,8 +68,11 @@ class TerminalLayout extends ChangeNotifier {
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 minimized = false;
bool maximized = false;
/// Whether the last change could not be saved.
@@ -95,7 +99,6 @@ class TerminalLayout extends ChangeNotifier {
);
}
}
minimized = json['minimized'] == true;
maximized = json['maximized'] == true;
} catch (_) {
return;
@@ -104,27 +107,11 @@ class TerminalLayout extends ChangeNotifier {
}
void setMode(TerminalMode value) => _change(() => mode = value);
void toggle() => _change(() {
visible = !visible;
if (visible) minimized = false;
});
void show() => _change(() {
visible = true;
minimized = false;
});
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);
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 {
@@ -140,7 +127,6 @@ class TerminalLayout extends ChangeNotifier {
floatRect.width,
floatRect.height,
],
'minimized': minimized,
'maximized': maximized,
}),
);
+4 -27
View File
@@ -1,16 +1,14 @@
import 'package:flutter/widgets.dart';
import '../../ui/floating/floating_layer.dart';
import 'terminal_float.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_panel.dart';
/// The app with the terminal over or below it: the bottom panel slides up
/// The app with the terminal's bottom panel below it: the panel slides up
/// from the bottom edge (fading in slightly) as [reveal] runs forward while
/// the app above shrinks with it, and the floating window grows and fades
/// in from its place; in reverse they leave the same way, taking no input
/// meanwhile. [present] is false once hidden (and on a narrow window).
/// the app above shrinks with it; in reverse it leaves the same way, taking
/// no input meanwhile. [present] is false once hidden (and on a narrow
/// window). The terminal's floating mode is a window among [windows].
class TerminalStage extends StatelessWidget {
const TerminalStage({
super.key,
@@ -43,7 +41,6 @@ class TerminalStage extends StatelessWidget {
builder: (context, constraints) {
final hiding = !layout.visible;
final panel = present && layout.mode == TerminalMode.panel;
final float = present && layout.mode == TerminalMode.float;
final height = terminalPanelHeight(
layout.panelHeight,
constraints.maxHeight,
@@ -87,26 +84,6 @@ class TerminalStage extends StatelessWidget {
),
),
),
// The window shares the app's floating layer, so it and a
// floating chat stack by which was used last.
if (float)
Positioned.fill(
child: FloatingLayer(
open: layout.visible,
builder: (context, bounds) => [
TerminalFloat(
key: const ValueKey('terminal-float'),
hub: hub,
layout: layout,
cupertino: cupertino,
bounds: bounds,
reveal: reveal,
focus: focus,
hiding: hiding,
),
],
),
),
if (windows != null) Positioned.fill(child: windows!),
],
);
+22 -15
View File
@@ -14,6 +14,7 @@ import 'terminal_scope.dart';
import 'terminal_shortcut_bubble.dart';
import 'terminal_shortcuts.dart';
import 'terminal_stage.dart';
import 'terminal_window.dart';
/// Below this width the terminal opens as a full-screen page.
const double terminalNarrowWidth = 600;
@@ -34,6 +35,7 @@ class TerminalSurface extends StatefulWidget {
required this.child,
this.shortcutHere = true,
this.windows,
this.floating,
});
final TerminalHub hub;
@@ -48,6 +50,10 @@ class TerminalSurface extends StatefulWidget {
/// The app's floating windows (see [TerminalStage.windows]).
final Widget? windows;
/// The terminal's own floating window among them; without it the float
/// mode shows nothing.
final TerminalWindows? floating;
@override
State<TerminalSurface> createState() => _TerminalSurfaceState();
}
@@ -58,7 +64,9 @@ class _TerminalSurfaceState extends State<TerminalSurface>
late final AnimationController _reveal = AnimationController(
vsync: this,
duration: Motion.standard,
value: widget.layout.visible ? 1 : 0,
value: widget.layout.visible && widget.layout.mode == TerminalMode.panel
? 1
: 0,
);
late final CurvedAnimation _curve = CurvedAnimation(
parent: _reveal,
@@ -68,11 +76,10 @@ class _TerminalSurfaceState extends State<TerminalSurface>
final TerminalFocus _focus = TerminalFocus();
final TerminalPageToggle _page = TerminalPageToggle();
/// What was last acted on: shown on a wide window, the mode, whether the
/// window was minimised and the selected tab.
/// What was last acted on: shown on a wide window, the panel shown, and
/// the selected tab.
bool _shown = false;
TerminalMode? _mode;
bool _minimized = false;
bool _panelShown = false;
String? _selected;
@override
@@ -112,10 +119,15 @@ class _TerminalSurfaceState extends State<TerminalSurface>
final layout = widget.layout;
final shown = layout.visible && !_narrow;
if (shown) widget.hub.activate();
widget.floating?.wide = !_narrow;
// The floating window has its own motion and focus (the window
// system's); this surface moves the panel and its keyboard.
final panel = shown && layout.mode == TerminalMode.panel;
_reveal.duration = Motion.of(context, Motion.standard);
if (shown != _shown) {
_shown = shown;
if (shown) {
_shown = shown;
if (panel != _panelShown) {
_panelShown = panel;
if (panel) {
_focus.remember();
unawaited(_reveal.forward());
_focus.focusSoon();
@@ -123,19 +135,14 @@ class _TerminalSurfaceState extends State<TerminalSurface>
_focus.release();
unawaited(_reveal.reverse());
}
} else if (shown &&
(layout.mode != _mode || (_minimized && !layout.minimized))) {
_focus.focusSoon();
}
_mode = layout.mode;
_minimized = layout.minimized;
}
void _hubChanged() {
final selected = widget.hub.selected?.key;
if (selected == _selected) return;
_selected = selected;
if (_shown) _focus.focusSoon();
if (_panelShown) _focus.focusSoon();
}
/// The shortcuts are handled before any focused widget sees the key, so
@@ -151,7 +158,7 @@ class _TerminalSurfaceState extends State<TerminalSurface>
final step = terminalTabShortcut(event, keyboard);
if (step == null || !claimsTabKeys) return KeyEventResult.ignored;
widget.hub.step(step);
if (!_page.open) _focus.focusSoon();
if (!_page.open && _panelShown) _focus.focusSoon();
return KeyEventResult.handled;
}
@@ -0,0 +1,157 @@
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)),
],
),
);
}