import 'dart:convert'; import 'dart:ui'; import 'detached_platform_stub.dart' if (dart.library.js_interop) 'detached_platform_web.dart' if (dart.library.io) 'detached_platform_io.dart' as host; import 'detached_route.dart'; /// How a launch went. enum DetachedLaunch { /// The window is open (or an open one came to the front). opened, /// The browser blocked the popup; a click may open it as a tab. blocked, /// Nothing could be opened. failed, } /// A message between a detached window and the main window, as JSON. typedef DetachedMessage = Map; /// The platform's side of detached windows: opening one, the messages they /// send back to the main window, and a hand-over of data too large for a /// URL (a diff's files). /// /// - Web: `window.open` with popup features, `postMessage` to the opener, /// and `localStorage` for the hand-over. /// - Linux and macOS: a second process of the app with `--detached `, /// a line on its standard output, and a temporary file. /// - Android and iOS: unsupported. abstract class DetachedPlatform { /// The app's platform. static DetachedPlatform instance = host.createDetachedPlatform(); /// Extra query parameters every detached browser window's URL carries. /// The capture fixture sets its own (`chats=2`, `theme=dark`), so the /// window it opens runs on the same fixture; the app sets none. static Map Function()? extraQuery; /// Whether this platform can open detached windows. bool get supported; /// Opens [route] in a new window of [size] named [title]; [tab] opens a /// browser tab instead (from a click, after a blocked popup). Future launch( DetachedRoute route, { required String title, required Size size, bool tab = false, }); /// Messages the detached windows send to this (main) window. Stream get messages; /// Raised when another window of this app changed the device's stored /// preferences (the browser's `storage` event); none on a desktop, whose /// windows are separate processes. Stream get preferencesChanged => const Stream.empty(); /// Keeps [json] for the window that will ask for [token]. Future put(String token, String json); /// The data kept under [token], for this window; null when there is none. Future take(String token); /// Sends [message] to the main window that opened this one; false when /// there is none to receive it. Future sendToMain(DetachedMessage message); /// Closes this detached window. Future closeSelf(); /// Opens the full app (the main window) at [appPath], in place of this /// detached window; false when this platform cannot. Future becomeMain(String appPath); } /// The detached window this launch opens, or null for the main window: the /// browser location on the web, `--detached ` (or the environment) on /// a desktop. DetachedRoute? detachedLaunchRoute(List args) => host.launchedDetachedRoute(args); /// The marker a message carries, so the main window ignores anything else. const detachedMessageMarker = 'pipelines.detached.v1'; String encodeDetachedMessage(DetachedMessage message) => jsonEncode({...message, 'marker': detachedMessageMarker}); DetachedMessage? decodeDetachedMessage(String text) { try { final value = jsonDecode(text); if (value is! Map || value['marker'] != detachedMessageMarker) return null; return Map.from(value)..remove('marker'); } on FormatException { return null; } }