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> 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.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? 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 _messenger; StreamSubscription? _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> 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 _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 _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('=', ''); }