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> 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()?.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()})';