Files
pipelines/app/lib/shell/windows/window_focus.dart
T
alan a99f7014f5 Add the shell window host with layering acceptance tests
Phase 1.3 and 1.4 of docs/plans/floating-windows.md. Windows are plain
children of the terminal stage's stack in the shell page, outside every
destination, so popups opened from one are above all of them. Each window
has its own page navigator: pushed pages open inside the window, and popup
routes go to the root navigator. One WindowFrame chrome (move, resize,
minimise, maximise, close), a focus scope per window, a ScaffoldMessenger
per window.

No feature uses it yet. The capture fixture opens probe windows with
?windows=N, and window_layering_test.dart covers every popup type from a
window over Chat and over another destination.
2026-11-10 20:48:31 +00:00

78 lines
2.4 KiB
Dart

import 'package:flutter/widgets.dart';
/// The floating windows' keyboard focus: each window has a focus scope; the
/// window that shows or comes to the top takes the keyboard, and when the
/// last one hides it goes back to what had it before (a composer, a field),
/// else to the screen.
class WindowFocus {
final Map<String, FocusScopeNode> _scopes = {};
FocusNode? _previous;
FocusScopeNode scopeFor(String id) =>
_scopes[id] ??= FocusScopeNode(debugLabel: 'window $id');
/// Forgets the scopes of windows no longer in [ids].
void retain(Set<String> ids) {
for (final id in _scopes.keys.where((id) => !ids.contains(id)).toList()) {
_scopes.remove(id)!.dispose();
}
}
bool _within(FocusNode node, FocusScopeNode scope) =>
identical(node, scope) || node.ancestors.contains(scope);
/// Whether [id]'s window has the keyboard.
bool has(String id) {
final current = FocusManager.instance.primaryFocus;
final scope = _scopes[id];
return current != null && scope != null && _within(current, scope);
}
/// The window that has the keyboard, if one does.
String? get focused => _scopes.keys.where(has).firstOrNull;
/// Notes what has the keyboard now, before a window takes it.
void remember() {
final current = FocusManager.instance.primaryFocus;
if (current == null || focused != null) return;
_previous = identical(current, FocusManager.instance.rootScope)
? null
: current;
}
/// Focuses [id]'s window once this frame has built it.
void focusSoon(String id) {
WidgetsBinding.instance
..addPostFrameCallback((_) {
final scope = _scopes[id];
if (scope != null && scope.context != null && scope.canRequestFocus) {
scope.requestFocus();
}
})
..ensureVisualUpdate();
}
/// Gives the keyboard back to what had it before the windows showed, if
/// a window has it now.
void release() {
final previous = _previous;
_previous = null;
final current = FocusManager.instance.primaryFocus;
if (current == null || focused == null) return;
if (previous != null &&
previous.context != null &&
previous.canRequestFocus) {
previous.requestFocus();
} else {
current.unfocus();
}
}
void dispose() {
for (final scope in _scopes.values) {
scope.dispose();
}
_scopes.clear();
}
}