summaryrefslogtreecommitdiff
path: root/examples/gtk4/simple-bar/js/src/ts/App.ts
blob: 012fafa59638e6b75d5be207bad9385ff6ffc435 (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
52
import GObject from "gi://GObject"
import Astal from "gi://Astal?version=4.0"
import Gio from "gi://Gio"
import GLib from "gi://GLib"
import AstalIO from "gi://AstalIO"
import Bar from "./Bar"

export default class App extends Astal.Application {
    static {
        GObject.registerClass(this)
    }

    static instance: App
    static instanceName = "simple-bar"

    // this is where request handlers can be implemented
    // that will be used to handle `astal` cli invocations
    vfunc_request(request: string, conn: Gio.SocketConnection): void {
        print("incoming request", request)
        AstalIO.write_sock(conn, "response", null)
    }

    // this is the method that will be invoked on `app.runAsync()`
    // this is where everything should be initialized and instantiated
    vfunc_activate(): void {
        this.apply_css("resource:///main.css", false)
        this.add_window(new Bar())
    }

    // entry point of our app
    static async main(argv: string[]): Promise<number> {
        GLib.set_prgname(App.instanceName)
        App.instance = new App({ instanceName: App.instanceName })

        try {
            // `app.acquire_socket()` needed for the request API to work
            App.instance.acquire_socket()

            // if it succeeds we can run the app
            return await App.instance.runAsync([])
        } catch (error) {
            // if it throws an error it means there is already an instance
            // with `instanceName` running, so we just send a request instead
            const response = AstalIO.send_request(
                App.instanceName,
                argv.join(" "),
            )
            print(response)
            return 0
        }
    }
}