Files
pipelines/app/test/application/detached/detached_fixtures.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

181 lines
4.8 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:ui';
import 'package:pipelines_app/application/detached/detached_platform.dart';
import 'package:pipelines_app/application/detached/detached_route.dart';
import 'package:pipelines_app/shell/detached/detached_window_scope.dart';
import '../../core/transport/pipelines_client_test.dart' show FakeChannel;
/// A platform that records what it was asked to open and send.
class FakeDetachedPlatform extends DetachedPlatform {
DetachedLaunch result = DetachedLaunch.opened;
final launches = <({DetachedRoute route, String title, bool tab})>[];
final stored = <String, String>{};
final sent = <DetachedMessage>[];
bool mainOpen = true;
bool closed = false;
String? becameMain;
final incoming = StreamController<DetachedMessage>.broadcast();
@override
bool get supported => true;
@override
Future<DetachedLaunch> launch(
DetachedRoute route, {
required String title,
required Size size,
bool tab = false,
}) async {
launches.add((route: route, title: title, tab: tab));
return tab ? DetachedLaunch.opened : result;
}
@override
Stream<DetachedMessage> get messages => incoming.stream;
@override
Future<void> put(String token, String json) async => stored[token] = json;
@override
Future<String?> take(String token) async => stored[token];
@override
Future<bool> sendToMain(DetachedMessage message) async {
if (!mainOpen) return false;
sent.add(message);
return true;
}
@override
Future<void> closeSelf() async => closed = true;
@override
Future<bool> becomeMain(String appPath) async {
becameMain = appPath;
return true;
}
}
/// An opener that records the items and opens them at once.
class FakeOpener implements DetachedWindowOpener {
final chats = <String>[];
final terminals = <String>[];
final diffs = <List<Map<String, Object?>>>[];
bool opens = true;
@override
void openChat(
String sessionId, {
required String title,
void Function()? onOpened,
}) {
chats.add(sessionId);
if (opens) onOpened?.call();
}
@override
void openTerminal({
String tabKey = '',
required String title,
void Function()? onOpened,
}) {
terminals.add(tabKey);
if (opens) onOpened?.call();
}
@override
void openDiff(
List<Map<String, Object?>> files, {
int initialIndex = 0,
required String title,
void Function()? onOpened,
}) {
diffs.add(files);
if (opens) onOpened?.call();
}
}
/// Answers a chat app's requests over [channel] like a server with chats
/// a, b and c, recording each request (type and body).
class ChatServer {
ChatServer(this.channel, {List<Object?> openChats = const []})
: _entries = openChats;
final FakeChannel channel;
final _answered = <Object?>{};
final requests = <Map<String, dynamic>>[];
List<Object?> _entries;
int _revision = 1;
/// The open-chat writes the app sent (`chat_open_workspace_request`).
List<Map<String, dynamic>> get workspaceWrites => [
for (final request in requests)
if (request['type'] == 'chat_open_workspace' &&
request['chat_open_workspace_request'] != null)
request,
];
void answer() {
for (final raw in channel.outgoing.toList()) {
final m = jsonDecode(raw as String) as Map<String, dynamic>;
final id = m['id'];
if (id == null || !_answered.add(id)) continue;
requests.add(m);
channel.incoming.add(jsonEncode(_reply('${m['type']}', id, m)));
}
}
Map<String, Object?> _reply(String type, Object? id, Map<String, dynamic> m) {
switch (type) {
case 'chat_sessions':
return {
'id': id,
'type': type,
'chat_sessions': [
for (final s in const ['a', 'b', 'c'])
{
'id': s,
'title': 'Chat $s',
'directory': '/workspace/demo',
'updated_at': '2026-11-12T10:00:00Z',
},
],
};
case 'chat_messages':
return {
'id': id,
'type': 'chat_history',
'chat_history': {
'messages': [
{
'id': 'm0',
'role': 'user',
'parts': [
{'type': 'text', 'text': 'Hello from chat'},
],
},
],
'next_cursor': '',
'has_more': false,
},
};
case 'chat_open_workspace':
final write = m['chat_open_workspace_request'] as Map?;
if (write != null) {
_revision++;
_entries = write['entries'] as List<Object?>;
}
return {
'id': id,
'type': type,
'chat_open_workspace': {'revision': _revision, 'entries': _entries},
};
default:
return {'id': id, 'type': type};
}
}
}