refactor(ags4): improve types and remove as many 'any' as possible
All checks were successful
Discord / discord commits (push) Has been skipped
All checks were successful
Discord / discord commits (push) Has been skipped
This commit is contained in:
parent
adb36c2b34
commit
c8485430b9
18 changed files with 144 additions and 126 deletions
|
@ -1,14 +1,44 @@
|
|||
import { execAsync, Variable } from 'astal';
|
||||
import { Gtk } from 'astal/gtk4';
|
||||
import Binding, { Connectable, kebabify, snakeify, Subscribable } from 'astal/binding';
|
||||
import { type Gdk, type Gtk, type ConstructProps } from 'astal/gtk4';
|
||||
import { Binding, type Connectable, kebabify, snakeify, type Subscribable } from 'astal/binding';
|
||||
|
||||
export interface EventController<Self extends Gtk.Widget> {
|
||||
onFocusEnter?: (self: Self) => void
|
||||
onFocusLeave?: (self: Self) => void
|
||||
|
||||
onKeyPressed?: (self: Self, keyval: number, keycode: number, state: Gdk.ModifierType) => void
|
||||
onKeyReleased?: (self: Self, keyval: number, keycode: number, state: Gdk.ModifierType) => void
|
||||
onKeyModifier?: (self: Self, state: Gdk.ModifierType) => void
|
||||
|
||||
onLegacy?: (self: Self, event: Gdk.Event) => void
|
||||
onButtonPressed?: (self: Self, state: Gdk.ButtonEvent) => void
|
||||
onButtonReleased?: (self: Self, state: Gdk.ButtonEvent) => void
|
||||
|
||||
onHoverEnter?: (self: Self, x: number, y: number) => void
|
||||
onHoverLeave?: (self: Self) => void
|
||||
onMotion?: (self: Self, x: number, y: number) => void
|
||||
|
||||
onScroll?: (self: Self, dx: number, dy: number) => void
|
||||
onScrollDecelerate?: (self: Self, vel_x: number, vel_y: number) => void
|
||||
}
|
||||
|
||||
export type BindableProps<T> = {
|
||||
[K in keyof T]: Binding<T[K]> | T[K];
|
||||
};
|
||||
|
||||
export interface AstalifyProps {
|
||||
css: string
|
||||
child: Gtk.Widget
|
||||
children: Gtk.Widget[]
|
||||
}
|
||||
|
||||
export const noImplicitDestroy = Symbol('no no implicit destroy');
|
||||
export const setChildren = Symbol('children setter method');
|
||||
|
||||
const mergeBindings = <Value = unknown>(
|
||||
array: (Value | Binding<Value>)[],
|
||||
array: (Value | Binding<Value> | Binding<Value[]>)[],
|
||||
): Value[] | Binding<Value[]> => {
|
||||
const getValues = (...args: Value[]) => {
|
||||
const getValues = (args: Value[]) => {
|
||||
let i = 0;
|
||||
|
||||
return array.map((value) => value instanceof Binding ?
|
||||
|
@ -23,26 +53,12 @@ const mergeBindings = <Value = unknown>(
|
|||
}
|
||||
|
||||
if (bindings.length === 1) {
|
||||
return bindings[0].as(getValues);
|
||||
return (bindings[0] as Binding<Value[]>).as(getValues);
|
||||
}
|
||||
|
||||
return Variable.derive(bindings, getValues)();
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const setProp = (obj: any, prop: string, value: unknown) => {
|
||||
try {
|
||||
const setter = `set_${snakeify(prop)}`;
|
||||
|
||||
if (typeof obj[setter] === 'function') { return obj[setter](value); }
|
||||
|
||||
return (obj[prop] = value);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`could not set property "${prop}" on ${obj}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
export const hook = <Widget extends Connectable>(
|
||||
widget: Widget,
|
||||
object: Connectable | Subscribable,
|
||||
|
@ -67,64 +83,82 @@ export const hook = <Widget extends Connectable>(
|
|||
}
|
||||
};
|
||||
|
||||
export const construct = <Widget extends Connectable & {
|
||||
[setChildren]: (children: Gtk.Widget[]) => void
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
}>(widget: Widget, config: any) => {
|
||||
// eslint-disable-next-line prefer-const
|
||||
let { setup, child, children = [], ...props } = config;
|
||||
export const construct = <
|
||||
Self extends InstanceType<typeof Gtk.Widget> & {
|
||||
[setChildren]: (children: Gtk.Widget[]) => void
|
||||
},
|
||||
Props extends Gtk.Widget.ConstructorProps,
|
||||
>(
|
||||
widget: Self,
|
||||
props: Omit<
|
||||
ConstructProps<Self, Props> & Partial<BindableProps<AstalifyProps>>,
|
||||
keyof EventController<Self>
|
||||
>,
|
||||
) => {
|
||||
type Key = keyof typeof props;
|
||||
const keys = Object.keys(props) as Key[];
|
||||
const entries = Object.entries(props) as [Key, unknown][];
|
||||
|
||||
if (children instanceof Binding) {
|
||||
children = [children];
|
||||
}
|
||||
const setProp = (prop: Key, value: Self[keyof Self]) => {
|
||||
try {
|
||||
const setter = `set_${snakeify(prop.toString())}` as keyof Self;
|
||||
|
||||
if (child) {
|
||||
children.unshift(child);
|
||||
if (typeof widget[setter] === 'function') {
|
||||
return widget[setter](value);
|
||||
}
|
||||
|
||||
return (widget[prop as keyof Self] = value);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`could not set property "${prop.toString()}" on ${widget}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
const children = props.children ?
|
||||
props.children instanceof Binding ?
|
||||
[props.children] as (Binding<Gtk.Widget[]> | Binding<Gtk.Widget> | Gtk.Widget)[] :
|
||||
props.children as Gtk.Widget[] :
|
||||
[];
|
||||
|
||||
if (props.child) {
|
||||
children.unshift(props.child);
|
||||
}
|
||||
|
||||
// remove undefined values
|
||||
for (const [key, value] of Object.entries(props)) {
|
||||
for (const [key, value] of entries) {
|
||||
if (typeof value === 'undefined') {
|
||||
delete props[key];
|
||||
}
|
||||
}
|
||||
|
||||
// collect bindings
|
||||
const bindings: [string, Binding<unknown>][] = Object
|
||||
.keys(props)
|
||||
.reduce((acc: [string, Binding<unknown>][], prop) => {
|
||||
if (props[prop] instanceof Binding) {
|
||||
const binding = props[prop];
|
||||
const bindings: [Key, Binding<unknown>][] = [];
|
||||
|
||||
delete props[prop];
|
||||
|
||||
return [...acc, [prop, binding]];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
for (const key of keys) {
|
||||
if (props[key] instanceof Binding) {
|
||||
bindings.push([key, props[key]]);
|
||||
delete props[key];
|
||||
}
|
||||
}
|
||||
|
||||
// collect signal handlers
|
||||
const onHandlers: [string, string | (() => unknown)][] = Object
|
||||
.keys(props)
|
||||
.reduce((acc: [string, Binding<unknown>][], key) => {
|
||||
if (key.startsWith('on')) {
|
||||
const sig = kebabify(key).split('-').slice(1).join('-');
|
||||
const handler = props[key];
|
||||
const onHandlers: [string, string | (() => void)][] = [];
|
||||
|
||||
delete props[key];
|
||||
for (const key of keys) {
|
||||
if (key.toString().startsWith('on')) {
|
||||
const sig = kebabify(key.toString()).split('-').slice(1).join('-');
|
||||
|
||||
return [...acc, [sig, handler]];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
onHandlers.push([sig, props[key] as string | (() => void)]);
|
||||
delete props[key];
|
||||
}
|
||||
}
|
||||
|
||||
// set children
|
||||
const mergedChildren = mergeBindings<Gtk.Widget>(children.flat(Infinity));
|
||||
|
||||
if (mergedChildren instanceof Binding) {
|
||||
widget[setChildren](mergedChildren.get());
|
||||
|
||||
widget.connect('destroy', mergedChildren.subscribe((v) => {
|
||||
widget[setChildren](v);
|
||||
}));
|
||||
|
@ -156,20 +190,20 @@ export const construct = <Widget extends Connectable & {
|
|||
}));
|
||||
}
|
||||
widget.connect('destroy', binding.subscribe((v: unknown) => {
|
||||
setProp(widget, prop, v);
|
||||
setProp(prop, v as Self[keyof Self]);
|
||||
}));
|
||||
setProp(widget, prop, binding.get());
|
||||
setProp(prop, binding.get() as Self[keyof Self]);
|
||||
}
|
||||
|
||||
// filter undefined values
|
||||
for (const [key, value] of Object.entries(props)) {
|
||||
for (const [key, value] of entries) {
|
||||
if (typeof value === 'undefined') {
|
||||
delete props[key];
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(widget, props);
|
||||
setup?.(widget);
|
||||
props.setup?.(widget);
|
||||
|
||||
return widget;
|
||||
};
|
||||
|
|
|
@ -1,44 +1,26 @@
|
|||
import Gdk from 'gi://Gdk?version=4.0';
|
||||
import Gtk from 'gi://Gtk?version=4.0';
|
||||
// A mixin class must have a constructor with a single rest parameter of type 'any[]'
|
||||
/* eslint "@typescript-eslint/no-explicit-any": ["error", { "ignoreRestArgs": true }] */
|
||||
|
||||
import { property, register } from 'astal';
|
||||
import { Gdk, Gtk } from 'astal/gtk4';
|
||||
import Binding, { type Connectable, type Subscribable } from 'astal/binding';
|
||||
|
||||
import {
|
||||
type EventController,
|
||||
hook,
|
||||
noImplicitDestroy,
|
||||
setChildren,
|
||||
construct,
|
||||
} from './_astal';
|
||||
|
||||
import { type AstalifyProps, type BindableProps } from './_astal';
|
||||
export { type AstalifyProps, type BindableProps };
|
||||
|
||||
export type BindableChild = Gtk.Widget | Binding<Gtk.Widget>;
|
||||
export interface AstalifyProps {
|
||||
css: string
|
||||
children: Gtk.Widget[]
|
||||
}
|
||||
|
||||
export const type = Symbol('child type');
|
||||
const dummyBuilder = new Gtk.Builder();
|
||||
|
||||
interface EventController<Self extends Gtk.Widget> {
|
||||
onFocusEnter?: (self: Self) => void
|
||||
onFocusLeave?: (self: Self) => void
|
||||
|
||||
onKeyPressed?: (self: Self, keyval: number, keycode: number, state: Gdk.ModifierType) => void
|
||||
onKeyReleased?: (self: Self, keyval: number, keycode: number, state: Gdk.ModifierType) => void
|
||||
onKeyModifier?: (self: Self, state: Gdk.ModifierType) => void
|
||||
|
||||
onLegacy?: (self: Self, event: Gdk.Event) => void
|
||||
onButtonPressed?: (self: Self, state: Gdk.ButtonEvent) => void
|
||||
onButtonReleased?: (self: Self, state: Gdk.ButtonEvent) => void
|
||||
|
||||
onHoverEnter?: (self: Self, x: number, y: number) => void
|
||||
onHoverLeave?: (self: Self) => void
|
||||
onMotion?: (self: Self, x: number, y: number) => void
|
||||
|
||||
onScroll?: (self: Self, dx: number, dy: number) => void
|
||||
onScrollDecelerate?: (self: Self, vel_x: number, vel_y: number) => void
|
||||
}
|
||||
|
||||
const setupControllers = <T>(widget: Gtk.Widget, {
|
||||
onFocusEnter,
|
||||
onFocusLeave,
|
||||
|
@ -79,7 +61,9 @@ const setupControllers = <T>(widget: Gtk.Widget, {
|
|||
onKeyReleased(widget, val, code, state));
|
||||
}
|
||||
|
||||
if (onKeyModifier) { key.connect('modifiers', (_, state) => onKeyModifier(widget, state)); }
|
||||
if (onKeyModifier) {
|
||||
key.connect('modifiers', (_, state) => onKeyModifier(widget, state));
|
||||
}
|
||||
}
|
||||
|
||||
if (onLegacy || onButtonPressed || onButtonReleased) {
|
||||
|
@ -105,11 +89,17 @@ const setupControllers = <T>(widget: Gtk.Widget, {
|
|||
|
||||
widget.add_controller(hover);
|
||||
|
||||
if (onHoverEnter) { hover.connect('enter', (_, x, y) => onHoverEnter(widget, x, y)); }
|
||||
if (onHoverEnter) {
|
||||
hover.connect('enter', (_, x, y) => onHoverEnter(widget, x, y));
|
||||
}
|
||||
|
||||
if (onHoverLeave) { hover.connect('leave', () => onHoverLeave(widget)); }
|
||||
if (onHoverLeave) {
|
||||
hover.connect('leave', () => onHoverLeave(widget));
|
||||
}
|
||||
|
||||
if (onMotion) { hover.connect('motion', (_, x, y) => onMotion(widget, x, y)); }
|
||||
if (onMotion) {
|
||||
hover.connect('motion', (_, x, y) => onMotion(widget, x, y));
|
||||
}
|
||||
}
|
||||
|
||||
if (onScroll || onScrollDecelerate) {
|
||||
|
@ -117,7 +107,9 @@ const setupControllers = <T>(widget: Gtk.Widget, {
|
|||
|
||||
widget.add_controller(scroll);
|
||||
|
||||
if (onScroll) { scroll.connect('scroll', (_, x, y) => onScroll(widget, x, y)); }
|
||||
if (onScroll) {
|
||||
scroll.connect('scroll', (_, x, y) => onScroll(widget, x, y));
|
||||
}
|
||||
|
||||
if (onScrollDecelerate) {
|
||||
scroll.connect('decelerate', (_, x, y) => onScrollDecelerate(widget, x, y));
|
||||
|
@ -127,8 +119,10 @@ const setupControllers = <T>(widget: Gtk.Widget, {
|
|||
return props;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export default <C extends new (...args: any[]) => Gtk.Widget>(
|
||||
export default <
|
||||
C extends new (...props: any[]) => Gtk.Widget,
|
||||
ConstructorProps,
|
||||
>(
|
||||
cls: C,
|
||||
clsName = cls.name,
|
||||
) => {
|
||||
|
@ -240,7 +234,6 @@ export default <C extends new (...args: any[]) => Gtk.Widget>(
|
|||
}
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(...params: any[]) {
|
||||
const props = params[0] || {};
|
||||
|
||||
|
@ -264,5 +257,12 @@ export default <C extends new (...args: any[]) => Gtk.Widget>(
|
|||
}
|
||||
}
|
||||
|
||||
return Widget;
|
||||
type Constructor<Instance, Props> = new (...args: Props[]) => Instance;
|
||||
|
||||
type WidgetClass = Constructor<
|
||||
Widget & Gtk.Widget & InstanceType<C>,
|
||||
Partial<BindableProps<ConstructorProps>>
|
||||
>;
|
||||
|
||||
return Widget as unknown as WidgetClass;
|
||||
};
|
||||
|
|
|
@ -12,8 +12,7 @@ export type BoxProps = ConstructProps<
|
|||
@register({ GTypeName: 'Box' })
|
||||
export class BoxClass extends astalify(Astal.Box) {
|
||||
constructor({ cssName = 'box', ...props }: BoxProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren(self: BoxClass) {
|
||||
|
|
|
@ -16,8 +16,7 @@ export type ButtonProps = ConstructProps<
|
|||
@register({ GTypeName: 'Button' })
|
||||
export class ButtonClass extends astalify(Gtk.Button) {
|
||||
constructor({ cssName = 'button', ...props }: ButtonProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,7 @@ export type CalendarProps = ConstructProps<
|
|||
@register({ GTypeName: 'Calendar' })
|
||||
export class CalendarClass extends astalify(Gtk.Calendar) {
|
||||
constructor({ cssName = 'calendar', ...props }: CalendarProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,7 @@ export type CenterBoxProps = ConstructProps<
|
|||
@register({ GTypeName: 'CenterBox' })
|
||||
export class CenterBoxClass extends astalify(Gtk.CenterBox) {
|
||||
constructor({ cssName = 'centerbox', ...props }: CenterBoxProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren(box: CenterBoxClass) {
|
||||
|
|
|
@ -17,8 +17,7 @@ export type EntryProps = ConstructProps<
|
|||
@register({ GTypeName: 'Entry' })
|
||||
export class EntryClass extends astalify(Gtk.Entry) {
|
||||
constructor({ cssName = 'entry', ...props }: EntryProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren() { return []; }
|
||||
|
|
|
@ -12,8 +12,7 @@ export type ImageProps = ConstructProps<
|
|||
@register({ GTypeName: 'Image' })
|
||||
export class ImageClass extends astalify(Gtk.Image) {
|
||||
constructor({ cssName = 'image', ...props }: ImageProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren() { return []; }
|
||||
|
|
|
@ -12,8 +12,7 @@ export type LabelProps = ConstructProps<
|
|||
@register({ GTypeName: 'Label' })
|
||||
export class LabelClass extends astalify(Gtk.Label) {
|
||||
constructor({ cssName = 'label', ...props }: LabelProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren() { return []; }
|
||||
|
|
|
@ -12,8 +12,7 @@ export type LevelBarProps = ConstructProps<
|
|||
@register({ GTypeName: 'LevelBar' })
|
||||
export class LevelBarClass extends astalify(Gtk.LevelBar) {
|
||||
constructor({ cssName = 'levelbar', ...props }: LevelBarProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren() { return []; }
|
||||
|
|
|
@ -12,8 +12,7 @@ export type MenuButtonProps = ConstructProps<
|
|||
@register({ GTypeName: 'MenuButton' })
|
||||
export class MenuButtonClass extends astalify(Gtk.MenuButton) {
|
||||
constructor({ cssName = 'menubutton', ...props }: MenuButtonProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren(self: MenuButtonClass) {
|
||||
|
|
|
@ -12,8 +12,7 @@ export type OverlayProps = ConstructProps<
|
|||
@register({ GTypeName: 'Overlay' })
|
||||
export class OverlayClass extends astalify(Gtk.Overlay) {
|
||||
constructor({ cssName = 'overlay', ...props }: OverlayProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren(self: OverlayClass) {
|
||||
|
|
|
@ -12,8 +12,7 @@ export type PopoverProps = ConstructProps<
|
|||
@register({ GTypeName: 'Popover' })
|
||||
export class PopoverClass extends astalify(Gtk.Popover) {
|
||||
constructor({ cssName = 'popover', ...props }: PopoverProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,7 @@ export type RevealerProps = ConstructProps<
|
|||
@register({ GTypeName: 'Revealer' })
|
||||
export class RevealerClass extends astalify(Gtk.Revealer) {
|
||||
constructor({ cssName = 'revealer', ...props }: RevealerProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,7 @@ export type SliderProps = ConstructProps<
|
|||
@register({ GTypeName: 'Slider' })
|
||||
export class SliderClass extends astalify(Astal.Slider) {
|
||||
constructor({ cssName = 'slider', ...props }: SliderProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren() { return []; }
|
||||
|
|
|
@ -12,8 +12,7 @@ export type StackProps = ConstructProps<
|
|||
@register({ GTypeName: 'Stack' })
|
||||
export class StackClass extends astalify(Gtk.Stack) {
|
||||
constructor({ cssName = 'stack', ...props }: StackProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
setChildren(self: StackClass, children: Gtk.Widget[]) {
|
||||
|
|
|
@ -12,8 +12,7 @@ export type SwitchProps = ConstructProps<
|
|||
@register({ GTypeName: 'Switch' })
|
||||
export class SwitchClass extends astalify(Gtk.Switch) {
|
||||
constructor({ cssName = 'switch', ...props }: SwitchProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
|
||||
getChildren() { return []; }
|
||||
|
|
|
@ -12,8 +12,7 @@ export type WindowProps = ConstructProps<
|
|||
@register({ GTypeName: 'Window' })
|
||||
export class WindowClass extends astalify(Astal.Window) {
|
||||
constructor({ cssName = 'window', ...props }: WindowProps = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
super({ cssName, ...props as any });
|
||||
super({ cssName, ...props });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue