forked from webview/webview_deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
111 lines (99 loc) · 1.84 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
WebViewNew,
WebViewNewParams,
WebViewRun,
WebViewLoop,
WebViewExit,
WebViewEval,
WebViewSetColor,
WebViewSetTitle,
WebViewSetFullscreen,
} from "./plugin.ts";
const DEFAULT_PARAMS: WebViewNewParams = {
title: "webview_deno",
url: "about:blank",
width: 800,
height: 600,
resizable: true,
debug: true,
frameless: false,
};
/**
* The constructor parameters
*/
export type WebViewParams = Partial<WebViewNewParams>;
/**
* A rgb(a) color
*/
export interface WebViewColor {
r: number;
g: number;
b: number;
a?: number;
}
/**
* A WebView instance
*/
export class WebView {
readonly #id: number = 0;
constructor(params: WebViewParams) {
this.#id = WebViewNew({ ...DEFAULT_PARAMS, ...params }).id;
}
/**
* Runs the event loop to completion
*/
public async run() {
await WebViewRun({ id: this.#id });
}
/**
* Iterates the event loop and returns `false` if the the `WebView` has been closed
*/
public step(): boolean {
return WebViewLoop({ id: this.#id, blocking: 1 }).code === 0;
}
/**
* Exits the `WebView`
*/
public exit() {
WebViewExit({ id: this.#id });
}
/**
* Evaluates the provided js code in the `WebView`
*/
public eval(js: string) {
WebViewEval({
id: this.#id,
js: js,
});
}
/**
* Sets the color of the title bar
*/
public setColor(color: WebViewColor) {
WebViewSetColor({
id: this.#id,
r: color.r,
g: color.g,
b: color.b,
a: color.a ?? 1,
});
}
/**
* Sets the window title
*/
public setTitle(title: string) {
WebViewSetTitle({
id: this.#id,
title: title,
});
}
/**
* Enables or disables fullscreen
*/
public setFullscreen(fullscreen: boolean) {
WebViewSetFullscreen({
id: this.#id,
fullscreen: fullscreen,
});
}
}