|
4 | 4 | You are currently viewing an unpublished version of the WebUI MX Engine API. This interface is subject to change at any time, including during internal development, alpha stages, and release candidate phases. |
5 | 5 | ::: |
6 | 6 |
|
7 | | -TODO |
| 7 | +::: details TypeScript Type Definition |
| 8 | +```ts |
| 9 | +/** |
| 10 | + * Metadata describing the host Android application. |
| 11 | + */ |
| 12 | +interface Application { |
| 13 | + /** The application's unique package identifier (e.g. `"com.example.app"`). */ |
| 14 | + packageName: string; |
| 15 | + |
| 16 | + /** The human-readable version name (e.g. `"1.4.2"`). */ |
| 17 | + version: string; |
| 18 | + |
| 19 | + /** The internal integer version code used for update comparisons. */ |
| 20 | + versionCode: number; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Labels for the action buttons shown in a {@link DialogOptions} or {@link PromptOptions} dialog. |
| 25 | + */ |
| 26 | +interface DialogButtons { |
| 27 | + /** |
| 28 | + * Label for the primary/confirm button. |
| 29 | + * |
| 30 | + * @default "Confirm" |
| 31 | + */ |
| 32 | + confirmText: string; |
| 33 | + |
| 34 | + /** |
| 35 | + * Label for the secondary/cancel button. |
| 36 | + * |
| 37 | + * @default "Cancel" |
| 38 | + */ |
| 39 | + cancelText: string; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Base configuration shared by all dialog types. |
| 44 | + * |
| 45 | + * Pass this to {@link WebUI.confirm} to show a simple yes/no dialog. |
| 46 | + * For an input dialog, use the extended {@link PromptOptions} instead. |
| 47 | + */ |
| 48 | +interface DialogOptions { |
| 49 | + /** |
| 50 | + * The dialog's heading text. |
| 51 | + * |
| 52 | + * @default "Confirm" |
| 53 | + */ |
| 54 | + title: string; |
| 55 | + |
| 56 | + /** |
| 57 | + * Optional body text displayed beneath the title. |
| 58 | + * |
| 59 | + * @throws If the message was null |
| 60 | + */ |
| 61 | + message: string; |
| 62 | + |
| 63 | + /** |
| 64 | + * Custom labels for the confirm and cancel buttons. |
| 65 | + * |
| 66 | + * @see DialogButtons |
| 67 | + */ |
| 68 | + buttons: DialogButtons; |
| 69 | + |
| 70 | + /** |
| 71 | + * Material Design theme variant applied to the dialog. |
| 72 | + * |
| 73 | + * @default "md3" |
| 74 | + */ |
| 75 | + theme: string; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Configuration for a dialog that collects a text value from the user. |
| 80 | + * |
| 81 | + * Extends {@link DialogOptions} with input-specific settings. |
| 82 | + * Pass this to {@link WebUI.prompt}. |
| 83 | + * |
| 84 | + * @example |
| 85 | + * ```ts |
| 86 | + * const name = await webui.prompt({ |
| 87 | + * title: "Enter your name", |
| 88 | + * message: null, |
| 89 | + * default: "", |
| 90 | + * launchKeyboard: true, |
| 91 | + * buttons: { confirmText: "OK", cancelText: "Skip" }, |
| 92 | + * theme: "md3", |
| 93 | + * }); |
| 94 | + * ``` |
| 95 | + */ |
| 96 | +interface PromptOptions extends DialogOptions { |
| 97 | + /** |
| 98 | + * Pre-filled value shown in the input field when the dialog opens. |
| 99 | + * |
| 100 | + * @default "" |
| 101 | + */ |
| 102 | + default: string; |
| 103 | + |
| 104 | + /** |
| 105 | + * Whether to open the soft keyboard automatically when the dialog appears. |
| 106 | + * Set to `false` to suppress the keyboard on devices where it would obscure the UI. |
| 107 | + * |
| 108 | + * @default true |
| 109 | + */ |
| 110 | + launchKeyboard: boolean; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * The `webui` global exposes the native Android host's capabilities to the |
| 115 | + * embedded web page. All async methods return Promises that resolve once the |
| 116 | + * corresponding native operation completes. |
| 117 | + * |
| 118 | + * @example |
| 119 | + * ```ts |
| 120 | + * if (webui.isDarkMode) { |
| 121 | + * document.documentElement.classList.add("dark"); |
| 122 | + * } |
| 123 | + * ``` |
| 124 | + * |
| 125 | + * @remarks |
| 126 | + * Some members are marked **subject to change** — their name or signature may |
| 127 | + * be altered in a future release without a major version bump. |
| 128 | + */ |
| 129 | +interface WebUI { |
| 130 | + /** |
| 131 | + * The Android platform SDK integer (e.g. `34` for Android 14). |
| 132 | + * |
| 133 | + * @remarks **Subject to change** — may be renamed to `platformSdk` in a future release. |
| 134 | + */ |
| 135 | + buildSdk: number; |
| 136 | + |
| 137 | + /** |
| 138 | + * Metadata about the Android application hosting this WebUI. |
| 139 | + * |
| 140 | + * @see Application |
| 141 | + */ |
| 142 | + currentApplication: Application; |
| 143 | + |
| 144 | + /** |
| 145 | + * `true` when the user has a home-screen shortcut pointing to this WebUI. |
| 146 | + * |
| 147 | + * @remarks **Subject to change** — not yet implemented; value is always `false`. |
| 148 | + */ |
| 149 | + hasShortcut: boolean; |
| 150 | + |
| 151 | + /** |
| 152 | + * `true` when the device or app is using a dark color scheme. |
| 153 | + * Use this to synchronize the web page's theme with the host UI. |
| 154 | + */ |
| 155 | + isDarkMode: boolean; |
| 156 | + |
| 157 | + /** |
| 158 | + * `true` when the host application is running in developer/debug mode. |
| 159 | + * Can be used to enable verbose logging or expose debug tooling in the UI. |
| 160 | + */ |
| 161 | + isDebug: boolean; |
| 162 | + |
| 163 | + /** |
| 164 | + * Resolves to `true` if the WebView has a page in its back history. |
| 165 | + * |
| 166 | + * @returns A Promise that resolves to a boolean. |
| 167 | + */ |
| 168 | + canGoBack(): Promise<boolean>; |
| 169 | + |
| 170 | + /** |
| 171 | + * Resolves to `true` if the WebView can navigate the given number of steps |
| 172 | + * through its history (positive = forward, negative = backward). |
| 173 | + * |
| 174 | + * @param steps - Number of history steps to check. Use negative values to |
| 175 | + * check backward navigation (e.g. `-2` checks two steps back). |
| 176 | + * @returns A Promise that resolves to a boolean. |
| 177 | + */ |
| 178 | + canGoBackOrForward(steps: number): Promise<boolean>; |
| 179 | + |
| 180 | + /** |
| 181 | + * Resolves to `true` if the WebView has a page in its forward history. |
| 182 | + * |
| 183 | + * @returns A Promise that resolves to a boolean. |
| 184 | + */ |
| 185 | + canGoForward(): Promise<boolean>; |
| 186 | + |
| 187 | + /** |
| 188 | + * Pins a home-screen shortcut for this WebUI on the device launcher. |
| 189 | + * |
| 190 | + * @remarks **Subject to change** — not yet implemented. The return type will |
| 191 | + * change to `Promise<boolean>` to indicate whether the shortcut was |
| 192 | + * successfully created. |
| 193 | + */ |
| 194 | + createShortcut(): void; |
| 195 | + |
| 196 | + /** |
| 197 | + * Closes the WebUI and returns control to the host application or launcher. |
| 198 | + */ |
| 199 | + exit(): void; |
| 200 | + |
| 201 | + /** |
| 202 | + * Opens the Android share sheet so the user can send text to another app. |
| 203 | + * |
| 204 | + * @param text - The content to share. |
| 205 | + * @param mimeType - MIME type of the content. Defaults to `"text/plain"`. |
| 206 | + * |
| 207 | + * @example |
| 208 | + * ```ts |
| 209 | + * webui.shareText("https://example.com", "text/plain"); |
| 210 | + * ``` |
| 211 | + */ |
| 212 | + shareText(text: string, mimeType?: string): void; |
| 213 | + |
| 214 | + /** |
| 215 | + * Shows a native confirmation dialog and resolves to `true` if the user |
| 216 | + * tapped the confirm button, or `false` if they tapped cancel or dismissed |
| 217 | + * the dialog. |
| 218 | + * |
| 219 | + * @param options - Dialog configuration. See {@link DialogOptions}. |
| 220 | + * @returns A Promise that resolves to `true` (confirmed) or `false` (cancelled). |
| 221 | + * |
| 222 | + * @example |
| 223 | + * ```ts |
| 224 | + * const ok = await webui.confirm({ |
| 225 | + * title: "Delete item?", |
| 226 | + * message: "This action cannot be undone.", |
| 227 | + * buttons: { confirmText: "Delete", cancelText: "Keep" }, |
| 228 | + * theme: "md3", |
| 229 | + * }); |
| 230 | + * if (ok) deleteItem(); |
| 231 | + * ``` |
| 232 | + * |
| 233 | + * @remarks **Unstable** — The current implementaion isn't stable enough |
| 234 | + */ |
| 235 | + confirm(options: DialogOptions): Promise<boolean>; |
| 236 | + |
| 237 | + /** |
| 238 | + * Shows a native text-input dialog and resolves to the string the user |
| 239 | + * entered, or an empty string if they cancelled. |
| 240 | + * |
| 241 | + * @param options - Dialog and input configuration. See {@link PromptOptions}. |
| 242 | + * @param onValid - Optional callback invoked with the current input value |
| 243 | + * whenever it changes. Return `true` to enable the confirm button, `false` |
| 244 | + * to disable it (e.g. for live validation). |
| 245 | + * @returns A Promise that resolves to the submitted string, or `null` if cancelled. |
| 246 | + * |
| 247 | + * @example |
| 248 | + * ```ts |
| 249 | + * const email = await webui.prompt( |
| 250 | + * { |
| 251 | + * title: "Enter your email", |
| 252 | + * message: null, |
| 253 | + * default: "", |
| 254 | + * launchKeyboard: true, |
| 255 | + * buttons: { confirmText: "Submit", cancelText: "Cancel" }, |
| 256 | + * theme: "md3", |
| 257 | + * }, |
| 258 | + * () => /\S+@\S+\.\S+/.test(currentInput), |
| 259 | + * ); |
| 260 | + * ``` |
| 261 | + * |
| 262 | + * @remarks **Unstable** — The current implementaion isn't stable enough |
| 263 | + */ |
| 264 | + prompt(options: PromptOptions, onValid?: () => boolean): Promise<string | null>; |
| 265 | +} |
| 266 | + |
| 267 | +/** |
| 268 | + * The global `webui` object injected by the Android host into `window`. |
| 269 | + * |
| 270 | + * Always check that `typeof webui !== "undefined"` before use if your page |
| 271 | + * can also run outside the host application (e.g. in a standard browser). |
| 272 | + * |
| 273 | + * @example |
| 274 | + * ```ts |
| 275 | + * if (typeof webui !== "undefined" && webui.isDarkMode) { |
| 276 | + * document.documentElement.classList.add("dark"); |
| 277 | + * } |
| 278 | + * ``` |
| 279 | + */ |
| 280 | +declare var webui: WebUI; |
| 281 | +``` |
| 282 | +::: |
0 commit comments