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

194 lines
5.2 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import '../../shell/detached/detached_window_scope.dart';
import '../clipboard_failures.dart' show appScaffoldMessengerKey;
import 'detached_platform.dart';
import 'detached_route.dart';
/// What a detached window hands back to the main window ("Bring back to
/// main window"): the item, and a diff's files.
class DetachedReturn {
const DetachedReturn(
this.kind,
this.id, {
this.files = const [],
this.initialIndex = 0,
});
final DetachedKind kind;
final String id;
final List<Map<String, Object?>> files;
final int initialIndex;
DetachedMessage toMessage() => {
'type': 'bring-back',
'kind': kind.name,
'id': id,
if (kind == DetachedKind.diff) ...{'files': files, 'index': initialIndex},
};
static DetachedReturn? fromMessage(DetachedMessage message) {
if (message['type'] != 'bring-back') return null;
final kind = DetachedKind.values.asNameMap()['${message['kind']}'];
if (kind == null) return null;
final files = message['files'];
final index = message['index'];
return DetachedReturn(
kind,
'${message['id'] ?? ''}',
files: files is List
? [
for (final file in files)
if (file is Map) Map<String, Object?>.from(file),
]
: const [],
initialIndex: index is int ? index : 0,
);
}
}
/// Window sizes a detached item opens at.
const detachedChatSize = Size(720, 860);
const detachedTerminalSize = Size(960, 600);
const detachedDiffSize = Size(1000, 820);
/// The main window's side of detached windows: opening them (see
/// [DetachedWindowOpener]) and taking items back from them.
class DetachedWindows implements DetachedWindowOpener {
DetachedWindows({
DetachedPlatform? platform,
required this.onReturn,
GlobalKey<ScaffoldMessengerState>? messenger,
}) : platform = platform ?? DetachedPlatform.instance,
_messenger = messenger ?? appScaffoldMessengerKey;
final DetachedPlatform platform;
/// Puts an item a detached window gave back into the main window.
final void Function(DetachedReturn item) onReturn;
final GlobalKey<ScaffoldMessengerState> _messenger;
StreamSubscription<DetachedMessage>? _messages;
bool get supported => platform.supported;
void start() {
if (!supported || _messages != null) return;
_messages = platform.messages.listen((message) {
final item = DetachedReturn.fromMessage(message);
if (item != null) onReturn(item);
});
}
void dispose() {
unawaited(_messages?.cancel());
_messages = null;
}
@override
void openChat(
String sessionId, {
required String title,
VoidCallback? onOpened,
}) {
final route = DetachedRoute.chat(sessionId);
if (route == null) return;
unawaited(_open(route, title, detachedChatSize, onOpened));
}
@override
void openTerminal({
String tabKey = '',
required String title,
VoidCallback? onOpened,
}) => unawaited(
_open(
DetachedRoute.terminal(tabKey),
title,
detachedTerminalSize,
onOpened,
),
);
@override
void openDiff(
List<Map<String, Object?>> files, {
int initialIndex = 0,
required String title,
VoidCallback? onOpened,
}) {
final token = _token();
final route = DetachedRoute.diff(token)!;
// The hand-over is written before the window opens, so its first read
// finds it. A popup must open within the click: on the web the write is
// synchronous, so no frame passes before it.
final written = platform.put(
token,
jsonEncode({'files': files, 'index': initialIndex}),
);
unawaited(
written.then((_) => _open(route, title, detachedDiffSize, onOpened)),
);
}
Future<void> _open(
DetachedRoute route,
String title,
Size size,
VoidCallback? onOpened,
) async {
switch (await platform.launch(route, title: title, size: size)) {
case DetachedLaunch.opened:
onOpened?.call();
case DetachedLaunch.blocked:
_say(
'The browser blocked the new window. Allow pop-ups for this site, '
'or open it in a tab.',
action: SnackBarAction(
label: 'Open in tab',
onPressed: () => unawaited(_openTab(route, title, size, onOpened)),
),
);
case DetachedLaunch.failed:
_say('Could not open a new window.');
}
}
Future<void> _openTab(
DetachedRoute route,
String title,
Size size,
VoidCallback? onOpened,
) async {
final result = await platform.launch(
route,
title: title,
size: size,
tab: true,
);
if (result == DetachedLaunch.opened) {
onOpened?.call();
} else {
_say('Could not open a new tab either.');
}
}
void _say(String text, {SnackBarAction? action}) =>
_messenger.currentState?.showSnackBar(
SnackBar(
content: Text(text),
action: action,
duration: Duration(seconds: action == null ? 4 : 10),
),
);
static final _random = Random.secure();
static String _token() => base64Url
.encode(List.generate(12, (_) => _random.nextInt(256)))
.replaceAll('=', '');
}