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 createState() => _WindowLayerState(); } class _WindowLayerState extends State 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, ); } /// 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() { if (!widget.focus.has(_id)) widget.focus.focusNow(_id); widget.manager.raise(_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( 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); 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( 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); }