Files
pipelines/app/lib/shell/windows/window_host.dart
T
alan fdc730422d Host floating chats on the shell window system
Phase 2.3 and 2.4 of docs/plans/floating-windows.md. A floating chat is a
window of the chat kind over the app's WindowManager, built from the
app-lifetime ChatRuntime, so it shows over every destination and at start
without the Chat destination being built. Float and Dock move the same
keyed view between the tiling and the window host. The pipelines app keeps
one runtime and one ChatWindows; saved chatFloat records (v1 and v2) are
brought over once.

ChatFloatHost, ChatFloatWindow, the float frames and binding, the
controller's float list and FloatingFocusRoot are gone; the workspace only
asks whether a chat is out in a window. Every MenuAnchor targets the root
overlay (guarded by a test), and menus and tooltips name the Alt chords.
2026-11-10 21:17:40 +00:00

123 lines
3.8 KiB
Dart

import 'package:flutter/material.dart';
import 'window_focus.dart';
import 'window_layer.dart';
import 'window_manager.dart';
/// The app's floating windows, drawn in the shell page above the
/// destinations and the terminal panel: plain children of one stack, in the
/// manager's order (the last pressed, focused or opened on top).
///
/// Because the windows are part of the shell page rather than an overlay
/// above it, every popup opened from one (a sheet, dialog, menu or dropdown
/// on the root navigator; a tooltip in the page's overlay) is drawn above
/// all of them. A window is built whatever destination shows, so its
/// tickers, focus and semantics never depend on a hidden tab.
///
/// The host fills its parent and takes no input between windows.
class WindowHost extends StatefulWidget {
const WindowHost({super.key, required this.manager});
final WindowManager manager;
@override
State<WindowHost> createState() => _WindowHostState();
}
class _WindowHostState extends State<WindowHost> {
final WindowFocus _focus = WindowFocus();
Set<String> _visible = const {};
String? _active;
@override
void initState() {
super.initState();
widget.manager.addListener(_changed);
WidgetsBinding.instance.addPostFrameCallback((_) => _changed());
}
@override
void didUpdateWidget(covariant WindowHost oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.manager != widget.manager) {
oldWidget.manager.removeListener(_changed);
widget.manager.addListener(_changed);
_changed();
}
}
@override
void dispose() {
widget.manager.removeListener(_changed);
_focus.dispose();
super.dispose();
}
/// Moves the keyboard with the windows: a window that shows or comes to
/// the top takes it; when the window that had it hides, the next one down
/// takes it, and after the last one it returns to where it was.
void _changed() {
if (!mounted) return;
final manager = widget.manager;
final visible = {
for (final w in manager.windows)
if (manager.isVisible(w)) w.id,
};
final active = manager.active?.id;
final focused = _focus.focused;
if (_visible.isEmpty && visible.isNotEmpty) _focus.remember();
if (active == null) {
if (_visible.isNotEmpty) _focus.release();
} else if (active != _active ||
!_visible.contains(active) ||
(focused != null && !visible.contains(focused))) {
_focusWindow(active);
}
_visible = visible;
_active = active;
setState(() {});
}
/// The window takes the keyboard, then its content places it.
void _focusWindow(String id) {
_focus.focusSoon(id);
final entry = widget.manager.byId(id);
if (entry != null) widget.manager.kinds[entry.kind]?.focusContent(entry);
}
@override
Widget build(BuildContext context) {
final manager = widget.manager;
final windows = manager.windows;
_focus.retain({for (final w in windows) w.id});
return ListenableBuilder(
listenable: Listenable.merge([
for (final kind in manager.kinds.all) ?kind.changes,
]),
builder: (context, _) => LayoutBuilder(
builder: (context, constraints) {
final bounds = constraints.biggest;
return Stack(
fit: StackFit.expand,
children: [
for (final entry in windows)
if (manager.kinds[entry.kind] case final kind?)
WindowLayer(
key: ValueKey(entry.id),
manager: manager,
kind: kind,
entry: entry,
visible:
manager.isVisible(entry) &&
bounds.width >= kind.minHostWidth,
bounds: bounds,
focus: _focus,
),
],
);
},
),
);
}
}