Files
pipelines/app/lib/application/detached/detached_route.dart
T
alan 61c7eee2b0 App: open a chat, the terminal or a diff in a new window
"Open in new window" sits beside Float: the chat pane and chat list row
menus, a floating chat window's title bar, the terminal options menu and
the diff bar, plus Alt+Shift+Y for the chat with the keyboard (listed in
the shortcut help). Once the window opens the item leaves the main window
as Float takes a pane out of the tiling.

The window is its own app instance at /window/chat/<id>,
/window/terminal?tab=<key> or /window/diff/<token>: only that item, named
after it, with "Bring back to main window". It has its own connection and
writes no shared layout (no open-chat workspace, window store or terminal
layout) and posts no notifications, so they stay the main window's.

Web opens a popup window (a blocked popup offers a tab); Linux and macOS
start a second process with --detached (Flutter's multi-window API is still
experimental); Android and iOS hide the action. The capture kit gains a
follow step for windows a page opens.

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

109 lines
3.6 KiB
Dart

import '../app_route.dart';
/// What a detached window shows.
enum DetachedKind { chat, terminal, diff }
/// The route of a detached window: one chat, the terminal (on one tab) or
/// one diff, alone in its own browser window or app process, without the
/// shell's navigation.
///
/// /window/chat/:sessionId one chat pane
/// /window/terminal[?tab=:key] the terminal, on the tab [key]
/// /window/diff/:token a diff handed over under [token]
///
/// A native process gets the same path as `--detached <path>` (or the
/// `PIPELINES_DETACHED` environment variable, which macOS also passes).
class DetachedRoute {
const DetachedRoute._(this.kind, this.id);
/// One chat; null when [sessionId] is not a chat session id.
static DetachedRoute? chat(String sessionId) =>
AppRoute.isChatSessionId(sessionId)
? DetachedRoute._(DetachedKind.chat, sessionId)
: null;
/// The terminal on the tab [tabKey] (empty: the one selected).
static DetachedRoute terminal([String tabKey = '']) => DetachedRoute._(
DetachedKind.terminal,
tabKey.length > _maxTabKey ? '' : tabKey,
);
/// A diff whose files were handed over under [token].
static DetachedRoute? diff(String token) =>
_token.hasMatch(token) ? DetachedRoute._(DetachedKind.diff, token) : null;
final DetachedKind kind;
/// The chat session id, the terminal tab key or the diff token.
final String id;
static const prefix = '/window';
static const argument = '--detached';
static const environment = 'PIPELINES_DETACHED';
static final _token = RegExp(r'^[A-Za-z0-9_-]{1,64}$');
static const _maxTabKey = 512;
/// The app-relative location of this window.
String get path => switch (kind) {
DetachedKind.chat => '$prefix/chat/${Uri.encodeComponent(id)}',
DetachedKind.terminal =>
id.isEmpty
? '$prefix/terminal'
: '$prefix/terminal?${Uri(queryParameters: {'tab': id}).query}',
DetachedKind.diff => '$prefix/diff/$id',
};
/// The detached route [location] names (a path with its query, or a full
/// URL), or null when it is an ordinary app location.
static DetachedRoute? parse(String location) {
final uri = Uri.tryParse(location.trim());
if (uri == null) return null;
final segments = uri.pathSegments.where((s) => s.isNotEmpty).toList();
if (segments.length < 2 || segments.first != prefix.substring(1)) {
return null;
}
return switch ((segments[1], segments.length)) {
('chat', 3) => chat(segments[2]),
('terminal', 2) => terminal(_query(uri)['tab'] ?? ''),
('diff', 3) => diff(segments[2]),
_ => null,
};
}
/// The route a native launch names: `--detached <path>` (or
/// `--detached=<path>`) in [args], else [env]'s `PIPELINES_DETACHED`.
static DetachedRoute? fromLaunch(
List<String> args, [
Map<String, String> env = const {},
]) {
for (var i = 0; i < args.length; i++) {
final arg = args[i];
if (arg == argument && i + 1 < args.length) return parse(args[i + 1]);
if (arg.startsWith('$argument=')) {
return parse(arg.substring(argument.length + 1));
}
}
final value = env[environment];
return value == null || value.isEmpty ? null : parse(value);
}
static Map<String, String> _query(Uri uri) {
try {
return uri.queryParameters;
} on FormatException {
return const {};
}
}
@override
bool operator ==(Object other) =>
other is DetachedRoute && other.kind == kind && other.id == id;
@override
int get hashCode => Object.hash(kind, id);
@override
String toString() => path;
}