Files
pipelines/app/lib/shell/detached/detached_window_scope.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

78 lines
2.7 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
/// Opens an item of the app in a window of its own: a browser window on the
/// web, a second app process on a desktop (see `notes/detached-windows.md`).
///
/// Each call removes nothing itself: [onOpened] runs once the window really
/// opened (at once, or after the operator allowed a blocked popup), and the
/// caller then takes the item out of the main window.
abstract interface class DetachedWindowOpener {
void openChat(
String sessionId, {
required String title,
VoidCallback? onOpened,
});
/// The terminal on [tabKey] (empty: whichever tab it selects).
void openTerminal({
String tabKey = '',
required String title,
VoidCallback? onOpened,
});
/// A diff's files, as the server's `file_changes.files[]` entries.
void openDiff(
List<Map<String, Object?>> files, {
int initialIndex = 0,
required String title,
VoidCallback? onOpened,
});
}
/// Hands the app's [DetachedWindowOpener] to the screens that offer
/// "Open in new window". Absent where the platform has no windows (Android,
/// iOS) and inside a detached window itself.
class DetachedWindowScope extends InheritedWidget {
const DetachedWindowScope({
super.key,
required this.opener,
required super.child,
});
final DetachedWindowOpener opener;
static DetachedWindowOpener? maybeOf(BuildContext context) =>
context.getInheritedWidgetOfExactType<DetachedWindowScope>()?.opener;
@override
bool updateShouldNotify(DetachedWindowScope oldWidget) =>
opener != oldWidget.opener;
}
/// The menus' label for the action.
const detachedWindowLabel = 'Open in new window';
/// Whether [event] sends the chat with the keyboard to a new window:
/// Alt+Shift+Y (Option+Shift+Y). Alt+Y alone is Float.
bool detachedWindowShortcut(KeyEvent event, HardwareKeyboard keyboard) {
if (event is KeyUpEvent || !keyboard.isAltPressed) return false;
if (!keyboard.isShiftPressed) return false;
if (keyboard.isControlPressed || keyboard.isMetaPressed) return false;
// Option+Shift+Y types a symbol on a Mac: match the key's position too.
return event.logicalKey == LogicalKeyboardKey.keyY ||
event.physicalKey == PhysicalKeyboardKey.keyY;
}
/// The shortcut as it reads on this platform.
String detachedWindowShortcutLabel([TargetPlatform? platform]) =>
switch (platform ?? defaultTargetPlatform) {
TargetPlatform.macOS || TargetPlatform.iOS => 'Option+Shift+Y',
_ => 'Alt+Shift+Y',
};
/// The label with the shortcut, for the chat menus.
String detachedChatWindowLabel() =>
'$detachedWindowLabel (${detachedWindowShortcutLabel()})';