e070ded799
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
6.1 KiB
Dart
193 lines
6.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
|
|
import 'window_frame.dart'
|
|
show WindowOutline, windowCornerRadius, windowTitleBarHeight;
|
|
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<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 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),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
// The window's edge, title bar included, over the page but under
|
|
// the tooltips and menus this navigator's overlay shows above it:
|
|
// drawn at the window layer it would cross them.
|
|
Positioned.fromRect(
|
|
rect: Rect.fromLTRB(
|
|
body.left,
|
|
body.top - windowTitleBarHeight(context),
|
|
body.right,
|
|
body.bottom,
|
|
),
|
|
child: const WindowOutline(),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 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);
|
|
}
|