diff options
author | Aylur <[email protected]> | 2024-07-20 14:05:47 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-07-20 14:05:47 +0200 |
commit | 1c9377ec696c1f8269bf30a7af1e4a9566d55625 (patch) | |
tree | 2a9a7e445b8e999f3fdac326d2386459d26868da /gjs/src | |
parent | a1f9272f4576fd3c366581464223e08a35ccf99f (diff) |
update eslint, ts-for-gir
Diffstat (limited to 'gjs/src')
-rw-r--r-- | gjs/src/application.ts | 11 | ||||
-rw-r--r-- | gjs/src/astalify.ts | 76 | ||||
-rw-r--r-- | gjs/src/file.ts | 6 | ||||
-rw-r--r-- | gjs/src/jsx/jsx-runtime.ts | 61 | ||||
-rw-r--r-- | gjs/src/process.ts | 6 | ||||
-rw-r--r-- | gjs/src/variable.ts | 18 | ||||
-rw-r--r-- | gjs/src/widgets.ts | 60 |
7 files changed, 122 insertions, 116 deletions
diff --git a/gjs/src/application.ts b/gjs/src/application.ts index 606bf30..d1bc13f 100644 --- a/gjs/src/application.ts +++ b/gjs/src/application.ts @@ -33,7 +33,8 @@ class AstalJS extends Astal.Application { fn()() .then(res) .catch(rej) - } catch (error) { + } + catch (error) { rej(error) } }) @@ -43,12 +44,13 @@ class AstalJS extends Astal.Application { vfunc_request(msg: string, conn: Gio.SocketConnection): void { if (typeof this.requestHandler === "function") { - this.requestHandler(msg, response => { + this.requestHandler(msg, (response) => { Astal.write_sock(conn, String(response), (_, res) => Astal.write_sock_finish(res), ) }) - } else { + } + else { super.vfunc_request(msg, conn) } } @@ -68,7 +70,6 @@ class AstalJS extends Astal.Application { this.requestHandler = requestHandler this.connect("activate", () => { - // @ts-expect-error missing url type const path: string[] = import.meta.url.split("/").slice(3) const file = path.at(-1)!.replace(".js", ".css") const css = `/${path.slice(0, -1).join("/")}/${file}` @@ -92,4 +93,4 @@ class AstalJS extends Astal.Application { } } -export default new AstalJS +export default new AstalJS() diff --git a/gjs/src/astalify.ts b/gjs/src/astalify.ts index ad39da0..df9845f 100644 --- a/gjs/src/astalify.ts +++ b/gjs/src/astalify.ts @@ -71,14 +71,15 @@ function setProp(obj: any, prop: string, value: any) { if (Object.hasOwn(obj, prop)) return (obj[prop] = value) - } catch (error) { + } + 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 { new(...args: any): Gtk.Widget }> = InstanceType<C> & { +export type Widget<C extends InstanceType<typeof Gtk.Widget>> = C & { className: string css: string cursor: Cursor @@ -170,7 +171,7 @@ function ctor(self: any, config: any = {}, children: any = []) { children = mergeBindings(children.flat(Infinity)) if (children instanceof Binding) { setChildren(self, children.get()) - self.connect("destroy", children.subscribe(v => { + self.connect("destroy", children.subscribe((v) => { setChildren(self, v) })) } @@ -184,60 +185,58 @@ function ctor(self: any, config: any = {}, children: any = []) { } function proxify< - C extends { new(...args: any[]): any }, + C extends typeof Gtk.Widget, >(klass: C) { - klass.prototype.hook = function (obj: any, sig: any, callback: any) { - return hook(this, obj, sig, callback) - } - - klass.prototype.toggleClassName = function (name: string, on = true) { - Astal.widget_toggle_class_name(this, name, on) - } - 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+/)) }, }) - klass.prototype.set_class_name = function (name: string) { - this.className = name - } - Object.defineProperty(klass.prototype, "css", { get() { return Astal.widget_get_css(this) }, set(v) { Astal.widget_set_css(this, v) }, }) - klass.prototype.set_css = function (css: string) { - this.css = css - } - Object.defineProperty(klass.prototype, "cursor", { get() { return Astal.widget_get_cursor(this) }, set(v) { Astal.widget_set_cursor(this, v) }, }) - klass.prototype.set_cursor = function (cursor: string) { - this.cursor = cursor - } - Object.defineProperty(klass.prototype, "clickThrough", { get() { return Astal.widget_get_click_through(this) }, set(v) { Astal.widget_set_click_through(this, v) }, }) - klass.prototype.set_click_through = function (clickThrough: boolean) { - this.clickThrough = clickThrough - } + Object.assign(klass.prototype, { + hook: function (obj: any, sig: any, callback: any) { + return hook(this as InstanceType<C>, obj, sig, callback) + }, + 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 + }, + 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]) { - const self = new klass - return ctor(self, conf, children) + // @ts-expect-error abstract class + return ctor(new klass(), conf, children) }, apply(_t, _a, [conf, ...children]) { - const self = new klass - return ctor(self, conf, children) + // @ts-expect-error abstract class + return ctor(new klass(), conf, children) }, }) @@ -251,33 +250,32 @@ export default function astalify< >(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> + 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> } - type BindableProps<T> = { [K in keyof T]: Binding<T[K]> | T[K]; } type SigHandler< - W extends { new(...args: any): Gtk.Widget }, + W extends InstanceType<typeof Gtk.Widget>, Args extends Array<unknown>, > = ((self: Widget<W>, ...args: Args) => unknown) | string | string[] export type ConstructProps< - Self extends { new(...args: any[]): any }, - Props = unknown, - Signals extends Record<`on${string}`, Array<unknown>> = Record<`on${string}`, any[]> + Self extends InstanceType<typeof Gtk.Widget>, + Props extends Gtk.Widget.ConstructorProps, + Signals extends Record<`on${string}`, Array<unknown>> = Record<`on${string}`, any[]>, > = Partial<{ // @ts-expect-error can't assign to unknown, but it works as expected though [S in keyof Signals]: SigHandler<Self, Signals[S]> }> & Partial<{ [Key in `on${string}`]: SigHandler<Self, any[]> -}> & BindableProps<Props & { +}> & BindableProps<Partial<Props> & { className?: string css?: string cursor?: string diff --git a/gjs/src/file.ts b/gjs/src/file.ts index d15474b..8875833 100644 --- a/gjs/src/file.ts +++ b/gjs/src/file.ts @@ -9,7 +9,8 @@ export function readFileAsync(path: string): Promise<string> { Astal.read_file_async(path, (_, res) => { try { resolve(Astal.read_file_finish(res) || "") - } catch (error) { + } + catch (error) { reject(error) } }) @@ -25,7 +26,8 @@ export function writeFileAsync(path: string, content: string): Promise<void> { Astal.write_file_async(path, content, (_, res) => { try { resolve(Astal.write_file_finish(res)) - } catch (error) { + } + catch (error) { reject(error) } }) diff --git a/gjs/src/jsx/jsx-runtime.ts b/gjs/src/jsx/jsx-runtime.ts index baa0444..70f098f 100644 --- a/gjs/src/jsx/jsx-runtime.ts +++ b/gjs/src/jsx/jsx-runtime.ts @@ -27,30 +27,31 @@ export function jsx( if (isArrowFunction(ctor)) return ctor(props) + // @ts-expect-error can be class or function return new ctor(props) } const ctors = { - "box": Widget.Box, - "button": Widget.Button, - "centerbox": Widget.CenterBox, + box: Widget.Box, + button: Widget.Button, + centerbox: Widget.CenterBox, // TODO: circularprogress - "drawingarea": Widget.DrawingArea, - "entry": Widget.Entry, - "eventbox": Widget.EventBox, + drawingarea: Widget.DrawingArea, + entry: Widget.Entry, + eventbox: Widget.EventBox, // TODO: fixed // TODO: flowbox - "icon": Widget.Icon, - "label": Widget.Label, - "levelbar": Widget.LevelBar, + icon: Widget.Icon, + label: Widget.Label, + levelbar: Widget.LevelBar, // TODO: listbox - "overlay": Widget.Overlay, - "revealer": Widget.Revealer, - "scrollable": Widget.Scrollable, - "slider": Widget.Slider, + overlay: Widget.Overlay, + revealer: Widget.Revealer, + scrollable: Widget.Scrollable, + slider: Widget.Slider, // TODO: stack - "switch": Widget.Switch, - "window": Widget.Window, + switch: Widget.Switch, + window: Widget.Window, } declare global { @@ -59,26 +60,26 @@ declare global { type Element = Gtk.Widget type ElementClass = Gtk.Widget interface IntrinsicElements { - "box": Widget.BoxProps, - "button": Widget.ButtonProps, - "centerbox": Widget.CenterBoxProps, + box: Widget.BoxProps + button: Widget.ButtonProps + centerbox: Widget.CenterBoxProps // TODO: circularprogress - "drawingarea": Widget.DrawingAreaProps, - "entry": Widget.EntryProps, - "eventbox": Widget.EventBoxProps, + drawingarea: Widget.DrawingAreaProps + entry: Widget.EntryProps + eventbox: Widget.EventBoxProps // TODO: fixed // TODO: flowbox - "icon": Widget.IconProps, - "label": Widget.LabelProps, - "levelbar": Widget.LevelBarProps, + icon: Widget.IconProps + label: Widget.LabelProps + levelbar: Widget.LevelBarProps // TODO: listbox - "overlay": Widget.OverlayProps, - "revealer": Widget.RevealerProps, - "scrollable": Widget.ScrollableProps, - "slider": Widget.SliderProps, + overlay: Widget.OverlayProps + revealer: Widget.RevealerProps + scrollable: Widget.ScrollableProps + slider: Widget.SliderProps // TODO: stack - "switch": Widget.SwitchProps, - "window": Widget.WindowProps, + switch: Widget.SwitchProps + window: Widget.WindowProps } } } diff --git a/gjs/src/process.ts b/gjs/src/process.ts index 59ab328..f7ff7b0 100644 --- a/gjs/src/process.ts +++ b/gjs/src/process.ts @@ -1,9 +1,9 @@ import { Astal } from "./imports.js" type Args<Out = void, Err = void> = { - cmd: string | string[], - out?: (stdout: string) => Out, - err?: (stderr: string) => Err, + cmd: string | string[] + out?: (stdout: string) => Out + err?: (stderr: string) => Err } function args<O, E>(argsOrCmd: Args | string | string[], onOut: O, onErr: E) { diff --git a/gjs/src/variable.ts b/gjs/src/variable.ts index b5f186b..05e142f 100644 --- a/gjs/src/variable.ts +++ b/gjs/src/variable.ts @@ -22,7 +22,7 @@ class VariableWrapper<T> extends Function { constructor(init: T) { super() this._value = init - this.variable = new Astal.VariableBase + this.variable = new Astal.VariableBase() this.variable.connect("dropped", () => { this.stopWatch() this.stopPoll() @@ -60,11 +60,13 @@ class VariableWrapper<T> extends Function { if (v instanceof Promise) { v.then(v => this.set(v)) .catch(err => this.variable.emit("error", err)) - } else { + } + else { this.set(v) } }) - } else if (this.pollExec) { + } + else if (this.pollExec) { this._poll = interval(this.pollInterval, () => { execAsync(this.pollExec!) .then(v => this.set(this.pollTransform!(v, this.get()))) @@ -142,7 +144,8 @@ class VariableWrapper<T> extends Function { if (typeof exec === "function") { this.pollFn = exec delete this.pollExec - } else { + } + else { this.pollExec = exec delete this.pollFn } @@ -183,7 +186,8 @@ class VariableWrapper<T> extends Function { const [o, s] = obj o.connect(s, set) } - } else { + } + else { if (typeof sigOrFn === "string") objs.connect(sigOrFn, set) } @@ -195,7 +199,7 @@ class VariableWrapper<T> extends Function { const Deps extends Array<Variable<any> | Binding<any>>, Args extends { [K in keyof Deps]: Deps[K] extends Variable<infer T> - ? T : Deps[K] extends Binding<infer T> ? T : never + ? T : Deps[K] extends Binding<infer T> ? T : never }, >(deps: Deps, fn: (...args: Args) => V) { const update = () => fn(...deps.map(d => d.get()) as Args) @@ -216,7 +220,7 @@ export const Variable = new Proxy(VariableWrapper as any, { }) as { derive: typeof VariableWrapper["derive"] <T>(init: T): Variable<T> - new <T>(init: T): Variable<T> + new<T>(init: T): Variable<T> } export default Variable diff --git a/gjs/src/widgets.ts b/gjs/src/widgets.ts index bf5ac50..a0f333b 100644 --- a/gjs/src/widgets.ts +++ b/gjs/src/widgets.ts @@ -5,14 +5,14 @@ import astalify, { type ConstructProps, type Widget } from "./astalify.js" export { astalify, ConstructProps } // Box -export type Box = Widget<typeof Astal.Box> +export type Box = Widget<Astal.Box> export const Box = astalify<typeof Astal.Box, BoxProps, "Box">(Astal.Box) -export type BoxProps = ConstructProps<typeof Astal.Box, Astal.Box.ConstructorProperties> +export type BoxProps = ConstructProps<Astal.Box, Astal.Box.ConstructorProps> // Button -export type Button = Widget<typeof Astal.Button> +export type Button = Widget<Astal.Button> export const Button = astalify<typeof Astal.Button, ButtonProps, "Button">(Astal.Button) -export type ButtonProps = ConstructProps<typeof Astal.Button, Astal.Button.ConstructorProperties, { +export type ButtonProps = ConstructProps<Astal.Button, Astal.Button.ConstructorProps, { onClicked: [] onClick: [event: Astal.ClickEvent] onClickRelease: [event: Astal.ClickEvent] @@ -22,31 +22,31 @@ export type ButtonProps = ConstructProps<typeof Astal.Button, Astal.Button.Const }> // CenterBox -export type CenterBox = Widget<typeof Astal.CenterBox> +export type CenterBox = Widget<Astal.CenterBox> export const CenterBox = astalify<typeof Astal.CenterBox, CenterBoxProps, "CenterBox">(Astal.CenterBox) -export type CenterBoxProps = ConstructProps<typeof Astal.CenterBox, Astal.CenterBox.ConstructorProperties> +export type CenterBoxProps = ConstructProps<Astal.CenterBox, Astal.CenterBox.ConstructorProps> // TODO: CircularProgress // DrawingArea -export type DrawingArea = Widget<typeof Gtk.DrawingArea> +export type DrawingArea = Widget<Gtk.DrawingArea> export const DrawingArea = astalify<typeof Gtk.DrawingArea, DrawingAreaProps, "DrawingArea">(Gtk.DrawingArea) -export type DrawingAreaProps = ConstructProps<typeof Gtk.DrawingArea, Gtk.DrawingArea.ConstructorProperties, { +export type DrawingAreaProps = ConstructProps<Gtk.DrawingArea, Gtk.DrawingArea.ConstructorProps, { onDraw: [cr: any] // TODO: cairo types }> // Entry -export type Entry = Widget<typeof Gtk.Entry> +export type Entry = Widget<Gtk.Entry> export const Entry = astalify<typeof Gtk.Entry, EntryProps, "Entry">(Gtk.Entry) -export type EntryProps = ConstructProps<typeof Gtk.Entry, Gtk.Entry.ConstructorProperties, { +export type EntryProps = ConstructProps<Gtk.Entry, Gtk.Entry.ConstructorProps, { onChanged: [] onActivate: [] }> // EventBox -export type EventBox = Widget<typeof Astal.EventBox> +export type EventBox = Widget<Astal.EventBox> export const EventBox = astalify<typeof Astal.EventBox, EventBoxProps, "EventBox">(Astal.EventBox) -export type EventBoxProps = ConstructProps<typeof Astal.EventBox, Astal.EventBox.ConstructorProperties, { +export type EventBoxProps = ConstructProps<Astal.EventBox, Astal.EventBox.ConstructorProps, { onClick: [event: Astal.ClickEvent] onClickRelease: [event: Astal.ClickEvent] onHover: [event: Astal.HoverEvent] @@ -58,52 +58,52 @@ export type EventBoxProps = ConstructProps<typeof Astal.EventBox, Astal.EventBox // TODO: FlowBox // Icon -export type Icon = Widget<typeof Astal.Icon> +export type Icon = Widget<Astal.Icon> export const Icon = astalify<typeof Astal.Icon, IconProps, "Icon">(Astal.Icon) -export type IconProps = ConstructProps<typeof Astal.Icon, Astal.Icon.ConstructorProperties> +export type IconProps = ConstructProps<Astal.Icon, Astal.Icon.ConstructorProps> // Label -export type Label = Widget<typeof Gtk.Label> +export type Label = Widget<Gtk.Label> export const Label = astalify<typeof Gtk.Label, LabelProps, "Label">(Gtk.Label) -export type LabelProps = ConstructProps<typeof Gtk.Label, Gtk.Label.ConstructorProperties> +export type LabelProps = ConstructProps<Gtk.Label, Gtk.Label.ConstructorProps> // LevelBar -export type LevelBar = Widget<typeof Astal.LevelBar> +export type LevelBar = Widget<Astal.LevelBar> export const LevelBar = astalify<typeof Astal.LevelBar, LevelBarProps, "LevelBar">(Astal.LevelBar) -export type LevelBarProps = ConstructProps<typeof Astal.LevelBar, Astal.LevelBar.ConstructorProperties> +export type LevelBarProps = ConstructProps<Astal.LevelBar, Astal.LevelBar.ConstructorProps> // TODO: ListBox // Overlay -export type Overlay = Widget<typeof Astal.Overlay> +export type Overlay = Widget<Astal.Overlay> export const Overlay = astalify<typeof Astal.Overlay, OverlayProps, "Overlay">(Astal.Overlay) -export type OverlayProps = ConstructProps<typeof Astal.Overlay, Astal.Overlay.ConstructorProperties> +export type OverlayProps = ConstructProps<Astal.Overlay, Astal.Overlay.ConstructorProps> // Revealer -export type Revealer = Widget<typeof Gtk.Revealer> +export type Revealer = Widget<Gtk.Revealer> export const Revealer = astalify<typeof Gtk.Revealer, RevealerProps, "Revealer">(Gtk.Revealer) -export type RevealerProps = ConstructProps<typeof Gtk.Revealer, Gtk.Revealer.ConstructorProperties> +export type RevealerProps = ConstructProps<Gtk.Revealer, Gtk.Revealer.ConstructorProps> // Scrollable -export type Scrollable = Widget<typeof Astal.Scrollable> +export type Scrollable = Widget<Astal.Scrollable> export const Scrollable = astalify<typeof Astal.Scrollable, ScrollableProps, "Scrollable">(Astal.Scrollable) -export type ScrollableProps = ConstructProps<typeof Astal.Scrollable, Astal.Scrollable.ConstructorProperties> +export type ScrollableProps = ConstructProps<Astal.Scrollable, Astal.Scrollable.ConstructorProps> // Slider -export type Slider = Widget<typeof Astal.Slider> +export type Slider = Widget<Astal.Slider> export const Slider = astalify<typeof Astal.Slider, SliderProps, "Slider">(Astal.Slider) -export type SliderProps = ConstructProps<typeof Astal.Slider, Astal.Slider.ConstructorProperties, { +export type SliderProps = ConstructProps<Astal.Slider, Astal.Slider.ConstructorProps, { onDragged: [] }> // TODO: Stack // Switch -export type Switch = Widget<typeof Gtk.Switch> +export type Switch = Widget<Gtk.Switch> export const Switch = astalify<typeof Gtk.Switch, SwitchProps, "Switch">(Gtk.Switch) -export type SwitchProps = ConstructProps<typeof Gtk.Switch, Gtk.Switch.ConstructorProperties> +export type SwitchProps = ConstructProps<Gtk.Switch, Gtk.Switch.ConstructorProps> // Window -export type Window = Widget<typeof Astal.Window> +export type Window = Widget<Astal.Window> export const Window = astalify<typeof Astal.Window, WindowProps, "Window">(Astal.Window) -export type WindowProps = ConstructProps<typeof Astal.Window, Astal.Window.ConstructorProperties> +export type WindowProps = ConstructProps<Astal.Window, Astal.Window.ConstructorProps> |