Merge main

This commit is contained in:
2026-11-10 22:42:42 +00:00
17 changed files with 535 additions and 598 deletions
+13
View File
@@ -7,6 +7,8 @@ import '../features/terminal/terminal_layout.dart';
import '../features/terminal/terminal_machines.dart';
import '../features/terminal/terminal_surface.dart';
import '../features/terminal/terminal_toggle_button.dart';
import '../features/terminal/terminal_window.dart';
import '../shell/windows/window_manager.dart';
/// The app's machine terminals: one hub over every connected machine (the
/// chat pool's, else the app's own server), kept for the app's lifetime so
@@ -20,6 +22,7 @@ class AppTerminals {
required String machineId,
required String machineName,
required this.layout,
WindowManager? windows,
}) : machines = pool != null
? PoolTerminalMachines(pool)
: SingleTerminalMachines(
@@ -32,12 +35,20 @@ class AppTerminals {
),
) {
hub = TerminalHub(machines);
if (windows != null) {
floating = TerminalWindows(manager: windows, hub: hub, layout: layout)
..start();
}
}
final ValueNotifier<List<TerminalMachine>> machines;
final TerminalLayout layout;
late final TerminalHub hub;
/// The terminal's floating window in the app's window system; null
/// without one (the float mode then shows nothing).
TerminalWindows? floating;
/// [shell] with the terminal panel, window or page around it, and the
/// app's floating [windows] above both.
/// [onChat]: the phone shortcut that reopens the terminal shows on the
@@ -53,6 +64,7 @@ class AppTerminals {
cupertino: cupertino,
shortcutHere: onChat,
windows: windows,
floating: floating,
child: shell,
);
@@ -61,6 +73,7 @@ class AppTerminals {
TerminalToggleButton(layout: layout, cupertino: cupertino);
void dispose() {
floating?.dispose();
hub.dispose();
machines.dispose();
}
+13 -10
View File
@@ -265,16 +265,6 @@ class _PipelinesAppState extends State<PipelinesApp>
_session.addListener(_chatSyncChanged);
widget.chatPool?.addListener(_chatSyncChanged);
_chatSyncChanged();
// Every kind is registered: saved windows can be restored, then the
// floating chats an older build saved are brought over once.
unawaited(
_windows.restore().then((_) {
if (!mounted) return null;
return _chatWindows.migrate(
SharedPreferencesChatFloatStore(serverScope: chatScope),
);
}),
);
_terminals = AppTerminals(
pool: widget.chatPool,
connection: _session,
@@ -286,6 +276,19 @@ class _PipelinesAppState extends State<PipelinesApp>
widget.server.uri?.host ??
'This server',
layout: widget.terminalLayout ?? TerminalLayout.instance,
windows: _windows,
);
// Every kind is registered: saved windows can be restored. Then the
// terminal may open its window, and the floating chats an older build
// saved are brought over once.
unawaited(
_windows.restore().then((_) {
if (!mounted) return null;
_terminals.floating?.ready();
return _chatWindows.migrate(
SharedPreferencesChatFloatStore(serverScope: chatScope),
);
}),
);
_ownsProvider = widget.routeInformationProvider == null;
_provider =
@@ -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)),
],
),
);
}
+10
View File
@@ -55,6 +55,16 @@ class WindowManager extends ChangeNotifier {
.where((w) => _here(w) && w.kind == kind && w.contentId == contentId)
.firstOrNull;
/// The frame the current server's window of [kind] for [contentId] has,
/// or had when it last closed.
Rect? lastFrame(String kind, String contentId) =>
find(kind, contentId)?.frame ??
_frames[windowFrameKey(
kind,
(kinds[kind]?.serverScoped ?? false) ? _server : null,
contentId,
)];
Iterable<WindowEntry> inGroup(String groupId) =>
windows.where((w) => w.groupId == groupId);
+45 -20
View File
@@ -20,6 +20,8 @@ bool usesDesktopPopups(BuildContext context) {
///
/// On a wide app it is a menu panel anchored to [anchor] (the button that
/// opened it): below it, or above when there is no room, kept inside the app.
/// When [anchor] is a region rather than a button (a pane), the panel opens
/// over its top left.
/// On a phone it is a bottom sheet. Either way it is on the root navigator,
/// so it is above every floating window. Escape, Back or a press outside
/// closes it; the content closes it with `Navigator.of(context).pop(value)`.
@@ -57,7 +59,8 @@ Future<T?> showAppChoiceMenu<T>(
/// Shows richer content (a chat's details, a file's details) built by
/// [builder]: a dialog on a wide app, a bottom sheet on a phone, on the root
/// navigator either way.
/// navigator either way. The content sizes itself (a `Column` with
/// `MainAxisSize.min`); the dialog scrolls it when it is taller than the app.
Future<T?> showAppDetails<T>(
BuildContext context, {
required WidgetBuilder builder,
@@ -83,7 +86,8 @@ Future<T?> showAppDetails<T>(
maxWidth: maxWidth,
maxHeight: MediaQuery.sizeOf(dialog).height * 0.85,
),
child: Padding(
// Content taller than the app scrolls inside the dialog.
child: SingleChildScrollView(
padding: const EdgeInsets.only(top: 16),
child: builder(dialog),
),
@@ -167,38 +171,59 @@ class _AnchoredMenuLayout extends SingleChildLayoutDelegate {
static const double margin = 8;
static const double gap = 4;
/// Less room than this on both sides means the anchor is not a button but
/// a region (a pane's bar, a whole pane): the panel then opens over it,
/// from its top.
static const double minRoom = 200;
double _below(Size size) => size.height - anchor.bottom - gap - margin;
double _above() => anchor.top - gap - margin;
bool _over(Size size) => _below(size) < minRoom && _above() < minRoom;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
final available = constraints.biggest;
final below = available.height - anchor.bottom - gap - margin;
final above = anchor.top - gap - margin;
final height = (below >= 240 || below >= above ? below : above).clamp(
0.0,
available.height - 2 * margin,
);
final below = _below(available);
final above = _above();
final double room;
if (_over(available)) {
room =
available.height -
anchor.top.clamp(margin, available.height) -
margin;
} else {
room = below >= minRoom || below >= above ? below : above;
}
final maxWidth = (available.width - 2 * margin).clamp(0.0, double.infinity);
return BoxConstraints(
minWidth: width.clamp(0.0, maxWidth),
maxWidth: width.clamp(0.0, maxWidth),
maxHeight: height,
maxHeight: room.clamp(0.0, available.height - 2 * margin),
);
}
@override
Offset getPositionForChild(Size size, Size childSize) {
final below = size.height - anchor.bottom - gap - margin;
final fitsBelow = childSize.height <= below;
final top = fitsBelow
? anchor.bottom + gap
: (anchor.top - gap - childSize.height).clamp(
margin,
size.height - margin - childSize.height,
);
final left = anchor.left.clamp(
final double top;
if (_over(size)) {
top = anchor.top + gap;
} else if (childSize.height <= _below(size)) {
top = anchor.bottom + gap;
} else {
top = anchor.top - gap - childSize.height;
}
final maxTop = (size.height - margin - childSize.height).clamp(
margin,
(size.width - margin - childSize.width).clamp(margin, double.infinity),
double.infinity,
);
final maxLeft = (size.width - margin - childSize.width).clamp(
margin,
double.infinity,
);
return Offset(
anchor.left.clamp(margin, maxLeft),
top.clamp(margin, maxTop),
);
return Offset(left, top);
}
@override
-99
View File
@@ -1,99 +0,0 @@
import 'package:flutter/widgets.dart';
/// A layer of floating windows painted in the app's root overlay, above the
/// page, while it keeps its place in the tree (its inherited widgets, focus
/// and state stay where it is built). Every [FloatingLayer] shares that one
/// overlay. Only the terminal's floating window still uses it; the app's
/// other windows are in the shell's window host.
///
/// [builder] returns the windows as [Positioned] children of a [Stack] the
/// size of [bounds]: this widget's own box, or the whole overlay when
/// [coverOverlay] is set. Space between windows takes no input. Without an
/// overlay (a bare widget test) the layer paints in place.
class FloatingLayer extends StatefulWidget {
const FloatingLayer({
super.key,
required this.builder,
this.open = true,
this.coverOverlay = false,
});
final List<Widget> Function(BuildContext context, Size bounds) builder;
/// Turning true brings the layer to the top, as its window opens.
final bool open;
final bool coverOverlay;
@override
State<FloatingLayer> createState() => _FloatingLayerState();
}
class _FloatingLayerState extends State<FloatingLayer> {
final OverlayPortalController _portal = OverlayPortalController(
debugLabel: 'floating layer',
);
@override
void initState() {
super.initState();
_portal.show();
}
@override
void didUpdateWidget(covariant FloatingLayer oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.open && !oldWidget.open) {
WidgetsBinding.instance.addPostFrameCallback((_) => _raise());
}
}
void _raise() {
if (mounted) _portal.show();
}
Widget _layer(BuildContext context, Size bounds) => Listener(
behavior: HitTestBehavior.deferToChild,
onPointerDown: (_) => _raise(),
child: Focus(
canRequestFocus: false,
skipTraversal: true,
onFocusChange: (focused) {
if (focused) _raise();
},
child: SizedBox.fromSize(
size: bounds,
child: Stack(
clipBehavior: Clip.none,
children: widget.builder(context, bounds),
),
),
),
);
@override
Widget build(BuildContext context) {
if (Overlay.maybeOf(context, rootOverlay: true) == null) {
return LayoutBuilder(
builder: (context, constraints) => _layer(context, constraints.biggest),
);
}
return OverlayPortal.overlayChildLayoutBuilder(
controller: _portal,
overlayLocation: OverlayChildLocation.rootOverlay,
overlayChildBuilder: (context, info) => widget.coverOverlay
? Positioned.fill(child: _layer(context, info.overlaySize))
: Positioned(
left: 0,
top: 0,
width: info.childSize.width,
height: info.childSize.height,
child: Transform(
transform: info.childPaintTransform,
child: _layer(context, info.childSize),
),
),
child: const SizedBox.expand(),
);
}
}