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/node/application.ts | 51 ++++++++++++++++++++++++++++++++++++++++ js/node/astal.ts | 38 ++++++++++++++++++++++++++++++ js/node/widgets.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 js/node/application.ts create mode 100644 js/node/astal.ts create mode 100644 js/node/widgets.ts (limited to 'js/node') diff --git a/js/node/application.ts b/js/node/application.ts new file mode 100644 index 0000000..875c1c8 --- /dev/null +++ b/js/node/application.ts @@ -0,0 +1,51 @@ +import gi from "node-gtk" +import { RequestHandler, Config, runJS } from "../src/application.js" +const Astal = gi.require("Astal", "0.1") + +class AstalJS extends Astal.Application { + static GTypeName = "AstalJS" + static { gi.registerClass(this) } + + eval = runJS + requestHandler?: RequestHandler + + vfunc_response(msg: string, conn: any): void { + if (typeof this.requestHandler === "function") { + this.requestHandler(msg, response => { + Astal.writeSock(conn, response, (_, res) => + Astal.writeSockFinish(res), + ) + }) + } else { + // @ts-expect-error missing type + super.vfunc_response(msg, conn) + } + } + + start( + { requestHandler, css, ...cfg }: Omit = {}, + callback?: (args: string[]) => any, + ) { + Object.assign(this, cfg) + + this.requestHandler = requestHandler + this.on("activate", () => { + callback?.(process.argv) + }) + + if (!this.acquireSocket()) { + console.error(`Astal instance "${this.instanceName}" already running`) + process.exit() + } + + if (css) + this.applyCss(css, false) + + // FIXME: promises never resolve + // https://github.com/romgrk/node-gtk/issues/121 + // https://gitlab.gnome.org/GNOME/gjs/-/issues/468 + App.run([]) + } +} + +export const App = new AstalJS diff --git a/js/node/astal.ts b/js/node/astal.ts new file mode 100644 index 0000000..9560a81 --- /dev/null +++ b/js/node/astal.ts @@ -0,0 +1,38 @@ +import gi from "node-gtk" +import Time from "../src/time.js" +import Process from "../src/process.js" +import * as variable from "../src/variable.js" +const Astal = gi.require("Astal", "0.1") + +const { interval, timeout, idle } = Time(Astal.Time) +const { subprocess, exec, execAsync } = Process({ + defaultOut: console.log, + defaultErr: console.error, + exec: Astal.Process.exec, + execv: Astal.Process.execv, + execAsync: Astal.Process.execAsync, + execAsyncv: Astal.Process.execAsyncv, + subprocess: Astal.Process.subprocess, + subprocessv: Astal.Process.subprocessv, +}) + +variable.config.defaultErrHandler = console.log +variable.config.execAsync = execAsync +variable.config.subprocess = subprocess +variable.config.interval = interval +variable.config.VariableBase = Astal.VariableBase +Object.freeze(variable.config) + +export { subprocess, exec, execAsync } +export { interval, timeout, idle } +export { bind } from "../src/binding.js" +export { Variable } from "../src/variable.js" +export * as Widget from "./widgets.js" +export { App } from "./application.js" + +// for convinience +export const GLib = gi.require("GLib", "2.0") +export const Gtk = gi.require("Gtk", "3.0") +export const Gio = gi.require("Gio", "2.0") +export const GObject = gi.require("GObject", "2.0") +export { Astal, gi } diff --git a/js/node/widgets.ts b/js/node/widgets.ts new file mode 100644 index 0000000..914821d --- /dev/null +++ b/js/node/widgets.ts @@ -0,0 +1,63 @@ +/* eslint-disable max-len */ +import gi from "node-gtk" +import proxy, { type ConstructProps, type Widget } from "../src/astalify.js" +import type GtkT from "@girs/node-gtk-3.0/node-gtk-3.0" +import type AstalT from "@girs/node-astal-0.1/node-astal-0.1" + +const Astal = gi.require("Astal", "0.1") +const Gtk = gi.require("Gtk", "3.0") + +const proxify = proxy(Gtk, + prop => `set${prop.charAt(0).toUpperCase() + prop.slice(1)}`, + { + cssGetter: Astal.widgetGetCss, + cssSetter: Astal.widgetSetCss, + classGetter: Astal.widgetGetClassNames, + classSetter: Astal.widgetSetClassNames, + cursorGetter: Astal.widgetGetCursor, + cursorSetter: Astal.widgetSetCursor, + }) + +export function astalify< + C extends typeof Gtk.Widget, + P extends Record, + N extends string = "Widget", +>(klass: C) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + type Astal = Omit & { + new(props: P, ...children: GtkT.Widget[]): Widget + (props: P, ...children: GtkT.Widget[]): Widget + } + + return proxify(klass) as unknown as Astal +} + +// Label +export const Label = astalify(Gtk.Label) +export type LabelProps = ConstructProps + +// Icon +export const Icon = astalify(Astal.Icon) +export type IconProps = ConstructProps + +// Button +export const Button = astalify(Astal.Button) +export type ButtonProps = ConstructProps) => void +}> + +// Window +export const Window = astalify(Astal.Window) +export type WindowProps = ConstructProps + +// Box +export const Box = astalify(Astal.Box) +export type BoxProps = ConstructProps + +// CenterBox +export const CenterBox = astalify(Astal.CenterBox) +export type CenterBoxProps = ConstructProps + +// EventBox +export const EventBox = astalify(Astal.EventBox) +export type EventBoxProps = ConstructProps -- cgit v1.2.3