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.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The floating windows' keyboard focus: each window has a focus scope; the
|
||||
/// window that shows or comes to the top takes the keyboard, and when the
|
||||
/// last one hides it goes back to what had it before (a composer, a field),
|
||||
/// else to the screen.
|
||||
class WindowFocus {
|
||||
final Map<String, FocusScopeNode> _scopes = {};
|
||||
FocusNode? _previous;
|
||||
|
||||
FocusScopeNode scopeFor(String id) =>
|
||||
_scopes[id] ??= FocusScopeNode(debugLabel: 'window $id');
|
||||
|
||||
/// Forgets the scopes of windows no longer in [ids].
|
||||
void retain(Set<String> ids) {
|
||||
for (final id in _scopes.keys.where((id) => !ids.contains(id)).toList()) {
|
||||
_scopes.remove(id)!.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
bool _within(FocusNode node, FocusScopeNode scope) =>
|
||||
identical(node, scope) || node.ancestors.contains(scope);
|
||||
|
||||
/// Whether [id]'s window has the keyboard.
|
||||
bool has(String id) {
|
||||
final current = FocusManager.instance.primaryFocus;
|
||||
final scope = _scopes[id];
|
||||
return current != null && scope != null && _within(current, scope);
|
||||
}
|
||||
|
||||
/// The window that has the keyboard, if one does.
|
||||
String? get focused => _scopes.keys.where(has).firstOrNull;
|
||||
|
||||
/// Notes what has the keyboard now, before a window takes it.
|
||||
void remember() {
|
||||
final current = FocusManager.instance.primaryFocus;
|
||||
if (current == null || focused != null) return;
|
||||
_previous = identical(current, FocusManager.instance.rootScope)
|
||||
? null
|
||||
: current;
|
||||
}
|
||||
|
||||
/// Focuses [id]'s window once this frame has built it.
|
||||
void focusSoon(String id) {
|
||||
WidgetsBinding.instance
|
||||
..addPostFrameCallback((_) {
|
||||
final scope = _scopes[id];
|
||||
if (scope != null && scope.context != null && scope.canRequestFocus) {
|
||||
scope.requestFocus();
|
||||
}
|
||||
})
|
||||
..ensureVisualUpdate();
|
||||
}
|
||||
|
||||
/// Gives the keyboard back to what had it before the windows showed, if
|
||||
/// a window has it now.
|
||||
void release() {
|
||||
final previous = _previous;
|
||||
_previous = null;
|
||||
final current = FocusManager.instance.primaryFocus;
|
||||
if (current == null || focused == null) return;
|
||||
if (previous != null &&
|
||||
previous.context != null &&
|
||||
previous.canRequestFocus) {
|
||||
previous.requestFocus();
|
||||
} else {
|
||||
current.unfocus();
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
for (final scope in _scopes.values) {
|
||||
scope.dispose();
|
||||
}
|
||||
_scopes.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'window_kind.dart';
|
||||
|
||||
const double windowCornerRadius = 12;
|
||||
|
||||
/// The height of a window's title bar: a compact row of icon buttons, taller
|
||||
/// with larger text.
|
||||
double windowTitleBarHeight(BuildContext context) =>
|
||||
MediaQuery.textScalerOf(context).scale(40).clamp(40.0, 64.0);
|
||||
|
||||
/// A floating window's card and title bar: the title (drag to move, double
|
||||
/// click to maximise), the kind's actions, then Minimise, Maximise and
|
||||
/// Close. The content is drawn over the card's body by the window's
|
||||
/// navigator.
|
||||
class WindowFrame extends StatelessWidget {
|
||||
const WindowFrame({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.icon,
|
||||
required this.actions,
|
||||
required this.maximized,
|
||||
required this.onMove,
|
||||
required this.onMoveEnd,
|
||||
required this.onMinimize,
|
||||
required this.onToggleMaximized,
|
||||
required this.onClose,
|
||||
});
|
||||
|
||||
final String title;
|
||||
final IconData icon;
|
||||
final List<WindowAction> actions;
|
||||
final bool maximized;
|
||||
final ValueChanged<Offset> onMove;
|
||||
final VoidCallback onMoveEnd;
|
||||
final VoidCallback onMinimize;
|
||||
final VoidCallback onToggleMaximized;
|
||||
final VoidCallback onClose;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final colors = theme.colorScheme;
|
||||
return Material(
|
||||
elevation: 8,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
borderRadius: BorderRadius.circular(windowCornerRadius),
|
||||
color: colors.surfaceContainer,
|
||||
child: Align(
|
||||
alignment: Alignment.topCenter,
|
||||
child: SizedBox(
|
||||
height: windowTitleBarHeight(context),
|
||||
child: IconButtonTheme(
|
||||
data: IconButtonThemeData(
|
||||
style: IconButton.styleFrom(
|
||||
visualDensity: VisualDensity.compact,
|
||||
foregroundColor: colors.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(child: _title(theme)),
|
||||
for (final action in actions)
|
||||
IconButton(
|
||||
tooltip: action.label,
|
||||
onPressed: action.onPressed,
|
||||
icon: Icon(action.icon),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Minimise',
|
||||
onPressed: onMinimize,
|
||||
icon: const Icon(Icons.minimize),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: maximized ? 'Restore size' : 'Maximise',
|
||||
onPressed: onToggleMaximized,
|
||||
icon: Icon(
|
||||
maximized ? Icons.fullscreen_exit : Icons.fullscreen,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Close',
|
||||
onPressed: onClose,
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _title(ThemeData theme) => MouseRegion(
|
||||
cursor: maximized ? SystemMouseCursors.basic : SystemMouseCursors.move,
|
||||
child: GestureDetector(
|
||||
key: const ValueKey('window-title'),
|
||||
behavior: HitTestBehavior.opaque,
|
||||
// The window follows the pointer from where it went down.
|
||||
dragStartBehavior: DragStartBehavior.down,
|
||||
onPanUpdate: maximized ? null : (details) => onMove(details.delta),
|
||||
onPanEnd: maximized ? null : (_) => onMoveEnd(),
|
||||
onDoubleTap: onToggleMaximized,
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.only(start: 12, end: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, size: 18, color: theme.colorScheme.onSurfaceVariant),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: theme.textTheme.titleSmall,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The lower right corner of a window: drag to resize.
|
||||
class WindowResizeCorner extends StatelessWidget {
|
||||
const WindowResizeCorner({
|
||||
super.key,
|
||||
required this.onResize,
|
||||
required this.onResizeEnd,
|
||||
});
|
||||
|
||||
final ValueChanged<Offset> onResize;
|
||||
final VoidCallback onResizeEnd;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Semantics(
|
||||
label: 'Resize window',
|
||||
child: MouseRegion(
|
||||
cursor: SystemMouseCursors.resizeDownRight,
|
||||
child: GestureDetector(
|
||||
key: const ValueKey('window-resize'),
|
||||
behavior: HitTestBehavior.opaque,
|
||||
dragStartBehavior: DragStartBehavior.down,
|
||||
onPanUpdate: (details) => onResize(details.delta),
|
||||
onPanEnd: (_) => onResizeEnd(),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(2),
|
||||
child: Icon(
|
||||
Icons.south_east,
|
||||
size: 16,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
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)) {
|
||||
_focus.focusSoon(active);
|
||||
} else if (focused != null && !visible.contains(focused)) {
|
||||
_focus.focusSoon(active);
|
||||
}
|
||||
_visible = visible;
|
||||
_active = active;
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final manager = widget.manager;
|
||||
final windows = manager.windows;
|
||||
_focus.retain({for (final w in windows) w.id});
|
||||
return 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: bounds,
|
||||
focus: _focus,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
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);
|
||||
}
|
||||
@@ -172,10 +172,8 @@ class WindowManager extends ChangeNotifier {
|
||||
void setMinimized(String id, bool minimized) =>
|
||||
_replace(id, (w) => w.copyWith(minimized: minimized));
|
||||
|
||||
void setMaximized(String id, bool maximized) => _replace(
|
||||
id,
|
||||
(w) => w.copyWith(maximized: maximized, minimized: false),
|
||||
);
|
||||
void setMaximized(String id, bool maximized) =>
|
||||
_replace(id, (w) => w.copyWith(maximized: maximized, minimized: false));
|
||||
|
||||
/// Replaces what [id] shows (a chat that moved to a new session).
|
||||
void setContent(String id, String contentId, {Map<String, Object?>? data}) =>
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import 'window_frame.dart' show windowCornerRadius;
|
||||
import 'window_scope.dart';
|
||||
|
||||
/// A floating window's own page stack. Pages pushed from the window's
|
||||
/// content (`Navigator.of(context).push`) open inside the window, with Back
|
||||
/// inside it. Transient routes (sheets, dialogs, popup menus, dropdowns) go
|
||||
/// to the app's root navigator instead, so they open above every window and
|
||||
/// are never clipped to this one.
|
||||
///
|
||||
/// The navigator itself covers the whole window host, sharing the root
|
||||
/// navigator's coordinates (a popup anchored to a button measures against
|
||||
/// its navigator) and letting tooltips paint outside the window. Each page
|
||||
/// is drawn inside the window's content area by [WindowPageBox], and only
|
||||
/// that area takes input.
|
||||
class WindowNavigator extends Navigator {
|
||||
const WindowNavigator({super.key, super.pages, super.onDidRemovePage});
|
||||
|
||||
/// [child] with page transitions that keep every `MaterialPageRoute` of a
|
||||
/// window navigator below it inside the window's content area.
|
||||
static Widget theme(BuildContext context, {required Widget child}) {
|
||||
final theme = Theme.of(context);
|
||||
final builders = theme.pageTransitionsTheme.builders;
|
||||
return Theme(
|
||||
data: theme.copyWith(
|
||||
pageTransitionsTheme: PageTransitionsTheme(
|
||||
builders: {
|
||||
for (final platform in TargetPlatform.values)
|
||||
platform: _WindowPageTransitions(
|
||||
builders[platform] ?? const ZoomPageTransitionsBuilder(),
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
NavigatorState createState() => _WindowNavigatorState();
|
||||
}
|
||||
|
||||
class _WindowNavigatorState extends NavigatorState {
|
||||
/// Transient routes this window sent to the root navigator and that are
|
||||
/// still open.
|
||||
final List<Route<Object?>> _popups = [];
|
||||
|
||||
NavigatorState? get _root =>
|
||||
context.findRootAncestorStateOfType<NavigatorState>();
|
||||
|
||||
@override
|
||||
Future<T?> push<T extends Object?>(Route<T> route) {
|
||||
final root = _root;
|
||||
if (route is! PopupRoute<T> || root == null) {
|
||||
assert(
|
||||
route is PopupRoute<T> || route is MaterialPageRoute<T>,
|
||||
'A window page must be a MaterialPageRoute to be drawn inside its '
|
||||
'window: ${route.runtimeType}',
|
||||
);
|
||||
return super.push(route);
|
||||
}
|
||||
_popups.add(route);
|
||||
return root.push(route).whenComplete(() => _popups.remove(route));
|
||||
}
|
||||
|
||||
/// Content that closes its own sheet or menu with this navigator closes
|
||||
/// the popup, not the window's page.
|
||||
@override
|
||||
void pop<T extends Object?>([T? result]) {
|
||||
final root = _root;
|
||||
if (root != null && (_popups.isNotEmpty || !canPop())) {
|
||||
if (_popups.isNotEmpty) root.pop(result);
|
||||
return;
|
||||
}
|
||||
super.pop(result);
|
||||
}
|
||||
}
|
||||
|
||||
class _WindowPageTransitions extends PageTransitionsBuilder {
|
||||
const _WindowPageTransitions(this.inner);
|
||||
|
||||
final PageTransitionsBuilder inner;
|
||||
|
||||
@override
|
||||
Duration get transitionDuration => inner.transitionDuration;
|
||||
|
||||
@override
|
||||
Duration get reverseTransitionDuration => inner.reverseTransitionDuration;
|
||||
|
||||
@override
|
||||
Widget buildTransitions<T>(
|
||||
PageRoute<T> route,
|
||||
BuildContext context,
|
||||
Animation<double> animation,
|
||||
Animation<double> secondaryAnimation,
|
||||
Widget child,
|
||||
) => WindowPageBox(
|
||||
child: inner.buildTransitions(
|
||||
route,
|
||||
context,
|
||||
animation,
|
||||
secondaryAnimation,
|
||||
child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// One page of a window, drawn inside the window's content area: clipped to
|
||||
/// it, sized by it (so layout that reads [MediaQuery] sees the window, not
|
||||
/// the app), with a transparent [Scaffold] for the window's SnackBars.
|
||||
class WindowPageBox extends StatelessWidget {
|
||||
const WindowPageBox({super.key, required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scope = WindowScope.maybeOf(context);
|
||||
if (scope == null) return child;
|
||||
final body = scope.body;
|
||||
const radius = Radius.circular(windowCornerRadius);
|
||||
return Stack(
|
||||
children: [
|
||||
Positioned.fromRect(
|
||||
rect: body,
|
||||
child: ClipRRect(
|
||||
borderRadius: const BorderRadius.only(
|
||||
bottomLeft: radius,
|
||||
bottomRight: radius,
|
||||
),
|
||||
child: MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(
|
||||
size: body.size,
|
||||
padding: EdgeInsets.zero,
|
||||
viewPadding: EdgeInsets.zero,
|
||||
viewInsets: EdgeInsets.zero,
|
||||
),
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
primary: false,
|
||||
body: child,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Lets [child] take input only inside [region]: a window's navigator covers
|
||||
/// the whole host (see [WindowNavigator]) and must not swallow what is
|
||||
/// outside its window.
|
||||
class WindowHitRegion extends SingleChildRenderObjectWidget {
|
||||
const WindowHitRegion({super.key, required this.region, super.child});
|
||||
|
||||
final Rect region;
|
||||
|
||||
@override
|
||||
RenderObject createRenderObject(BuildContext context) =>
|
||||
_RenderWindowHitRegion(region);
|
||||
|
||||
@override
|
||||
void updateRenderObject(
|
||||
BuildContext context,
|
||||
covariant RenderObject renderObject,
|
||||
) => (renderObject as _RenderWindowHitRegion).region = region;
|
||||
}
|
||||
|
||||
class _RenderWindowHitRegion extends RenderProxyBox {
|
||||
_RenderWindowHitRegion(this.region);
|
||||
|
||||
Rect region;
|
||||
|
||||
@override
|
||||
bool hitTest(BoxHitTestResult result, {required Offset position}) =>
|
||||
region.contains(position) && super.hitTest(result, position: position);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'window_entry.dart';
|
||||
import 'window_manager.dart';
|
||||
|
||||
/// The floating window some content is in: its entry, the manager, and where
|
||||
/// the content area is drawn in the window host. Absent outside a window, so
|
||||
/// shared content can tell the tiled app from a window.
|
||||
class WindowScope extends InheritedWidget {
|
||||
const WindowScope({
|
||||
super.key,
|
||||
required this.manager,
|
||||
required this.entry,
|
||||
required this.body,
|
||||
required super.child,
|
||||
});
|
||||
|
||||
final WindowManager manager;
|
||||
final WindowEntry entry;
|
||||
|
||||
/// The content area (the window less its title bar), in the host's
|
||||
/// coordinates.
|
||||
final Rect body;
|
||||
|
||||
static WindowScope? maybeOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<WindowScope>();
|
||||
|
||||
static WindowScope of(BuildContext context) => maybeOf(context)!;
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(WindowScope oldWidget) =>
|
||||
oldWidget.manager != manager ||
|
||||
oldWidget.entry != entry ||
|
||||
oldWidget.body != body;
|
||||
}
|
||||
@@ -122,12 +122,7 @@ class WindowsRecord {
|
||||
);
|
||||
}
|
||||
|
||||
static List<double> _encodeRect(Rect r) => [
|
||||
r.left,
|
||||
r.top,
|
||||
r.width,
|
||||
r.height,
|
||||
];
|
||||
static List<double> _encodeRect(Rect r) => [r.left, r.top, r.width, r.height];
|
||||
|
||||
static Rect? _rect(Object? values) {
|
||||
if (values is! List ||
|
||||
|
||||
Reference in New Issue
Block a user