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 createState() => _WindowHostState(); } class _WindowHostState extends State { final WindowFocus _focus = WindowFocus(); Set _visible = const {}; String? _active; @override void initState() { super.initState(); widget.manager.addListener(_changed); // The shell page is not the top route while a popup or page covers it. _focus.covered = () => mounted && !(ModalRoute.of(context)?.isCurrent ?? true); 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) || (focused != null && !visible.contains(focused))) { _focusWindow(active); } _visible = visible; _active = active; setState(() {}); } /// The window takes the keyboard, then its content places it. void _focusWindow(String id) { _focus.focusSoon(id); final entry = widget.manager.byId(id); if (entry != null) widget.manager.kinds[entry.kind]?.focusContent(entry); } @override Widget build(BuildContext context) { final manager = widget.manager; final windows = manager.windows; _focus.retain({for (final w in windows) w.id}); return ListenableBuilder( listenable: Listenable.merge([ for (final kind in manager.kinds.all) ?kind.changes, ]), builder: (context, _) => 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.width >= kind.minHostWidth, bounds: bounds, focus: _focus, ), ], ); }, ), ); } }