summaryrefslogtreecommitdiff
path: root/js/gjs/application.ts
blob: 32dcda7ea57e536eb60ad1accfb1f4a84cef0f6d (plain)
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
import Astal from "gi://Astal"
import GObject from "gi://GObject"
import Gio from "gi://Gio"
import { RequestHandler, Config, runJS } from "../src/application.js"

// @ts-expect-error missing types
// https://github.com/gjsify/ts-for-gir/issues/164
import { setConsoleLogDomain } from "console"
import { exit, programArgs } from "system"

class AstalJS extends Astal.Application {
    static { GObject.registerClass(this) }

    eval = runJS
    requestHandler?: RequestHandler

    vfunc_response(msg: string, conn: Gio.SocketConnection): void {
        if (typeof this.requestHandler === "function") {
            this.requestHandler(msg, response => {
                Astal.write_sock(conn, response, (_, res) =>
                    Astal.write_sock_finish(res),
                )
            })
        } else {
            super.vfunc_response(msg, conn)
        }
    }

    start({ requestHandler, css, hold, ...cfg }: Config = {}, callback?: (args: string[]) => void) {
        Object.assign(this, cfg)
        setConsoleLogDomain(this.instanceName)

        this.requestHandler = requestHandler
        this.connect("activate", () => callback?.(programArgs))
        if (!this.acquire_socket()) {
            print(`Astal instance "${this.instanceName}" already running`)
            exit(1)
        }

        if (css)
            this.apply_css(css, false)

        hold ??= true
        if (hold)
            this.hold()

        this.runAsync([])
    }
}

export const App = new AstalJS