summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-05-25 18:50:16 +0200
committerAylur <[email protected]>2024-05-25 18:50:16 +0200
commitca26397c808d2fc202d0f00c054798775f115cc4 (patch)
tree5ecf110ad0217a3fb740d936f1b870b63677bf18
parentd11c29d81695860f9331ec4afb6201e39529dd61 (diff)
improve jsx
edge cases will need to be handled as widgets are added
-rw-r--r--gjs/src/jsx/jsx-runtime.ts55
1 files changed, 29 insertions, 26 deletions
diff --git a/gjs/src/jsx/jsx-runtime.ts b/gjs/src/jsx/jsx-runtime.ts
index f7f0582..9495da9 100644
--- a/gjs/src/jsx/jsx-runtime.ts
+++ b/gjs/src/jsx/jsx-runtime.ts
@@ -1,41 +1,44 @@
-import { Gtk, Astal } from "../imports.js"
+import { Gtk } from "../imports.js"
import * as Widget from "../widgets.js"
export function jsx(
ctor: keyof typeof ctors | typeof Gtk.Widget,
{ children, ...props }: any,
) {
- let ch: any[] = []
- if (Array.isArray(children)) {
- ch = children.map(v => {
- if (v instanceof Gtk.Widget)
- return v
+ children ??= []
- return Widget.Label({ label: String(v) })
- })
+ if (!Array.isArray(children))
+ children = [children]
+
+ if (children.length === 1) {
+ props.child = children[0]
+ delete props.children
+ }
+
+ children = children.map((v: any) => {
+ if (v instanceof Gtk.Widget)
+ return v
+
+ return Widget.Label({ label: String(v) })
+ })
+
+ if (ctor === Widget.CenterBox) {
+ if (children[0])
+ props.startWidget = children[0]
+ if (children[1])
+ props.centerWidget = children[1]
+ if (children[2])
+ props.endWidget = children[2]
}
- else if (children instanceof Gtk.Widget) {
- ch = [children]
+
+ if (ctor === Widget.Label) {
+ if (children[0])
+ props.label = children[0]
}
- const widget = typeof ctor === "string"
+ return typeof ctor === "string"
? (ctors as any)[ctor](props)
: new ctor(props)
-
- if (widget instanceof Astal.CenterBox) {
- if (ch[0])
- widget.startWidget = ch[0]
- if (ch[1])
- widget.centerWidget = ch[1]
- if (ch[2])
- widget.endWidget = ch[2]
- } else {
- for (const w of ch) {
- if (widget instanceof Gtk.Container)
- widget.add(w)
- }
- }
- return widget
}
const ctors = {