From 2f09ed83386b334f0dfb7f376b99739b15e49fc9 Mon Sep 17 00:00:00 2001 From: Aylur Date: Sun, 24 Nov 2024 00:04:07 +0000 Subject: gjs gtk4 support export jsx-runtime --- lang/gjs/src/gtk4/widget.ts | 165 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 1 deletion(-) (limited to 'lang/gjs/src/gtk4/widget.ts') diff --git a/lang/gjs/src/gtk4/widget.ts b/lang/gjs/src/gtk4/widget.ts index 6c8ea4d..10d4b9f 100644 --- a/lang/gjs/src/gtk4/widget.ts +++ b/lang/gjs/src/gtk4/widget.ts @@ -1 +1,164 @@ -// TODO: +import Astal from "gi://Astal?version=4.0" +import Gtk from "gi://Gtk?version=4.0" +import astalify, { type, type ConstructProps } from "./astalify.js" + +function filter(children: any[]) { + return children.flat(Infinity).map(ch => ch instanceof Gtk.Widget + ? ch + : new Gtk.Label({ visible: true, label: String(ch) })) +} + +// Box +Object.defineProperty(Astal.Box.prototype, "children", { + get() { return this.get_children() }, + set(v) { this.set_children(v) }, +}) + +export type BoxProps = ConstructProps +export const Box = astalify(Astal.Box, { + getChildren(self) { return self.get_children() }, + setChildren(self, children) { return self.set_children(filter(children)) }, +}) + +// Button +export type ButtonProps = ConstructProps +export const Button = astalify(Gtk.Button) + +// CenterBox +export type CenterBoxProps = ConstructProps +export const CenterBox = astalify(Gtk.CenterBox, { + getChildren(box) { + return [box.startWidget, box.centerWidget, box.endWidget] + }, + setChildren(box, children) { + const ch = filter(children) + box.startWidget = ch[0] || new Gtk.Box + box.centerWidget = ch[1] || new Gtk.Box + box.endWidget = ch[2] || new Gtk.Box + }, +}) + +// TODO: CircularProgress +// TODO: DrawingArea + +// Entry +type EntrySignals = { + onActivate: [] + onNotifyText: [] +} + +export type EntryProps = ConstructProps +export const Entry = astalify(Gtk.Entry, { + getChildren() { return [] }, +}) + +// Image +export type ImageProps = ConstructProps +export const Image = astalify(Gtk.Image, { + getChildren() { return [] }, +}) + +// Label +export type LabelProps = ConstructProps +export const Label = astalify(Gtk.Label, { + getChildren() { return [] }, + setChildren(self, children) { self.label = String(children) }, +}) + +// LevelBar +export type LevelBarProps = ConstructProps +export const LevelBar = astalify(Gtk.LevelBar, { + getChildren() { return [] }, +}) + +// TODO: ListBox + +// Overlay +export type OverlayProps = ConstructProps +export const Overlay = astalify(Gtk.Overlay, { + getChildren(self) { + const children: Array = [] + let ch = self.get_first_child() + while (ch !== null) { + children.push(ch) + ch = ch.get_next_sibling() + } + + return children.filter(ch => ch !== self.child) + }, + setChildren(self, children) { + for (const child of filter(children)) { + const types = type in child + ? (child[type] as string).split(/\s+/) + : [] + + if (types.includes("overlay")) { + self.add_overlay(child) + } else { + self.set_child(child) + } + + self.set_measure_overlay(child, types.includes("measure")) + self.set_clip_overlay(child, types.includes("clip")) + } + }, +}) + +// Revealer +export type RevealerProps = ConstructProps +export const Revealer = astalify(Gtk.Revealer) + +// Slider +type SliderSignals = { + onChangeValue: [] +} + +export type SliderProps = ConstructProps +export const Slider = astalify(Astal.Slider, { + getChildren() { return [] }, +}) + +// Stack +export type StackProps = ConstructProps +export const Stack = astalify(Gtk.Stack, { + setChildren(self, children) { + for (const child of filter(children)) { + if (child.name != "" && child.name != null) { + self.add_named(child, child.name) + } else { + self.add_child(child) + } + } + }, +}) + +// Switch +export type SwitchProps = ConstructProps +export const Switch = astalify(Gtk.Switch, { + getChildren() { return [] }, +}) + +// Window +export type WindowProps = ConstructProps +export const Window = astalify(Astal.Window) + +// MenuButton +export type MenuButtonProps = ConstructProps +export const MenuButton = astalify(Gtk.MenuButton, { + getChildren(self) { return [self.popover, self.child] }, + setChildren(self, children) { + for (const child of filter(children)) { + if (child instanceof Gtk.Popover) { + self.set_popover(child) + } else { + self.set_child(child) + } + } + }, +}) + +// Popoper +export type PopoverProps = ConstructProps +export const Popover = astalify(Gtk.Popover) -- cgit v1.2.3 From 27544705dfe77b50f919a166f06d5b96560dd2fd Mon Sep 17 00:00:00 2001 From: Aylur Date: Sat, 21 Dec 2024 00:25:04 +0100 Subject: fix types --- lang/gjs/src/gtk4/widget.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lang/gjs/src/gtk4/widget.ts') diff --git a/lang/gjs/src/gtk4/widget.ts b/lang/gjs/src/gtk4/widget.ts index 10d4b9f..bd9091b 100644 --- a/lang/gjs/src/gtk4/widget.ts +++ b/lang/gjs/src/gtk4/widget.ts @@ -21,10 +21,12 @@ export const Box = astalify(Astal.Box, { }) // Button -export type ButtonProps = ConstructProps -export const Button = astalify(Gtk.Button) +} + +export type ButtonProps = ConstructProps +export const Button = astalify(Gtk.Button) // CenterBox export type CenterBoxProps = ConstructProps -- cgit v1.2.3