diff options
Diffstat (limited to 'core/gjs/src/astalify.ts')
-rw-r--r-- | core/gjs/src/astalify.ts | 452 |
1 files changed, 214 insertions, 238 deletions
diff --git a/core/gjs/src/astalify.ts b/core/gjs/src/astalify.ts index a568a50..c4cbc5c 100644 --- a/core/gjs/src/astalify.ts +++ b/core/gjs/src/astalify.ts @@ -1,58 +1,8 @@ import Binding, { kebabify, snakeify, type Connectable, type Subscribable } from "./binding.js" -import { Astal, Gtk, Gdk } from "./imports.js" +import { Astal, Gtk, Gdk, GObject } from "./imports.js" import { execAsync } from "./process.js" import Variable from "./variable.js" -Object.defineProperty(Astal.Box.prototype, "children", { - get() { return this.get_children() }, - set(v) { this.set_children(v) }, -}) - -function setChildren(parent: Gtk.Widget, children: Gtk.Widget[]) { - children = children.flat(Infinity).map(ch => ch instanceof Gtk.Widget - ? ch - : new Gtk.Label({ visible: true, label: String(ch) })) - - // remove - if (parent instanceof Gtk.Bin) { - const ch = parent.get_child() - if (ch) - parent.remove(ch) - } - else if (parent instanceof Gtk.Container && - !(parent instanceof Astal.Box || - parent instanceof Astal.Stack)) { - for(const ch of parent.get_children()) - parent.remove(ch) - } - - // TODO: add more container types - if (parent instanceof Astal.Box) { - parent.set_children(children) - } - - else if (parent instanceof Astal.Stack) { - parent.set_children(children) - } - - else if (parent instanceof Astal.CenterBox) { - parent.startWidget = children[0] - parent.centerWidget = children[1] - parent.endWidget = children[2] - } - - else if (parent instanceof Astal.Overlay) { - const [child, ...overlays] = children - parent.set_child(child) - parent.set_overlays(overlays) - } - - else if (parent instanceof Gtk.Container) { - for (const ch of children) - parent.add(ch) - } -} - function mergeBindings(array: any[]) { function getValues(...args: any[]) { let i = 0 @@ -75,204 +25,228 @@ function mergeBindings(array: any[]) { function setProp(obj: any, prop: string, value: any) { try { + // the setter method has to be used because + // array like properties are not bound correctly as props const setter = `set_${snakeify(prop)}` if (typeof obj[setter] === "function") return obj[setter](value) - if (Object.hasOwn(obj, prop)) - return (obj[prop] = value) + return (obj[prop] = value) } catch (error) { console.error(`could not set property "${prop}" on ${obj}:`, error) } - - console.error(`could not set property "${prop}" on ${obj}`) -} - -export type Widget<C extends InstanceType<typeof Gtk.Widget>> = C & { - className: string - css: string - cursor: Cursor - clickThrough: boolean - toggleClassName(name: string, on?: boolean): void - hook( - object: Connectable, - signal: string, - callback: (self: Widget<C>, ...args: any[]) => void, - ): Widget<C> - hook( - object: Subscribable, - callback: (self: Widget<C>, ...args: any[]) => void, - ): Widget<C> -} - -function hook( - self: Gtk.Widget, - object: Connectable | Subscribable, - signalOrCallback: string | ((self: Gtk.Widget, ...args: any[]) => void), - callback?: (self: Gtk.Widget, ...args: any[]) => void, -) { - if (typeof object.connect === "function" && callback) { - const id = object.connect(signalOrCallback, (_: any, ...args: unknown[]) => { - callback(self, ...args) - }) - self.connect("destroy", () => { - (object.disconnect as Connectable["disconnect"])(id) - }) - } - - else if (typeof object.subscribe === "function" && typeof signalOrCallback === "function") { - const unsub = object.subscribe((...args: unknown[]) => { - signalOrCallback(self, ...args) - }) - self.connect("destroy", unsub) - } - - return self } -function ctor(self: any, config: any = {}, children: any = []) { - const { setup, ...props } = config - props.visible ??= true - - // collect bindings - const bindings = Object.keys(props).reduce((acc: any, prop) => { - if (props[prop] instanceof Binding) { - const binding = props[prop] - delete props[prop] - return [...acc, [prop, binding]] - } - return acc - }, []) - - // collect signal handlers - const onHandlers = Object.keys(props).reduce((acc: any, key) => { - if (key.startsWith("on")) { - const sig = kebabify(key).split("-").slice(1).join("-") - const handler = props[key] - delete props[key] - return [...acc, [sig, handler]] - } - return acc - }, []) - - // set children - children = mergeBindings(children.flat(Infinity)) - if (children instanceof Binding) { - setChildren(self, children.get()) - self.connect("destroy", children.subscribe((v) => { - setChildren(self, v) - })) - } - else { - if (children.length > 0) { - setChildren(self, children) +export default function astalify< + C extends { new(...args: any[]): Gtk.Widget }, +>(cls: C) { + class Widget extends cls { + get css(): string { return Astal.widget_get_css(this) } + set css(css: string) { Astal.widget_set_css(this, css) } + get_css(): string { return this.css } + set_css(css: string) { this.css = css } + + get className(): string { return Astal.widget_get_class_names(this).join(" ") } + set className(className: string) { Astal.widget_set_class_names(this, className.split(/\s+/)) } + get_class_name(): string { return this.className } + set_class_name(className: string) { this.className = className } + + get cursor(): Cursor { return Astal.widget_get_cursor(this) as Cursor } + set cursor(cursor: Cursor) { Astal.widget_set_cursor(this, cursor) } + get_cursor(): Cursor { return this.cursor } + set_cursor(cursor: Cursor) { this.cursor = cursor } + + get clickThrough(): boolean { return Astal.widget_get_click_through(this) } + set clickThrough(clickThrough: boolean) { Astal.widget_set_click_through(this, clickThrough) } + get_click_through(): boolean { return this.clickThrough } + set_click_through(clickThrough: boolean) { this.clickThrough = clickThrough } + + declare __no_implicit_destroy: boolean + get noImplicitDestroy(): boolean { return this.__no_implicit_destroy } + set noImplicitDestroy(value: boolean) { this.__no_implicit_destroy = value } + + _setChildren(children: Gtk.Widget[]) { + children = children.flat(Infinity).map(ch => ch instanceof Gtk.Widget + ? ch + : new Gtk.Label({ visible: true, label: String(ch) })) + + // remove + if (this instanceof Gtk.Bin) { + const ch = this.get_child() + if (ch) + this.remove(ch) + if (ch && !children.includes(ch) && !this.noImplicitDestroy) + ch?.destroy() + } + else if (this instanceof Gtk.Container) { + for (const ch of this.get_children()) { + this.remove(ch) + if (!children.includes(ch) && !this.noImplicitDestroy) + ch?.destroy() + } + } + + // TODO: add more container types + if (this instanceof Astal.Box) { + this.set_children(children) + } + + else if (this instanceof Astal.Stack) { + this.set_children(children) + } + + else if (this instanceof Astal.CenterBox) { + this.startWidget = children[0] + this.centerWidget = children[1] + this.endWidget = children[2] + } + + else if (this instanceof Astal.Overlay) { + const [child, ...overlays] = children + this.set_child(child) + this.set_overlays(overlays) + } + + else if (this instanceof Gtk.Container) { + for (const ch of children) + this.add(ch) + } } - } - // setup signal handlers - for (const [signal, callback] of onHandlers) { - if (typeof callback === "function") { - self.connect(signal, callback) + toggleClassName(cn: string, cond = true) { + Astal.widget_toggle_class_name(this, cn, cond) } - else { - self.connect(signal, () => execAsync(callback) - .then(print).catch(console.error)) + + hook( + object: Connectable, + signal: string, + callback: (self: this, ...args: any[]) => void, + ): this + hook( + object: Subscribable, + callback: (self: this, ...args: any[]) => void, + ): this + hook( + object: Connectable | Subscribable, + signalOrCallback: string | ((self: this, ...args: any[]) => void), + callback?: (self: this, ...args: any[]) => void, + ) { + if (typeof object.connect === "function" && callback) { + const id = object.connect(signalOrCallback, (_: any, ...args: unknown[]) => { + callback(this, ...args) + }) + this.connect("destroy", () => { + (object.disconnect as Connectable["disconnect"])(id) + }) + } + + else if (typeof object.subscribe === "function" && typeof signalOrCallback === "function") { + const unsub = object.subscribe((...args: unknown[]) => { + signalOrCallback(this, ...args) + }) + this.connect("destroy", unsub) + } + + return this } - } - // setup bindings handlers - for (const [prop, binding] of bindings) { - if (prop === "child" || prop === "children") { - self.connect("destroy", binding.subscribe((v: any) => { - setChildren(self, v) - })) + constructor(...params: any[]) { + super() + const [config] = params + + const { setup, child, children = [], ...props } = config + props.visible ??= true + + if (child) + children.unshift(child) + + // collect bindings + const bindings = Object.keys(props).reduce((acc: any, prop) => { + if (props[prop] instanceof Binding) { + const binding = props[prop] + delete props[prop] + return [...acc, [prop, binding]] + } + return acc + }, []) + + // collect signal handlers + const onHandlers = Object.keys(props).reduce((acc: any, key) => { + if (key.startsWith("on")) { + const sig = kebabify(key).split("-").slice(1).join("-") + const handler = props[key] + delete props[key] + return [...acc, [sig, handler]] + } + return acc + }, []) + + // set children + const mergedChildren = mergeBindings(children.flat(Infinity)) + if (mergedChildren instanceof Binding) { + this._setChildren(mergedChildren.get()) + this.connect("destroy", mergedChildren.subscribe((v) => { + this._setChildren(v) + })) + } + else { + if (mergedChildren.length > 0) { + this._setChildren(mergedChildren) + } + } + + // setup signal handlers + for (const [signal, callback] of onHandlers) { + if (typeof callback === "function") { + this.connect(signal, callback) + } + else { + this.connect(signal, () => execAsync(callback) + .then(print).catch(console.error)) + } + } + + // setup bindings handlers + for (const [prop, binding] of bindings) { + if (prop === "child" || prop === "children") { + this.connect("destroy", binding.subscribe((v: any) => { + this._setChildren(v) + })) + } + this.connect("destroy", binding.subscribe((v: any) => { + setProp(this, prop, v) + })) + setProp(this, prop, binding.get()) + } + + Object.assign(this, props) + setup?.(this) } - self.connect("destroy", binding.subscribe((v: any) => { - setProp(self, prop, v) - })) - setProp(self, prop, binding.get()) } - Object.assign(self, props) - setup?.(self) - return self -} - -function proxify< - C extends typeof Gtk.Widget, ->(klass: C) { - Object.defineProperty(klass.prototype, "className", { - get() { return Astal.widget_get_class_names(this).join(" ") }, - set(v) { Astal.widget_set_class_names(this, v.split(/\s+/)) }, - }) - - Object.defineProperty(klass.prototype, "css", { - get() { return Astal.widget_get_css(this) }, - set(v) { Astal.widget_set_css(this, v) }, - }) - - Object.defineProperty(klass.prototype, "cursor", { - get() { return Astal.widget_get_cursor(this) }, - set(v) { Astal.widget_set_cursor(this, v) }, - }) - - Object.defineProperty(klass.prototype, "clickThrough", { - get() { return Astal.widget_get_click_through(this) }, - set(v) { Astal.widget_set_click_through(this, v) }, - }) - - Object.assign(klass.prototype, { - hook: function (obj: any, sig: any, callback: any) { - return hook(this as InstanceType<C>, obj, sig, callback) - }, - toggleClassName: function name(cn: string, cond = true) { - Astal.widget_toggle_class_name(this as InstanceType<C>, cn, cond) - }, - set_class_name: function (name: string) { - // @ts-expect-error unknown key - this.className = name - }, - set_css: function (css: string) { - // @ts-expect-error unknown key - this.css = css + GObject.registerClass({ + GTypeName: `Astal_${cls.name}`, + Properties: { + "class-name": GObject.ParamSpec.string( + "class-name", "", "", GObject.ParamFlags.READWRITE, "", + ), + "css": GObject.ParamSpec.string( + "css", "", "", GObject.ParamFlags.READWRITE, "", + ), + "cursor": GObject.ParamSpec.string( + "cursor", "", "", GObject.ParamFlags.READWRITE, "default", + ), + "click-through": GObject.ParamSpec.boolean( + "click-through", "", "", GObject.ParamFlags.READWRITE, false, + ), + "no-implicit-destroy": GObject.ParamSpec.boolean( + "no-implicit-destroy", "", "", GObject.ParamFlags.READWRITE, false, + ), }, - set_cursor: function (cursor: string) { - // @ts-expect-error unknown key - this.cursor = cursor - }, - set_click_through: function (clickThrough: boolean) { - // @ts-expect-error unknown key - this.clickThrough = clickThrough - }, - }) - - const proxy = new Proxy(klass, { - construct(_, [conf, ...children]) { - // @ts-expect-error abstract class - return ctor(new klass(), conf, children) - }, - apply(_t, _a, [conf, ...children]) { - // @ts-expect-error abstract class - return ctor(new klass(), conf, children) - }, - }) + }, Widget) - return proxy -} - -export default 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<InstanceType<C>> - (props?: P, ...children: Gtk.Widget[]): Widget<InstanceType<C>> - } - - return proxify(klass) as unknown as Astal<N> + return Widget } type BindableProps<T> = { @@ -282,7 +256,7 @@ type BindableProps<T> = { type SigHandler< W extends InstanceType<typeof Gtk.Widget>, Args extends Array<unknown>, -> = ((self: Widget<W>, ...args: Args) => unknown) | string | string[] +> = ((self: W, ...args: Args) => unknown) | string | string[] export type ConstructProps< Self extends InstanceType<typeof Gtk.Widget>, @@ -299,16 +273,18 @@ export type ConstructProps< cursor?: string clickThrough?: boolean }> & { - onDestroy?: (self: Widget<Self>) => unknown - onDraw?: (self: Widget<Self>) => unknown - onKeyPressEvent?: (self: Widget<Self>, event: Gdk.Event) => unknown - onKeyReleaseEvent?: (self: Widget<Self>, event: Gdk.Event) => unknown - onButtonPressEvent?: (self: Widget<Self>, event: Gdk.Event) => unknown - onButtonReleaseEvent?: (self: Widget<Self>, event: Gdk.Event) => unknown - onRealize?: (self: Widget<Self>) => unknown - setup?: (self: Widget<Self>) => void + onDestroy?: (self: Self) => unknown + onDraw?: (self: Self) => unknown + onKeyPressEvent?: (self: Self, event: Gdk.Event) => unknown + onKeyReleaseEvent?: (self: Self, event: Gdk.Event) => unknown + onButtonPressEvent?: (self: Self, event: Gdk.Event) => unknown + onButtonReleaseEvent?: (self: Self, event: Gdk.Event) => unknown + onRealize?: (self: Self) => unknown + setup?: (self: Self) => void } +export type BindableChild = Gtk.Widget | Binding<Gtk.Widget> + type Cursor = | "default" | "help" |