Skip to content

Commit 5ba43fc

Browse files
committed
New APIs
.focus() .setEventBlocking() .setCloseHandlerWv() .getWindowId() .waitAsync()
1 parent 18856c4 commit 5ba43fc

File tree

2 files changed

+142
-3
lines changed

2 files changed

+142
-3
lines changed

src/lib.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,42 @@ const symbols = {
442442
parameters: ["usize"],
443443
result: "usize",
444444
},
445+
webui_focus: {
446+
// void webui_focus(size_t window)
447+
parameters: ["usize"],
448+
result: "void",
449+
},
450+
webui_wait_async: {
451+
// bool webui_wait_async(void)
452+
parameters: [],
453+
result: "bool",
454+
nonblocking: true,
455+
},
456+
webui_get_count: {
457+
// size_t webui_get_count(webui_event_t* e)
458+
parameters: ["pointer"],
459+
result: "usize",
460+
},
461+
webui_return_int: {
462+
// void webui_return_int(webui_event_t* e, long long int n)
463+
parameters: ["pointer", "i64"],
464+
result: "void",
465+
},
466+
webui_return_float: {
467+
// void webui_return_float(webui_event_t* e, double f)
468+
parameters: ["pointer", "f64"],
469+
result: "void",
470+
},
471+
webui_return_string: {
472+
// void webui_return_string(webui_event_t* e, const char* s)
473+
parameters: ["pointer", "buffer"],
474+
result: "void",
475+
},
476+
webui_return_bool: {
477+
// void webui_return_bool(webui_event_t* e, bool b)
478+
parameters: ["pointer", "bool"],
479+
result: "void",
480+
},
445481
} as const;
446482

447483
export function loadLib(): Deno.DynamicLibrary<typeof symbols> {

src/webui.ts

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ export class WebUI {
236236
this.#lib.symbols.webui_maximize(BigInt(this.#window));
237237
}
238238

239+
/**
240+
* Bring the window to the front and focus it.
241+
*
242+
* @example
243+
* ```ts
244+
* const myWindow = new WebUI();
245+
* myWindow.focus();
246+
* ```
247+
*/
248+
focus(): void {
249+
this.#lib.symbols.webui_focus(BigInt(this.#window));
250+
}
251+
239252
/**
240253
* Execute a JavaScript string in the UI and returns a boolean indicating whether the
241254
* script execution was successful.
@@ -782,11 +795,11 @@ export class WebUI {
782795
}
783796

784797
/**
785-
* Chose between Deno and Nodejs as runtime for .js and .ts files.
798+
* Choose the JavaScript runtime for `.js` and `.ts` files.
786799
*
787-
* @param runtime - The runtime value.
800+
* @param runtime - The runtime to use (see `WebUI.Runtime`).
788801
*/
789-
setRuntime(runtime: number): void {
802+
setRuntime(runtime: WebUI.Runtime): void {
790803
this.#lib.symbols.webui_set_runtime(BigInt(this.#window), BigInt(runtime));
791804
}
792805

@@ -948,6 +961,68 @@ export class WebUI {
948961
);
949962
}
950963

964+
/**
965+
* Control if UI events from this window are processed one at a time in a
966+
* single blocking thread (`true`), or in a new non-blocking thread per
967+
* event (`false`).
968+
*
969+
* @param status - `true` to block, `false` for non-blocking (default).
970+
* @example
971+
* ```ts
972+
* myWindow.setEventBlocking(true);
973+
* ```
974+
*/
975+
setEventBlocking(status: boolean): void {
976+
this.#lib.symbols.webui_set_event_blocking(BigInt(this.#window), status);
977+
}
978+
979+
/**
980+
* Set a callback to intercept the WebView window close event.
981+
* Return `false` from the handler to prevent the window from closing.
982+
*
983+
* @param handler - Called with the `WebUI` instance being closed.
984+
* Return `true` to allow closing, `false` to prevent it.
985+
* @example
986+
* ```ts
987+
* myWindow.setCloseHandlerWv((win) => {
988+
* console.log("close requested");
989+
* return false; // prevent close
990+
* });
991+
* ```
992+
*/
993+
setCloseHandlerWv(handler: (window: WebUI) => boolean): void {
994+
const callbackResource = new Deno.UnsafeCallback(
995+
{
996+
parameters: ["usize"],
997+
result: "bool",
998+
} as const,
999+
(win: number | bigint) => {
1000+
const w = windows.get(typeof win === "bigint" ? win : BigInt(win));
1001+
if (w === undefined) return true;
1002+
return handler(w);
1003+
},
1004+
);
1005+
this.#lib.symbols.webui_set_close_handler_wv(
1006+
BigInt(this.#window),
1007+
callbackResource.pointer,
1008+
);
1009+
}
1010+
1011+
/**
1012+
* Get the unique window ID.
1013+
*
1014+
* @return The unique window ID as a number.
1015+
* @example
1016+
* ```ts
1017+
* const id = myWindow.getWindowId();
1018+
* ```
1019+
*/
1020+
getWindowId(): number {
1021+
return Number(
1022+
this.#lib.symbols.webui_interface_get_window_id(BigInt(this.#window)),
1023+
);
1024+
}
1025+
9511026
// Static methods
9521027

9531028
/**
@@ -1051,6 +1126,24 @@ export class WebUI {
10511126
_lib.symbols.webui_set_config(BigInt(2), status);
10521127
}
10531128

1129+
/**
1130+
* Wait asynchronously until all opened windows get closed, yielding control
1131+
* back to the caller on each iteration. Useful for WebView mode where you
1132+
* need to run code on the main thread.
1133+
*
1134+
* @returns `true` if windows are still open, `false` when all are closed.
1135+
* @example
1136+
* ```ts
1137+
* while (await WebUI.waitAsync()) {
1138+
* // main thread work here
1139+
* }
1140+
* ```
1141+
*/
1142+
static async waitAsync(): Promise<boolean> {
1143+
WebUI.init();
1144+
return await _lib.symbols.webui_wait_async();
1145+
}
1146+
10541147
// --[ Static Methods ]------------------------
10551148

10561149
/**
@@ -1286,6 +1379,7 @@ export namespace WebUI {
12861379
Epic, // 10. The Epic Browser
12871380
Yandex, // 11. The Yandex Browser
12881381
ChromiumBased, // 12. Any Chromium based browser
1382+
Webview, // 13. WebView (non-web-browser)
12891383
}
12901384
/**
12911385
* Enum representing the types of events WebUI can handle.
@@ -1297,4 +1391,13 @@ export namespace WebUI {
12971391
Navigation, // 3. Window navigation event
12981392
Callback, // 4. Function call event
12991393
}
1394+
/**
1395+
* Enum for the JavaScript runtime used to execute `.js` and `.ts` files.
1396+
*/
1397+
export enum Runtime {
1398+
None = 0, // 0. No runtime
1399+
Deno, // 1. Deno
1400+
NodeJS, // 2. Node.js
1401+
Bun, // 3. Bun
1402+
}
13001403
}

0 commit comments

Comments
 (0)