Files
alan ef1c120f57 Add the Windows menu, window cycling, edge snapping and size per kind
Phase 4 of docs/plans/floating-windows.md. A bar button lists the app's
windows on every tab (show one, Show all, Minimise all, Close all). Alt+`
and Alt+Shift+` bring the next or previous window to the front. Dropping a
title at the left or right edge fills that half, at the top it maximises.
A new window of a kind opens at the size that kind was last given.
2026-11-11 00:05:16 +00:00

146 lines
4.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'window_focus.dart';
import 'window_layer.dart';
import 'window_manager.dart';
import 'window_shortcuts.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<WindowHost> createState() => _WindowHostState();
}
class _WindowHostState extends State<WindowHost> {
final WindowFocus _focus = WindowFocus();
Set<String> _visible = const {};
String? _active;
@override
void initState() {
super.initState();
widget.manager.addListener(_changed);
FocusManager.instance.addEarlyKeyEventHandler(_key);
// 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() {
FocusManager.instance.removeEarlyKeyEventHandler(_key);
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(() {});
}
/// Alt+` and Alt+Shift+` bring the next or previous window to the front,
/// from anywhere (a composer, the terminal). With fewer than two windows
/// showing, or while a popup covers the app, the key is left alone.
KeyEventResult _key(KeyEvent event) {
if (!mounted) return KeyEventResult.ignored;
final step = windowCycleShortcut(event, HardwareKeyboard.instance);
final manager = widget.manager;
if (step == null ||
(_focus.covered?.call() ?? false) ||
manager.windows.where(manager.isVisible).length < 2) {
return KeyEventResult.ignored;
}
manager.cycle(step);
return KeyEventResult.handled;
}
/// 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,
),
],
);
},
),
);
}
}