-
Notifications
You must be signed in to change notification settings - Fork 1
/
wry.ts
65 lines (51 loc) · 1.51 KB
/
wry.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
import { sync, unwrap } from "./plugin.ts";
type Event = {
event: string
}
export type Size = { physical: PhysicalSize } | { logical: LogicalSize };
export type PhysicalSize = { width: number; height: number };
export type LogicalSize = { width: number; height: number };
export class Wry {
readonly id: bigint;
constructor(url: string) {
this.id = unwrap(sync("wry_new", { url }));
}
set_minimized(minimized: boolean): boolean {
return unwrap(sync("wry_set_minimized", { id: this.id, minimized }));
}
set_maximized(maximized: boolean): boolean {
return unwrap(sync("wry_set_maximized", { id: this.id, maximized }));
}
set_visible(visible: boolean): boolean {
return unwrap(sync("wry_set_visible", { id: this.id, visible }));
}
set_inner_size(size: Size): void {
unwrap(sync("wry_set_inner_size", { id: this.id, size }));
}
loop(): boolean {
return unwrap(sync("wry_loop", { id: this.id })) === false;
}
step(): Event[] {
return unwrap(sync("wry_step", { id: this.id }));
}
run(
callback?: (events: Event) => void,
delta = 1,
): Promise<void> {
return new Promise((resolve) => {
const interval = setInterval(() => {
const success = this.loop();
if (callback !== undefined) {
const events = this.step();
for (const event of events) {
callback(event);
}
}
if (!success) {
resolve();
clearInterval(interval);
}
}, delta);
});
}
}