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

247 lines
8.0 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import '../../core/servers/chat/chat_server_pool.dart';
import '../../core/servers/server_context.dart';
import '../../core/servers/server_scope.dart';
import '../../core/transport/pipelines_client.dart';
import '../../features/settings/server_connections.dart';
import '../../features/terminal/terminal_layout.dart';
import '../../ui/adaptive/window_margins.dart';
import '../../ui/scale/ui_scale.dart';
import '../../ui/theme/app_font_controller.dart';
import '../../ui/theme/app_theme.dart';
import '../app_route.dart';
import '../app_session.dart';
import '../app_terminals.dart';
import '../clipboard_failures.dart' show appScaffoldMessengerKey;
import '../server_endpoint.dart';
import 'detached_chat.dart';
import 'detached_diff.dart';
import 'detached_platform.dart';
import 'detached_route.dart';
import 'detached_terminal.dart';
import 'detached_window_body.dart';
import 'detached_windows.dart';
/// A detached window's whole app: one item ([route]) over its own
/// connection, without the shell (no navigation rail, lists, floating
/// windows, notifications or update checks).
///
/// It writes none of the shared layout the main window owns: not the
/// server's open-chat workspace (no Chat destination is built), not the
/// floating windows (there is no window manager) and not the terminal
/// layout ([TerminalLayout.persist] is off). System notifications stay the
/// main window's, so nothing is posted twice.
class DetachedWindowApp extends StatefulWidget {
const DetachedWindowApp({
super.key,
required this.route,
required this.server,
this.client,
this.chatPool,
this.servers,
this.serverId,
this.serverAddresses,
this.platform,
this.web = kIsWeb,
});
final DetachedRoute route;
final ServerEndpoint server;
final PipelinesClient? client;
final ChatServerPool? chatPool;
final ServerConnections? servers;
final String? serverId;
final List<Uri>? serverAddresses;
final DetachedPlatform? platform;
final bool web;
@override
State<DetachedWindowApp> createState() => _DetachedWindowAppState();
}
class _DetachedWindowAppState extends State<DetachedWindowApp> {
late final AppSession _session;
late final DetachedPlatform _platform =
widget.platform ?? DetachedPlatform.instance;
final _title = ValueNotifier<String>('');
final _ready = ValueNotifier<bool>(false);
final _sync = ValueNotifier<int>(0);
AppTerminals? _terminals;
late final DetachedWindowControls _controls = DetachedWindowControls(
ready: _ready,
sync: _sync,
setTitle: _setTitle,
close: () => unawaited(_platform.closeSelf()),
bringBackItem: (kind, id, files, index) => unawaited(
_bringBack(DetachedReturn(kind, id, files: files, initialIndex: index)),
),
);
@override
void initState() {
super.initState();
TerminalLayout.instance.persist = false;
_session = AppSession(
endpoint: widget.server.uri,
client: widget.client,
knownServer: true,
addresses: widget.serverAddresses,
);
bindServerIdentity(_session.gateway, widget.serverId);
bindServerIdentity(_session.client, widget.serverId);
_session.addListener(_connectionChanged);
widget.chatPool?.addListener(_connectionChanged);
_connectionChanged();
if (widget.route.kind == DetachedKind.terminal) {
_terminals = AppTerminals(
pool: widget.chatPool,
connection: Listenable.merge([_session, ?widget.servers]),
connected: () => _session.connected,
gateway: _session.gateway,
machineId: widget.serverId ?? 'server',
machineName: _serverName,
layout: TerminalLayout.instance,
);
}
unawaited(_session.start());
}
@override
void dispose() {
_session.removeListener(_connectionChanged);
widget.chatPool?.removeListener(_connectionChanged);
_terminals?.dispose();
_session.dispose();
_title.dispose();
_ready.dispose();
_sync.dispose();
super.dispose();
}
void _connectionChanged() {
final pool = widget.chatPool;
_ready.value =
(pool != null && pool.servers.catalog.servers.isNotEmpty) ||
_session.connected;
_sync.value = pool?.revision ?? _session.connectionEpoch;
}
String _serverName() {
final servers = widget.servers;
for (final server in servers?.catalog.servers ?? const []) {
if (server.id == widget.serverId) return server.name;
}
return servers?.originHostname ?? _session.endpoint?.host ?? 'Server';
}
/// Titles change from listeners that may fire while a frame is built; the
/// app's title follows after it.
void _setTitle(String title) {
if (SchedulerBinding.instance.schedulerPhase ==
SchedulerPhase.persistentCallbacks) {
SchedulerBinding.instance.addPostFrameCallback((_) {
if (mounted) _title.value = title;
});
return;
}
_title.value = title;
}
/// Hands the item to the main window and closes this one. With no main
/// window, a chat or the terminal opens the full app here instead; a diff
/// stays (it exists only in this window).
Future<void> _bringBack(DetachedReturn item) async {
if (await _platform.sendToMain(item.toMessage())) {
await _platform.closeSelf();
return;
}
final path = switch (item.kind) {
DetachedKind.chat => AppRoute.chatSession(item.id).path,
DetachedKind.terminal => AppRoute.chat.path,
DetachedKind.diff => null,
};
if (path != null && await _platform.becomeMain(path)) return;
appScaffoldMessengerKey.currentState?.showSnackBar(
const SnackBar(content: Text('No main window is open to take it back.')),
);
}
Widget _body() => switch (widget.route.kind) {
DetachedKind.chat => DetachedChat(
sessionId:
widget.chatPool?.gateway.normalizeId(widget.route.id) ??
widget.route.id,
gateway: widget.chatPool?.gateway ?? _session.gateway,
serverKey: widget.chatPool != null
? 'all-saved-servers'
: serverStorageScope(widget.serverId),
window: _controls,
),
DetachedKind.terminal => DetachedTerminal(
terminals: _terminals!,
tabKey: widget.route.id,
window: _controls,
),
DetachedKind.diff => DetachedDiff(
token: widget.route.id,
platform: _platform,
window: _controls,
),
};
Route<void> _page() => MaterialPageRoute<void>(
settings: RouteSettings(name: widget.route.path),
builder: (_) => _DetachedHome(child: _body()),
);
@override
Widget build(BuildContext context) {
final fonts = AppFontController.instance;
final app = ListenableBuilder(
listenable: Listenable.merge([fonts, _title]),
builder: (context, _) => MaterialApp(
title: _title.value.isEmpty
? 'Pipelines'
: '${_title.value} · Pipelines',
scaffoldMessengerKey: appScaffoldMessengerKey,
debugShowCheckedModeBanner: false,
builder: (context, child) => UiScaleHost(
child: WindowMargins(child: child ?? const SizedBox.shrink()),
),
theme: appLightTheme(fontFamily: fonts.family),
darkTheme: appDarkTheme(fontFamily: fonts.family),
themeMode: ThemeMode.system,
// The window's one page is named by its route, so the address bar
// (and a reload) keeps the detached location instead of `/`.
initialRoute: widget.route.path,
onGenerateInitialRoutes: (_) => [_page()],
onGenerateRoute: (_) => _page(),
),
);
final servers = widget.servers;
return servers == null
? app
: ServerContext(
connections: servers,
serverId: widget.serverId,
child: app,
);
}
}
/// The window's single page: the item fills it.
class _DetachedHome extends StatelessWidget {
const _DetachedHome({required this.child});
final Widget child;
@override
Widget build(BuildContext context) =>
Scaffold(key: const ValueKey('detached-window'), body: child);
}