@gtkx/testing 1.0.0-rc.4 → 1.0.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.
Files changed (182) hide show
  1. package/README.md +5 -5
  2. package/dist/accessible-native.d.ts +16 -0
  3. package/dist/accessible-native.d.ts.map +1 -0
  4. package/dist/accessible-native.js +206 -0
  5. package/dist/accessible-native.js.map +1 -0
  6. package/dist/build-queries.d.ts +27 -10
  7. package/dist/build-queries.d.ts.map +1 -1
  8. package/dist/build-queries.js +22 -24
  9. package/dist/build-queries.js.map +1 -1
  10. package/dist/cleanup-registry.d.ts +1 -1
  11. package/dist/cleanup-registry.d.ts.map +1 -1
  12. package/dist/cleanup-registry.js.map +1 -1
  13. package/dist/errors.d.ts +10 -1
  14. package/dist/errors.d.ts.map +1 -1
  15. package/dist/errors.js +10 -1
  16. package/dist/errors.js.map +1 -1
  17. package/dist/fire-event.d.ts +35 -6
  18. package/dist/fire-event.d.ts.map +1 -1
  19. package/dist/fire-event.js +23 -7
  20. package/dist/fire-event.js.map +1 -1
  21. package/dist/frame-sync.d.ts +2 -1
  22. package/dist/frame-sync.d.ts.map +1 -1
  23. package/dist/frame-sync.js +15 -5
  24. package/dist/frame-sync.js.map +1 -1
  25. package/dist/harness-window.d.ts +4 -0
  26. package/dist/harness-window.d.ts.map +1 -0
  27. package/dist/harness-window.js +6 -0
  28. package/dist/harness-window.js.map +1 -0
  29. package/dist/index.d.ts +14 -7
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +9 -5
  32. package/dist/index.js.map +1 -1
  33. package/dist/internal.d.ts +4 -0
  34. package/dist/internal.d.ts.map +1 -0
  35. package/dist/internal.js +4 -0
  36. package/dist/internal.js.map +1 -0
  37. package/dist/matchers.d.ts +88 -46
  38. package/dist/matchers.d.ts.map +1 -1
  39. package/dist/matchers.js +156 -18
  40. package/dist/matchers.js.map +1 -1
  41. package/dist/pretty-format.d.ts +2 -0
  42. package/dist/pretty-format.d.ts.map +1 -0
  43. package/dist/pretty-format.js +2 -0
  44. package/dist/pretty-format.js.map +1 -0
  45. package/dist/pretty-widget.d.ts +5 -2
  46. package/dist/pretty-widget.d.ts.map +1 -1
  47. package/dist/pretty-widget.js +73 -56
  48. package/dist/pretty-widget.js.map +1 -1
  49. package/dist/production.d.ts +8 -0
  50. package/dist/production.d.ts.map +1 -0
  51. package/dist/production.js +34 -0
  52. package/dist/production.js.map +1 -0
  53. package/dist/queries.d.ts +98 -56
  54. package/dist/queries.d.ts.map +1 -1
  55. package/dist/queries.js +123 -83
  56. package/dist/queries.js.map +1 -1
  57. package/dist/query-helpers.d.ts +44 -0
  58. package/dist/query-helpers.d.ts.map +1 -0
  59. package/dist/query-helpers.js +57 -0
  60. package/dist/query-helpers.js.map +1 -0
  61. package/dist/render-children.d.ts +9 -0
  62. package/dist/render-children.d.ts.map +1 -0
  63. package/dist/render-children.js +11 -0
  64. package/dist/render-children.js.map +1 -0
  65. package/dist/render.d.ts.map +1 -1
  66. package/dist/render.js +11 -9
  67. package/dist/render.js.map +1 -1
  68. package/dist/role-helpers.d.ts +12 -3
  69. package/dist/role-helpers.d.ts.map +1 -1
  70. package/dist/role-helpers.js +13 -4
  71. package/dist/role-helpers.js.map +1 -1
  72. package/dist/role-naming.d.ts +5 -0
  73. package/dist/role-naming.d.ts.map +1 -0
  74. package/dist/role-naming.js +96 -0
  75. package/dist/role-naming.js.map +1 -0
  76. package/dist/screenshot.d.ts +12 -20
  77. package/dist/screenshot.d.ts.map +1 -1
  78. package/dist/screenshot.js +183 -91
  79. package/dist/screenshot.js.map +1 -1
  80. package/dist/traversal.d.ts +4 -1
  81. package/dist/traversal.d.ts.map +1 -1
  82. package/dist/traversal.js +25 -6
  83. package/dist/traversal.js.map +1 -1
  84. package/dist/types.d.ts +26 -14
  85. package/dist/types.d.ts.map +1 -1
  86. package/dist/types.js.map +1 -1
  87. package/dist/user-event/adjustment.d.ts +3 -0
  88. package/dist/user-event/adjustment.d.ts.map +1 -1
  89. package/dist/user-event/adjustment.js +23 -2
  90. package/dist/user-event/adjustment.js.map +1 -1
  91. package/dist/user-event/click.d.ts +43 -8
  92. package/dist/user-event/click.d.ts.map +1 -1
  93. package/dist/user-event/click.js +175 -44
  94. package/dist/user-event/click.js.map +1 -1
  95. package/dist/user-event/column-header-click.d.ts +4 -0
  96. package/dist/user-event/column-header-click.d.ts.map +1 -0
  97. package/dist/user-event/column-header-click.js +65 -0
  98. package/dist/user-event/column-header-click.js.map +1 -0
  99. package/dist/user-event/event-wrapper.d.ts.map +1 -1
  100. package/dist/user-event/event-wrapper.js +2 -1
  101. package/dist/user-event/event-wrapper.js.map +1 -1
  102. package/dist/user-event/index.d.ts +51 -10
  103. package/dist/user-event/index.d.ts.map +1 -1
  104. package/dist/user-event/index.js +43 -26
  105. package/dist/user-event/index.js.map +1 -1
  106. package/dist/user-event/indexed-children.d.ts +10 -0
  107. package/dist/user-event/indexed-children.d.ts.map +1 -0
  108. package/dist/user-event/indexed-children.js +50 -0
  109. package/dist/user-event/indexed-children.js.map +1 -0
  110. package/dist/user-event/keyboard.d.ts.map +1 -1
  111. package/dist/user-event/keyboard.js +33 -4
  112. package/dist/user-event/keyboard.js.map +1 -1
  113. package/dist/user-event/list-row-click.d.ts +4 -0
  114. package/dist/user-event/list-row-click.d.ts.map +1 -0
  115. package/dist/user-event/list-row-click.js +39 -0
  116. package/dist/user-event/list-row-click.js.map +1 -0
  117. package/dist/user-event/native-click.d.ts +6 -0
  118. package/dist/user-event/native-click.d.ts.map +1 -0
  119. package/dist/user-event/native-click.js +32 -0
  120. package/dist/user-event/native-click.js.map +1 -0
  121. package/dist/user-event/pointer.d.ts +1 -1
  122. package/dist/user-event/pointer.d.ts.map +1 -1
  123. package/dist/user-event/pointer.js +33 -9
  124. package/dist/user-event/pointer.js.map +1 -1
  125. package/dist/user-event/selection.d.ts +11 -8
  126. package/dist/user-event/selection.d.ts.map +1 -1
  127. package/dist/user-event/selection.js +49 -38
  128. package/dist/user-event/selection.js.map +1 -1
  129. package/dist/user-event/text.d.ts +6 -2
  130. package/dist/user-event/text.d.ts.map +1 -1
  131. package/dist/user-event/text.js +68 -18
  132. package/dist/user-event/text.js.map +1 -1
  133. package/dist/widget-accessible-properties.d.ts +14 -26
  134. package/dist/widget-accessible-properties.d.ts.map +1 -1
  135. package/dist/widget-accessible-properties.js +135 -190
  136. package/dist/widget-accessible-properties.js.map +1 -1
  137. package/dist/widget-getters.d.ts +8 -0
  138. package/dist/widget-getters.d.ts.map +1 -0
  139. package/dist/widget-getters.js +19 -0
  140. package/dist/widget-getters.js.map +1 -0
  141. package/dist/within.d.ts +3 -2
  142. package/dist/within.d.ts.map +1 -1
  143. package/dist/within.js +3 -2
  144. package/dist/within.js.map +1 -1
  145. package/package.json +12 -7
  146. package/src/accessible-native.ts +333 -0
  147. package/src/build-queries.ts +47 -41
  148. package/src/cleanup-registry.ts +1 -1
  149. package/src/errors.ts +12 -1
  150. package/src/fire-event.ts +61 -8
  151. package/src/frame-sync.ts +21 -5
  152. package/src/harness-window.ts +9 -0
  153. package/src/index.ts +18 -11
  154. package/src/internal.ts +11 -0
  155. package/src/matchers.ts +428 -70
  156. package/src/pretty-format.ts +1 -0
  157. package/src/pretty-widget.ts +97 -70
  158. package/src/production.ts +52 -0
  159. package/src/queries.ts +127 -89
  160. package/src/query-helpers.ts +97 -0
  161. package/src/render-children.ts +25 -0
  162. package/src/render.tsx +16 -11
  163. package/src/role-helpers.ts +14 -4
  164. package/src/role-naming.ts +99 -0
  165. package/src/screenshot.ts +272 -103
  166. package/src/traversal.ts +44 -6
  167. package/src/types.ts +28 -16
  168. package/src/user-event/adjustment.ts +29 -2
  169. package/src/user-event/click.ts +247 -52
  170. package/src/user-event/column-header-click.ts +90 -0
  171. package/src/user-event/event-wrapper.ts +3 -1
  172. package/src/user-event/index.ts +100 -37
  173. package/src/user-event/indexed-children.ts +75 -0
  174. package/src/user-event/keyboard.ts +52 -4
  175. package/src/user-event/list-row-click.ts +57 -0
  176. package/src/user-event/native-click.ts +46 -0
  177. package/src/user-event/pointer.ts +48 -9
  178. package/src/user-event/selection.ts +67 -41
  179. package/src/user-event/text.ts +92 -21
  180. package/src/widget-accessible-properties.ts +183 -230
  181. package/src/widget-getters.ts +32 -0
  182. package/src/within.ts +3 -2
@@ -1,55 +1,196 @@
1
+ import * as Gdk from "@gtkx/gi/gdk";
2
+ import * as Graphene from "@gtkx/gi/graphene";
1
3
  import * as Gsk from "@gtkx/gi/gsk";
2
4
  import * as Gtk from "@gtkx/gi/gtk";
3
- import { mkdtempSync, writeFileSync } from "node:fs";
4
- import { tmpdir } from "node:os";
5
- import { join } from "node:path";
5
+ import { mkdirSync, writeFileSync } from "node:fs";
6
+ import { dirname } from "node:path";
7
+ import { getConfig } from "./config.js";
8
+ import { descendants } from "./traversal.js";
6
9
  import { waitFor } from "./wait-for.js";
7
10
  const DEFAULT_SCREENSHOT_INTERVAL = 10;
11
+ const PRESENTATION_PROBE_MS = 250;
12
+ const NOT_PRESENTING_MESSAGE = "Screenshot failed: the display is not presenting frames to this window, so GTK has no rendered " +
13
+ "content to capture. The window is hidden, minimized, or its compositor is not presenting it; this " +
14
+ "is a display problem, not a widget problem.";
15
+ const EMPTY_WIDGET_HINT = "The display is presenting frames to this window, so the widget itself is empty: it painted nothing.";
16
+ const OTHER_FAILURE_HINT = "The display is presenting frames to this window, so the capture failed for another reason.";
17
+ const NOTHING_ON_SCREEN_MESSAGE = "Nothing is on screen to capture: no toplevel window is mapped";
8
18
  const bytesToBase64 = (bytes) => {
9
19
  return Buffer.from(bytes).toString("base64");
10
20
  };
11
21
  const describeWidgetState = (widget) => `realized=${String(widget.getRealized())} mapped=${String(widget.getMapped())} ` +
12
22
  `visible=${String(widget.getVisible())}`;
13
- const captureSnapshot = (widget, scale) => {
14
- const paintable = new Gtk.WidgetPaintable({ widget });
23
+ const getMappedPopovers = (widget) => {
24
+ const popovers = [];
25
+ for (const descendant of descendants(widget)) {
26
+ if (descendant instanceof Gtk.Popover && descendant.getMapped()) {
27
+ popovers.push(descendant);
28
+ }
29
+ }
30
+ return popovers;
31
+ };
32
+ const appendPopover = (snapshot, target, popover) => {
33
+ const [hasBounds, bounds] = popover.computeBounds(target);
34
+ const paintable = new Gtk.WidgetPaintable({ widget: popover });
15
35
  const width = paintable.getIntrinsicWidth();
16
36
  const height = paintable.getIntrinsicHeight();
17
- if (width <= 0 || height <= 0) {
18
- throw new Error(`Widget has no size: ensure it is realized and visible (${describeWidgetState(widget)})`);
37
+ if (!hasBounds || width <= 0 || height <= 0) {
38
+ return;
39
+ }
40
+ snapshot.save();
41
+ snapshot.translate(new Graphene.Point({ x: bounds.origin.x, y: bounds.origin.y }));
42
+ paintable.snapshot(snapshot, width, height);
43
+ snapshot.restore();
44
+ };
45
+ const appendPopovers = (snapshot, target) => {
46
+ for (const popover of getMappedPopovers(target)) {
47
+ appendPopover(snapshot, target, popover);
48
+ }
49
+ };
50
+ const getSurface = (widget) => widget.getNative()?.getSurface() ?? null;
51
+ const isSurfaceOnScreen = (widget) => getSurface(widget)?.getMapped() ?? false;
52
+ const getFrameClock = (widget) => widget.getRoot()?.getFrameClock() ?? widget.getFrameClock();
53
+ const getFrameCounter = (widget) => getFrameClock(widget)?.getFrameCounter() ?? null;
54
+ const describePresentation = (widget) => `${describeWidgetState(widget)} surfaceMapped=${String(isSurfaceOnScreen(widget))} ` +
55
+ `frameCounter=${String(getFrameCounter(widget))}`;
56
+ const requestFrame = (widget) => {
57
+ getFrameClock(widget)?.requestPhase(Gdk.FrameClockPhase.UPDATE);
58
+ };
59
+ const startFrameProbe = (widget) => {
60
+ requestFrame(widget);
61
+ return { counter: getFrameCounter(widget), startedAt: Date.now() };
62
+ };
63
+ const updateFrameProbe = (widget, probe) => {
64
+ requestFrame(widget);
65
+ if (probe.counter !== null) {
66
+ return;
67
+ }
68
+ const counter = getFrameCounter(widget);
69
+ if (counter !== null) {
70
+ probe.counter = counter;
71
+ probe.startedAt = Date.now();
72
+ }
73
+ };
74
+ const hasFrameAdvanced = (widget, probe) => {
75
+ const counter = getFrameCounter(widget);
76
+ return counter !== null && probe.counter !== null && counter > probe.counter;
77
+ };
78
+ const isPresenting = (widget, probe) => isSurfaceOnScreen(widget) && hasFrameAdvanced(widget, probe);
79
+ const isPresentationStalled = (widget, probe) => Date.now() - probe.startedAt >= PRESENTATION_PROBE_MS && !isPresenting(widget, probe);
80
+ const allocateRoot = (widget) => {
81
+ const root = widget.getRoot();
82
+ if (!root) {
83
+ return;
84
+ }
85
+ const width = root.getWidth();
86
+ const height = root.getHeight();
87
+ if (width > 0 && height > 0) {
88
+ root.allocate(width, height, -1, null);
19
89
  }
90
+ };
91
+ const isDirectlyPaintable = (child) => child.getMapped() && child.getWidth() > 0 && !(child instanceof Gtk.Popover);
92
+ const getPaintableNode = (widget, scale, width, height) => {
93
+ const paintable = new Gtk.WidgetPaintable({ widget });
20
94
  const snapshot = new Gtk.Snapshot();
21
95
  snapshot.scale(scale, scale);
22
96
  paintable.snapshot(snapshot, width, height);
23
- const renderNode = snapshot.toNode();
24
- if (!renderNode) {
25
- throw new Error(`Widget produced no render content (${describeWidgetState(widget)})`);
97
+ appendPopovers(snapshot, widget);
98
+ return snapshot.toNode();
99
+ };
100
+ const getChildrenNode = (widget, scale) => {
101
+ const snapshot = new Gtk.Snapshot();
102
+ snapshot.scale(scale, scale);
103
+ for (let child = widget.getFirstChild(); child !== null; child = child.getNextSibling()) {
104
+ if (isDirectlyPaintable(child)) {
105
+ widget.snapshotChild(child, snapshot);
106
+ }
107
+ }
108
+ appendPopovers(snapshot, widget);
109
+ return snapshot.toNode();
110
+ };
111
+ const getRenderNode = (widget, scale, width, height) => {
112
+ const presented = getPaintableNode(widget, scale, width, height);
113
+ if (presented) {
114
+ return presented;
26
115
  }
27
- const display = widget.getDisplay();
116
+ allocateRoot(widget);
117
+ return getPaintableNode(widget, scale, width, height) ?? getChildrenNode(widget, scale);
118
+ };
119
+ const renderToPng = (widget, node, width, height) => {
28
120
  const renderer = new Gsk.CairoRenderer();
29
- renderer.realizeForDisplay(display);
121
+ renderer.realizeForDisplay(widget.getDisplay());
30
122
  try {
31
- const texture = renderer.renderTexture(renderNode, null);
32
- const pngBytes = texture.saveToPngBytes();
33
- const data = pngBytes.getData();
123
+ const viewport = Graphene.Rect.alloc().init(0, 0, width, height);
124
+ const texture = renderer.renderTexture(node, viewport);
125
+ const data = texture.saveToPngBytes().getData();
34
126
  if (!data) {
35
127
  throw new Error("Failed to serialize screenshot to PNG");
36
128
  }
37
- return {
38
- data: bytesToBase64(data),
39
- mimeType: "image/png",
40
- width: Math.round(width * scale),
41
- height: Math.round(height * scale),
42
- };
129
+ return { data: bytesToBase64(data), mimeType: "image/png", width, height };
43
130
  }
44
131
  finally {
45
132
  renderer.unrealize();
46
133
  }
47
134
  };
135
+ const captureSnapshot = (widget, scale) => {
136
+ const paintable = new Gtk.WidgetPaintable({ widget });
137
+ const width = paintable.getIntrinsicWidth();
138
+ const height = paintable.getIntrinsicHeight();
139
+ if (width <= 0 || height <= 0) {
140
+ return { status: "failed", failure: "no-size" };
141
+ }
142
+ const renderNode = getRenderNode(widget, scale, width, height);
143
+ if (!renderNode) {
144
+ return { status: "failed", failure: "no-content" };
145
+ }
146
+ const scaledWidth = Math.round(width * scale);
147
+ const scaledHeight = Math.round(height * scale);
148
+ return { status: "captured", result: renderToPng(widget, renderNode, scaledWidth, scaledHeight) };
149
+ };
150
+ const failureMessage = (widget, failure) => failure === "no-size"
151
+ ? `Widget has no size: ensure it is realized and visible (${describeWidgetState(widget)})`
152
+ : `Widget produced no render content (${describeWidgetState(widget)})`;
153
+ const notPresentingMessage = (widget) => `${NOT_PRESENTING_MESSAGE} (${describePresentation(widget)})`;
154
+ const stalledMessage = (widget, failure) => `${notPresentingMessage(widget)}\n\n${failureMessage(widget, failure)}`;
155
+ const presentingHint = (failure) => failure === "no-content" ? EMPTY_WIDGET_HINT : OTHER_FAILURE_HINT;
156
+ const timeoutHint = (widget, state) => isPresenting(widget, state.probe) ? presentingHint(state.failure) : notPresentingMessage(widget);
157
+ const explainTimeout = (error, widget, state) => getConfig().getElementError(`${error.message}\n\n${timeoutHint(widget, state)}`);
158
+ const attemptCapture = (widget, scale, state) => {
159
+ const attempt = captureSnapshot(widget, scale);
160
+ if (attempt.status === "captured") {
161
+ return attempt;
162
+ }
163
+ state.failure = attempt.failure;
164
+ updateFrameProbe(widget, state.probe);
165
+ if (isPresentationStalled(widget, state.probe)) {
166
+ return { status: "stalled", failure: attempt.failure };
167
+ }
168
+ throw new Error(failureMessage(widget, attempt.failure));
169
+ };
170
+ const writeScreenshot = (result, path) => {
171
+ mkdirSync(dirname(path), { recursive: true });
172
+ writeFileSync(path, Buffer.from(result.data, "base64"));
173
+ };
174
+ const captureUntilPaintable = (widget, scale, options) => {
175
+ const state = { probe: startFrameProbe(widget), failure: null };
176
+ return waitFor(() => attemptCapture(widget, scale, state), {
177
+ timeout: options?.timeout,
178
+ interval: options?.interval ?? DEFAULT_SCREENSHOT_INTERVAL,
179
+ onTimeout: (error) => explainTimeout(error, widget, state),
180
+ });
181
+ };
48
182
  /**
49
- * Captures a PNG snapshot of a widget, retrying until it has a paintable size.
183
+ * Captures a PNG snapshot of a widget, retrying until it has a paintable size,
184
+ * and writes the image to `options.path` when one is given, creating any
185
+ * missing parent directories. Popovers open inside the widget render on their
186
+ * own surfaces, so they are composited into the image at their on-screen
187
+ * positions. A display that never presents a frame leaves GTK without a cached
188
+ * render node, so the widget's contents are then snapshotted directly; when
189
+ * even that yields nothing, the failure names the display instead of blaming
190
+ * the widget.
50
191
  *
51
192
  * @param widget The widget to render to an image.
52
- * @param options Optional scale, timeout, and retry interval.
193
+ * @param options Optional scale, timeout, retry interval, and output path.
53
194
  * @returns The base64-encoded PNG data along with its mime type and dimensions.
54
195
  */
55
196
  const screenshot = async (widget, options) => {
@@ -57,79 +198,30 @@ const screenshot = async (widget, options) => {
57
198
  if (!Number.isFinite(scale) || scale <= 0) {
58
199
  throw new Error(`Screenshot scale must be a positive number, got ${String(scale)}`);
59
200
  }
60
- return waitFor(() => captureSnapshot(widget, scale), {
61
- timeout: options?.timeout,
62
- interval: options?.interval ?? DEFAULT_SCREENSHOT_INTERVAL,
63
- });
64
- };
65
- const firstToplevelWindow = (windows) => {
66
- const [first] = windows;
67
- if (!(first instanceof Gtk.Window)) {
68
- throw new TypeError("First toplevel is not a Window");
201
+ const outcome = await captureUntilPaintable(widget, scale, options);
202
+ if (outcome.status === "stalled") {
203
+ throw new Error(stalledMessage(widget, outcome.failure));
69
204
  }
70
- return first;
71
- };
72
- const windowAtIndex = (windows, index) => {
73
- const indexed = windows[index];
74
- if (!(indexed instanceof Gtk.Window)) {
75
- throw new TypeError(`Window at index ${String(index)} not found`);
205
+ if (options?.path) {
206
+ writeScreenshot(outcome.result, options.path);
76
207
  }
77
- return indexed;
208
+ return outcome.result;
78
209
  };
79
210
  const isWindow = (widget) => widget instanceof Gtk.Window;
80
- const hasMatchingTitle = (window, selector) => {
81
- const title = window.getTitle() ?? "";
82
- return selector instanceof RegExp ? selector.test(title) : title.includes(selector);
83
- };
84
- const describeTitleSelector = (selector) => selector instanceof RegExp ? selector.toString() : `"${selector}"`;
85
- const windowByTitle = (windows, selector) => {
86
- const found = windows.filter(isWindow).find((window) => hasMatchingTitle(window, selector));
87
- if (!found) {
88
- throw new Error(`No window found with title matching ${describeTitleSelector(selector)}`);
89
- }
90
- return found;
91
- };
92
- const resolveWindow = (selector) => {
93
- const windows = Gtk.Window.listToplevels();
94
- if (windows.length === 0) {
95
- throw new Error("No windows available for screenshot");
96
- }
97
- if (selector === undefined) {
98
- return firstToplevelWindow(windows);
211
+ const isOnScreen = (window) => window.getMapped();
212
+ const isActiveWindow = (window) => window.isActive();
213
+ const activeToplevel = () => {
214
+ const toplevels = Gtk.Window.listToplevels().filter((widget) => isWindow(widget));
215
+ const onScreen = toplevels.filter((window) => isOnScreen(window));
216
+ const target = onScreen.find((window) => isActiveWindow(window)) ?? onScreen[0];
217
+ if (!target) {
218
+ throw new Error(NOTHING_ON_SCREEN_MESSAGE);
99
219
  }
100
- if (typeof selector === "number") {
101
- return windowAtIndex(windows, selector);
102
- }
103
- return windowByTitle(windows, selector);
104
- };
105
- const saveScreenshotToTempFile = (result) => {
106
- const dir = mkdtempSync(join(tmpdir(), "gtkx-screenshots-"));
107
- const filepath = join(dir, "screenshot.png");
108
- writeFileSync(filepath, Buffer.from(result.data, "base64"));
109
- return filepath;
220
+ return target;
110
221
  };
111
- /**
112
- * Prints a clickable file URL for a saved screenshot to the console.
113
- *
114
- * @param filepath Absolute path of the saved screenshot file.
115
- */
116
- const logScreenshotPath = (filepath) => {
117
- console.log(`Screenshot saved: file://${filepath}`);
118
- };
119
- /**
120
- * Captures a screenshot of a toplevel window, writes it to a temporary file,
121
- * logs its path, and returns the image data.
122
- *
123
- * @param selector Chooses the window by index, title substring, or regular
124
- * expression; defaults to the first toplevel.
125
- * @param options Optional scale, timeout, and retry interval.
126
- * @returns The base64-encoded PNG data along with its mime type and dimensions.
127
- */
128
- const captureAndSaveScreenshot = async (selector, options) => {
129
- const target = resolveWindow(selector);
130
- const result = await screenshot(target, options);
131
- logScreenshotPath(saveScreenshotToTempFile(result));
132
- return result;
222
+ const captureScreen = async (options) => {
223
+ const target = activeToplevel();
224
+ return screenshot(target, options);
133
225
  };
134
- export { screenshot, logScreenshotPath, captureAndSaveScreenshot };
226
+ export { captureScreen, screenshot };
135
227
  //# sourceMappingURL=screenshot.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"screenshot.js","sourceRoot":"","sources":["../src/screenshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEj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Q,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;IAE5D,OAAO,QAAQ,CAAC;AACpB,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAQ,EAAE;IACjD,OAAO,CAAC,GAAG,CAAC,4BAA4B,QAAQ,EAAE,CAAC,CAAC;AACxD,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,wBAAwB,GAAG,KAAK,EAClC,QAAyB,EACzB,OAA2B,EACF,EAAE;IAC3B,MAAM,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IACvC,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjD,iBAAiB,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC,CAAC;IAEpD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,CAAC","sourcesContent":["import * as Gsk from \"@gtkx/gi/gsk\";\nimport * as Gtk from \"@gtkx/gi/gtk\";\nimport { mkdtempSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { ScreenshotOptions, ScreenshotResult, WindowSelector } from \"./types.js\";\nimport { waitFor } from \"./wait-for.js\";\n\nconst DEFAULT_SCREENSHOT_INTERVAL = 10;\n\nconst bytesToBase64 = (bytes: number[]): string => {\n return Buffer.from(bytes).toString(\"base64\");\n};\n\nconst describeWidgetState = (widget: Gtk.Widget): string =>\n `realized=${String(widget.getRealized())} mapped=${String(widget.getMapped())} ` +\n `visible=${String(widget.getVisible())}`;\n\nconst captureSnapshot = (widget: Gtk.Widget, scale: number): ScreenshotResult => {\n const paintable = new Gtk.WidgetPaintable({ widget });\n const width = paintable.getIntrinsicWidth();\n const height = paintable.getIntrinsicHeight();\n\n if (width <= 0 || height <= 0) {\n throw new Error(`Widget has no size: ensure it is realized and visible (${describeWidgetState(widget)})`);\n }\n\n const snapshot = new Gtk.Snapshot();\n snapshot.scale(scale, scale);\n paintable.snapshot(snapshot, width, height);\n const renderNode = snapshot.toNode();\n\n if (!renderNode) {\n throw new Error(`Widget produced no render content (${describeWidgetState(widget)})`);\n }\n\n const display = widget.getDisplay();\n const renderer = new Gsk.CairoRenderer();\n renderer.realizeForDisplay(display);\n\n try {\n const texture = renderer.renderTexture(renderNode, null);\n const pngBytes = texture.saveToPngBytes();\n const data = pngBytes.getData();\n\n if (!data) {\n throw new Error(\"Failed to serialize screenshot to PNG\");\n }\n\n return {\n data: bytesToBase64(data),\n mimeType: \"image/png\",\n width: Math.round(width * scale),\n height: Math.round(height * scale),\n };\n } finally {\n renderer.unrealize();\n }\n};\n\n/**\n * Captures a PNG snapshot of a widget, retrying until it has a paintable size.\n *\n * @param widget The widget to render to an image.\n * @param options Optional scale, timeout, and retry interval.\n * @returns The base64-encoded PNG data along with its mime type and dimensions.\n */\nconst screenshot = async (widget: Gtk.Widget, options?: ScreenshotOptions): Promise<ScreenshotResult> => {\n const scale = options?.scale ?? 1;\n\n if (!Number.isFinite(scale) || scale <= 0) {\n throw new Error(`Screenshot scale must be a positive number, got ${String(scale)}`);\n }\n\n return waitFor(() => captureSnapshot(widget, scale), {\n timeout: options?.timeout,\n interval: options?.interval ?? DEFAULT_SCREENSHOT_INTERVAL,\n });\n};\n\nconst firstToplevelWindow = (windows: Gtk.Widget[]): Gtk.Window => {\n const [first] = windows;\n\n if (!(first instanceof Gtk.Window)) {\n throw new TypeError(\"First toplevel is not a Window\");\n }\n\n return first;\n};\n\nconst windowAtIndex = (windows: Gtk.Widget[], index: number): Gtk.Window => {\n const indexed = windows[index];\n\n if (!(indexed instanceof Gtk.Window)) {\n throw new TypeError(`Window at index ${String(index)} not found`);\n }\n\n return indexed;\n};\n\nconst isWindow = (widget: Gtk.Widget): widget is Gtk.Window => widget instanceof Gtk.Window;\n\nconst hasMatchingTitle = (window: Gtk.Window, selector: string | RegExp): boolean => {\n const title = window.getTitle() ?? \"\";\n\n return selector instanceof RegExp ? selector.test(title) : title.includes(selector);\n};\n\nconst describeTitleSelector = (selector: string | RegExp): string =>\n selector instanceof RegExp ? selector.toString() : `\"${selector}\"`;\n\nconst windowByTitle = (windows: Gtk.Widget[], selector: string | RegExp): Gtk.Window => {\n const found = windows.filter(isWindow).find((window) => hasMatchingTitle(window, selector));\n\n if (!found) {\n throw new Error(`No window found with title matching ${describeTitleSelector(selector)}`);\n }\n\n return found;\n};\n\nconst resolveWindow = (selector?: WindowSelector): Gtk.Window => {\n const windows = Gtk.Window.listToplevels();\n\n if (windows.length === 0) {\n throw new Error(\"No windows available for screenshot\");\n }\n\n if (selector === undefined) {\n return firstToplevelWindow(windows);\n }\n\n if (typeof selector === \"number\") {\n return windowAtIndex(windows, selector);\n }\n\n return windowByTitle(windows, selector);\n};\n\nconst saveScreenshotToTempFile = (result: ScreenshotResult): string => {\n const dir = mkdtempSync(join(tmpdir(), \"gtkx-screenshots-\"));\n const filepath = join(dir, \"screenshot.png\");\n writeFileSync(filepath, Buffer.from(result.data, \"base64\"));\n\n return filepath;\n};\n\n/**\n * Prints a clickable file URL for a saved screenshot to the console.\n *\n * @param filepath Absolute path of the saved screenshot file.\n */\nconst logScreenshotPath = (filepath: string): void => {\n console.log(`Screenshot saved: file://${filepath}`);\n};\n\n/**\n * Captures a screenshot of a toplevel window, writes it to a temporary file,\n * logs its path, and returns the image data.\n *\n * @param selector Chooses the window by index, title substring, or regular\n * expression; defaults to the first toplevel.\n * @param options Optional scale, timeout, and retry interval.\n * @returns The base64-encoded PNG data along with its mime type and dimensions.\n */\nconst captureAndSaveScreenshot = async (\n selector?: WindowSelector,\n options?: ScreenshotOptions,\n): Promise<ScreenshotResult> => {\n const target = resolveWindow(selector);\n const result = await screenshot(target, options);\n logScreenshotPath(saveScreenshotToTempFile(result));\n\n return result;\n};\n\nexport { screenshot, logSc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CAAC,MAAM,CAAC,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEhF,IAAI,CAAC,MAAM,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,aAAa,GAAG,KAAK,EAAE,OAA2B,EAA6B,EAAE;IACnF,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,OAAO,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACvC,CAAC,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,CAAC","sourcesContent":["import * as Gdk from \"@gtkx/gi/gdk\";\nimport * as Graphene from \"@gtkx/gi/graphene\";\nimport * as Gsk from \"@gtkx/gi/gsk\";\nimport * as Gtk from \"@gtkx/gi/gtk\";\nimport { mkdirSync, writeFileSync } from \"node:fs\";\nimport { dirname } from \"node:path\";\nimport type { ScreenshotOptions, ScreenshotResult } from \"./types.js\";\nimport { getConfig } from \"./config.js\";\nimport { descendants } from \"./traversal.js\";\nimport { waitFor } from \"./wait-for.js\";\n\ntype FrameProbe = {\n counter: bigint | null;\n startedAt: number;\n};\n\ntype CaptureFailure = \"no-size\" | \"no-content\";\n\ntype CaptureState = {\n probe: FrameProbe;\n failure: CaptureFailure | null;\n};\n\ntype CaptureResult =\n { status: \"captured\"; result: ScreenshotResult } |\n { status: \"failed\"; failure: CaptureFailure };\n\ntype CaptureOutcome =\n { status: \"captured\"; result: ScreenshotResult } |\n { status: \"stalled\"; failure: CaptureFailure };\n\nconst DEFAULT_SCREENSHOT_INTERVAL = 10;\nconst PRESENTATION_PROBE_MS = 250;\n\nconst NOT_PRESENTING_MESSAGE =\n \"Screenshot failed: the display is not presenting frames to this window, so GTK has no rendered \" +\n \"content to capture. The window is hidden, minimized, or its compositor is not presenting it; this \" +\n \"is a display problem, not a widget problem.\";\n\nconst EMPTY_WIDGET_HINT =\n \"The display is presenting frames to this window, so the widget itself is empty: it painted nothing.\";\n\nconst OTHER_FAILURE_HINT =\n \"The display is presenting frames to this window, so the capture failed for another reason.\";\n\nconst NOTHING_ON_SCREEN_MESSAGE = \"Nothing is on screen to capture: no toplevel window is mapped\";\n\nconst bytesToBase64 = (bytes: number[]): string => {\n return Buffer.from(bytes).toString(\"base64\");\n};\n\nconst describeWidgetState = (widget: Gtk.Widget): string =>\n `realized=${String(widget.getRealized())} mapped=${String(widget.getMapped())} ` +\n `visible=${String(widget.getVisible())}`;\n\nconst getMappedPopovers = (widget: Gtk.Widget): Gtk.Popover[] => {\n const popovers: Gtk.Popover[] = [];\n\n for (const descendant of descendants(widget)) {\n if (descendant instanceof Gtk.Popover && descendant.getMapped()) {\n popovers.push(descendant);\n }\n }\n\n return popovers;\n};\n\nconst appendPopover = (snapshot: Gtk.Snapshot, target: Gtk.Widget, popover: Gtk.Popover): void => {\n const [hasBounds, bounds] = popover.computeBounds(target);\n const paintable = new Gtk.WidgetPaintable({ widget: popover });\n const width = paintable.getIntrinsicWidth();\n const height = paintable.getIntrinsicHeight();\n\n if (!hasBounds || width <= 0 || height <= 0) {\n return;\n }\n\n snapshot.save();\n snapshot.translate(new Graphene.Point({ x: bounds.origin.x, y: bounds.origin.y }));\n paintable.snapshot(snapshot, width, height);\n snapshot.restore();\n};\n\nconst appendPopovers = (snapshot: Gtk.Snapshot, target: Gtk.Widget): void => {\n for (const popover of getMappedPopovers(target)) {\n appendPopover(snapshot, target, popover);\n }\n};\n\nconst getSurface = (widget: Gtk.Widget): Gdk.Surface | null => widget.getNative()?.getSurface() ?? null;\nconst isSurfaceOnScreen = (widget: Gtk.Widget): boolean => getSurface(widget)?.getMapped() ?? false;\n\nconst getFrameClock = (widget: Gtk.Widget): Gdk.FrameClock | null =>\n widget.getRoot()?.getFrameClock() ?? widget.getFrameClock();\n\nconst getFrameCounter = (widget: Gtk.Widget): bigint | null => getFrameClock(widget)?.getFrameCounter() ?? null;\n\nconst describePresentation = (widget: Gtk.Widget): string =>\n `${describeWidgetState(widget)} surfaceMapped=${String(isSurfaceOnScreen(widget))} ` +\n `frameCounter=${String(getFrameCounter(widget))}`;\n\nconst requestFrame = (widget: Gtk.Widget): void => {\n getFrameClock(widget)?.requestPhase(Gdk.FrameClockPhase.UPDATE);\n};\n\nconst startFrameProbe = (widget: Gtk.Widget): FrameProbe => {\n requestFrame(widget);\n\n return { counter: getFrameCounter(widget), startedAt: Date.now() };\n};\n\nconst updateFrameProbe = (widget: Gtk.Widget, probe: FrameProbe): void => {\n requestFrame(widget);\n\n if (probe.counter !== null) {\n return;\n }\n\n const counter = getFrameCounter(widget);\n\n if (counter !== null) {\n probe.counter = counter;\n probe.startedAt = Date.now();\n }\n};\n\nconst hasFrameAdvanced = (widget: Gtk.Widget, probe: FrameProbe): boolean => {\n const counter = getFrameCounter(widget);\n\n return counter !== null && probe.counter !== null && counter > probe.counter;\n};\n\nconst isPresenting = (widget: Gtk.Widget, probe: FrameProbe): boolean =>\n isSurfaceOnScreen(widget) && hasFrameAdvanced(widget, probe);\n\nconst isPresentationStalled = (widget: Gtk.Widget, probe: FrameProbe): boolean =>\n Date.now() - probe.startedAt >= PRESENTATION_PROBE_MS && !isPresenting(widget, probe);\n\nconst allocateRoot = (widget: Gtk.Widget): void => {\n const root = widget.getRoot();\n\n if (!root) {\n return;\n }\n\n const width = root.getWidth();\n const height = root.getHeight();\n\n if (width > 0 && height > 0) {\n root.allocate(width, height, -1, null);\n }\n};\n\nconst isDirectlyPaintable = (child: Gtk.Widget): boolean =>\n child.getMapped() && child.getWidth() > 0 && !(child instanceof Gtk.Popover);\n\nconst getPaintableNode = (widget: Gtk.Widget, scale: number, width: number, height: number): Gsk.RenderNode | null => {\n const paintable = new Gtk.WidgetPaintable({ widget });\n const snapshot = new Gtk.Snapshot();\n snapshot.scale(scale, scale);\n paintable.snapshot(snapshot, width, height);\n appendPopovers(snapshot, widget);\n\n return snapshot.toNode();\n};\n\nconst getChildrenNode = (widget: Gtk.Widget, scale: number): Gsk.RenderNode | null => {\n const snapshot = new Gtk.Snapshot();\n snapshot.scale(scale, scale);\n\n for (let child = widget.getFirstChild(); child !== null; child = child.getNextSibling()) {\n if (isDirectlyPaintable(child)) {\n widget.snapshotChild(child, snapshot);\n }\n }\n\n appendPopovers(snapshot, widget);\n\n return snapshot.toNode();\n};\n\nconst getRenderNode = (widget: Gtk.Widget, scale: number, width: number, height: number): Gsk.RenderNode | null => {\n const presented = getPaintableNode(widget, scale, width, height);\n\n if (presented) {\n return presented;\n }\n\n allocateRoot(widget);\n\n return getPaintableNode(widget, scale, width, height) ?? getChildrenNode(widget, scale);\n};\n\nconst renderToPng = (widget: Gtk.Widget, node: Gsk.RenderNode, width: number, height: number): ScreenshotResult => {\n const renderer = new Gsk.CairoRenderer();\n renderer.realizeForDisplay(widget.getDisplay());\n\n try {\n const viewport = Graphene.Rect.alloc().init(0, 0, width, height);\n const texture = renderer.renderTexture(node, viewport);\n const data = texture.saveToPngBytes().getData();\n\n if (!data) {\n throw new Error(\"Failed to serialize screenshot to PNG\");\n }\n\n return { data: bytesToBase64(data), mimeType: \"image/png\", width, height };\n } finally {\n renderer.unrealize();\n }\n};\n\nconst captureSnapshot = (widget: Gtk.Widget, scale: number): CaptureResult => {\n const paintable = new Gtk.WidgetPaintable({ widget });\n const width = paintable.getIntrinsicWidth();\n const height = paintable.getIntrinsicHeight();\n\n if (width <= 0 || height <= 0) {\n return { status: \"failed\", failure: \"no-size\" };\n }\n\n const renderNode = getRenderNode(widget, scale, width, height);\n\n if (!renderNode) {\n return { status: \"failed\", failure: \"no-content\" };\n }\n\n const scaledWidth = Math.round(width * scale);\n const scaledHeight = Math.round(height * scale);\n\n return { status: \"captured\", result: renderToPng(widget, renderNode, scaledWidth, scaledHeight) };\n};\n\nconst failureMessage = (widget: Gtk.Widget, failure: CaptureFailure): string =>\n failure === \"no-size\"\n ? `Widget has no size: ensure it is realized and visible (${describeWidgetState(widget)})`\n : `Widget produced no render content (${describeWidgetState(widget)})`;\n\nconst notPresentingMessage = (widget: Gtk.Widget): string =>\n `${NOT_PRESENTING_MESSAGE} (${describePresentation(widget)})`;\n\nconst stalledMessage = (widget: Gtk.Widget, failure: CaptureFailure): string =>\n `${notPresentingMessage(widget)}\\n\\n${failureMessage(widget, failure)}`;\n\nconst presentingHint = (failure: CaptureFailure | null): string =>\n failure === \"no-content\" ? EMPTY_WIDGET_HINT : OTHER_FAILURE_HINT;\n\nconst timeoutHint = (widget: Gtk.Widget, state: CaptureState): string =>\n isPresenting(widget, state.probe) ? presentingHint(state.failure) : notPresentingMessage(widget);\n\nconst explainTimeout = (error: Error, widget: Gtk.Widget, state: CaptureState): Error =>\n getConfig().getElementError(`${error.message}\\n\\n${timeoutHint(widget, state)}`);\n\nconst attemptCapture = (widget: Gtk.Widget, scale: number, state: CaptureState): CaptureOutcome => {\n const attempt = captureSnapshot(widget, scale);\n\n if (attempt.status === \"captured\") {\n return attempt;\n }\n\n state.failure = attempt.failure;\n updateFrameProbe(widget, state.probe);\n\n if (isPresentationStalled(widget, state.probe)) {\n return { status: \"stalled\", failure: attempt.failure };\n }\n\n throw new Error(failureMessage(widget, attempt.failure));\n};\n\nconst writeScreenshot = (result: ScreenshotResult, path: string): void => {\n mkdirSync(dirname(path), { recursive: true });\n writeFileSync(path, Buffer.from(result.data, \"base64\"));\n};\n\nconst captureUntilPaintable = (\n widget: Gtk.Widget,\n scale: number,\n options: ScreenshotOptions | undefined,\n): Promise<CaptureOutcome> => {\n const state: CaptureState = { probe: startFrameProbe(widget), failure: null };\n\n return waitFor(() => attemptCapture(widget, scale, state), {\n timeout: options?.timeout,\n interval: options?.interval ?? DEFAULT_SCREENSHOT_INTERVAL,\n onTimeout: (error) => explainTimeout(error, widget, state),\n });\n};\n\n/**\n * Captures a PNG snapshot of a widget, retrying until it has a paintable size,\n * and writes the image to `options.path` when one is given, creating any\n * missing parent directories. Popovers open inside the widget render on their\n * own surfaces, so they are composited into the image at their on-screen\n * positions. A display that never presents a frame leaves GTK without a cached\n * render node, so the widget's contents are then snapshotted directly; when\n * even that yields nothing, the failure names the display instead of blaming\n * the widget.\n *\n * @param widget The widget to render to an image.\n * @param options Optional scale, timeout, retry interval, and output path.\n * @returns The base64-encoded PNG data along with its mime type and dimensions.\n */\nconst screenshot = async (widget: Gtk.Widget, options?: ScreenshotOptions): Promise<ScreenshotResult> => {\n const scale = options?.scale ?? 1;\n\n if (!Number.isFinite(scale) || scale <= 0) {\n throw new Error(`Screenshot scale must be a positive number, got ${String(scale)}`);\n }\n\n const outcome = await captureUntilPaintable(widget, scale, options);\n\n if (outcome.status === \"stalled\") {\n throw new Error(stalledMessage(widget, outcome.failure));\n }\n\n if (options?.path) {\n writeScreenshot(outcome.result, options.path);\n }\n\n return outcome.result;\n};\n\nconst isWindow = (widget: Gtk.Widget): widget is Gtk.Window => widget instanceof Gtk.Window;\nconst isOnScreen = (window: Gtk.Window): boolean => window.getMapped();\nconst isActiveWindow = (window: Gtk.Window): boolean => window.isActive();\n\nconst activeToplevel = (): Gtk.Window => {\n const toplevels = Gtk.Window.listToplevels().filter((widget) => isWindow(widget));\n const onScreen = toplevels.filter((window) => isOnScreen(window));\n const target = onScreen.find((window) => isActiveWindow(window)) ?? onScreen[0];\n\n if (!target) {\n throw new Error(NOTHING_ON_SCREEN_MESSAGE);\n }\n\n return target;\n};\n\nconst captureScreen = async (options?: ScreenshotOptions): Promise<ScreenshotResult> => {\n const target = activeToplevel();\n\n return screenshot(target, options);\n};\n\nexport { captureScreen, screenshot };\n"]}
@@ -12,9 +12,12 @@ type QueryContainer = Gtk.Widget | Gtk.EventController | Gtk.LayoutManager | Gtk
12
12
  type Container = QueryContainer | Gtk.Application | typeof TOPLEVELS;
13
13
  /** Container sentinel that widens a query to every toplevel window currently open. */
14
14
  declare const TOPLEVELS: unique symbol;
15
+ declare const isOnScreen: (widget: Gtk.Widget) => boolean;
16
+ declare const children: (widget: Gtk.Widget) => Generator<Gtk.Widget>;
15
17
  declare const descendants: (widget: Gtk.Widget) => Generator<Gtk.Widget>;
18
+ declare const relationCandidates: (widget: Gtk.Widget) => Gtk.Accessible[];
16
19
  declare const roots: (container: Container) => Generator<Gtk.Widget>;
17
20
  declare const traverse: (container: Container) => Generator<Gtk.Widget>;
18
21
  declare const findAll: (container: Container, isMatch: (node: Gtk.Widget) => boolean) => Gtk.Widget[];
19
- export { TOPLEVELS, descendants, roots, traverse, findAll, type Container };
22
+ export { TOPLEVELS, children, descendants, findAll, isOnScreen, relationCandidates, roots, traverse, type Container, };
20
23
  //# sourceMappingURL=traversal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"traversal.d.ts","sourceRoot":"","sources":["../src/traversal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAEpC;;;GAGG;AACH,KAAK,cAAc,GAAG,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,eAAe,GAAG,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,UAAU,CAAC;AAC3G;;;;GAIG;AACH,KAAK,SAAS,GAAG,cAAc,GAAG,GAAG,CAAC,WAAW,GAAG,OAAO,SAAS,CAAC;AAErE,sFAAsF;AACtF,QAAA,MAAM,SAAS,EAAE,OAAO,MAAiC,CAAC;AAc1D,QAAA,MAAM,WAAW,GAAc,QAAQ,GAAG,CAAC,MAAM,KAAG,SAAS,CAAC,GAAG,CAAC,MAAM,CAIvE,CAAC;AAsBF,QAAA,MAAM,KAAK,GAAc,WAAW,SAAS,KAAG,SAAS,CAAC,GAAG,CAAC,MAAM,CAYnE,CAAC;AAEF,QAAA,MAAM,QAAQ,GAAc,WAAW,SAAS,KAAG,SAAS,CAAC,GAAG,CAAC,MAAM,CAItE,CAAC;AAEF,QAAA,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AEF,QAAA,MAAM,OAAO,GAAI,WAAW,SAAS,EAAE,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,KAAK,OAAO,KAAG,GAAG,CAAC,MAAM,EAUzF,CAAC;AAEF,OAAO,EACH,SAAS,EACT,QAAQ,EACR,WAAW,EACX,OAAO,EACP,UAAU,EACV,kBAAkB,EAClB,KAAK,EACL,QAAQ,EACR,KAAK,SAAS,GACjB,CAAC"}
package/dist/traversal.js CHANGED
@@ -2,19 +2,38 @@ import * as Gtk from "@gtkx/gi/gtk";
2
2
  /** Container sentinel that widens a query to every toplevel window currently open. */
3
3
  const TOPLEVELS = Symbol("gtkx.toplevels");
4
4
  const isApplication = (container) => container instanceof Gtk.Application;
5
- const traverseWidgetTree = function* (root) {
6
- yield root;
5
+ const isAnyWidget = () => true;
6
+ const isOnScreen = (widget) => widget.getMapped();
7
+ const traverseWidgetTree = function* (root, isIncluded) {
8
+ if (isIncluded(root)) {
9
+ yield root;
10
+ }
7
11
  let child = root.getFirstChild();
8
12
  while (child) {
9
- yield* traverseWidgetTree(child);
13
+ yield* traverseWidgetTree(child, isIncluded);
14
+ child = child.getNextSibling();
15
+ }
16
+ };
17
+ const children = function* (widget) {
18
+ let child = widget.getFirstChild();
19
+ while (child) {
20
+ yield child;
10
21
  child = child.getNextSibling();
11
22
  }
12
23
  };
13
24
  const descendants = function* (widget) {
14
- const tree = traverseWidgetTree(widget);
25
+ const tree = traverseWidgetTree(widget, isAnyWidget);
15
26
  tree.next();
16
27
  yield* tree;
17
28
  };
29
+ const relationCandidates = (widget) => {
30
+ const pool = [...widget.listMnemonicLabels(), ...descendants(widget)];
31
+ const root = widget.getRoot();
32
+ if (root instanceof Gtk.Widget) {
33
+ pool.push(...traverseWidgetTree(root, isAnyWidget));
34
+ }
35
+ return [...new Set(pool)];
36
+ };
18
37
  const resolveRoot = (container) => {
19
38
  if (container instanceof Gtk.Widget) {
20
39
  return container;
@@ -42,7 +61,7 @@ const roots = function* (container) {
42
61
  };
43
62
  const traverse = function* (container) {
44
63
  for (const root of roots(container)) {
45
- yield* traverseWidgetTree(root);
64
+ yield* traverseWidgetTree(root, isOnScreen);
46
65
  }
47
66
  };
48
67
  const findAll = (container, isMatch) => {
@@ -54,5 +73,5 @@ const findAll = (container, isMatch) => {
54
73
  }
55
74
  return results;
56
75
  };
57
- export { TOPLEVELS, descendants, roots, traverse, findAll };
76
+ export { TOPLEVELS, children, descendants, findAll, isOnScreen, relationCandidates, roots, traverse, };
58
77
  //# sourceMappingURL=traversal.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"traversal.js","sourceRoot":"","sources":["../src/traversal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAcpC,sFAAsF;AACtF,MAAM,SAAS,GAAkB,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAE1D,MAAM,aAAa,GAAG,CAAC,SAAoB,EAAgC,EAAE,CAAC,SAAS,YAAY,GAAG,CAAC,WAAW,CAAC;AAEnH,MAAM,kBAAkB,GAAG,QAAQ,CAAC,EAAE,IAAgB;IAClD,MAAM,IAAI,CAAC;IACX,IAAI,KAAK,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;IAEjC,OAAO,KAAK,EAAE,CAAC;QACX,KAAK,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACjC,KAAK,GAAG,KAAK,CAAC,cAAc,EAAE,CAAC;IACnC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,QAAQ,CAAC,EAAE,MAAkB;IAC7C,MAAM,IAAI,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACxC,IAAI,CAAC,IAAI,EAAE,CAAC;IACZ,KAAK,CAAC,CAAC,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAC,SAAyB,EAAqB,EAAE;IACjE,IAAI,SAAS,YAAY,GAAG,CAAC,MAAM,EAAE,CAAC;QAClC,OAAO,S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CAAC,CAAC;QACvB,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAkB,CAAC","sourcesContent":["import * as Gtk from \"@gtkx/gi/gtk\";\n\n/**\n * A scope that resolves to a single root widget: the widget itself, the widget a controller or\n * layout manager is attached to, or a list item's or header's child.\n */\ntype QueryContainer = Gtk.Widget | Gtk.EventController | Gtk.LayoutManager | Gtk.ListItem | Gtk.ListHeader;\n/**\n * A scope that queries and traversal can run against: a widget, an event\n * controller, a layout manager, a list item or header, an application, or the\n * sentinel representing all current toplevel windows.\n */\ntype Container = QueryContainer | Gtk.Application | typeof TOPLEVELS;\n\n/** Container sentinel that widens a query to every toplevel window currently open. */\nconst TOPLEVELS: unique symbol = Symbol(\"gtkx.toplevels\");\n\nconst isApplication = (container: Container): container is Gtk.Application => container instanceof Gtk.Application;\n\nconst traverseWidgetTree = function* (root: Gtk.Widget): Generator<Gtk.Widget> {\n yield root;\n let child = root.getFirstChild();\n\n while (child) {\n yield* traverseWidgetTree(child);\n child = child.getNextSibling();\n }\n};\n\nconst descendants = function* (widget: Gtk.Widget): Generator<Gtk.Widget> {\n const tree = traverseWidgetTree(widget);\n tree.next();\n yield* tree;\n};\n\nconst resolveRoot = (container: QueryContainer): Gtk.Widget | null => {\n if (container instanceof Gtk.Widget) {\n return container;\n }\n\n if (container instanceof Gtk.EventController) {\n return container.getWidget();\n }\n\n if (container instanceof Gtk.LayoutManager) {\n return container.getWidget();\n }\n\n if (container instanceof Gtk.ListItem || container instanceof Gtk.ListHeader) {\n return container.getChild();\n }\n\n return null;\n};\n\nconst roots = function* (container: Container): Generator<Gtk.Widget> {\n if (container === TOPLEVELS || isApplication(container)) {\n yield* Gtk.Window.listToplevels();\n\n return;\n }\n\n const root = resolveRoot(container);\n\n if (root) {\n yield root;\n }\n};\n\nconst traverse = function* (container: Container): Generator<Gtk.Widget> {\n for (const root of roots(container)) {\n yield* traverseWidgetTree(root);\n }\n};\n\nconst findAll = (container: Container, isMatch: (node: Gtk.Widget) => boolean): Gtk.Widget[] => {\n const results: Gtk.Widget[] = [];\n\n for (const node of traverse(container)) {\n if (isMatch(node)) {\n results.push(node);\n }\n }\n\n return results;\n};\n\nexport { TOPLEVELS, descendants, roots, traverse, findAll, type Container };\n"]}
1
+ {"version":3,"file":"traversal.js","sourceRoot":"","sources":["../src/traversal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAcpC,sFAAsF;AACtF,MAAM,SAAS,GAAkB,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAE1D,MAAM,aAAa,GAAG,CAAC,SAAoB,EAAgC,EAAE,CAAC,SAAS,YAAY,GAAG,CAAC,WAAW,CAAC;AACnH,MAAM,WAAW,GAAG,GAAY,EAAE,CAAC,IAAI,CAAC;AACxC,MAAM,UAAU,GAAG,CAAC,MAAkB,EAAW,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;AAEvE,MAAM,kBAAkB,GAAG,QAAQ,CAAC,EAChC,IAAgB,EAChB,UAA2C;IAE3C,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,CAAC;IACf,CAAC;IAED,IAAI,KAAK,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;IAEjC,OAAO,KAAK,EAAE,CAAC;QACX,KAAK,CAAC,CAAC,kBAAkB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QAC7C,KAAK,GAAG,KAAK,CAAC,cAAc,EAAE,CAAC;IACnC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,QAAQ,GAAG,QAAQ,CAAC,EAAE,MAAkB;IAC1C,IAAI,KAAK,GAAG,MAAM,CAAC,aAAa,EAAE,CAAC;IAEnC,OAAO,KAAK,EAAE,CAAC;QACX,MAAM,KAAK,CAAC;QACZ,KAAK,GAAG,KAAK,C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O,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF,MAAM,KAAK,GAAG,QAAQ,CAAC,EAAE,SAAoB;IACzC,IAAI,SAAS,KAAK,SAAS,IAAI,aAAa,CAAC,SAAS,CAAC,EAAE,CAAC;QACtD,KAAK,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;QAElC,OAAO;IACX,CAAC;IAED,MAAM,IAAI,GAAG,WAAW,CAAC,SAAS,CAAC,CAAC;IAEpC,IAAI,IAAI,EAAE,CAAC;QACP,MAAM,IAAI,CAAC;IACf,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,QAAQ,GAAG,QAAQ,CAAC,EAAE,SAAoB;IAC5C,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;QAClC,KAAK,CAAC,CAAC,kBAAkB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IAChD,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,CAAC,SAAoB,EAAE,OAAsC,EAAgB,EAAE;IAC3F,MAAM,OAAO,GAAiB,EAAE,CAAC;IAEjC,KAAK,MAAM,IAAI,IAAI,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QACrC,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAChB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC;AAEF,OAAO,EACH,SAAS,EACT,QAAQ,EACR,WAAW,EACX,OAAO,EACP,UAAU,EACV,kBAAkB,EAClB,KAAK,EACL,QAAQ,GAEX,CAAC","sourcesContent":["import * as Gtk from \"@gtkx/gi/gtk\";\n\n/**\n * A scope that resolves to a single root widget: the widget itself, the widget a controller or\n * layout manager is attached to, or a list item's or header's child.\n */\ntype QueryContainer = Gtk.Widget | Gtk.EventController | Gtk.LayoutManager | Gtk.ListItem | Gtk.ListHeader;\n/**\n * A scope that queries and traversal can run against: a widget, an event\n * controller, a layout manager, a list item or header, an application, or the\n * sentinel representing all current toplevel windows.\n */\ntype Container = QueryContainer | Gtk.Application | typeof TOPLEVELS;\n\n/** Container sentinel that widens a query to every toplevel window currently open. */\nconst TOPLEVELS: unique symbol = Symbol(\"gtkx.toplevels\");\n\nconst isApplication = (container: Container): container is Gtk.Application => container instanceof Gtk.Application;\nconst isAnyWidget = (): boolean => true;\nconst isOnScreen = (widget: Gtk.Widget): boolean => widget.getMapped();\n\nconst traverseWidgetTree = function* (\n root: Gtk.Widget,\n isIncluded: (widget: Gtk.Widget) => boolean,\n): Generator<Gtk.Widget> {\n if (isIncluded(root)) {\n yield root;\n }\n\n let child = root.getFirstChild();\n\n while (child) {\n yield* traverseWidgetTree(child, isIncluded);\n child = child.getNextSibling();\n }\n};\n\nconst children = function* (widget: Gtk.Widget): Generator<Gtk.Widget> {\n let child = widget.getFirstChild();\n\n while (child) {\n yield child;\n child = child.getNextSibling();\n }\n};\n\nconst descendants = function* (widget: Gtk.Widget): Generator<Gtk.Widget> {\n const tree = traverseWidgetTree(widget, isAnyWidget);\n tree.next();\n yield* tree;\n};\n\nconst relationCandidates = (widget: Gtk.Widget): Gtk.Accessible[] => {\n const pool: Gtk.Accessible[] = [...widget.listMnemonicLabels(), ...descendants(widget)];\n const root = widget.getRoot();\n\n if (root instanceof Gtk.Widget) {\n pool.push(...traverseWidgetTree(root, isAnyWidget));\n }\n\n return [...new Set(pool)];\n};\n\nconst resolveRoot = (container: QueryContainer): Gtk.Widget | null => {\n if (container instanceof Gtk.Widget) {\n return container;\n }\n\n if (container instanceof Gtk.EventController) {\n return container.getWidget();\n }\n\n if (container instanceof Gtk.LayoutManager) {\n return container.getWidget();\n }\n\n if (container instanceof Gtk.ListItem || container instanceof Gtk.ListHeader) {\n return container.getChild();\n }\n\n return null;\n};\n\nconst roots = function* (container: Container): Generator<Gtk.Widget> {\n if (container === TOPLEVELS || isApplication(container)) {\n yield* Gtk.Window.listToplevels();\n\n return;\n }\n\n const root = resolveRoot(container);\n\n if (root) {\n yield root;\n }\n};\n\nconst traverse = function* (container: Container): Generator<Gtk.Widget> {\n for (const root of roots(container)) {\n yield* traverseWidgetTree(root, isOnScreen);\n }\n};\n\nconst findAll = (container: Container, isMatch: (node: Gtk.Widget) => boolean): Gtk.Widget[] => {\n const results: Gtk.Widget[] = [];\n\n for (const node of traverse(container)) {\n if (isMatch(node)) {\n results.push(node);\n }\n }\n\n return results;\n};\n\nexport {\n TOPLEVELS,\n children,\n descendants,\n findAll,\n isOnScreen,\n relationCandidates,\n roots,\n traverse,\n type Container,\n};\n"]}
package/dist/types.d.ts CHANGED
@@ -50,13 +50,19 @@ type MatcherOptions<T extends Gtk.Widget = Gtk.Widget> = {
50
50
  /** Restricts matches to instances of this widget class, and narrows the query's return type to it. */
51
51
  as?: WidgetType<T> | undefined;
52
52
  } & WaitForOptions;
53
- /** Constraints on a widget's numeric range value used by role queries. */
53
+ /**
54
+ * Constraints on a widget's numeric range value used by role queries. The numbers come from the
55
+ * accessibility tree and match within 0.001, the resolution GTK keeps them to.
56
+ */
54
57
  type ByRoleValue = {
55
58
  /** The current value. */
56
59
  now?: number | undefined;
57
60
  /** The lower bound of the value's range. */
58
61
  min?: number | undefined;
59
- /** The upper bound of the value's range. */
62
+ /**
63
+ * The highest value the widget can reach, which on a paged widget such as a scrollbar is its
64
+ * adjustment's upper bound less one page.
65
+ */
60
66
  max?: number | undefined;
61
67
  /** Matcher for the value's textual representation. */
62
68
  text?: Matcher | undefined;
@@ -81,7 +87,7 @@ type ByRoleOptions<T extends Gtk.Widget = Gtk.Widget> = MatcherOptions<T> & {
81
87
  description?: Matcher | undefined;
82
88
  /** Constraints on the widget's range value, each checked only when given. */
83
89
  value?: ByRoleValue | undefined;
84
- /** When true, include widgets excluded from the accessibility tree. */
90
+ /** When true, include widgets excluded from the accessibility tree. Widgets that are not mapped stay excluded. */
85
91
  hidden?: boolean | undefined;
86
92
  };
87
93
  /** A React component that wraps rendered content, receiving it as its children. */
@@ -157,8 +163,11 @@ type DebugUtilities = {
157
163
  debug: (element?: Container | Container[], options?: PrettyWidgetOptions) => void;
158
164
  /** Prints the accessible roles found in the scope, with the widgets carrying each one. */
159
165
  logRoles: () => void;
160
- /** Captures a window, writes the image to a temporary file, and logs its path. */
161
- screenshot: (selector?: WindowSelector, options?: ScreenshotOptions) => Promise<ScreenshotResult>;
166
+ /**
167
+ * Captures what is on screen, which is the active toplevel window: GTK4 exposes no position for
168
+ * a toplevel, so windows cannot be composited into a single image.
169
+ */
170
+ screenshot: (options?: ScreenshotOptions) => Promise<ScreenshotResult>;
162
171
  };
163
172
  /** A captured screenshot: base64-encoded image data, its MIME type, and pixel dimensions. */
164
173
  type ScreenshotResult = {
@@ -171,23 +180,25 @@ type ScreenshotResult = {
171
180
  /** Image height in pixels, after the scale factor is applied. */
172
181
  height: number;
173
182
  };
174
- /** Options for capturing a screenshot: the poll timeout and interval, plus a rendering scale factor. */
183
+ /**
184
+ * Options for capturing a screenshot: the poll timeout and interval, a rendering scale factor, and
185
+ * a file to write the PNG to.
186
+ */
175
187
  type ScreenshotOptions = Pick<WaitForOptions, "timeout" | "interval"> & {
176
188
  /** Device scale factor applied when rendering. */
177
- scale?: number;
189
+ scale?: number | undefined;
190
+ /** File the PNG is written to, with its parent directories created as needed. */
191
+ path?: string | undefined;
178
192
  };
179
- /**
180
- * Selects the window to screenshot by index, or by title (exact string or regular expression);
181
- * undefined targets the default window.
182
- */
183
- type WindowSelector = number | string | RegExp | undefined;
184
193
  /**
185
194
  * Options for {@link renderHook}: an optional wrapper and the initial props (required unless the
186
195
  * props type permits undefined).
187
196
  */
188
197
  type RenderHookOptions<Props> = {
198
+ /** Component wrapped around the one calling the hook, such as a context provider. */
189
199
  wrapper?: WrapperComponent;
190
200
  } & (undefined extends Props ? {
201
+ /** Props the hook is invoked with until `rerender` is given new ones. */
191
202
  initialProps?: Props;
192
203
  } : {
193
204
  initialProps: Props;
@@ -197,8 +208,9 @@ type RenderHookOptions<Props> = {
197
208
  * props and to unmount.
198
209
  */
199
210
  type RenderHookResult<Result, Props> = {
200
- /** Holds the most recent value returned by the hook under `current`. */
211
+ /** Stable object the hook's return value is written to on every render. */
201
212
  result: {
213
+ /** The hook's latest return value. */
202
214
  current: Result;
203
215
  };
204
216
  /** Re-invokes the hook, keeping the previous props when none are given. */
@@ -206,5 +218,5 @@ type RenderHookResult<Result, Props> = {
206
218
  /** Unmounts the component that calls the hook. */
207
219
  unmount: () => Promise<void>;
208
220
  };
209
- export { type MatcherFunction, type Matcher, type NormalizerFn, type NormalizerOptions, type WaitForOptions, type MatcherOptions, type ByRoleValue, type ByRoleOptions, type WidgetType, type WrapperComponent, type QueryMap, type BoundCustomQueries, type QueryFamilies, type RenderOptions, type DebugUtilities, type ScreenshotResult, type ScreenshotOptions, type WindowSelector, type RenderHookOptions, type RenderHookResult, };
221
+ export { type MatcherFunction, type Matcher, type NormalizerFn, type NormalizerOptions, type WaitForOptions, type MatcherOptions, type ByRoleValue, type ByRoleOptions, type WidgetType, type WrapperComponent, type QueryMap, type BoundCustomQueries, type QueryFamilies, type RenderOptions, type DebugUtilities, type ScreenshotResult, type ScreenshotOptions, type RenderHookOptions, type RenderHookResult, };
210
222
  //# sourceMappingURL=types.d.ts.map