Files
pipelines/app/lib/shell/windows/window_layer.dart
T
alan ef1c120f57 Add the Windows menu, window cycling, edge snapping and size per kind
Phase 4 of docs/plans/floating-windows.md. A bar button lists the app's
windows on every tab (show one, Show all, Minimise all, Close all). Alt+`
and Alt+Shift+` bring the next or previous window to the front. Dropping a
title at the left or right edge fills that half, at the top it maximises.
A new window of a kind opens at the size that kind was last given.
2026-11-11 00:05:16 +00:00

305 lines
8.9 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);
}
/// Where the pointer is in the host while the title is dragged.
Offset? _pointer;
void _move(Offset delta, Offset globalPosition) {
final box = context.findRenderObject();
_pointer = box is RenderBox ? box.globalToLocal(globalPosition) : null;
widget.manager.setFrame(
_id,
fitWindowFrame(_current.shift(delta), widget.bounds, widget.kind.minSize),
save: false,
);
}
/// Dropping the title at the app's left or right edge fills that half;
/// at the top edge it maximises.
void _moveEnded() {
final pointer = _pointer;
_pointer = null;
final bounds = widget.bounds;
final snap = pointer == null ? null : windowSnapFor(pointer, bounds);
if (snap == WindowSnap.maximize) {
widget.manager.setMaximized(_id, true);
return;
}
if (snap != null) {
widget.manager.setFrame(
_id,
fitWindowFrame(
windowSnapRect(snap, bounds),
bounds,
widget.kind.minSize,
),
save: false,
);
// A snapped half is not the size to give this kind's next window.
widget.manager.commit();
return;
}
widget.manager.commit(_id);
}
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,
);
}
/// A press brings the window to the top and gives it the keyboard at
/// once, before the press does anything else (focus a field, open a
/// popup that takes the keyboard itself).
void _pressed() {
if (!widget.focus.has(_id)) widget.focus.focusNow(_id);
widget.manager.raise(_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: _moveEnded,
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(_id),
),
),
],
);
final center = widget.bounds.center(Offset.zero);
final current = ModalRoute.of(context)?.isCurrent ?? true;
return AnimatedBuilder(
animation: _reveal,
builder: (context, child) {
final offstage = hiding && _reveal.isDismissed;
return Offstage(
offstage: offstage,
child: TickerMode(
enabled: !offstage,
child: SurfaceVisibility(
shown: !offstage,
// Not while a page, sheet or dialog covers the app: the
// window's own route is always its navigator's current one.
current: !hiding && current,
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);
}