Files
pipelines/app/lib/application/detached/detached_preferences.dart
T
alan 03983cfd1f Detached chat: read the chat again once its machine connects, and follow the main window's look
A detached chat window read the chat list once, before any saved machine had
connected, and never again; its pane also did not rebuild when the runtime
adopted a row. Without the row the header showed a bare "Chat" with no folder
or model, the window was named by the session id, and the chat's colour (bar
tint, focus ring, prompt bar) was missing. The window now re-reads the list on
each connection change and rebuilds on the runtime, as the Chat destination
does. It also re-reads the stored design and font on the browser's storage
event and on coming to the front, so a Settings change in the main window
follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 17:28:47 +00:00

65 lines
2.2 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../ui/theme/app_design_controller.dart';
import '../../ui/theme/app_font_controller.dart';
/// Keeps a detached window's look in step with the main window's.
///
/// Both read the design and the font from the device's preferences at
/// launch, but each window is its own app instance: a choice made later in
/// the main window's Settings reaches this one only when it reads them
/// again. It does so on [changes] (the browser's `storage` event, which
/// another window of the origin raises as it writes) and whenever the window
/// comes back to the front (a desktop process has no such event).
class DetachedPreferencesFollower with WidgetsBindingObserver {
DetachedPreferencesFollower({
required this._changes,
AppDesignController? designs,
AppFontController? fonts,
Future<void> Function()? reload,
}) : _designs = designs ?? AppDesignController.instance,
_fonts = fonts ?? AppFontController.instance,
_reload = reload ?? _reloadSharedPreferences;
final Stream<void> _changes;
final AppDesignController _designs;
final AppFontController _fonts;
final Future<void> Function() _reload;
StreamSubscription<void>? _subscription;
void start() {
WidgetsBinding.instance.addObserver(this);
_subscription = _changes.listen((_) => unawaited(follow()));
}
void dispose() {
WidgetsBinding.instance.removeObserver(this);
unawaited(_subscription?.cancel());
_subscription = null;
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) unawaited(follow());
}
/// Reads the stored design and font again and applies any change.
Future<void> follow() async {
await _reload();
await Future.wait([_designs.restore(), _fonts.restore()]);
}
/// The preferences are cached per app instance; another window's writes
/// show only after a reload.
static Future<void> _reloadSharedPreferences() async {
try {
await (await SharedPreferences.getInstance()).reload();
} catch (_) {
// An unreadable store keeps the look this window has.
}
}
}