import '../app_route.dart'; /// What a detached window shows. enum DetachedKind { chat, terminal, diff } /// The route of a detached window: one chat, the terminal (on one tab) or /// one diff, alone in its own browser window or app process, without the /// shell's navigation. /// /// /window/chat/:sessionId one chat pane /// /window/terminal[?tab=:key] the terminal, on the tab [key] /// /window/diff/:token a diff handed over under [token] /// /// A native process gets the same path as `--detached ` (or the /// `PIPELINES_DETACHED` environment variable, which macOS also passes). class DetachedRoute { const DetachedRoute._(this.kind, this.id); /// One chat; null when [sessionId] is not a chat session id. static DetachedRoute? chat(String sessionId) => AppRoute.isChatSessionId(sessionId) ? DetachedRoute._(DetachedKind.chat, sessionId) : null; /// The terminal on the tab [tabKey] (empty: the one selected). static DetachedRoute terminal([String tabKey = '']) => DetachedRoute._( DetachedKind.terminal, tabKey.length > _maxTabKey ? '' : tabKey, ); /// A diff whose files were handed over under [token]. static DetachedRoute? diff(String token) => _token.hasMatch(token) ? DetachedRoute._(DetachedKind.diff, token) : null; final DetachedKind kind; /// The chat session id, the terminal tab key or the diff token. final String id; static const prefix = '/window'; static const argument = '--detached'; static const environment = 'PIPELINES_DETACHED'; static final _token = RegExp(r'^[A-Za-z0-9_-]{1,64}$'); static const _maxTabKey = 512; /// The app-relative location of this window. String get path => switch (kind) { DetachedKind.chat => '$prefix/chat/${Uri.encodeComponent(id)}', DetachedKind.terminal => id.isEmpty ? '$prefix/terminal' : '$prefix/terminal?${Uri(queryParameters: {'tab': id}).query}', DetachedKind.diff => '$prefix/diff/$id', }; /// The detached route [location] names (a path with its query, or a full /// URL), or null when it is an ordinary app location. static DetachedRoute? parse(String location) { final uri = Uri.tryParse(location.trim()); if (uri == null) return null; final segments = uri.pathSegments.where((s) => s.isNotEmpty).toList(); if (segments.length < 2 || segments.first != prefix.substring(1)) { return null; } return switch ((segments[1], segments.length)) { ('chat', 3) => chat(segments[2]), ('terminal', 2) => terminal(_query(uri)['tab'] ?? ''), ('diff', 3) => diff(segments[2]), _ => null, }; } /// The route a native launch names: `--detached ` (or /// `--detached=`) in [args], else [env]'s `PIPELINES_DETACHED`. static DetachedRoute? fromLaunch( List args, [ Map env = const {}, ]) { for (var i = 0; i < args.length; i++) { final arg = args[i]; if (arg == argument && i + 1 < args.length) return parse(args[i + 1]); if (arg.startsWith('$argument=')) { return parse(arg.substring(argument.length + 1)); } } final value = env[environment]; return value == null || value.isEmpty ? null : parse(value); } static Map _query(Uri uri) { try { return uri.queryParameters; } on FormatException { return const {}; } } @override bool operator ==(Object other) => other is DetachedRoute && other.kind == kind && other.id == id; @override int get hashCode => Object.hash(kind, id); @override String toString() => path; }