Files
pipelines/app/lib/shell/windows/window_layer.dart
T
alan a99f7014f5 Add the shell window host with layering acceptance tests
Phase 1.3 and 1.4 of docs/plans/floating-windows.md. Windows are plain
children of the terminal stage's stack in the shell page, outside every
destination, so popups opened from one are above all of them. Each window
has its own page navigator: pushed pages open inside the window, and popup
routes go to the root navigator. One WindowFrame chrome (move, resize,
minimise, maximise, close), a focus scope per window, a ScaffoldMessenger
per window.

No feature uses it yet. The capture fixture opens probe windows with
?windows=N, and window_layering_test.dart covers every popup type from a
window over Chat and over another destination.
2026-11-10 20:48:31 +00:00

264 lines
7.5 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../ui/motion/motion.dart';
import '../../ui/motion/surface_visibility.dart';
import 'window_entry.dart';
import 'window_focus.dart';
import 'window_frame.dart';
import 'window_kind.dart';
import 'window_manager.dart';
import 'window_navigator.dart';
import 'window_scope.dart';
/// One window in the host: its card and title bar, its page navigator over
/// the card's body, and its resize corner, with the grow-and-fade motion as
/// it shows and hides. A hidden window stays built (its content keeps its
/// state) but is not painted and takes no input or focus.
class WindowLayer extends StatefulWidget {
const WindowLayer({
super.key,
required this.manager,
required this.kind,
required this.entry,
required this.visible,
required this.bounds,
required this.focus,
});
final WindowManager manager;
final WindowKind kind;
final WindowEntry entry;
final bool visible;
final Size bounds;
final WindowFocus focus;
/// The scale a window grows from as it shows.
static const double revealScale = 0.92;
@override
State<WindowLayer> createState() => _WindowLayerState();
}
class _WindowLayerState extends State<WindowLayer>
with SingleTickerProviderStateMixin {
late final AnimationController _reveal = AnimationController(
vsync: this,
duration: Motion.standard,
);
late final CurvedAnimation _curve = CurvedAnimation(
parent: _reveal,
curve: Motion.curve,
reverseCurve: Curves.easeInCubic,
);
final GlobalKey _navigator = GlobalKey();
late final LocalKey _rootPage = ValueKey('window-root-${widget.entry.id}');
String get _id => widget.entry.id;
@override
void initState() {
super.initState();
if (widget.visible) {
WidgetsBinding.instance.addPostFrameCallback((_) => _run());
}
}
@override
void didUpdateWidget(covariant WindowLayer oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.visible != widget.visible) _run();
}
void _run() {
if (!mounted) return;
_reveal.duration = Motion.of(context, Motion.standard);
unawaited(widget.visible ? _reveal.forward() : _reveal.reverse());
}
@override
void dispose() {
_curve.dispose();
_reveal.dispose();
super.dispose();
}
Rect get _frame =>
drawnWindowFrame(widget.entry, widget.bounds, widget.kind.minSize);
/// The frame now: several drag events can arrive before a rebuild.
Rect get _current {
final entry = widget.manager.byId(_id) ?? widget.entry;
return drawnWindowFrame(entry, widget.bounds, widget.kind.minSize);
}
void _move(Offset delta) => widget.manager.setFrame(
_id,
fitWindowFrame(_current.shift(delta), widget.bounds, widget.kind.minSize),
save: false,
);
void _resize(Offset delta) {
final rect = _current;
widget.manager.setFrame(
_id,
fitWindowFrame(
Rect.fromLTWH(
rect.left,
rect.top,
rect.width + delta.dx,
rect.height + delta.dy,
),
widget.bounds,
widget.kind.minSize,
),
save: false,
);
}
void _pressed() {
widget.manager.raise(_id);
if (!widget.focus.has(_id)) widget.focus.focusSoon(_id);
}
@override
Widget build(BuildContext context) {
final manager = widget.manager;
final entry = widget.entry;
final kind = widget.kind;
final frame = _frame;
final titleHeight = windowTitleBarHeight(context).clamp(0.0, frame.height);
final body = Rect.fromLTRB(
frame.left,
frame.top + titleHeight,
frame.right,
frame.bottom,
);
final title = kind.title(context, entry);
final hiding = !widget.visible;
final content = Stack(
fit: StackFit.expand,
children: [
Positioned.fromRect(
rect: frame,
child: Semantics(
container: true,
explicitChildNodes: true,
label: 'Window: $title',
identifier: 'window-${entry.kind}',
child: WindowFrame(
title: title,
icon: kind.icon,
actions: kind.actions(context, entry),
maximized: entry.maximized,
onMove: _move,
onMoveEnd: manager.commit,
onMinimize: () => manager.setMinimized(_id, true),
onToggleMaximized: () =>
manager.setMaximized(_id, !entry.maximized),
onClose: () => manager.close(_id),
),
),
),
WindowHitRegion(
region: body,
// Its own semantics container: a route's barrier hides what is
// painted before it in its container, which must not be the
// title bar or the app.
child: Semantics(
container: true,
explicitChildNodes: true,
child: WindowScope(
manager: manager,
entry: entry,
body: body,
child: ScaffoldMessenger(
child: WindowNavigator.theme(
context,
child: WindowNavigator(
key: _navigator,
pages: [
MaterialPage<void>(
key: _rootPage,
child: _WindowContent(kind: kind),
),
],
onDidRemovePage: (_) {},
),
),
),
),
),
),
if (!entry.maximized)
Positioned(
right: widget.bounds.width - frame.right,
bottom: widget.bounds.height - frame.bottom,
child: WindowResizeCorner(
onResize: _resize,
onResizeEnd: manager.commit,
),
),
],
);
final center = widget.bounds.center(Offset.zero);
return AnimatedBuilder(
animation: _reveal,
builder: (context, child) {
final offstage = hiding && _reveal.isDismissed;
return Offstage(
offstage: offstage,
child: TickerMode(
enabled: !offstage,
child: SurfaceVisibility(
shown: !offstage,
current: !hiding,
child: IgnorePointer(
ignoring: hiding,
child: ExcludeFocus(excluding: hiding, child: child!),
),
),
),
);
},
child: FadeTransition(
opacity: _curve,
child: ScaleTransition(
scale: Tween<double>(
begin: WindowLayer.revealScale,
end: 1,
).animate(_curve),
// Grows from the window's own centre, not the host's.
alignment: Alignment(
center.dx == 0 ? 0 : (frame.center.dx - center.dx) / center.dx,
center.dy == 0 ? 0 : (frame.center.dy - center.dy) / center.dy,
),
child: Listener(
behavior: HitTestBehavior.deferToChild,
onPointerDown: (_) => _pressed(),
child: FocusScope(
node: widget.focus.scopeFor(_id),
onFocusChange: (focused) {
if (focused) manager.raise(_id);
},
child: content,
),
),
),
),
);
}
}
/// The kind's content, rebuilt with the window's current entry.
class _WindowContent extends StatelessWidget {
const _WindowContent({required this.kind});
final WindowKind kind;
@override
Widget build(BuildContext context) =>
kind.build(context, WindowScope.of(context).entry);
}