Files
pipelines/app/lib/application/detached/detached_platform.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

102 lines
3.5 KiB
Dart

import 'dart:convert';
import 'dart:ui';
import 'detached_platform_stub.dart'
if (dart.library.js_interop) 'detached_platform_web.dart'
if (dart.library.io) 'detached_platform_io.dart'
as host;
import 'detached_route.dart';
/// How a launch went.
enum DetachedLaunch {
/// The window is open (or an open one came to the front).
opened,
/// The browser blocked the popup; a click may open it as a tab.
blocked,
/// Nothing could be opened.
failed,
}
/// A message between a detached window and the main window, as JSON.
typedef DetachedMessage = Map<String, Object?>;
/// The platform's side of detached windows: opening one, the messages they
/// send back to the main window, and a hand-over of data too large for a
/// URL (a diff's files).
///
/// - Web: `window.open` with popup features, `postMessage` to the opener,
/// and `localStorage` for the hand-over.
/// - Linux and macOS: a second process of the app with `--detached <path>`,
/// a line on its standard output, and a temporary file.
/// - Android and iOS: unsupported.
abstract class DetachedPlatform {
/// The app's platform.
static DetachedPlatform instance = host.createDetachedPlatform();
/// Extra query parameters every detached browser window's URL carries.
/// The capture fixture sets its own (`chats=2`, `theme=dark`), so the
/// window it opens runs on the same fixture; the app sets none.
static Map<String, String> Function()? extraQuery;
/// Whether this platform can open detached windows.
bool get supported;
/// Opens [route] in a new window of [size] named [title]; [tab] opens a
/// browser tab instead (from a click, after a blocked popup).
Future<DetachedLaunch> launch(
DetachedRoute route, {
required String title,
required Size size,
bool tab = false,
});
/// Messages the detached windows send to this (main) window.
Stream<DetachedMessage> get messages;
/// Raised when another window of this app changed the device's stored
/// preferences (the browser's `storage` event); none on a desktop, whose
/// windows are separate processes.
Stream<void> get preferencesChanged => const Stream.empty();
/// Keeps [json] for the window that will ask for [token].
Future<void> put(String token, String json);
/// The data kept under [token], for this window; null when there is none.
Future<String?> take(String token);
/// Sends [message] to the main window that opened this one; false when
/// there is none to receive it.
Future<bool> sendToMain(DetachedMessage message);
/// Closes this detached window.
Future<void> closeSelf();
/// Opens the full app (the main window) at [appPath], in place of this
/// detached window; false when this platform cannot.
Future<bool> becomeMain(String appPath);
}
/// The detached window this launch opens, or null for the main window: the
/// browser location on the web, `--detached <path>` (or the environment) on
/// a desktop.
DetachedRoute? detachedLaunchRoute(List<String> args) =>
host.launchedDetachedRoute(args);
/// The marker a message carries, so the main window ignores anything else.
const detachedMessageMarker = 'pipelines.detached.v1';
String encodeDetachedMessage(DetachedMessage message) =>
jsonEncode({...message, 'marker': detachedMessageMarker});
DetachedMessage? decodeDetachedMessage(String text) {
try {
final value = jsonDecode(text);
if (value is! Map || value['marker'] != detachedMessageMarker) return null;
return Map<String, Object?>.from(value)..remove('marker');
} on FormatException {
return null;
}
}