2025-01-14 10:12:20 -05:00
|
|
|
import { execAsync } from 'astal';
|
|
|
|
import { type Gtk, type ConstructProps } from 'astal/gtk4';
|
|
|
|
import { Binding, kebabify, snakeify } from 'astal/binding';
|
2025-01-14 00:09:11 -05:00
|
|
|
|
2025-01-14 10:12:20 -05:00
|
|
|
import { mergeBindings } from './bindings';
|
|
|
|
import { type EventController } from './controller';
|
|
|
|
import { type AstalifyProps, type BindableProps, type GenericWidget, setChildren } from './generics';
|
2025-01-14 00:09:11 -05:00
|
|
|
|
|
|
|
|
2025-01-14 10:12:20 -05:00
|
|
|
export default <
|
|
|
|
Self extends GenericWidget,
|
2025-01-14 00:09:11 -05:00
|
|
|
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][];
|
2025-01-09 12:59:25 -05:00
|
|
|
|
2025-01-14 00:09:11 -05:00
|
|
|
const setProp = (prop: Key, value: Self[keyof Self]) => {
|
|
|
|
try {
|
|
|
|
const setter = `set_${snakeify(prop.toString())}` as keyof Self;
|
|
|
|
|
|
|
|
if (typeof widget[setter] === 'function') {
|
|
|
|
return widget[setter](value);
|
|
|
|
}
|
2025-01-09 12:59:25 -05:00
|
|
|
|
2025-01-14 00:09:11 -05:00
|
|
|
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);
|
2025-01-09 12:59:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove undefined values
|
2025-01-14 00:09:11 -05:00
|
|
|
for (const [key, value] of entries) {
|
2025-01-09 12:59:25 -05:00
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
delete props[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect bindings
|
2025-01-14 00:09:11 -05:00
|
|
|
const bindings: [Key, Binding<unknown>][] = [];
|
2025-01-09 12:59:25 -05:00
|
|
|
|
2025-01-14 00:09:11 -05:00
|
|
|
for (const key of keys) {
|
|
|
|
if (props[key] instanceof Binding) {
|
|
|
|
bindings.push([key, props[key]]);
|
|
|
|
delete props[key];
|
|
|
|
}
|
|
|
|
}
|
2025-01-09 12:59:25 -05:00
|
|
|
|
|
|
|
// collect signal handlers
|
2025-01-14 00:09:11 -05:00
|
|
|
const onHandlers: [string, string | (() => void)][] = [];
|
2025-01-09 12:59:25 -05:00
|
|
|
|
2025-01-14 00:09:11 -05:00
|
|
|
for (const key of keys) {
|
|
|
|
if (key.toString().startsWith('on')) {
|
|
|
|
const sig = kebabify(key.toString()).split('-').slice(1).join('-');
|
2025-01-09 12:59:25 -05:00
|
|
|
|
2025-01-14 00:09:11 -05:00
|
|
|
onHandlers.push([sig, props[key] as string | (() => void)]);
|
|
|
|
delete props[key];
|
|
|
|
}
|
|
|
|
}
|
2025-01-09 12:59:25 -05:00
|
|
|
|
|
|
|
// set children
|
2025-01-12 20:12:46 -05:00
|
|
|
const mergedChildren = mergeBindings<Gtk.Widget>(children.flat(Infinity));
|
2025-01-09 12:59:25 -05:00
|
|
|
|
|
|
|
if (mergedChildren instanceof Binding) {
|
|
|
|
widget[setChildren](mergedChildren.get());
|
2025-01-14 00:09:11 -05:00
|
|
|
|
2025-01-09 12:59:25 -05:00
|
|
|
widget.connect('destroy', mergedChildren.subscribe((v) => {
|
|
|
|
widget[setChildren](v);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
else if (mergedChildren.length > 0) {
|
|
|
|
widget[setChildren](mergedChildren);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup signal handlers
|
|
|
|
for (const [signal, callback] of onHandlers) {
|
|
|
|
const sig = signal.startsWith('notify') ?
|
|
|
|
signal.replace('-', '::') :
|
|
|
|
signal;
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
widget.connect(sig, callback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget.connect(sig, () => execAsync(callback)
|
|
|
|
.then(print).catch(console.error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup bindings handlers
|
|
|
|
for (const [prop, binding] of bindings) {
|
|
|
|
if (prop === 'child' || prop === 'children') {
|
2025-01-12 20:12:46 -05:00
|
|
|
widget.connect('destroy', (binding as Binding<Gtk.Widget[]>).subscribe((v: Gtk.Widget[]) => {
|
2025-01-09 12:59:25 -05:00
|
|
|
widget[setChildren](v);
|
|
|
|
}));
|
|
|
|
}
|
2025-01-12 20:12:46 -05:00
|
|
|
widget.connect('destroy', binding.subscribe((v: unknown) => {
|
2025-01-14 00:09:11 -05:00
|
|
|
setProp(prop, v as Self[keyof Self]);
|
2025-01-09 12:59:25 -05:00
|
|
|
}));
|
2025-01-14 00:09:11 -05:00
|
|
|
setProp(prop, binding.get() as Self[keyof Self]);
|
2025-01-09 12:59:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// filter undefined values
|
2025-01-14 00:09:11 -05:00
|
|
|
for (const [key, value] of entries) {
|
2025-01-09 12:59:25 -05:00
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
delete props[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(widget, props);
|
2025-01-14 00:09:11 -05:00
|
|
|
props.setup?.(widget);
|
2025-01-09 12:59:25 -05:00
|
|
|
|
|
|
|
return widget;
|
|
|
|
};
|