@gtkx/react 1.4.0 → 1.6.0

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.
@@ -1,9 +1,7 @@
1
- import type * as Gtk from "@gtkx/gi/gtk";
2
1
  import * as Adw from "@gtkx/gi/adw";
2
+ import * as Gtk from "@gtkx/gi/gtk";
3
3
  import type { AlertDialogResponse } from "./prop-types.js";
4
4
  import {
5
- addRemoveSlot,
6
- adoptedChildrenSlot,
7
5
  applicationCreator,
8
6
  boxSlot,
9
7
  childMatcher,
@@ -12,6 +10,8 @@ import {
12
10
  deferred,
13
11
  indexedSlot,
14
12
  list,
13
+ methodSlot,
14
+ setterSlot,
15
15
  slot,
16
16
  } from "../reconciler/behaviors.js";
17
17
  import { type ElementBehavior, type ElementConfig, forTypes, registerElements } from "../reconciler/registry.js";
@@ -35,60 +35,27 @@ type PrefixSuffixRow = Adw.ActionRow | Adw.EntryRow | Adw.ExpanderRow;
35
35
 
36
36
  const SLOT_SUFFIX = "Slot";
37
37
  const childSetter = childSetterSlot<AdwChildSetter>();
38
- const contentSetter = contentSetterSlot<AdwContentSetter>();
39
-
40
- const breakpoints = slot<BreakpointHost, Adw.Breakpoint>("breakpoints", "AdwBreakpoint", {
41
- attach: (host, breakpoint) => {
42
- host.addBreakpoint(breakpoint);
43
- },
44
- });
38
+ const contentSetter = contentSetterSlot<AdwContentSetter>(Gtk.Widget);
39
+ const breakpoints = methodSlot<BreakpointHost, Adw.Breakpoint>("breakpoints", Adw.Breakpoint, "addBreakpoint");
45
40
 
46
41
  const prefixSuffix = [
47
- addRemoveSlot<Gtk.Widget, PrefixSuffixRow>(
48
- "prefix",
49
- "GtkWidget",
50
- (row, child) => {
51
- row.addPrefix(child);
52
- },
53
- (row, child) => {
54
- row.remove(child);
55
- },
56
- ),
57
- addRemoveSlot<Gtk.Widget, PrefixSuffixRow>(
58
- "suffix",
59
- "GtkWidget",
60
- (row, child) => {
61
- row.addSuffix(child);
62
- },
63
- (row, child) => {
64
- row.remove(child);
65
- },
66
- ),
42
+ methodSlot<PrefixSuffixRow, Gtk.Widget>("prefix", Gtk.Widget, "addPrefix", "remove"),
43
+ methodSlot<PrefixSuffixRow, Gtk.Widget>("suffix", Gtk.Widget, "addSuffix", "remove"),
67
44
  ];
68
45
 
69
- const preferencesDialogChildren = addRemoveSlot<Adw.PreferencesPage, Adw.PreferencesDialog>(
70
- "children",
71
- "AdwPreferencesPage",
72
- (dialog, page) => {
73
- dialog.add(page);
74
- },
75
- (dialog, page) => {
76
- dialog.remove(page);
77
- },
46
+ const preferencesDialogChildren = methodSlot<Adw.PreferencesDialog, Adw.PreferencesPage>(
47
+ "children", Adw.PreferencesPage, "add", "remove",
78
48
  );
79
49
 
80
- const alertDialogExtraChild = slot<Adw.AlertDialog, Gtk.Widget>("children", "GtkWidget", {
81
- attach: (dialog, child) => {
82
- dialog.setExtraChild(child);
83
- },
84
- detach: (dialog) => {
85
- dialog.setExtraChild(null);
86
- },
87
- });
50
+ const alertDialogExtraChild = setterSlot<Adw.AlertDialog, Gtk.Widget>("children", Gtk.Widget, "setExtraChild");
51
+ const sidebarSections = indexedSlot<Adw.Sidebar, Adw.SidebarSection>("children", Adw.SidebarSection);
52
+ const sidebarItems = indexedSlot<Adw.SidebarSection, Adw.SidebarItem>("children", Adw.SidebarItem);
53
+ const isWidget = childMatcher(Gtk.Widget);
88
54
 
89
- const sidebarSections = indexedSlot<Adw.Sidebar, Adw.SidebarSection>("children", "AdwSidebarSection");
90
- const sidebarItems = indexedSlot<Adw.SidebarSection, Adw.SidebarItem>("children", "AdwSidebarItem");
91
- const isWidget = childMatcher("GtkWidget");
55
+ const scrollableWidget = {
56
+ [Symbol.hasInstance]: (value: unknown): value is Gtk.Scrollable & Gtk.Widget =>
57
+ value instanceof Gtk.Scrollable && value instanceof Gtk.Widget,
58
+ };
92
59
 
93
60
  const multiLayoutSlots: ElementBehavior<Adw.MultiLayoutView> = {
94
61
  attach: (view, child, info) => {
@@ -98,7 +65,7 @@ const multiLayoutSlots: ElementBehavior<Adw.MultiLayoutView> = {
98
65
  return;
99
66
  }
100
67
 
101
- view.setChild(id, child as Gtk.Widget);
68
+ view.setChild(id, child);
102
69
 
103
70
  return true;
104
71
  },
@@ -109,15 +76,13 @@ const multiLayoutSlots: ElementBehavior<Adw.MultiLayoutView> = {
109
76
  return;
110
77
  }
111
78
 
112
- const widget = child as Gtk.Widget;
113
-
114
- if (view.getChild(id) === widget) {
115
- widget.unparent();
79
+ if (view.getChild(id) === child) {
80
+ child.unparent();
116
81
  }
117
82
  },
118
83
  };
119
84
 
120
- const multiLayoutLayouts = slot<Adw.MultiLayoutView, Gtk.Widget>("layouts", "GtkWidget", {
85
+ const multiLayoutLayouts = slot<Adw.MultiLayoutView, Gtk.Widget>("layouts", Gtk.Widget, {
121
86
  attach: (view, content) => {
122
87
  const layout = new Adw.Layout({ content });
123
88
  view.addLayout(layout);
@@ -151,14 +116,7 @@ const BUILTIN_BEHAVIORS: Record<string, ElementConfig<never>> = {
151
116
  },
152
117
  AdwClampScrollable: {
153
118
  behaviors: [
154
- slot<Adw.ClampScrollable, Gtk.Scrollable & Gtk.Widget>("children", "GtkScrollable", {
155
- attach: (clamp, child) => {
156
- clamp.setChild(child);
157
- },
158
- detach: (clamp) => {
159
- clamp.setChild(null);
160
- },
161
- }),
119
+ setterSlot<Adw.ClampScrollable, Gtk.Scrollable & Gtk.Widget>("children", scrollableWidget, "setChild"),
162
120
  ],
163
121
  },
164
122
  ...forTypes(CHILD_SETTER_TYPES, {
@@ -179,15 +137,8 @@ const BUILTIN_BEHAVIORS: Record<string, ElementConfig<never>> = {
179
137
  AdwBreakpointBin: {
180
138
  behaviors: [
181
139
  childSetter,
182
- addRemoveSlot<Adw.Breakpoint, Adw.BreakpointBin>(
183
- "breakpoints",
184
- "AdwBreakpoint",
185
- (bin, breakpoint) => {
186
- bin.addBreakpoint(breakpoint);
187
- },
188
- (bin, breakpoint) => {
189
- bin.removeBreakpoint(breakpoint);
190
- },
140
+ methodSlot<Adw.BreakpointBin, Adw.Breakpoint>(
141
+ "breakpoints", Adw.Breakpoint, "addBreakpoint", "removeBreakpoint",
191
142
  ),
192
143
  ],
193
144
  },
@@ -200,27 +151,18 @@ const BUILTIN_BEHAVIORS: Record<string, ElementConfig<never>> = {
200
151
  AdwExpanderRow: {
201
152
  behaviors: [
202
153
  ...prefixSuffix,
203
- addRemoveSlot<Gtk.Widget, Adw.ExpanderRow>(
204
- "rows",
205
- "GtkWidget",
206
- (row, child) => {
207
- row.addRow(child);
208
- },
209
- (row, child) => {
210
- row.remove(child);
211
- },
212
- ),
154
+ methodSlot<Adw.ExpanderRow, Gtk.Widget>("rows", Gtk.Widget, "addRow", "remove"),
213
155
  ],
214
156
  },
215
157
  AdwNavigationSplitView: {
216
- behaviors: [contentSetterSlot<Adw.NavigationSplitView, Adw.NavigationPage>("AdwNavigationPage")],
158
+ behaviors: [contentSetterSlot<Adw.NavigationSplitView, Adw.NavigationPage>(Adw.NavigationPage)],
217
159
  },
218
160
  AdwWrapBox: {
219
161
  behaviors: [boxSlot<Adw.WrapBox>()],
220
162
  },
221
163
  AdwCarousel: {
222
164
  behaviors: [
223
- slot<Adw.Carousel, Gtk.Widget>("children", "GtkWidget", {
165
+ slot<Adw.Carousel, Gtk.Widget>("children", Gtk.Widget, {
224
166
  attach: (carousel, child, info) => {
225
167
  carousel.insert(child, info.index);
226
168
  },
@@ -235,7 +177,7 @@ const BUILTIN_BEHAVIORS: Record<string, ElementConfig<never>> = {
235
177
  },
236
178
  AdwPreferencesPage: {
237
179
  behaviors: [
238
- slot<Adw.PreferencesPage, Adw.PreferencesGroup>("children", "AdwPreferencesGroup", {
180
+ slot<Adw.PreferencesPage, Adw.PreferencesGroup>("children", Adw.PreferencesGroup, {
239
181
  attach: (page, group, info) => {
240
182
  page.insert(group, info.index);
241
183
  },
@@ -249,22 +191,11 @@ const BUILTIN_BEHAVIORS: Record<string, ElementConfig<never>> = {
249
191
  behaviors: [preferencesDialogChildren],
250
192
  },
251
193
  AdwPreferencesGroup: {
252
- behaviors: [
253
- addRemoveSlot<Gtk.Widget, Adw.PreferencesGroup>(
254
- "children",
255
- "GtkWidget",
256
- (group, child) => {
257
- group.add(child);
258
- },
259
- (group, child) => {
260
- group.remove(child);
261
- },
262
- ),
263
- ],
194
+ behaviors: [methodSlot<Adw.PreferencesGroup, Gtk.Widget>("children", Gtk.Widget, "add", "remove")],
264
195
  },
265
196
  AdwTabView: {
266
197
  behaviors: [
267
- slot<Adw.TabView, Gtk.Widget>("children", "GtkWidget", {
198
+ slot<Adw.TabView, Gtk.Widget>("children", Gtk.Widget, {
268
199
  attach: (view, child, info) => view.insert(child, info.index),
269
200
  reorder: (view, _child, info) => {
270
201
  if (info.adopted instanceof Adw.TabPage) {
@@ -282,105 +213,37 @@ const BUILTIN_BEHAVIORS: Record<string, ElementConfig<never>> = {
282
213
  },
283
214
  AdwNavigationView: {
284
215
  behaviors: [
285
- addRemoveSlot<Adw.NavigationPage, Adw.NavigationView>(
286
- "children",
287
- "AdwNavigationPage",
288
- (view, page) => {
289
- view.add(page);
290
- },
291
- (view, page) => {
292
- view.remove(page);
293
- },
294
- ),
216
+ methodSlot<Adw.NavigationView, Adw.NavigationPage>("children", Adw.NavigationPage, "add", "remove"),
295
217
  ],
296
218
  },
297
219
  AdwViewStack: {
298
220
  behaviors: [
299
- adoptedChildrenSlot<Adw.ViewStack, Gtk.Widget>(
300
- "GtkWidget",
301
- (stack, child) => stack.add(child),
302
- (stack, child) => {
303
- stack.remove(child);
304
- },
305
- ),
221
+ methodSlot<Adw.ViewStack, Gtk.Widget>("children", Gtk.Widget, "add", "remove"),
306
222
  deferred<Adw.ViewStack, string>("visibleChildName", (stack, name) => stack.getChildByName(name) !== null),
307
223
  ],
308
224
  },
309
225
  AdwToolbarView: {
310
226
  behaviors: [
311
- contentSetterSlot<Adw.ToolbarView>(),
312
- addRemoveSlot<Gtk.Widget, Adw.ToolbarView>(
313
- "topBar",
314
- "GtkWidget",
315
- (view, child) => {
316
- view.addTopBar(child);
317
- },
318
- (view, child) => {
319
- view.remove(child);
320
- },
321
- ),
322
- addRemoveSlot<Gtk.Widget, Adw.ToolbarView>(
323
- "bottomBar",
324
- "GtkWidget",
325
- (view, child) => {
326
- view.addBottomBar(child);
327
- },
328
- (view, child) => {
329
- view.remove(child);
330
- },
331
- ),
227
+ contentSetterSlot<Adw.ToolbarView>(Gtk.Widget),
228
+ methodSlot<Adw.ToolbarView, Gtk.Widget>("topBar", Gtk.Widget, "addTopBar", "remove"),
229
+ methodSlot<Adw.ToolbarView, Gtk.Widget>("bottomBar", Gtk.Widget, "addBottomBar", "remove"),
332
230
  ],
333
231
  },
334
232
  AdwHeaderBar: {
335
233
  behaviors: [
336
- slot<Adw.HeaderBar, Gtk.Widget>("start", "GtkWidget", {
337
- attach: (bar, child) => {
338
- bar.packStart(child);
339
- },
340
- detach: (bar, child) => {
341
- bar.remove(child);
342
- },
343
- }),
344
- slot<Adw.HeaderBar, Gtk.Widget>("end", "GtkWidget", {
345
- attach: (bar, child) => {
346
- bar.packEnd(child);
347
- },
348
- detach: (bar, child) => {
349
- bar.remove(child);
350
- },
351
- }),
234
+ methodSlot<Adw.HeaderBar, Gtk.Widget>("start", Gtk.Widget, "packStart", "remove"),
235
+ methodSlot<Adw.HeaderBar, Gtk.Widget>("end", Gtk.Widget, "packEnd", "remove"),
352
236
  ],
353
237
  },
354
238
  AdwShortcutsDialog: {
355
- behaviors: [
356
- slot<Adw.ShortcutsDialog, Adw.ShortcutsSection>("children", "AdwShortcutsSection", {
357
- attach: (dialog, section) => {
358
- dialog.add(section);
359
- },
360
- }),
361
- ],
239
+ behaviors: [methodSlot<Adw.ShortcutsDialog, Adw.ShortcutsSection>("children", Adw.ShortcutsSection, "add")],
362
240
  },
363
241
  AdwShortcutsSection: {
364
- behaviors: [
365
- slot<Adw.ShortcutsSection, Adw.ShortcutsItem>("children", "AdwShortcutsItem", {
366
- attach: (section, item) => {
367
- section.add(item);
368
- },
369
- }),
370
- ],
242
+ behaviors: [methodSlot<Adw.ShortcutsSection, Adw.ShortcutsItem>("children", Adw.ShortcutsItem, "add")],
371
243
  },
372
244
  AdwToggleGroup: {
373
245
  behaviors: [
374
- addRemoveSlot<Adw.Toggle, Adw.ToggleGroup>(
375
- "children",
376
- "AdwToggle",
377
- (group, toggle) => {
378
- group.add(toggle);
379
- },
380
- (group, toggle) => {
381
- group.remove(toggle);
382
- },
383
- ),
246
+ methodSlot<Adw.ToggleGroup, Adw.Toggle>("children", Adw.Toggle, "add", "remove"),
384
247
  deferred<Adw.ToggleGroup, string>("activeName", (group, name) => group.getToggleByName(name) !== null),
385
248
  deferred("active"),
386
249
  ],
@@ -72,11 +72,19 @@ const buildElement = (typeName: string, record: Props): ReactElement => {
72
72
  * Left out, the component takes `unknown`, which accepts no attributes at all.
73
73
  *
74
74
  * @param typeName GType name to render, such as `GtkButton` or the `typeName` given to `registerClass`.
75
+ * @param cls Wrapper class the name resolves to. Referencing it keeps the class, and with it the
76
+ * registration the reconciler's name lookup depends on, in a tree-shaken bundle; the component
77
+ * itself renders through the name.
78
+ * @param metadata Registered metadata entry for the type. Referencing it keeps the type's property
79
+ * and signal tables, which register themselves when evaluated, in a tree-shaken bundle.
75
80
  * @returns A component taking that type's props.
76
81
  */
77
82
  /* eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- callers name the props */
78
- function createElementComponent<P = unknown>(typeName: string): (props: P) => ReactNode {
79
- return (props: P): ReactNode => buildElement(typeName, isRecord(props) ? props : {});
80
- }
83
+ const createElementComponent: <P = unknown>(
84
+ typeName: string,
85
+ cls?: unknown,
86
+ metadata?: unknown,
87
+ ) => (props: P) => ReactNode =
88
+ (typeName) => (props): ReactNode => buildElement(typeName, isRecord(props) ? props : {});
81
89
 
82
90
  export { Prop, createElementComponent };