From 1425b396b08f0e91d45bbd0f92b1309115c7c870 Mon Sep 17 00:00:00 2001 From: Aylur Date: Sun, 19 May 2024 02:39:53 +0200 Subject: init 0.1.0 --- js/gjs/application.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 js/gjs/application.ts (limited to 'js/gjs/application.ts') diff --git a/js/gjs/application.ts b/js/gjs/application.ts new file mode 100644 index 0000000..32dcda7 --- /dev/null +++ b/js/gjs/application.ts @@ -0,0 +1,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 -- cgit v1.2.3