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
|
import { Astal, GObject, Gio, GLib } from "./imports.js"
type RequestHandler = {
(request: string, res: (response: any) => void): void
}
type Config = Partial<{
icons: string
instanceName: string
gtkTheme: string
iconTheme: string
cursorTheme: string
css: string
requestHandler: RequestHandler
main(...args: string[]): void
client(message: (msg: string) => string, ...args: string[]): void
hold: boolean
}>
// @ts-expect-error missing types
// https://github.com/gjsify/ts-for-gir/issues/164
import { setConsoleLogDomain } from "console"
import { exit, programArgs } from "system"
export default new (class AstalJS extends Astal.Application {
static { GObject.registerClass(this) }
eval(body: string): Promise<any> {
return new Promise((res, rej) => {
try {
const fn = Function(`return (async function() {
${body.includes(";") ? body : `return ${body};`}
})`)
fn()()
.then(res)
.catch(rej)
}
catch (error) {
rej(error)
}
})
}
requestHandler?: RequestHandler
vfunc_request(msg: string, conn: Gio.SocketConnection): void {
if (typeof this.requestHandler === "function") {
this.requestHandler(msg, (response) => {
Astal.write_sock(conn, String(response), (_, res) =>
Astal.write_sock_finish(res),
)
})
}
else {
super.vfunc_request(msg, conn)
}
}
apply_css(style: string, reset = false) {
super.apply_css(style, reset)
}
quit(code?: number): void {
super.quit()
exit(code ?? 0)
}
start({ requestHandler, css, hold, main, client, icons, ...cfg }: Config = {}) {
client ??= () => {
print(`Astal instance "${this.instanceName}" already running`)
exit(1)
}
Object.assign(this, cfg)
setConsoleLogDomain(this.instanceName)
this.requestHandler = requestHandler
this.connect("activate", () => {
const path: string[] = import.meta.url.split("/").slice(3)
const file = path.at(-1)!.replace(".js", ".css")
const css = `/${path.slice(0, -1).join("/")}/${file}`
if (file.endsWith(".css") && GLib.file_test(css, GLib.FileTest.EXISTS))
this.apply_css(css, false)
main?.(...programArgs)
})
if (!this.acquire_socket())
return client(msg => Astal.Application.send_message(this.instanceName, msg)!, ...programArgs)
if (css)
this.apply_css(css, false)
if (icons)
this.add_icons(icons)
hold ??= true
if (hold)
this.hold()
this.runAsync([])
}
})
|