@gtkx/testing 2.0.0-beta.1 → 2.0.0-beta.11

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.
Files changed (143) hide show
  1. package/README.md +8 -165
  2. package/dist/accessible-native.d.ts +1 -1
  3. package/dist/accessible-native.d.ts.map +1 -1
  4. package/dist/accessible-native.js +31 -10
  5. package/dist/accessible-native.js.map +1 -1
  6. package/dist/build-queries.d.ts +15 -16
  7. package/dist/build-queries.d.ts.map +1 -1
  8. package/dist/build-queries.js +25 -23
  9. package/dist/build-queries.js.map +1 -1
  10. package/dist/cleanup-registry.d.ts +2 -1
  11. package/dist/cleanup-registry.d.ts.map +1 -1
  12. package/dist/cleanup-registry.js +11 -4
  13. package/dist/cleanup-registry.js.map +1 -1
  14. package/dist/config.d.ts +3 -4
  15. package/dist/config.d.ts.map +1 -1
  16. package/dist/config.js.map +1 -1
  17. package/dist/fire-event.d.ts +3 -3
  18. package/dist/fire-event.d.ts.map +1 -1
  19. package/dist/fire-event.js +14 -4
  20. package/dist/fire-event.js.map +1 -1
  21. package/dist/matchers.d.ts +56 -62
  22. package/dist/matchers.d.ts.map +1 -1
  23. package/dist/matchers.js +6 -18
  24. package/dist/matchers.js.map +1 -1
  25. package/dist/pretty-widget.d.ts.map +1 -1
  26. package/dist/pretty-widget.js +13 -2
  27. package/dist/pretty-widget.js.map +1 -1
  28. package/dist/queries.d.ts +7 -7
  29. package/dist/queries.d.ts.map +1 -1
  30. package/dist/queries.js +21 -12
  31. package/dist/queries.js.map +1 -1
  32. package/dist/query-helpers.d.ts +2 -2
  33. package/dist/query-helpers.d.ts.map +1 -1
  34. package/dist/query-helpers.js +10 -3
  35. package/dist/query-helpers.js.map +1 -1
  36. package/dist/render-hook.js +3 -3
  37. package/dist/render-hook.js.map +1 -1
  38. package/dist/render.d.ts +17 -17
  39. package/dist/render.d.ts.map +1 -1
  40. package/dist/render.js +66 -62
  41. package/dist/render.js.map +1 -1
  42. package/dist/role-helpers.d.ts +1 -1
  43. package/dist/role-helpers.d.ts.map +1 -1
  44. package/dist/role-helpers.js +2 -1
  45. package/dist/role-helpers.js.map +1 -1
  46. package/dist/role-naming.d.ts +2 -1
  47. package/dist/role-naming.d.ts.map +1 -1
  48. package/dist/role-naming.js +2 -1
  49. package/dist/role-naming.js.map +1 -1
  50. package/dist/screenshot.d.ts +11 -12
  51. package/dist/screenshot.d.ts.map +1 -1
  52. package/dist/screenshot.js +35 -16
  53. package/dist/screenshot.js.map +1 -1
  54. package/dist/suggestions.d.ts +1 -1
  55. package/dist/suggestions.d.ts.map +1 -1
  56. package/dist/suggestions.js +5 -3
  57. package/dist/suggestions.js.map +1 -1
  58. package/dist/traversal.d.ts +7 -6
  59. package/dist/traversal.d.ts.map +1 -1
  60. package/dist/traversal.js +32 -7
  61. package/dist/traversal.js.map +1 -1
  62. package/dist/types.d.ts +13 -15
  63. package/dist/types.d.ts.map +1 -1
  64. package/dist/types.js.map +1 -1
  65. package/dist/user-event/click.d.ts +17 -4
  66. package/dist/user-event/click.d.ts.map +1 -1
  67. package/dist/user-event/click.js +28 -9
  68. package/dist/user-event/click.js.map +1 -1
  69. package/dist/user-event/controller.d.ts +5 -5
  70. package/dist/user-event/controller.d.ts.map +1 -1
  71. package/dist/user-event/controller.js +7 -4
  72. package/dist/user-event/controller.js.map +1 -1
  73. package/dist/user-event/gesture.d.ts +1 -1
  74. package/dist/user-event/gesture.d.ts.map +1 -1
  75. package/dist/user-event/gesture.js +103 -29
  76. package/dist/user-event/gesture.js.map +1 -1
  77. package/dist/user-event/index.d.ts +22 -22
  78. package/dist/user-event/index.d.ts.map +1 -1
  79. package/dist/user-event/index.js +28 -25
  80. package/dist/user-event/index.js.map +1 -1
  81. package/dist/user-event/keyboard.d.ts.map +1 -1
  82. package/dist/user-event/keyboard.js +26 -4
  83. package/dist/user-event/keyboard.js.map +1 -1
  84. package/dist/user-event/list-row-click.d.ts +1 -1
  85. package/dist/user-event/list-row-click.d.ts.map +1 -1
  86. package/dist/user-event/list-row-click.js +3 -2
  87. package/dist/user-event/list-row-click.js.map +1 -1
  88. package/dist/user-event/pointer.d.ts.map +1 -1
  89. package/dist/user-event/pointer.js +9 -11
  90. package/dist/user-event/pointer.js.map +1 -1
  91. package/dist/user-event/selection.d.ts +2 -2
  92. package/dist/user-event/selection.d.ts.map +1 -1
  93. package/dist/user-event/selection.js +3 -2
  94. package/dist/user-event/selection.js.map +1 -1
  95. package/dist/user-event/text.d.ts.map +1 -1
  96. package/dist/user-event/text.js +31 -27
  97. package/dist/user-event/text.js.map +1 -1
  98. package/dist/wait-for.d.ts +2 -2
  99. package/dist/wait-for.d.ts.map +1 -1
  100. package/dist/wait-for.js +34 -25
  101. package/dist/wait-for.js.map +1 -1
  102. package/dist/widget-accessible-properties.d.ts +5 -4
  103. package/dist/widget-accessible-properties.d.ts.map +1 -1
  104. package/dist/widget-accessible-properties.js +85 -41
  105. package/dist/widget-accessible-properties.js.map +1 -1
  106. package/dist/widget-getters.d.ts +7 -9
  107. package/dist/widget-getters.d.ts.map +1 -1
  108. package/dist/widget-getters.js.map +1 -1
  109. package/dist/widget-target.d.ts +4 -0
  110. package/dist/widget-target.d.ts.map +1 -0
  111. package/dist/widget-target.js +9 -0
  112. package/dist/widget-target.js.map +1 -0
  113. package/package.json +14 -11
  114. package/src/accessible-native.ts +47 -11
  115. package/src/build-queries.ts +84 -53
  116. package/src/cleanup-registry.ts +11 -5
  117. package/src/config.ts +3 -4
  118. package/src/fire-event.ts +32 -8
  119. package/src/matchers.ts +69 -93
  120. package/src/pretty-widget.ts +17 -2
  121. package/src/queries.ts +68 -31
  122. package/src/query-helpers.ts +17 -9
  123. package/src/render-hook.tsx +3 -3
  124. package/src/render.tsx +71 -79
  125. package/src/role-helpers.ts +3 -1
  126. package/src/role-naming.ts +2 -1
  127. package/src/screenshot.ts +58 -16
  128. package/src/suggestions.ts +5 -3
  129. package/src/traversal.ts +47 -13
  130. package/src/types.ts +17 -15
  131. package/src/user-event/click.ts +37 -17
  132. package/src/user-event/controller.ts +10 -7
  133. package/src/user-event/gesture.ts +142 -33
  134. package/src/user-event/index.ts +133 -47
  135. package/src/user-event/keyboard.ts +35 -5
  136. package/src/user-event/list-row-click.ts +3 -3
  137. package/src/user-event/pointer.ts +11 -15
  138. package/src/user-event/selection.ts +5 -4
  139. package/src/user-event/text.ts +36 -32
  140. package/src/wait-for.ts +46 -36
  141. package/src/widget-accessible-properties.ts +112 -52
  142. package/src/widget-getters.ts +11 -13
  143. package/src/widget-target.ts +11 -0
@@ -1,3 +1,4 @@
1
+ import * as Adw from "@gtkx/gi/adw";
1
2
  import * as Gtk from "@gtkx/gi/gtk";
2
3
  import { sortStringsBy } from "@gtkx/utils";
3
4
  import { type Config, format, type NewPlugin, type PrettyFormatOptions } from "@vitest/pretty-format";
@@ -176,13 +177,27 @@ const formatBody = (widget: Gtk.Widget, indentation: string, depth: number, ctx:
176
177
  return formatCollapsedChildrenLine(widget, indentation, ctx.config, collapseReason);
177
178
  };
178
179
 
180
+ const isPasswordWidget = (widget: Gtk.Widget): boolean =>
181
+ widget instanceof Gtk.PasswordEntry ||
182
+ widget instanceof Adw.PasswordEntryRow ||
183
+ (widget instanceof Gtk.Entry && !widget.getVisibility()) ||
184
+ (widget instanceof Gtk.Text && !widget.getVisibility());
185
+
186
+ const printableText = (widget: Gtk.Widget): string | null => {
187
+ if (widget instanceof Adw.PasswordEntryRow) {
188
+ return widget.getTitle() || null;
189
+ }
190
+
191
+ return isPasswordWidget(widget) ? null : getWidgetText(widget);
192
+ };
193
+
179
194
  const formatWidget = (widget: Gtk.Widget, indentation: string, depth: number, ctx: FormatContext): string => {
180
195
  const { config } = ctx;
181
196
  const tag = getTypeTag(widget);
182
197
  const attrs = formatAttrs(buildAttrs(widget, ctx.getId), config.colors);
183
198
  const openTag = `${paint(config.colors.tag, "<" + tag)}${attrs}${paint(config.colors.tag, ">")}`;
184
199
  const closeTag = paint(config.colors.tag, `</${tag}>`);
185
- const text = getWidgetText(widget);
200
+ const text = printableText(widget);
186
201
  const openLine = `${indentation}${openTag}${config.spacingOuter}`;
187
202
 
188
203
  if (!text && !widget.getFirstChild()) {
@@ -190,7 +205,7 @@ const formatWidget = (widget: Gtk.Widget, indentation: string, depth: number, ct
190
205
  }
191
206
 
192
207
  const textLine = text ? `${indentation}${config.indent}${text}${config.spacingOuter}` : "";
193
- const body = formatBody(widget, indentation, depth, ctx);
208
+ const body = isPasswordWidget(widget) ? "" : formatBody(widget, indentation, depth, ctx);
194
209
 
195
210
  return `${openLine}${textLine}${body}${indentation}${closeTag}${config.spacingOuter}`;
196
211
  };
package/src/queries.ts CHANGED
@@ -12,6 +12,7 @@ import { multipleFoundError, notFoundError } from "./errors.js";
12
12
  import { getDefaultNormalizer } from "./normalize.js";
13
13
  import { type Container, findAll, traverse } from "./traversal.js";
14
14
  import {
15
+ buildInaccessibilityCheck,
15
16
  getWidgetAccessibleName,
16
17
  getWidgetBusyState,
17
18
  getWidgetDescription,
@@ -26,7 +27,6 @@ import {
26
27
  getWidgetPressedState,
27
28
  getWidgetSelectedState,
28
29
  getWidgetValueText,
29
- isInaccessible,
30
30
  isWidgetChecked,
31
31
  isWidgetValueMatch,
32
32
  namingLabelText,
@@ -39,7 +39,7 @@ type BuiltinQueries = QueryFamilies<[container: Container]>;
39
39
  const roleQueries = nameQueryFamily(
40
40
  "Role",
41
41
  queryAllByRole,
42
- buildQueries<[role: Gtk.AccessibleRole, options?: ByRoleOptions]>(
42
+ buildQueries<[role: Gtk.AccessibleRole, options?: ByRoleOptions<Gtk.Widget>], Gtk.Widget>(
43
43
  queryAllByRole,
44
44
  (container, matches, role, options) =>
45
45
  multipleFoundError(container, { queryType: "role", role, options }, matches),
@@ -50,7 +50,7 @@ const roleQueries = nameQueryFamily(
50
50
  const labelTextQueries = nameQueryFamily(
51
51
  "LabelText",
52
52
  queryAllByLabelText,
53
- buildQueries<[text: Matcher, options?: MatcherOptions]>(
53
+ buildQueries<[text: Matcher, options?: MatcherOptions<Gtk.Widget>], Gtk.Widget>(
54
54
  queryAllByLabelText,
55
55
  (container, matches, text) => multipleFoundError(container, { queryType: "labelText", text }, matches),
56
56
  (container, text) => notFoundError(container, { queryType: "labelText", text }),
@@ -60,7 +60,7 @@ const labelTextQueries = nameQueryFamily(
60
60
  const textQueries = nameQueryFamily(
61
61
  "Text",
62
62
  queryAllByText,
63
- buildQueries<[text: Matcher, options?: MatcherOptions]>(
63
+ buildQueries<[text: Matcher, options?: MatcherOptions<Gtk.Widget>], Gtk.Widget>(
64
64
  queryAllByText,
65
65
  (container, matches, text) => multipleFoundError(container, { queryType: "text", text }, matches),
66
66
  (container, text) => notFoundError(container, { queryType: "text", text }),
@@ -70,7 +70,7 @@ const textQueries = nameQueryFamily(
70
70
  const nameQueries = nameQueryFamily(
71
71
  "Name",
72
72
  queryAllByName,
73
- buildQueries<[name: Matcher, options?: MatcherOptions]>(
73
+ buildQueries<[name: Matcher, options?: MatcherOptions<Gtk.Widget>], Gtk.Widget>(
74
74
  queryAllByName,
75
75
  (container, matches, name) => multipleFoundError(container, { queryType: "name", name }, matches),
76
76
  (container, name) => notFoundError(container, { queryType: "name", name }),
@@ -80,7 +80,7 @@ const nameQueries = nameQueryFamily(
80
80
  const placeholderTextQueries = nameQueryFamily(
81
81
  "PlaceholderText",
82
82
  queryAllByPlaceholderText,
83
- buildQueries<[text: Matcher, options?: MatcherOptions]>(
83
+ buildQueries<[text: Matcher, options?: MatcherOptions<Gtk.Widget>], Gtk.Widget>(
84
84
  queryAllByPlaceholderText,
85
85
  (container, matches, text) => multipleFoundError(container, { queryType: "placeholderText", text }, matches),
86
86
  (container, text) => notFoundError(container, { queryType: "placeholderText", text }),
@@ -90,7 +90,7 @@ const placeholderTextQueries = nameQueryFamily(
90
90
  const displayValueQueries = nameQueryFamily(
91
91
  "DisplayValue",
92
92
  queryAllByDisplayValue,
93
- buildQueries<[value: Matcher, options?: MatcherOptions]>(
93
+ buildQueries<[value: Matcher, options?: MatcherOptions<Gtk.Widget>], Gtk.Widget>(
94
94
  queryAllByDisplayValue,
95
95
  (container, matches, value) => multipleFoundError(container, { queryType: "displayValue", value }, matches),
96
96
  (container, value) => notFoundError(container, { queryType: "displayValue", value }),
@@ -284,25 +284,27 @@ const hasMatchingAccessibleStates = (widget: Gtk.Widget, options: ByRoleOptions)
284
284
  hasMatchingDescriptionState(widget, options) &&
285
285
  hasMatchingValueState(widget, options);
286
286
 
287
- const isMatchingWidgetType = (widget: Gtk.Widget, options?: MatcherOptions): boolean =>
287
+ const isMatchingWidgetType = <T extends Gtk.Accessible>(
288
+ widget: Gtk.Widget,
289
+ options?: MatcherOptions<T>,
290
+ ): widget is Gtk.Widget & T =>
288
291
  options?.as === undefined || widget instanceof options.as;
289
292
 
290
- const hasMatchingByRoleOptions = (widget: Gtk.Widget, options?: ByRoleOptions): boolean => {
293
+ const hasMatchingAccessibleOptions = (widget: Gtk.Widget, options?: ByRoleOptions): boolean => {
291
294
  if (!options) {
292
295
  return true;
293
296
  }
294
297
 
295
298
  return (
296
- isMatchingWidgetType(widget, options) &&
297
299
  hasMatchingAccessibleName(widget, options) &&
298
300
  hasMatchingAccessibleStates(widget, options)
299
301
  );
300
302
  };
301
303
 
302
- function nameQueryFamily<Args extends unknown[]>(
304
+ function nameQueryFamily<Args extends unknown[], Element extends Gtk.Accessible>(
303
305
  suffix: string,
304
- queryAllBy: QueryAllBy<Args>,
305
- built: BuiltQueries<Args>,
306
+ queryAllBy: QueryAllBy<Args, Element>,
307
+ built: BuiltQueries<Args, Element>,
306
308
  ): Record<string, unknown> {
307
309
  const [queryBy, getAllBy, getBy, findAllBy, findBy] = built;
308
310
 
@@ -325,22 +327,30 @@ function nameQueryFamily<Args extends unknown[]>(
325
327
  * @param options Additional accessible name, state, and value constraints.
326
328
  * @returns Every matching widget, or an empty array when none match.
327
329
  */
328
- function queryAllByRole(
330
+ function queryAllByRole<T extends Gtk.Accessible = Gtk.Widget>(
329
331
  container: Container,
330
332
  role: Gtk.AccessibleRole,
331
- options?: ByRoleOptions,
332
- ): Gtk.Widget[] {
333
- return findAll(container, (widget) => {
333
+ options?: ByRoleOptions<T>,
334
+ ): T[] {
335
+ const isInaccessible = buildInaccessibilityCheck();
336
+
337
+ const matches = findAll(container, (widget) => {
334
338
  if (widget.getAccessibleRole() !== role) {
335
339
  return false;
336
340
  }
337
341
 
342
+ if (!isMatchingWidgetType(widget, options)) {
343
+ return false;
344
+ }
345
+
338
346
  if (!options?.hidden && isInaccessible(widget)) {
339
347
  return false;
340
348
  }
341
349
 
342
- return hasMatchingByRoleOptions(widget, options);
350
+ return hasMatchingAccessibleOptions(widget, options);
343
351
  });
352
+
353
+ return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
344
354
  }
345
355
 
346
356
  const collectMnemonicMatch = (
@@ -394,14 +404,21 @@ const collectLabelMatches = (
394
404
  * @param options Text matching options.
395
405
  * @returns Every matching widget, or an empty array when none match.
396
406
  */
397
- function queryAllByLabelText(container: Container, text: Matcher, options?: MatcherOptions): Gtk.Widget[] {
407
+ function queryAllByLabelText<T extends Gtk.Accessible = Gtk.Widget>(
408
+ container: Container,
409
+ text: Matcher,
410
+ options?: MatcherOptions<T>,
411
+ ): T[] {
398
412
  const results: Set<Gtk.Widget> = new Set();
413
+ const widgets = new Set(traverse(container));
399
414
 
400
- for (const widget of traverse(container)) {
415
+ for (const widget of widgets) {
401
416
  collectLabelMatches(results, widget, text, options);
402
417
  }
403
418
 
404
- return [...results].filter((widget) => isMatchingWidgetType(widget, options));
419
+ return [...results].filter((widget): widget is Gtk.Widget & T =>
420
+ widgets.has(widget) && isMatchingWidgetType(widget, options),
421
+ );
405
422
  }
406
423
 
407
424
  /**
@@ -411,12 +428,18 @@ function queryAllByLabelText(container: Container, text: Matcher, options?: Matc
411
428
  * @param options Text matching options.
412
429
  * @returns Every matching widget, or an empty array when none match.
413
430
  */
414
- function queryAllByText(container: Container, text: Matcher, options?: MatcherOptions): Gtk.Widget[] {
415
- return findAll(
431
+ function queryAllByText<T extends Gtk.Accessible = Gtk.Widget>(
432
+ container: Container,
433
+ text: Matcher,
434
+ options?: MatcherOptions<T>,
435
+ ): T[] {
436
+ const matches = findAll(
416
437
  container,
417
438
  (widget) =>
418
439
  isMatchingWidgetType(widget, options) && isTextMatch(getWidgetLabelText(widget), text, widget, options),
419
440
  );
441
+
442
+ return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
420
443
  }
421
444
 
422
445
  /**
@@ -426,11 +449,17 @@ function queryAllByText(container: Container, text: Matcher, options?: MatcherOp
426
449
  * @param options Text matching options.
427
450
  * @returns Every matching widget, or an empty array when none match.
428
451
  */
429
- function queryAllByName(container: Container, name: Matcher, options?: MatcherOptions): Gtk.Widget[] {
430
- return findAll(
452
+ function queryAllByName<T extends Gtk.Accessible = Gtk.Widget>(
453
+ container: Container,
454
+ name: Matcher,
455
+ options?: MatcherOptions<T>,
456
+ ): T[] {
457
+ const matches = findAll(
431
458
  container,
432
459
  (widget) => isMatchingWidgetType(widget, options) && isTextMatch(getWidgetName(widget), name, widget, options),
433
460
  );
461
+
462
+ return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
434
463
  }
435
464
 
436
465
  /**
@@ -440,17 +469,19 @@ function queryAllByName(container: Container, name: Matcher, options?: MatcherOp
440
469
  * @param options Text matching options.
441
470
  * @returns Every matching widget, or an empty array when none match.
442
471
  */
443
- function queryAllByPlaceholderText(
472
+ function queryAllByPlaceholderText<T extends Gtk.Accessible = Gtk.Widget>(
444
473
  container: Container,
445
474
  text: Matcher,
446
- options?: MatcherOptions,
447
- ): Gtk.Widget[] {
448
- return findAll(
475
+ options?: MatcherOptions<T>,
476
+ ): T[] {
477
+ const matches = findAll(
449
478
  container,
450
479
  (widget) =>
451
480
  isMatchingWidgetType(widget, options) &&
452
481
  isTextMatch(getWidgetPlaceholderText(widget), text, widget, options),
453
482
  );
483
+
484
+ return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
454
485
  }
455
486
 
456
487
  /**
@@ -460,13 +491,19 @@ function queryAllByPlaceholderText(
460
491
  * @param options Text matching options.
461
492
  * @returns Every matching widget, or an empty array when none match.
462
493
  */
463
- function queryAllByDisplayValue(container: Container, value: Matcher, options?: MatcherOptions): Gtk.Widget[] {
464
- return findAll(
494
+ function queryAllByDisplayValue<T extends Gtk.Accessible = Gtk.Widget>(
495
+ container: Container,
496
+ value: Matcher,
497
+ options?: MatcherOptions<T>,
498
+ ): T[] {
499
+ const matches = findAll(
465
500
  container,
466
501
  (widget) =>
467
502
  isMatchingWidgetType(widget, options) &&
468
503
  isTextMatch(getWidgetDisplayValue(widget), value, widget, options),
469
504
  );
505
+
506
+ return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
470
507
  }
471
508
 
472
509
  export {
@@ -1,5 +1,5 @@
1
1
  import type * as Gtk from "@gtkx/gi/gtk";
2
- import { camelCase } from "@gtkx/utils";
2
+ import { getProperty } from "@gtkx/gi/gobject";
3
3
  import type { Matcher, MatcherOptions } from "./types.js";
4
4
  import { buildQueries } from "./build-queries.js";
5
5
  import { getElementError } from "./errors.js";
@@ -27,7 +27,13 @@ const queryHelpers: QueryHelpers = {
27
27
  };
28
28
 
29
29
  const readObjectProperty = (widget: Gtk.Widget, property: string): string | null => {
30
- const value: unknown = Reflect.get(widget, camelCase(property));
30
+ let value: unknown;
31
+
32
+ try {
33
+ value = Reflect.apply(getProperty, undefined, [widget, property]);
34
+ } catch {
35
+ return null;
36
+ }
31
37
 
32
38
  if (typeof value === "string") {
33
39
  return value;
@@ -51,18 +57,20 @@ const readObjectProperty = (widget: Gtk.Widget, property: string): string | null
51
57
  * @param options Text matching options.
52
58
  * @returns Every matching widget, or an empty array when none match.
53
59
  */
54
- function queryAllByObjectProperty(
60
+ function queryAllByObjectProperty<T extends Gtk.Accessible = Gtk.Widget>(
55
61
  property: string,
56
62
  container: Container,
57
63
  text: Matcher,
58
- options?: MatcherOptions,
59
- ): Gtk.Widget[] {
60
- return findAll(
64
+ options?: MatcherOptions<T>,
65
+ ): T[] {
66
+ const matches = findAll(
61
67
  container,
62
68
  (widget) =>
63
69
  isMatchingWidgetType(widget, options) &&
64
70
  isTextMatch(readObjectProperty(widget, property), text, widget, options),
65
71
  );
72
+
73
+ return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
66
74
  }
67
75
 
68
76
  /**
@@ -76,12 +84,12 @@ function queryAllByObjectProperty(
76
84
  * @returns The matching widget, or null when none match.
77
85
  * @throws When more than one widget matches.
78
86
  */
79
- function queryByObjectProperty(
87
+ function queryByObjectProperty<T extends Gtk.Accessible = Gtk.Widget>(
80
88
  property: string,
81
89
  container: Container,
82
90
  text: Matcher,
83
- options?: MatcherOptions,
84
- ): Gtk.Widget | null {
91
+ options?: MatcherOptions<T>,
92
+ ): T | null {
85
93
  const matches = queryAllByObjectProperty(property, container, text, options);
86
94
 
87
95
  if (matches.length > 1) {
@@ -50,9 +50,9 @@ async function renderHook<Result, Props>(
50
50
 
51
51
  return {
52
52
  result: resultRef as { current: Result },
53
- rerender: async (newProps?: Props) => {
54
- if (newProps !== undefined) {
55
- currentProps = newProps;
53
+ rerender: async (...args: [] | [Props]) => {
54
+ if (args.length === 1) {
55
+ [currentProps] = args;
56
56
  }
57
57
 
58
58
  await renderResult.rerender(<TestComponent props={currentProps} />);
package/src/render.tsx CHANGED
@@ -1,17 +1,12 @@
1
- import type { RootElement } from "@gtkx/react";
1
+ import type { CaughtErrorInfo, Root, RootElement } from "@gtkx/react";
2
2
  import * as Gtk from "@gtkx/gi/gtk";
3
- import {
4
- createReconcilerRoot,
5
- isRootElement,
6
- type ReconcilerRoot,
7
- setReconcilerErrorHandler,
8
- settleAccessible,
9
- } from "@gtkx/react/internal";
3
+ import { createRoot } from "@gtkx/react";
4
+ import { isRootElement, settleAccessible } from "@gtkx/react/internal";
10
5
  import { type ErrorInfo, type ReactNode, StrictMode } from "react";
11
6
  import type { RenderResult } from "./bound-queries.js";
12
7
  import type { QueryMap, RenderOptions, ScreenshotOptions } from "./types.js";
13
8
  import { runInAct, runWithActEnvironment } from "./act.js";
14
- import { addToCleanupQueue, runCleanup } from "./cleanup-registry.js";
9
+ import { addToCleanupQueue, runCleanup, runCleanupCallbacks } from "./cleanup-registry.js";
15
10
  import { getConfig } from "./config.js";
16
11
  import { scheduleWhenWindowReady } from "./frame-sync.js";
17
12
  import { createHarnessWindow, presentHarnessWindow } from "./harness-window.js";
@@ -21,15 +16,14 @@ import { clearScreen, setScreen } from "./screen.js";
21
16
  import { captureScreen } from "./screenshot.js";
22
17
  import { type Container, roots, TOPLEVELS } from "./traversal.js";
23
18
  import { resetClipboard } from "./user-event/index.js";
19
+ import { requireWidget } from "./widget-target.js";
24
20
  import { findPresentedWindowFailure, findRenderedWindowFailure, mappedToplevels } from "./window-state.js";
25
21
  import { within } from "./within.js";
26
22
 
27
- /** A mounted render tracked so cleanup can unmount it. */
28
23
  type ActiveRender = {
29
- /** Root that rerenders drive and that cleanup tears the tree down through. */
30
- root: ReconcilerRoot;
31
- /** Harness window created for the render, null when it renders into a container the caller supplied. */
24
+ root: Root;
32
25
  window: Gtk.Window | null;
26
+ errors: RenderErrorState;
33
27
  };
34
28
 
35
29
  type ResolvedContainer = {
@@ -37,15 +31,13 @@ type ResolvedContainer = {
37
31
  window: Gtk.Window | null;
38
32
  };
39
33
 
40
- type ReconcilerErrorState = {
34
+ type RenderErrorState = {
41
35
  lastError: Error | null;
42
- isHandlerInstalled: boolean;
43
36
  };
44
37
 
45
38
  type WindowFailureReporter = (window: Gtk.Window) => string | null;
46
39
  type SettleAction = "render" | "rerender";
47
40
 
48
- const reconcilerErrors: ReconcilerErrorState = { lastError: null, isHandlerInstalled: false };
49
41
  const activeRenders: Set<ActiveRender> = new Set();
50
42
 
51
43
  const settleWindow = async (
@@ -74,15 +66,18 @@ const settleWindow = async (
74
66
  }
75
67
  };
76
68
 
77
- const update = async (element: ReactNode, root: ReconcilerRoot): Promise<void> => {
78
- await runInAct(() => {
79
- root.update(element);
80
- });
81
-
82
- if (reconcilerErrors.lastError) {
83
- const captured = reconcilerErrors.lastError;
84
- reconcilerErrors.lastError = null;
85
- throw captured;
69
+ const flushRender = async (active: ActiveRender, action: () => void): Promise<void> => {
70
+ try {
71
+ await runInAct(async () => {
72
+ action();
73
+ await Promise.resolve();
74
+ });
75
+
76
+ if (active.errors.lastError !== null) {
77
+ throw active.errors.lastError;
78
+ }
79
+ } finally {
80
+ active.errors.lastError = null;
86
81
  }
87
82
  };
88
83
 
@@ -91,29 +86,15 @@ const disposeActiveRender = async (active: ActiveRender): Promise<void> => {
91
86
  return;
92
87
  }
93
88
 
94
- await active.root.unmount(async (root) => {
95
- await update(null, root);
89
+ try {
90
+ await flushRender(active, active.root.unmount);
91
+ } finally {
96
92
  active.window?.destroy();
97
- });
98
- };
99
-
100
- const disposeAllActiveRenders = async (): Promise<void> => {
101
- for (const active of activeRenders) {
102
- await disposeActiveRender(active);
103
93
  }
104
94
  };
105
95
 
106
- const handleError = (error: unknown): void => {
107
- reconcilerErrors.lastError = error instanceof Error ? error : new Error(String(error));
108
- };
109
-
110
- const installErrorHandler = (): void => {
111
- if (reconcilerErrors.isHandlerInstalled) {
112
- return;
113
- }
114
-
115
- setReconcilerErrorHandler(handleError);
116
- reconcilerErrors.isHandlerInstalled = true;
96
+ const disposeAllActiveRenders = async (): Promise<void> => {
97
+ await runCleanupCallbacks(activeRenders.values().map((active) => () => disposeActiveRender(active)));
117
98
  };
118
99
 
119
100
  const resolveContainer = (container: RenderOptions["container"]): ResolvedContainer => {
@@ -125,6 +106,10 @@ const resolveContainer = (container: RenderOptions["container"]): ResolvedContai
125
106
  return { containerInfo: container, window: null };
126
107
  }
127
108
 
109
+ if (container !== undefined) {
110
+ requireWidget(container);
111
+ }
112
+
128
113
  const window = createHarnessWindow();
129
114
  window.setDecorated(false);
130
115
 
@@ -186,16 +171,22 @@ const settleRender = async (
186
171
  await settleWindow(settleTarget(resolved, container), findFailure, action);
187
172
  };
188
173
 
189
- const renderErrorHandlers = <Q extends QueryMap>(options: RenderOptions<Q> | undefined) => ({
190
- onUncaughtError: handleError,
191
- onCaughtError: (error: unknown, errorInfo: ErrorInfo): void => {
192
- handleError(error);
193
- options?.onCaughtError?.(error, errorInfo);
194
- },
195
- onRecoverableError: (error: unknown, errorInfo: ErrorInfo): void => {
196
- options?.onRecoverableError?.(error, errorInfo);
197
- },
198
- });
174
+ const renderErrorHandlers = <Q extends QueryMap>(errors: RenderErrorState, options: RenderOptions<Q> | undefined) => {
175
+ const handleError = (error: unknown): void => {
176
+ errors.lastError = error instanceof Error ? error : new Error(String(error));
177
+ };
178
+
179
+ return {
180
+ onUncaughtError: handleError,
181
+ onCaughtError: (error: unknown, errorInfo: CaughtErrorInfo): void => {
182
+ handleError(error);
183
+ options?.onCaughtError?.(error, errorInfo);
184
+ },
185
+ onRecoverableError: (error: unknown, errorInfo: ErrorInfo): void => {
186
+ options?.onRecoverableError?.(error, errorInfo);
187
+ },
188
+ };
189
+ };
199
190
 
200
191
  const applyEnableAnimations = (areAnimationsEnabled: boolean): void => {
201
192
  const settings = Gtk.Settings.getDefault();
@@ -206,41 +197,38 @@ const applyEnableAnimations = (areAnimationsEnabled: boolean): void => {
206
197
  };
207
198
 
208
199
  /**
209
- * Renders a React element into a GTK4 widget tree and returns queries
210
- * scoped to it along with controls for rerendering and unmounting. When no
211
- * container is supplied, a harness window is created and presented, and the editable it hands the
212
- * focus to keeps its text unselected, with its caret at the end. The returned promise settles once
213
- * the window the tree is shown in has been laid out and activation has arrived, so platform state
214
- * such as focus is already readable when it resolves. A presented harness window is waited on until
215
- * it is itself active; a window the caller or the tree owns is waited on until the application holds
216
- * activation, since only a present can claim it. Rendering into a container that is not shown waits
217
- * for nothing, and a window that never becomes readable within `windowActivationTimeout` throws an
218
- * error naming the condition that failed rather than resolving with unreadable platform state. A
219
- * tree mounted into a container that sits outside every toplevel, or inside a window that is not
220
- * visible, is out of reach of a pointer and a keyboard, so every `userEvent` helper aimed at it
221
- * rejects after `actionabilityTimeout` naming that condition, and `fireEvent` drives it instead.
200
+ * Renders React into native widgets and returns queries, debug helpers, and lifecycle controls.
201
+ *
202
+ * @remarks
203
+ * Without a container, creates and presents a harness window. Its initially focused editable
204
+ * keeps its text unselected, with the caret at the end.
205
+ *
206
+ * Waits for layout and activation before resolving, so focus and other platform state can be
207
+ * read immediately. A new harness waits for its own activation; a caller-owned or rendered
208
+ * window waits for application activation, since only presentation can claim it. A container
209
+ * that is not shown requires no activation wait.
222
210
  *
223
- * @param element The React element to render.
224
- * @param options Optional container, wrapper, custom queries, and other render settings.
225
- * @returns A render result with bound queries, debug helpers, and lifecycle controls.
226
- * @throws When the window the tree is shown in is not laid out and activated in time.
211
+ * Widgets outside all toplevels or inside a hidden window cannot receive pointer or keyboard
212
+ * input. `userEvent` rejects after `actionabilityTimeout`; use `fireEvent` to drive them directly.
213
+ *
214
+ * @param element React element to render.
215
+ * @param options Container, wrapper, custom queries, and render settings.
216
+ * @returns Bound queries, debug helpers, and rerender/unmount controls.
217
+ * @throws If a shown window is not laid out and activated within `windowActivationTimeout`.
227
218
  */
228
219
  const render = async <Q extends QueryMap = Record<never, never>>(
229
220
  element: ReactNode,
230
221
  options?: RenderOptions<Q>,
231
222
  ): Promise<RenderResult<Q>> => {
232
- installErrorHandler();
233
223
  applyEnableAnimations(options?.areAnimationsEnabled === true);
234
224
  const baseElement: Container = options?.baseElement ?? TOPLEVELS;
235
225
  const Wrapper = options?.wrapper;
236
226
  const resolved = resolveContainer(options?.container);
237
227
 
238
- const root = createReconcilerRoot({
239
- containerInfo: resolved.containerInfo,
240
- ...renderErrorHandlers(options),
241
- });
228
+ const errors: RenderErrorState = { lastError: null };
229
+ const root = createRoot(resolved.containerInfo, renderErrorHandlers(errors, options));
242
230
 
243
- const active: ActiveRender = { root, window: resolved.window };
231
+ const active: ActiveRender = { root, window: resolved.window, errors };
244
232
  activeRenders.add(active);
245
233
  addToCleanupQueue(disposeAllActiveRenders);
246
234
  addToCleanupQueue(clearScreen);
@@ -252,7 +240,9 @@ const render = async <Q extends QueryMap = Record<never, never>>(
252
240
  return options?.isReactStrictMode ? <StrictMode>{wrapped}</StrictMode> : wrapped;
253
241
  };
254
242
 
255
- await update(wrap(element), root);
243
+ await flushRender(active, () => {
244
+ root.render(wrap(element));
245
+ });
256
246
  presentHarnessWindow(resolved.window);
257
247
  await settleRender(resolved, options?.container, "render");
258
248
  const container = resolveResultContainer(resolved, options?.container, baseElement);
@@ -265,7 +255,9 @@ const render = async <Q extends QueryMap = Record<never, never>>(
265
255
  await disposeActiveRender(active);
266
256
  },
267
257
  rerender: async (newElement: ReactNode) => {
268
- await update(wrap(newElement), root);
258
+ await flushRender(active, () => {
259
+ root.render(wrap(newElement));
260
+ });
269
261
  await settleRender(resolved, options?.container, "rerender");
270
262
  },
271
263
  debug: (element: Container | Container[] = baseElement, debugOptions?: PrettyWidgetOptions) => {
@@ -3,6 +3,7 @@ import { sortStringsBy } from "@gtkx/utils";
3
3
  import { type Container, traverse } from "./traversal.js";
4
4
  import { getWidgetAccessibleName, getWidgetLevel } from "./widget-accessible-properties.js";
5
5
  import { getTypeTag } from "./widget-getters.js";
6
+ import { requireWidget } from "./widget-target.js";
6
7
 
7
8
  const ROLE_NAMES_BY_VALUE = enumNamesByValue(Gtk.AccessibleRole);
8
9
 
@@ -125,6 +126,7 @@ const logRoles = (container: Container): void => {
125
126
  *
126
127
  * @param widget The widget to read the level from.
127
128
  */
128
- const computeHeadingLevel = (widget: Gtk.Widget): number | undefined => getWidgetLevel(widget) ?? undefined;
129
+ const computeHeadingLevel = (widget: Gtk.Accessible): number | undefined =>
130
+ getWidgetLevel(requireWidget(widget)) ?? undefined;
129
131
 
130
132
  export { computeHeadingLevel, formatRole, formatRoleList, getRoles, prettyRoles, logRoles };
@@ -95,5 +95,6 @@ const NAMING: Partial<Record<Gtk.AccessibleRole, number>> = {
95
95
  const namingFor = (role: Gtk.AccessibleRole): number => NAMING[role] ?? 0;
96
96
  const isNameProhibited = (role: Gtk.AccessibleRole): boolean => (namingFor(role) & PROHIBITED) !== 0;
97
97
  const isNameFromAuthor = (role: Gtk.AccessibleRole): boolean => (namingFor(role) & FROM_AUTHOR) !== 0;
98
+ const isNameFromContent = (role: Gtk.AccessibleRole): boolean => (namingFor(role) & FROM_CONTENT) !== 0;
98
99
 
99
- export { isNameFromAuthor, isNameProhibited };
100
+ export { isNameFromAuthor, isNameFromContent, isNameProhibited };