|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | + |
| 3 | +/// RuntimeBridge — Unified IPC bridge for Formatrix Docs. |
| 4 | +/// |
| 5 | +/// Detects the available runtime (Gossamer, Tauri, or browser-only) and |
| 6 | +/// dispatches `invoke` calls to the appropriate backend. This allows all |
| 7 | +/// command modules to use a single import instead of binding directly |
| 8 | +/// to `@tauri-apps/api/core`. |
| 9 | +/// |
| 10 | +/// Priority order: |
| 11 | +/// 1. Gossamer (`window.__gossamer_invoke`) — own stack, preferred |
| 12 | +/// 2. Tauri (`window.__TAURI_INTERNALS__`) — legacy, transition |
| 13 | +/// 3. Browser (direct HTTP fetch) — development fallback |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// Raw external bindings — exactly one of these will be available at runtime |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +/// Gossamer IPC: injected by gossamer_channel_open() into the webview. |
| 20 | +%%raw(` |
| 21 | +function isGossamerRuntime() { |
| 22 | + return typeof window !== 'undefined' |
| 23 | + && typeof window.__gossamer_invoke === 'function'; |
| 24 | +} |
| 25 | +`) |
| 26 | +@val external isGossamerRuntime: unit => bool = "isGossamerRuntime" |
| 27 | + |
| 28 | +%%raw(` |
| 29 | +function gossamerInvoke(cmd, args) { |
| 30 | + return window.__gossamer_invoke(cmd, args); |
| 31 | +} |
| 32 | +`) |
| 33 | +@val external gossamerInvoke: (string, 'a) => promise<'b> = "gossamerInvoke" |
| 34 | + |
| 35 | +/// Tauri IPC: injected by the Tauri runtime into the webview. |
| 36 | +%%raw(` |
| 37 | +function isTauriRuntime() { |
| 38 | + return typeof window !== 'undefined' |
| 39 | + && window.__TAURI_INTERNALS__ != null |
| 40 | + && !window.__TAURI_INTERNALS__.__BROWSER_SHIM__; |
| 41 | +} |
| 42 | +`) |
| 43 | +@val external isTauriRuntime: unit => bool = "isTauriRuntime" |
| 44 | + |
| 45 | +@module("@tauri-apps/api/core") |
| 46 | +external tauriInvoke: (string, 'a) => promise<'b> = "invoke" |
| 47 | + |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | +// Unified invoke — detects runtime and dispatches |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | + |
| 52 | +/// The runtime currently in use. Cached after first detection for performance. |
| 53 | +type runtime = |
| 54 | + | Gossamer |
| 55 | + | Tauri |
| 56 | + | BrowserOnly |
| 57 | + |
| 58 | +%%raw(` |
| 59 | +var _detectedRuntime = null; |
| 60 | +function detectRuntime() { |
| 61 | + if (_detectedRuntime !== null) return _detectedRuntime; |
| 62 | + if (typeof window !== 'undefined' && typeof window.__gossamer_invoke === 'function') { |
| 63 | + _detectedRuntime = 'gossamer'; |
| 64 | + } else if (typeof window !== 'undefined' && window.__TAURI_INTERNALS__ != null && !window.__TAURI_INTERNALS__.__BROWSER_SHIM__) { |
| 65 | + _detectedRuntime = 'tauri'; |
| 66 | + } else { |
| 67 | + _detectedRuntime = 'browser'; |
| 68 | + } |
| 69 | + return _detectedRuntime; |
| 70 | +} |
| 71 | +`) |
| 72 | +@val external detectRuntimeRaw: unit => string = "detectRuntime" |
| 73 | + |
| 74 | +/// Detect and return the current runtime. |
| 75 | +let detectRuntime = (): runtime => { |
| 76 | + switch detectRuntimeRaw() { |
| 77 | + | "gossamer" => Gossamer |
| 78 | + | "tauri" => Tauri |
| 79 | + | _ => BrowserOnly |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Invoke a backend command through whatever runtime is available. |
| 84 | +let invoke = (cmd: string, args: 'a): promise<'b> => { |
| 85 | + if isGossamerRuntime() { |
| 86 | + gossamerInvoke(cmd, args) |
| 87 | + } else if isTauriRuntime() { |
| 88 | + tauriInvoke(cmd, args) |
| 89 | + } else { |
| 90 | + Promise.reject( |
| 91 | + JsError.throwWithMessage( |
| 92 | + `No desktop runtime — "${cmd}" requires Gossamer or Tauri`, |
| 93 | + ), |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Check whether any desktop runtime is available. |
| 99 | +let hasDesktopRuntime = (): bool => { |
| 100 | + isGossamerRuntime() || isTauriRuntime() |
| 101 | +} |
| 102 | + |
| 103 | +/// Get a human-readable name for the current runtime. |
| 104 | +let runtimeName = (): string => { |
| 105 | + switch detectRuntime() { |
| 106 | + | Gossamer => "Gossamer" |
| 107 | + | Tauri => "Tauri" |
| 108 | + | BrowserOnly => "Browser" |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// --------------------------------------------------------------------------- |
| 113 | +// Dialog abstraction — Gossamer dialogs vs Tauri plugin-dialog |
| 114 | +// --------------------------------------------------------------------------- |
| 115 | + |
| 116 | +module Dialog = { |
| 117 | + @module("@tauri-apps/plugin-dialog") |
| 118 | + external tauriOpenRaw: JSON.t => promise<Nullable.t<JSON.t>> = "open" |
| 119 | + |
| 120 | + @module("@tauri-apps/plugin-dialog") |
| 121 | + external tauriSaveRaw: JSON.t => promise<Nullable.t<JSON.t>> = "save" |
| 122 | + |
| 123 | + /// Open a file picker dialog. |
| 124 | + let open = (opts: JSON.t): promise<Nullable.t<JSON.t>> => { |
| 125 | + if isGossamerRuntime() { |
| 126 | + gossamerInvoke("__gossamer_dialog_open", opts) |
| 127 | + } else if isTauriRuntime() { |
| 128 | + tauriOpenRaw(opts) |
| 129 | + } else { |
| 130 | + Promise.reject( |
| 131 | + JsError.throwWithMessage( |
| 132 | + "No desktop runtime — file dialogs require Gossamer or Tauri", |
| 133 | + ), |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// Open a save dialog. |
| 139 | + let save = (opts: JSON.t): promise<Nullable.t<JSON.t>> => { |
| 140 | + if isGossamerRuntime() { |
| 141 | + gossamerInvoke("__gossamer_dialog_save", opts) |
| 142 | + } else if isTauriRuntime() { |
| 143 | + tauriSaveRaw(opts) |
| 144 | + } else { |
| 145 | + Promise.reject( |
| 146 | + JsError.throwWithMessage( |
| 147 | + "No desktop runtime — save dialogs require Gossamer or Tauri", |
| 148 | + ), |
| 149 | + ) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// --------------------------------------------------------------------------- |
| 155 | +// Filesystem abstraction — Gossamer fs vs Tauri plugin-fs |
| 156 | +// --------------------------------------------------------------------------- |
| 157 | + |
| 158 | +module Fs = { |
| 159 | + @module("@tauri-apps/plugin-fs") |
| 160 | + external tauriReadTextFileRaw: string => promise<string> = "readTextFile" |
| 161 | + |
| 162 | + @module("@tauri-apps/plugin-fs") |
| 163 | + external tauriWriteTextFileRaw: (string, string) => promise<unit> = "writeTextFile" |
| 164 | + |
| 165 | + /// Read a text file from the local filesystem. |
| 166 | + let readTextFile = (path: string): promise<string> => { |
| 167 | + if isGossamerRuntime() { |
| 168 | + gossamerInvoke("__gossamer_fs_read_text", {"path": path}) |
| 169 | + } else if isTauriRuntime() { |
| 170 | + tauriReadTextFileRaw(path) |
| 171 | + } else { |
| 172 | + Promise.reject( |
| 173 | + JsError.throwWithMessage( |
| 174 | + "No desktop runtime — filesystem access requires Gossamer or Tauri", |
| 175 | + ), |
| 176 | + ) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /// Write a text file to the local filesystem. |
| 181 | + let writeTextFile = (path: string, contents: string): promise<unit> => { |
| 182 | + if isGossamerRuntime() { |
| 183 | + gossamerInvoke("__gossamer_fs_write_text", {"path": path, "contents": contents}) |
| 184 | + } else if isTauriRuntime() { |
| 185 | + tauriWriteTextFileRaw(path, contents) |
| 186 | + } else { |
| 187 | + Promise.reject( |
| 188 | + JsError.throwWithMessage( |
| 189 | + "No desktop runtime — filesystem access requires Gossamer or Tauri", |
| 190 | + ), |
| 191 | + ) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +// --------------------------------------------------------------------------- |
| 197 | +// Utility — decode dialog path from either runtime's response format |
| 198 | +// --------------------------------------------------------------------------- |
| 199 | + |
| 200 | +/// Decode a dialog result into a file path string. |
| 201 | +/// Handles both single-path (String) and multi-path (Array) responses. |
| 202 | +let decodeDialogPath = (value: JSON.t): option<string> => { |
| 203 | + switch JSON.Classify.classify(value) { |
| 204 | + | String(path) => Some(path) |
| 205 | + | Array(arr) => |
| 206 | + switch Array.get(arr, 0) { |
| 207 | + | Some(item) => |
| 208 | + switch JSON.Classify.classify(item) { |
| 209 | + | String(s) => Some(s) |
| 210 | + | _ => None |
| 211 | + } |
| 212 | + | None => None |
| 213 | + } |
| 214 | + | _ => None |
| 215 | + } |
| 216 | +} |
0 commit comments