Files
pipelines/app/lib/shell/windows/window_navigator.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

181 lines
5.6 KiB
Dart

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);
}