Files
pipelines/app/lib/shell/windows/window_store.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

181 lines
5.4 KiB
Dart

import 'dart:convert';
import 'dart:ui';
import 'package:shared_preferences/shared_preferences.dart';
import 'window_entry.dart';
import 'window_kind.dart';
const windowsPreferenceKey = 'pipelines.windows.v1';
/// The most windows and remembered frames saved (and restored).
const int windowStoreMaxWindows = 48;
const int windowStoreMaxFrames = 256;
/// The windows as saved on this device, for every server: the open windows
/// bottom to top, the groups hidden by their chord, and the frames of closed
/// windows by [windowFrameKey]. Identities and geometry only.
class WindowsRecord {
const WindowsRecord({
this.windows = const [],
this.hiddenGroups = const {},
this.frames = const {},
});
final List<WindowEntry> windows;
final Set<String> hiddenGroups;
final Map<String, Rect> frames;
String encode(WindowKinds kinds) => jsonEncode({
'version': 1,
'windows': [
for (final w in windows)
{
'id': w.id,
'kind': w.kind,
'content': w.contentId,
if (w.serverKey != null) 'server': w.serverKey,
'group': w.groupId,
'frame': _encodeRect(w.frame),
if (w.minimized) 'minimized': true,
if (w.maximized) 'maximized': true,
'data': ?kinds[w.kind]?.encodeData(w),
},
],
'hidden_groups': hiddenGroups.toList()..sort(),
'frames': {
for (final MapEntry(:key, :value) in frames.entries)
key: _encodeRect(value),
},
});
/// Null when [source] is not a record. A window of an unregistered kind,
/// or whose kind rejects its data, is dropped; the rest are kept.
static WindowsRecord? decode(String source, WindowKinds kinds) {
if (source.length > 262144) return null;
try {
final data = jsonDecode(source);
if (data is! Map || data['version'] != 1) return null;
final rawWindows = data['windows'];
final rawHidden = data['hidden_groups'] ?? const [];
final rawFrames = data['frames'] ?? const {};
if (rawWindows is! List || rawHidden is! List || rawFrames is! Map) {
return null;
}
if (rawWindows.length > windowStoreMaxWindows ||
rawFrames.length > windowStoreMaxFrames) {
return null;
}
final windows = <WindowEntry>[];
for (final raw in rawWindows) {
final window = _window(raw, kinds);
if (window == null || windows.any((w) => w.id == window.id)) continue;
windows.add(window);
}
final frames = <String, Rect>{};
for (final MapEntry(:key, :value) in rawFrames.entries) {
final rect = _rect(value);
if (key is String && key.length <= 1024 && rect != null) {
frames[key] = rect;
}
}
return WindowsRecord(
windows: windows,
hiddenGroups: {
for (final g in rawHidden)
if (_identity(g, 256)) g as String,
},
frames: frames,
);
} on Object {
return null;
}
}
static WindowEntry? _window(Object? raw, WindowKinds kinds) {
if (raw is! Map) return null;
final id = raw['id'], kindKey = raw['kind'], content = raw['content'];
final server = raw['server'], group = raw['group'];
final frame = _rect(raw['frame']);
if (!_identity(id, 256) ||
!_identity(kindKey, 64) ||
!_identity(content, 512) ||
(server != null && !_identity(server, 512)) ||
(group != null && !_identity(group, 256)) ||
frame == null) {
return null;
}
final kind = kinds[kindKey as String];
if (kind == null || kind.serverScoped != (server != null)) return null;
final data = kind.decodeData(raw['data']);
if (data == null) return null;
return WindowEntry(
id: id as String,
kind: kindKey,
contentId: content as String,
serverKey: server as String?,
groupId: group as String?,
frame: frame,
minimized: raw['minimized'] == true,
maximized: raw['maximized'] == true,
data: data,
);
}
static List<double> _encodeRect(Rect r) => [r.left, r.top, r.width, r.height];
static Rect? _rect(Object? values) {
if (values is! List ||
values.length != 4 ||
values.any((v) => v is! num || !v.isFinite)) {
return null;
}
final [left, top, width, height] = [
for (final v in values) (v as num).toDouble(),
];
if (width <= 0 || height <= 0) return null;
return Rect.fromLTWH(left, top, width, height);
}
}
bool _identity(Object? value, int limit) =>
value is String &&
value.isNotEmpty &&
value.length <= limit &&
value.trim() == value &&
!value.contains(RegExp(r'[\x00-\x1f\x7f]'));
/// Where the windows are kept. Device-local: one record for every server,
/// each server-scoped window carrying its server's key.
abstract interface class WindowStore {
Future<String?> read();
Future<bool> write(String record);
}
class SharedPreferencesWindowStore implements WindowStore {
const SharedPreferencesWindowStore();
@override
Future<String?> read() async {
try {
return (await SharedPreferences.getInstance()).getString(
windowsPreferenceKey,
);
} on Object {
return null;
}
}
@override
Future<bool> write(String record) async {
try {
return await (await SharedPreferences.getInstance()).setString(
windowsPreferenceKey,
record,
);
} on Object {
return false;
}
}
}