cfd9f22f4b
Install directional hit regions in the shared window layer so floating chats can be moved and resized on every destination. Keep geometry updates in the shared manager and cover directional resizing with window host regressions.
465 lines
14 KiB
Dart
465 lines
14 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}');
|
|
|
|
/// The page-transition theme handed to the window's navigator, rebuilt only
|
|
/// when the app's theme itself changes.
|
|
ThemeData? _themeSource;
|
|
ThemeData? _pageTheme;
|
|
|
|
/// The root page kept across builds: a new page object would rebuild the
|
|
/// window's content on every move and resize.
|
|
WindowKind? _pageKind;
|
|
Page<void>? _page;
|
|
|
|
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;
|
|
|
|
/// The frame the current move or resize started from and the pointer delta
|
|
/// summed since. Every event sets the frame from `start + total`: fitting
|
|
/// one fractional step after another would round part of the pointer's
|
|
/// movement away each time and drift, and shifting the fitted frame would
|
|
/// change its size in its last bits.
|
|
Rect? _dragStart;
|
|
Offset _dragTotal = Offset.zero;
|
|
|
|
void _move(Offset delta, Offset globalPosition) {
|
|
final box = context.findRenderObject();
|
|
_pointer = box is RenderBox ? box.globalToLocal(globalPosition) : null;
|
|
final start = _dragStart ??= _current;
|
|
_dragTotal += delta;
|
|
widget.manager.setFrame(
|
|
_id,
|
|
fitWindowFrame(
|
|
start.shift(_dragTotal),
|
|
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;
|
|
_endDrag();
|
|
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, Set<WindowResizeEdge> edges) {
|
|
final start = _dragStart ??= _current;
|
|
_dragTotal += delta;
|
|
final left = edges.contains(WindowResizeEdge.left);
|
|
final top = edges.contains(WindowResizeEdge.top);
|
|
widget.manager.setFrame(
|
|
_id,
|
|
resizeWindowFrame(
|
|
start,
|
|
_dragTotal,
|
|
left: left,
|
|
right: edges.contains(WindowResizeEdge.right),
|
|
top: top,
|
|
bottom: edges.contains(WindowResizeEdge.bottom),
|
|
bounds: widget.bounds,
|
|
minSize: widget.kind.minSize,
|
|
),
|
|
save: false,
|
|
);
|
|
}
|
|
|
|
void _resizeCorner(Offset delta, Set<WindowResizeEdge> edges) =>
|
|
_resize(delta, edges);
|
|
|
|
void _resizeEnded() {
|
|
_endDrag();
|
|
widget.manager.commit(_id);
|
|
}
|
|
|
|
/// A drag ends here even if it is cancelled or another gesture begins.
|
|
void _endDrag() {
|
|
_dragStart = null;
|
|
_dragTotal = Offset.zero;
|
|
}
|
|
|
|
/// 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() {
|
|
// A new gesture measures from where it starts, even if the last drag was
|
|
// cancelled before it ended.
|
|
_endDrag();
|
|
if (!widget.focus.has(_id)) widget.focus.focusNow(_id);
|
|
widget.manager.raise(_id);
|
|
}
|
|
|
|
ThemeData _pageThemeFor(ThemeData theme) {
|
|
// Equal to the last theme means the same page theme, even when the app's
|
|
// animated theme hands over a new but equal `ThemeData` object: a new
|
|
// page theme would notify every content widget that reads the theme on
|
|
// every frame of a move.
|
|
if (theme == _themeSource) return _pageTheme!;
|
|
_themeSource = theme;
|
|
return _pageTheme = WindowNavigator.themeData(theme);
|
|
}
|
|
|
|
Page<void> _rootPageFor(WindowKind kind) {
|
|
final page = _page;
|
|
if (page != null && identical(kind, _pageKind)) return page;
|
|
_pageKind = kind;
|
|
return _page = MaterialPage<void>(
|
|
key: _rootPage,
|
|
child: _WindowContent(kind: kind),
|
|
);
|
|
}
|
|
|
|
@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 pageTheme = _pageThemeFor(Theme.of(context));
|
|
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}',
|
|
// Its own layer: moving the window moves the title bar without
|
|
// repainting it.
|
|
child: RepaintBoundary(
|
|
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),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
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,
|
|
child: WindowBodyScope(
|
|
body: body,
|
|
child: ScaffoldMessenger(
|
|
child: Theme(
|
|
data: pageTheme,
|
|
child: WindowNavigator(
|
|
key: _navigator,
|
|
pages: [_rootPageFor(kind)],
|
|
onDidRemovePage: (_) {},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (!entry.maximized) ..._resizeHandles(frame),
|
|
Positioned.fromRect(rect: frame, child: const WindowOutline()),
|
|
],
|
|
);
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
List<Widget> _resizeHandles(Rect frame) {
|
|
const corner = 16.0;
|
|
const edge = 10.0;
|
|
Widget handle({
|
|
required Rect rect,
|
|
required Set<WindowResizeEdge> edges,
|
|
required MouseCursor cursor,
|
|
Widget? child,
|
|
Key? key,
|
|
}) => Positioned.fromRect(
|
|
rect: rect,
|
|
child: WindowResizeHandle(
|
|
key: key,
|
|
cursor: cursor,
|
|
onResize: (delta) => _resizeCorner(delta, edges),
|
|
onResizeEnd: _resizeEnded,
|
|
child: child,
|
|
),
|
|
);
|
|
|
|
return [
|
|
handle(
|
|
key: const ValueKey('window-resize-top-left'),
|
|
rect: Rect.fromLTWH(frame.left, frame.top, corner, corner),
|
|
edges: const {WindowResizeEdge.left, WindowResizeEdge.top},
|
|
cursor: SystemMouseCursors.resizeUpLeft,
|
|
),
|
|
handle(
|
|
key: const ValueKey('window-resize-top-right'),
|
|
rect: Rect.fromLTWH(frame.right - corner, frame.top, corner, corner),
|
|
edges: const {WindowResizeEdge.right, WindowResizeEdge.top},
|
|
cursor: SystemMouseCursors.resizeUpRight,
|
|
),
|
|
handle(
|
|
key: const ValueKey('window-resize-bottom-left'),
|
|
rect: Rect.fromLTWH(frame.left, frame.bottom - corner, corner, corner),
|
|
edges: const {WindowResizeEdge.left, WindowResizeEdge.bottom},
|
|
cursor: SystemMouseCursors.resizeUpRight,
|
|
),
|
|
Positioned.fromRect(
|
|
rect: Rect.fromLTWH(
|
|
frame.right - corner,
|
|
frame.bottom - corner,
|
|
corner,
|
|
corner,
|
|
),
|
|
child: WindowResizeCorner(
|
|
onResize: (delta) => _resizeCorner(delta, const {
|
|
WindowResizeEdge.right,
|
|
WindowResizeEdge.bottom,
|
|
}),
|
|
onResizeEnd: _resizeEnded,
|
|
),
|
|
),
|
|
handle(
|
|
key: const ValueKey('window-resize-left'),
|
|
rect: Rect.fromLTWH(
|
|
frame.left,
|
|
frame.top + corner,
|
|
edge,
|
|
frame.height - corner * 2,
|
|
),
|
|
edges: const {WindowResizeEdge.left},
|
|
cursor: SystemMouseCursors.resizeLeftRight,
|
|
),
|
|
handle(
|
|
key: const ValueKey('window-resize-right'),
|
|
rect: Rect.fromLTWH(
|
|
frame.right - edge,
|
|
frame.top + corner,
|
|
edge,
|
|
frame.height - corner * 2,
|
|
),
|
|
edges: const {WindowResizeEdge.right},
|
|
cursor: SystemMouseCursors.resizeLeftRight,
|
|
),
|
|
handle(
|
|
key: const ValueKey('window-resize-top'),
|
|
rect: Rect.fromLTWH(
|
|
frame.left + corner,
|
|
frame.top,
|
|
frame.width - corner * 2,
|
|
edge,
|
|
),
|
|
edges: const {WindowResizeEdge.top},
|
|
cursor: SystemMouseCursors.resizeUpDown,
|
|
),
|
|
handle(
|
|
key: const ValueKey('window-resize-bottom'),
|
|
rect: Rect.fromLTWH(
|
|
frame.left + corner,
|
|
frame.bottom - edge,
|
|
frame.width - corner * 2,
|
|
edge,
|
|
),
|
|
edges: const {WindowResizeEdge.bottom},
|
|
cursor: SystemMouseCursors.resizeUpDown,
|
|
),
|
|
];
|
|
}
|
|
}
|
|
|
|
/// 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);
|
|
}
|