@gtkx/react 0.1.12 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -179,8 +179,8 @@ console.log(ref.value);
179
179
  ```tsx
180
180
  <Switch
181
181
  active={enabled}
182
- onStateSet={() => {
183
- setEnabled(e => !e);
182
+ onStateSet={(self, state) => {
183
+ setEnabled(state);
184
184
  return true;
185
185
  }}
186
186
  />
@@ -197,14 +197,15 @@ console.log(ref.value);
197
197
  **SpinButton** — Numeric input with increment/decrement
198
198
 
199
199
  ```tsx
200
- const adjustment = useRef(new Gtk.Adjustment({ value: 50, lower: 0, upper: 100, stepIncrement: 1 }));
201
- <SpinButton adjustment={adjustment.current.ptr} onValueChanged={() => setValue(adjustment.current.getValue())} />
200
+ // Adjustment args: value, lower, upper, stepIncrement, pageIncrement, pageSize
201
+ const adjustment = useMemo(() => new Gtk.Adjustment(50, 0, 100, 1, 10, 0), []);
202
+ <SpinButton adjustment={adjustment} onValueChanged={(self) => setValue(self.getValue())} />
202
203
  ```
203
204
 
204
205
  **Scale** — Horizontal or vertical slider
205
206
 
206
207
  ```tsx
207
- <Scale hexpand drawValue adjustment={adjustment.current.ptr} />
208
+ <Scale hexpand drawValue adjustment={adjustment} onValueChanged={(self) => setValue(self.getValue())} />
208
209
  ```
209
210
 
210
211
  ### Display Widgets
@@ -111,14 +111,14 @@ export class JsxGenerator {
111
111
  `import type { ReactNode, Ref } from "react";`,
112
112
  ...externalImports,
113
113
  `import type * as Gtk from "@gtkx/ffi/gtk";`,
114
- `import type { GridChildProps, ItemProps, SlotProps } from "../types.js";`,
114
+ `import type { GridChildProps, ListItemProps, SlotProps } from "../types.js";`,
115
115
  "",
116
116
  ].join("\n");
117
117
  }
118
118
  generateCommonTypes(widgetClass) {
119
119
  const widgetPropsContent = this.generateWidgetPropsContent(widgetClass);
120
120
  return `
121
- export { SlotProps, GridChildProps, ItemProps };
121
+ export { SlotProps, GridChildProps, ListItemProps };
122
122
 
123
123
  ${widgetPropsContent}
124
124
  `;
@@ -145,7 +145,7 @@ ${widgetPropsContent}
145
145
  if (signal.doc) {
146
146
  lines.push(formatDoc(signal.doc, "\t").trimEnd());
147
147
  }
148
- lines.push(`\t${this.generateSignalHandler(signal)}`);
148
+ lines.push(`\t${this.generateSignalHandler(signal, "Widget")}`);
149
149
  }
150
150
  }
151
151
  }
@@ -304,7 +304,7 @@ ${widgetPropsContent}
304
304
  if (signal.doc) {
305
305
  lines.push(formatDoc(signal.doc, "\t").trimEnd());
306
306
  }
307
- lines.push(`\t${this.generateSignalHandler(signal)}`);
307
+ lines.push(`\t${this.generateSignalHandler(signal, widgetName)}`);
308
308
  }
309
309
  }
310
310
  if (isListWidget(widget.name)) {
@@ -412,10 +412,10 @@ ${widgetPropsContent}
412
412
  }
413
413
  return undefined;
414
414
  }
415
- generateSignalHandler(signal) {
415
+ generateSignalHandler(signal, widgetName) {
416
416
  const signalName = toCamelCase(signal.name);
417
417
  const handlerName = `on${signalName.charAt(0).toUpperCase()}${signalName.slice(1)}`;
418
- const handlerType = this.buildSignalHandlerType(signal);
418
+ const handlerType = this.buildSignalHandlerType(signal, widgetName);
419
419
  return `${handlerName}?: ${handlerType};`;
420
420
  }
421
421
  getSignalParamFfiType(typeName) {
@@ -446,8 +446,9 @@ ${widgetPropsContent}
446
446
  return tsType;
447
447
  return `Gtk.${tsType}`;
448
448
  }
449
- buildSignalHandlerType(signal) {
450
- const params = signal.parameters
449
+ buildSignalHandlerType(signal, widgetName) {
450
+ const selfParam = `self: Gtk.${toPascalCase(widgetName)}`;
451
+ const otherParams = signal.parameters
451
452
  ?.map((p) => {
452
453
  const ffiType = this.getSignalParamFfiType(p.type.name);
453
454
  if (ffiType) {
@@ -461,7 +462,8 @@ ${widgetPropsContent}
461
462
  const returnType = signal.returnType
462
463
  ? this.addNamespacePrefix(this.typeMapper.mapType(signal.returnType).ts)
463
464
  : "void";
464
- return params ? `(${params}) => ${returnType}` : `() => ${returnType}`;
465
+ const params = otherParams ? `${selfParam}, ${otherParams}` : selfParam;
466
+ return `(${params}) => ${returnType}`;
465
467
  }
466
468
  generateExports(widgets, containerMetadata) {
467
469
  const lines = [];
@@ -516,10 +518,10 @@ ${widgetPropsContent}
516
518
  elements.push(`"${widgetName}.${slot.slotName}": SlotProps;`);
517
519
  }
518
520
  if (isListWidget(widget.name)) {
519
- elements.push(`"${widgetName}.Item": ItemProps<any>;`);
521
+ elements.push(`"${widgetName}.Item": ListItemProps;`);
520
522
  }
521
523
  if (isDropDownWidget(widget.name)) {
522
- elements.push(`"${widgetName}.Item": ItemProps<any>;`);
524
+ elements.push(`"${widgetName}.Item": ListItemProps;`);
523
525
  }
524
526
  if (isGridWidget(widget.name)) {
525
527
  elements.push(`"${widgetName}.Child": GridChildProps;`);
package/dist/factory.js CHANGED
@@ -1,11 +1,6 @@
1
- import * as Gtk from "@gtkx/ffi/gtk";
2
- import { CONSTRUCTOR_PARAMS } from "./generated/jsx.js";
3
- import { ActionBarNode } from "./nodes/action-bar.js";
4
- import { DialogNode } from "./nodes/dialog.js";
5
1
  import { DropDownItemNode, DropDownNode } from "./nodes/dropdown.js";
6
2
  import { GridChildNode, GridNode } from "./nodes/grid.js";
7
3
  import { ListItemNode, ListViewNode } from "./nodes/list.js";
8
- import { NotebookNode } from "./nodes/notebook.js";
9
4
  import { OverlayNode } from "./nodes/overlay.js";
10
5
  import { SlotNode } from "./nodes/slot.js";
11
6
  import { WidgetNode } from "./nodes/widget.js";
@@ -16,43 +11,14 @@ const NODE_CLASSES = [
16
11
  DropDownNode,
17
12
  GridChildNode,
18
13
  GridNode,
19
- NotebookNode,
20
14
  OverlayNode,
21
- ActionBarNode,
22
15
  ListViewNode,
23
- DialogNode,
24
16
  WidgetNode,
25
17
  ];
26
- const extractConstructorArgs = (type, props) => {
27
- const params = CONSTRUCTOR_PARAMS[type];
28
- if (!params)
29
- return [];
30
- return params.map((p) => props[p.name]);
31
- };
32
- const createWidget = (type, props, currentApp) => {
33
- const WidgetClass = Gtk[type];
34
- if (!WidgetClass)
35
- throw new Error(`Unknown GTK widget type: ${type}`);
36
- if (type === "ApplicationWindow") {
37
- return new WidgetClass(currentApp);
38
- }
39
- const args = extractConstructorArgs(type, props);
40
- return new WidgetClass(...args);
41
- };
42
- const normalizeType = (type) => (type.endsWith(".Root") ? type.slice(0, -5) : type);
43
18
  export const createNode = (type, props, currentApp) => {
44
- const normalizedType = normalizeType(type);
45
- let widget = null;
46
19
  for (const NodeClass of NODE_CLASSES) {
47
- if (NodeClass.needsWidget && !widget) {
48
- widget = createWidget(normalizedType, props, currentApp);
49
- }
50
- if (NodeClass.matches(type, widget)) {
51
- const node = new NodeClass(type, widget, props);
52
- if (NodeClass.needsWidget) {
53
- node.updateProps({}, props);
54
- }
55
- return node;
20
+ if (NodeClass.matches(type)) {
21
+ return new NodeClass(type, props, currentApp);
56
22
  }
57
23
  }
58
24
  throw new Error(`No matching node class for type: ${type}`);