diff options
author | Aylur <[email protected]> | 2024-05-19 02:39:53 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-05-19 02:39:53 +0200 |
commit | 1425b396b08f0e91d45bbd0f92b1309115c7c870 (patch) | |
tree | 8af1a899a14d8a01a9ef50e248c077b48aed25bc /js/gjs |
init 0.1.0
Diffstat (limited to 'js/gjs')
-rw-r--r-- | js/gjs/application.ts | 51 | ||||
-rw-r--r-- | js/gjs/astal.ts | 37 | ||||
-rw-r--r-- | js/gjs/widgets.ts | 60 |
3 files changed, 148 insertions, 0 deletions
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 diff --git a/js/gjs/astal.ts b/js/gjs/astal.ts new file mode 100644 index 0000000..0665498 --- /dev/null +++ b/js/gjs/astal.ts @@ -0,0 +1,37 @@ +import Astal from "gi://Astal" +import Time from "../src/time.js" +import Process from "../src/process.js" +import * as variable from "../src/variable.js" + +const { interval, timeout, idle } = Time(Astal.Time) +const { subprocess, exec, execAsync } = Process({ + defaultOut: print, + defaultErr: console.error, + exec: Astal.Process.exec, + execv: Astal.Process.execv, + execAsync: Astal.Process.exec_async, + execAsyncv: Astal.Process.exec_asyncv, + subprocess: Astal.Process.subprocess, + subprocessv: Astal.Process.subprocessv, +}) + +variable.config.defaultErrHandler = print +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 { default as GLib } from "gi://GLib?version=2.0" +export { default as Gtk } from "gi://Gtk?version=3.0" +export { default as Gio } from "gi://Gio?version=2.0" +export { default as GObject } from "gi://GObject?version=2.0" +export { default as Astal } from "gi://Astal?version=0.1" diff --git a/js/gjs/widgets.ts b/js/gjs/widgets.ts new file mode 100644 index 0000000..29af9a2 --- /dev/null +++ b/js/gjs/widgets.ts @@ -0,0 +1,60 @@ +/* eslint-disable max-len */ +import Gtk from "gi://Gtk" +import Astal from "gi://Astal" +import { kebabify } from "../src/binding.js" +import proxy, { type ConstructProps, type Widget } from "../src/astalify.js" + +const proxify = proxy(Gtk, + prop => `set_${kebabify(prop).replaceAll("-", "_")}`, + { + cssGetter: Astal.widget_get_css, + cssSetter: Astal.widget_set_css, + classGetter: Astal.widget_get_class_names, + classSetter: Astal.widget_set_class_names, + cursorGetter: Astal.widget_get_cursor, + cursorSetter: Astal.widget_set_cursor, + }) + +export function astalify< + C extends typeof Gtk.Widget, + P extends Record<string, any>, + N extends string = "Widget", +>(klass: C) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + type Astal<N> = Omit<C, "new"> & { + new(props?: P, ...children: Gtk.Widget[]): Widget<C> + (props?: P, ...children: Gtk.Widget[]): Widget<C> + } + + return proxify(klass) as unknown as Astal<N> +} + +// Label +export const Label = astalify<typeof Gtk.Label, LabelProps, "Label">(Gtk.Label) +export type LabelProps = ConstructProps<typeof Gtk.Label, Gtk.Label.ConstructorProperties> + +// Icon +export const Icon = astalify<typeof Astal.Icon, IconProps, "Icon">(Astal.Icon) +export type IconProps = ConstructProps<typeof Astal.Icon, Astal.Icon.ConstructorProperties> + +// Button +export const Button = astalify<typeof Astal.Button, ButtonProps, "Button">(Astal.Button) +export type ButtonProps = ConstructProps<typeof Astal.Button, Astal.Button.ConstructorProperties, { + onClicked: (self: Widget<typeof Astal.Button>) => void +}> + +// Window +export const Window = astalify<typeof Astal.Window, WindowProps, "Window">(Astal.Window) +export type WindowProps = ConstructProps<typeof Astal.Window, Astal.Window.ConstructorProperties> + +// Box +export const Box = astalify<typeof Astal.Box, BoxProps, "Box">(Astal.Box) +export type BoxProps = ConstructProps<typeof Astal.Box, Astal.Box.ConstructorProperties> + +// CenterBox +export const CenterBox = astalify<typeof Astal.CenterBox, CenterBoxProps, "CenterBox">(Astal.CenterBox) +export type CenterBoxProps = ConstructProps<typeof Astal.CenterBox, Astal.CenterBox.ConstructorProperties> + +// EventBox +export const EventBox = astalify<typeof Astal.EventBox, EventBoxProps, "EventBox">(Astal.EventBox) +export type EventBoxProps = ConstructProps<typeof Astal.EventBox, Astal.EventBox.ConstructorProperties> |