summaryrefslogtreecommitdiff
path: root/gjs/src
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-05-25 18:01:46 +0200
committerAylur <[email protected]>2024-05-25 18:01:46 +0200
commitd11c29d81695860f9331ec4afb6201e39529dd61 (patch)
tree3fde44709665e7c5d0c145d34144bd8d631c4478 /gjs/src
parent24322cb85c337bd70b36452c1b4d92a5b7b4caf4 (diff)
add(gjs): jsx support
Diffstat (limited to 'gjs/src')
-rw-r--r--gjs/src/jsx/jsx-runtime.ts65
1 files changed, 65 insertions, 0 deletions
diff --git a/gjs/src/jsx/jsx-runtime.ts b/gjs/src/jsx/jsx-runtime.ts
new file mode 100644
index 0000000..f7f0582
--- /dev/null
+++ b/gjs/src/jsx/jsx-runtime.ts
@@ -0,0 +1,65 @@
+import { Gtk, Astal } 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
+
+ return Widget.Label({ label: String(v) })
+ })
+ }
+ else if (children instanceof Gtk.Widget) {
+ ch = [children]
+ }
+
+ const widget = 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 = {
+ "box": Widget.Box,
+ "button": Widget.Button,
+ "centerbox": Widget.CenterBox,
+ "eventbox": Widget.EventBox,
+ "icon": Widget.Icon,
+ "label": Widget.Label,
+ "window": Widget.Window,
+}
+
+// eslint-disable-next-line @typescript-eslint/no-namespace
+export namespace JSX {
+ export type Element = Gtk.Widget
+ export interface IntrinsicElements {
+ "box": Widget.BoxProps
+ "button": Widget.ButtonProps
+ "centerbox": Widget.CenterBoxProps
+ "eventbox": Widget.EventBoxProps
+ "icon": Widget.IconProps
+ "label": Widget.LabelProps
+ "window": Widget.WindowProps
+ }
+}
+
+export const jsxs = jsx