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}); /// [theme] with page transitions that keep every `MaterialPageRoute` of a /// window navigator inside the window's content area. The caller keeps the /// result, so rebuilding a window layer does not hand its contents a new /// theme. static ThemeData themeData(ThemeData theme) { final builders = theme.pageTransitionsTheme.builders; return theme.copyWith( pageTransitionsTheme: PageTransitionsTheme( builders: { for (final platform in TargetPlatform.values) platform: _WindowPageTransitions( builders[platform] ?? const ZoomPageTransitionsBuilder(), ), }, ), ); } @override NavigatorState createState() => _WindowNavigatorState(); } class _WindowNavigatorState extends NavigatorState { /// Transient routes this window sent to the root navigator and that are /// still open. final List> _popups = []; NavigatorState? get _root => context.findRootAncestorStateOfType(); @override Future push(Route route) { final root = _root; if (route is! PopupRoute || root == null) { assert( route is PopupRoute || route is MaterialPageRoute, '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? 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( PageRoute route, BuildContext context, Animation animation, Animation 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 body = WindowBodyScope.maybeOf(context); if (body == null) return child; 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, // Its own layer: moving or resizing the window repositions the // page without repainting it. body: RepaintBoundary(child: 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); }