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

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 (215) hide show
  1. package/README.md +3 -3
  2. package/dist/accessible-native.d.ts.map +1 -1
  3. package/dist/accessible-native.js.map +1 -1
  4. package/dist/act.d.ts +7 -1
  5. package/dist/act.d.ts.map +1 -1
  6. package/dist/act.js +54 -4
  7. package/dist/act.js.map +1 -1
  8. package/dist/bound-queries.d.ts +1 -1
  9. package/dist/bound-queries.d.ts.map +1 -1
  10. package/dist/bound-queries.js.map +1 -1
  11. package/dist/build-queries.d.ts +1 -1
  12. package/dist/build-queries.d.ts.map +1 -1
  13. package/dist/build-queries.js +1 -1
  14. package/dist/build-queries.js.map +1 -1
  15. package/dist/cleanup-registry.d.ts.map +1 -1
  16. package/dist/cleanup-registry.js +4 -1
  17. package/dist/cleanup-registry.js.map +1 -1
  18. package/dist/config.d.ts.map +1 -1
  19. package/dist/editable.d.ts.map +1 -1
  20. package/dist/errors.d.ts.map +1 -1
  21. package/dist/errors.js.map +1 -1
  22. package/dist/fire-event.d.ts.map +1 -1
  23. package/dist/fire-event.js +16 -4
  24. package/dist/fire-event.js.map +1 -1
  25. package/dist/frame-sync.d.ts.map +1 -1
  26. package/dist/frame-sync.js +1 -10
  27. package/dist/frame-sync.js.map +1 -1
  28. package/dist/harness-window.d.ts +8 -2
  29. package/dist/harness-window.d.ts.map +1 -1
  30. package/dist/harness-window.js +28 -5
  31. package/dist/harness-window.js.map +1 -1
  32. package/dist/index.d.ts +2 -2
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +1 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/internal.d.ts +5 -0
  37. package/dist/internal.d.ts.map +1 -1
  38. package/dist/internal.js +5 -0
  39. package/dist/internal.js.map +1 -1
  40. package/dist/matchers.d.ts +4 -1
  41. package/dist/matchers.d.ts.map +1 -1
  42. package/dist/matchers.js +29 -9
  43. package/dist/matchers.js.map +1 -1
  44. package/dist/normalize.d.ts +1 -1
  45. package/dist/normalize.d.ts.map +1 -1
  46. package/dist/normalize.js +1 -1
  47. package/dist/normalize.js.map +1 -1
  48. package/dist/pretty-widget.d.ts +4 -2
  49. package/dist/pretty-widget.d.ts.map +1 -1
  50. package/dist/pretty-widget.js +7 -5
  51. package/dist/pretty-widget.js.map +1 -1
  52. package/dist/production.d.ts.map +1 -1
  53. package/dist/queries.d.ts +2 -1
  54. package/dist/queries.d.ts.map +1 -1
  55. package/dist/queries.js +22 -11
  56. package/dist/queries.js.map +1 -1
  57. package/dist/register-test-hooks.js +8 -0
  58. package/dist/register-test-hooks.js.map +1 -1
  59. package/dist/render-hook.js.map +1 -1
  60. package/dist/render.d.ts +14 -6
  61. package/dist/render.d.ts.map +1 -1
  62. package/dist/render.js +46 -31
  63. package/dist/render.js.map +1 -1
  64. package/dist/role-helpers.d.ts.map +1 -1
  65. package/dist/role-helpers.js.map +1 -1
  66. package/dist/role-naming.d.ts.map +1 -1
  67. package/dist/screen.d.ts.map +1 -1
  68. package/dist/screenshot.d.ts.map +1 -1
  69. package/dist/screenshot.js.map +1 -1
  70. package/dist/suggestions.d.ts +1 -1
  71. package/dist/suggestions.d.ts.map +1 -1
  72. package/dist/suggestions.js +1 -1
  73. package/dist/suggestions.js.map +1 -1
  74. package/dist/timers.d.ts.map +1 -1
  75. package/dist/traversal.d.ts.map +1 -1
  76. package/dist/types.d.ts +10 -2
  77. package/dist/types.d.ts.map +1 -1
  78. package/dist/types.js.map +1 -1
  79. package/dist/usage-error.d.ts +9 -0
  80. package/dist/usage-error.d.ts.map +1 -0
  81. package/dist/usage-error.js +13 -0
  82. package/dist/usage-error.js.map +1 -0
  83. package/dist/user-event/adjustment.d.ts.map +1 -1
  84. package/dist/user-event/adjustment.js +6 -5
  85. package/dist/user-event/adjustment.js.map +1 -1
  86. package/dist/user-event/click.d.ts +15 -7
  87. package/dist/user-event/click.d.ts.map +1 -1
  88. package/dist/user-event/click.js +165 -44
  89. package/dist/user-event/click.js.map +1 -1
  90. package/dist/user-event/column-header-click.d.ts.map +1 -1
  91. package/dist/user-event/column-header-click.js +1 -1
  92. package/dist/user-event/column-header-click.js.map +1 -1
  93. package/dist/user-event/controller.d.ts +11 -3
  94. package/dist/user-event/controller.d.ts.map +1 -1
  95. package/dist/user-event/controller.js +26 -4
  96. package/dist/user-event/controller.js.map +1 -1
  97. package/dist/user-event/dispatch.d.ts +1 -1
  98. package/dist/user-event/dispatch.d.ts.map +1 -1
  99. package/dist/user-event/dispatch.js +9 -6
  100. package/dist/user-event/dispatch.js.map +1 -1
  101. package/dist/user-event/editable-label-click.d.ts +5 -0
  102. package/dist/user-event/editable-label-click.d.ts.map +1 -0
  103. package/dist/user-event/editable-label-click.js +12 -0
  104. package/dist/user-event/editable-label-click.js.map +1 -0
  105. package/dist/user-event/event-wrapper.d.ts +11 -1
  106. package/dist/user-event/event-wrapper.d.ts.map +1 -1
  107. package/dist/user-event/event-wrapper.js +66 -12
  108. package/dist/user-event/event-wrapper.js.map +1 -1
  109. package/dist/user-event/gesture.d.ts.map +1 -1
  110. package/dist/user-event/gesture.js +70 -29
  111. package/dist/user-event/gesture.js.map +1 -1
  112. package/dist/user-event/index.d.ts +55 -18
  113. package/dist/user-event/index.d.ts.map +1 -1
  114. package/dist/user-event/index.js +43 -9
  115. package/dist/user-event/index.js.map +1 -1
  116. package/dist/user-event/indexed-children.d.ts.map +1 -1
  117. package/dist/user-event/keyboard.d.ts +27 -2
  118. package/dist/user-event/keyboard.d.ts.map +1 -1
  119. package/dist/user-event/keyboard.js +291 -44
  120. package/dist/user-event/keyboard.js.map +1 -1
  121. package/dist/user-event/list-row-click.d.ts +2 -1
  122. package/dist/user-event/list-row-click.d.ts.map +1 -1
  123. package/dist/user-event/list-row-click.js +10 -6
  124. package/dist/user-event/list-row-click.js.map +1 -1
  125. package/dist/user-event/native-click.d.ts +2 -1
  126. package/dist/user-event/native-click.d.ts.map +1 -1
  127. package/dist/user-event/native-click.js +17 -5
  128. package/dist/user-event/native-click.js.map +1 -1
  129. package/dist/user-event/notebook-tab-click.d.ts.map +1 -1
  130. package/dist/user-event/notebook-tab-click.js +1 -1
  131. package/dist/user-event/notebook-tab-click.js.map +1 -1
  132. package/dist/user-event/pointer.d.ts +5 -3
  133. package/dist/user-event/pointer.d.ts.map +1 -1
  134. package/dist/user-event/pointer.js +72 -27
  135. package/dist/user-event/pointer.js.map +1 -1
  136. package/dist/user-event/selection.d.ts.map +1 -1
  137. package/dist/user-event/selection.js +46 -15
  138. package/dist/user-event/selection.js.map +1 -1
  139. package/dist/user-event/state.d.ts +2 -1
  140. package/dist/user-event/state.d.ts.map +1 -1
  141. package/dist/user-event/state.js +1 -1
  142. package/dist/user-event/state.js.map +1 -1
  143. package/dist/user-event/tab-view-click.d.ts +8 -0
  144. package/dist/user-event/tab-view-click.d.ts.map +1 -0
  145. package/dist/user-event/tab-view-click.js +37 -0
  146. package/dist/user-event/tab-view-click.js.map +1 -0
  147. package/dist/user-event/text.d.ts +3 -2
  148. package/dist/user-event/text.d.ts.map +1 -1
  149. package/dist/user-event/text.js +34 -14
  150. package/dist/user-event/text.js.map +1 -1
  151. package/dist/wait-for.d.ts.map +1 -1
  152. package/dist/wait-for.js.map +1 -1
  153. package/dist/widget-accessible-properties.d.ts +13 -3
  154. package/dist/widget-accessible-properties.d.ts.map +1 -1
  155. package/dist/widget-accessible-properties.js +59 -21
  156. package/dist/widget-accessible-properties.js.map +1 -1
  157. package/dist/widget-getters.d.ts.map +1 -1
  158. package/dist/widget-getters.js +0 -1
  159. package/dist/widget-getters.js.map +1 -1
  160. package/dist/widget-target.d.ts.map +1 -1
  161. package/dist/widget-target.js +2 -1
  162. package/dist/widget-target.js.map +1 -1
  163. package/dist/window-state.d.ts.map +1 -1
  164. package/dist/window-state.js +3 -1
  165. package/dist/window-state.js.map +1 -1
  166. package/dist/within.d.ts.map +1 -1
  167. package/dist/within.js.map +1 -1
  168. package/package.json +17 -19
  169. package/src/accessible-native.ts +2 -2
  170. package/src/act.ts +70 -6
  171. package/src/bound-queries.ts +1 -1
  172. package/src/build-queries.ts +52 -70
  173. package/src/cleanup-registry.ts +5 -1
  174. package/src/errors.ts +6 -6
  175. package/src/fire-event.ts +23 -9
  176. package/src/frame-sync.ts +1 -13
  177. package/src/harness-window.ts +37 -6
  178. package/src/index.ts +2 -12
  179. package/src/internal.ts +5 -0
  180. package/src/matchers.ts +70 -42
  181. package/src/normalize.ts +1 -4
  182. package/src/pretty-widget.ts +8 -5
  183. package/src/queries.ts +42 -21
  184. package/src/register-test-hooks.ts +11 -0
  185. package/src/render-hook.tsx +1 -1
  186. package/src/render.tsx +57 -36
  187. package/src/role-helpers.ts +1 -1
  188. package/src/screenshot.ts +3 -8
  189. package/src/suggestions.ts +1 -1
  190. package/src/types.ts +22 -17
  191. package/src/usage-error.ts +16 -0
  192. package/src/user-event/adjustment.ts +10 -7
  193. package/src/user-event/click.ts +221 -48
  194. package/src/user-event/column-header-click.ts +1 -1
  195. package/src/user-event/controller.ts +49 -4
  196. package/src/user-event/dispatch.ts +17 -7
  197. package/src/user-event/editable-label-click.ts +17 -0
  198. package/src/user-event/event-wrapper.ts +105 -14
  199. package/src/user-event/gesture.ts +105 -35
  200. package/src/user-event/index.ts +113 -57
  201. package/src/user-event/keyboard.ts +416 -52
  202. package/src/user-event/list-row-click.ts +12 -6
  203. package/src/user-event/native-click.ts +23 -6
  204. package/src/user-event/notebook-tab-click.ts +1 -1
  205. package/src/user-event/pointer.ts +101 -35
  206. package/src/user-event/selection.ts +72 -25
  207. package/src/user-event/state.ts +4 -2
  208. package/src/user-event/tab-view-click.ts +59 -0
  209. package/src/user-event/text.ts +42 -18
  210. package/src/wait-for.ts +1 -4
  211. package/src/widget-accessible-properties.ts +85 -26
  212. package/src/widget-getters.ts +1 -3
  213. package/src/widget-target.ts +2 -1
  214. package/src/window-state.ts +4 -3
  215. package/src/within.ts +2 -2
@@ -1,12 +1,17 @@
1
1
  import * as Gdk from "@gtkx/gi/gdk";
2
2
  import * as Gio from "@gtkx/gi/gio";
3
3
  import * as Gtk from "@gtkx/gi/gtk";
4
+ import { getInstanceType, TYPE_INVALID, typeFromName, typeIsA } from "@gtkx/runtime";
4
5
  import { hasSignalListener } from "@gtkx/runtime/internal";
5
6
  import type { UserEventState } from "./state.js";
6
7
  import { getEditableDelegate } from "../editable.js";
7
8
  import { fireEvent } from "../fire-event.js";
8
- import { ancestors, descendants } from "../traversal.js";
9
+ import { ancestors, children, descendants } from "../traversal.js";
10
+ import { receivesEventsFor } from "./controller.js";
9
11
  import { wrapEvent } from "./event-wrapper.js";
12
+ import { usageError } from "../usage-error.js";
13
+ import { getWidgetTypeName } from "../widget-getters.js";
14
+ import { activeToplevel, mappedToplevels } from "../window-state.js";
10
15
 
11
16
  /** Options for `userEvent.tab`. */
12
17
  type TabOptions = {
@@ -16,9 +21,16 @@ type TabOptions = {
16
21
 
17
22
  type KeyAction = { keyval: number; isPress: boolean };
18
23
  type ParseStep = { actions: KeyAction[]; next: number };
24
+ type TypedStep = { text: string } | { actions: KeyAction[] };
25
+ type TypedParseStep = { step: TypedStep; next: number };
19
26
  type KeyStopController = Gtk.EventControllerKey | Gtk.ShortcutController;
20
- type KeyStop = { widget: Gtk.Widget; controller: KeyStopController };
27
+ type ControllerStop = { widget: Gtk.Widget; controller: KeyStopController };
28
+ type CompletionStop = { widget: Gtk.Widget; completionList: Gtk.Widget };
29
+ type KeyStop = ControllerStop | CompletionStop;
21
30
  type KeyTarget = { root: Gtk.Root | null; focus: Gtk.Widget | null };
31
+ type MatchingShortcut = { shortcut: Gtk.Shortcut; host: Gtk.Widget; index: number };
32
+
33
+ const lastActivatedShortcuts: WeakMap<Gtk.ShortcutController, number> = new WeakMap();
22
34
 
23
35
  const KEY_MAP: Record<string, number> = {
24
36
  Enter: Gdk.KEY_Return,
@@ -54,6 +66,7 @@ const KEY_MAP: Record<string, number> = {
54
66
  };
55
67
 
56
68
  const KEY_CONTROLLER_SIGNALS = ["key-pressed", "key-released"];
69
+ const LITERAL_BRACE = "{{";
57
70
 
58
71
  const MODIFIER_KEYVAL_TO_MASK: Record<number, number> = {
59
72
  [Gdk.KEY_Shift_L]: Gdk.ModifierType.SHIFT_MASK,
@@ -66,6 +79,17 @@ const MODIFIER_KEYVAL_TO_MASK: Record<number, number> = {
66
79
  [Gdk.KEY_Meta_R]: Gdk.ModifierType.META_MASK,
67
80
  };
68
81
 
82
+ const MODIFIER_NAMES: [string, number][] = [
83
+ ["Control", Gdk.ModifierType.CONTROL_MASK],
84
+ ["Shift", Gdk.ModifierType.SHIFT_MASK],
85
+ ["Alt", Gdk.ModifierType.ALT_MASK],
86
+ ["Meta", Gdk.ModifierType.META_MASK],
87
+ ];
88
+
89
+ /** The modifier keys pressed and not yet released, named as `keyboard` input names them. */
90
+ const heldModifiers = (state: UserEventState): string[] =>
91
+ MODIFIER_NAMES.filter(([, mask]) => (state.modifierState & mask) !== 0).map(([name]) => name);
92
+
69
93
  /** Moves focus within the widget's root. */
70
94
  const tab = (widget: Gtk.Widget, options?: TabOptions): Promise<void> =>
71
95
  wrapEvent(widget, () => {
@@ -77,6 +101,12 @@ const tab = (widget: Gtk.Widget, options?: TabOptions): Promise<void> =>
77
101
  }
78
102
  });
79
103
 
104
+ const characterKeyval = (keyName: string): number | undefined => {
105
+ const [character, ...rest] = Array.from(keyName);
106
+
107
+ return character === undefined || rest.length > 0 ? undefined : Gdk.unicodeToKeyval(character.codePointAt(0) ?? 0);
108
+ };
109
+
80
110
  const parseKeyToken = (token: string): { keyval: number; isPress: boolean; isRelease: boolean } => {
81
111
  let keyName = token;
82
112
  let isPress = true;
@@ -90,10 +120,10 @@ const parseKeyToken = (token: string): { keyval: number; isPress: boolean; isRel
90
120
  isRelease = false;
91
121
  }
92
122
 
93
- const keyval = KEY_MAP[keyName];
123
+ const keyval = KEY_MAP[keyName] ?? characterKeyval(keyName);
94
124
 
95
125
  if (keyval === undefined) {
96
- throw new Error(`Unknown key: {${keyName}}`);
126
+ throw usageError(new Error(`Unknown key: {${keyName}}`));
97
127
  }
98
128
 
99
129
  return { keyval, isPress, isRelease };
@@ -154,6 +184,46 @@ const parseKeyboardInput = (input: string): KeyAction[] => {
154
184
  return actions;
155
185
  };
156
186
 
187
+ const characterActions = (text: string): KeyAction[] =>
188
+ Array.from(text, (character) => parseCharAt(character, 0).actions).flat();
189
+
190
+ const parseTypedCharAt = (input: string, i: number): TypedParseStep => {
191
+ const text = String.fromCodePoint(input.codePointAt(i) ?? 0);
192
+
193
+ return { step: { text }, next: i + text.length };
194
+ };
195
+
196
+ const parseTypedStepAt = (input: string, i: number): TypedParseStep => {
197
+ if (input.startsWith(LITERAL_BRACE, i)) {
198
+ return { step: { text: "{" }, next: i + LITERAL_BRACE.length };
199
+ }
200
+
201
+ if (input[i] !== "{") {
202
+ return parseTypedCharAt(input, i);
203
+ }
204
+
205
+ const brace = parseBraceAt(input, i);
206
+
207
+ if (brace === null) {
208
+ throw usageError(new Error(`Unclosed key descriptor at ${String(i)} in "${input}"; type a literal { as {{`));
209
+ }
210
+
211
+ return { step: { actions: brace.actions }, next: brace.next };
212
+ };
213
+
214
+ const parseTypedInput = (input: string): TypedStep[] => {
215
+ const steps: TypedStep[] = [];
216
+ let i = 0;
217
+
218
+ while (i < input.length) {
219
+ const { step, next } = parseTypedStepAt(input, i);
220
+ steps.push(step);
221
+ i = next;
222
+ }
223
+
224
+ return steps;
225
+ };
226
+
157
227
  const updateModifierState = (state: UserEventState, action: KeyAction): void => {
158
228
  const mask = MODIFIER_KEYVAL_TO_MASK[action.keyval];
159
229
 
@@ -172,15 +242,22 @@ const isTriggerMatch = (
172
242
  trigger: Gtk.ShortcutTrigger | null,
173
243
  keyval: number,
174
244
  modifiers: Gdk.ModifierType,
245
+ areMnemonicsEnabled: boolean,
175
246
  ): boolean => {
176
247
  if (trigger instanceof Gtk.KeyvalTrigger) {
177
248
  return trigger.getKeyval() === keyval && trigger.getModifiers() === modifiers;
178
249
  }
179
250
 
251
+ if (trigger instanceof Gtk.MnemonicTrigger) {
252
+ const mnemonicKeyval = keyval === Gdk.KEY_ISO_Left_Tab ? Gdk.KEY_Tab : Gdk.keyvalToLower(keyval);
253
+
254
+ return areMnemonicsEnabled && trigger.getKeyval() === mnemonicKeyval;
255
+ }
256
+
180
257
  if (trigger instanceof Gtk.AlternativeTrigger) {
181
258
  return (
182
- isTriggerMatch(trigger.getFirst(), keyval, modifiers) ||
183
- isTriggerMatch(trigger.getSecond(), keyval, modifiers)
259
+ isTriggerMatch(trigger.getFirst(), keyval, modifiers, areMnemonicsEnabled) ||
260
+ isTriggerMatch(trigger.getSecond(), keyval, modifiers, areMnemonicsEnabled)
184
261
  );
185
262
  }
186
263
 
@@ -227,32 +304,62 @@ const managedShortcutHost = (manager: Gtk.Widget, shortcut: Gtk.Shortcut): Gtk.W
227
304
  return null;
228
305
  };
229
306
 
230
- const isMovingFocus = (action: Gtk.ShortcutAction): boolean =>
231
- action instanceof Gtk.SignalAction && action.getSignalName() === "move-focus";
232
-
233
307
  const isShortcutLive = (host: Gtk.Widget): boolean => host.isSensitive() && host.getMapped();
234
308
 
235
- const didRunShortcutAction = (shortcut: Gtk.Shortcut, host: Gtk.Widget): boolean => {
309
+ const didRunShortcutAction = (shortcut: Gtk.Shortcut, host: Gtk.Widget, flags: Gtk.ShortcutActionFlags): boolean => {
236
310
  const action = shortcut.getAction();
237
311
 
238
- if (action === null || isMovingFocus(action) || !isShortcutLive(host)) {
312
+ if (action === null || !isShortcutLive(host)) {
239
313
  return false;
240
314
  }
241
315
 
242
- return action.activate(0 as Gtk.ShortcutActionFlags, host, shortcut.getArguments());
316
+ return action.activate(flags, host, shortcut.getArguments());
317
+ };
318
+
319
+ const mnemonicModifiersFor = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): Gdk.ModifierType => {
320
+ const display = widget.getDisplay();
321
+ const [, keys] = display.mapKeyval(keyval);
322
+ const key = keys[0];
323
+ const consumed = key === undefined ? 0 : display.translateKey(key.keycode, modifiers, key.group)[4];
324
+
325
+ return modifiers & ~consumed & Gtk.acceleratorGetDefaultModMask();
243
326
  };
244
327
 
245
- const didActivateShortcut = (
246
- shortcut: Gtk.Shortcut,
328
+ const matchingShortcuts = (
329
+ controller: Gtk.ShortcutController,
247
330
  widget: Gtk.Widget,
248
331
  keyval: number,
249
332
  modifiers: Gdk.ModifierType,
250
- ): boolean => {
251
- if (!isTriggerMatch(shortcut.getTrigger(), keyval, modifiers)) {
252
- return false;
333
+ ): MatchingShortcut[] => {
334
+ const count = controller.getNItems();
335
+
336
+ if (count === 0) {
337
+ return [];
253
338
  }
254
339
 
255
- return didRunShortcutAction(shortcut, managedShortcutHost(widget, shortcut) ?? widget);
340
+ const areMnemonicsEnabled = mnemonicModifiersFor(widget, keyval, modifiers) === controller.getMnemonicsModifiers();
341
+ const offset = areMnemonicsEnabled ? (lastActivatedShortcuts.get(controller) ?? 0) + 1 : 0;
342
+ const matches: MatchingShortcut[] = [];
343
+
344
+ for (let j = 0; j < count; j++) {
345
+ const index = (offset + j) % count;
346
+ const shortcut = controller.getItem(index);
347
+
348
+ if (
349
+ !(shortcut instanceof Gtk.Shortcut) ||
350
+ !isTriggerMatch(shortcut.getTrigger(), keyval, modifiers, areMnemonicsEnabled)
351
+ ) {
352
+ continue;
353
+ }
354
+
355
+ const host = managedShortcutHost(widget, shortcut) ?? widget;
356
+
357
+ if (isShortcutLive(host)) {
358
+ matches.push({ shortcut, host, index });
359
+ }
360
+ }
361
+
362
+ return matches;
256
363
  };
257
364
 
258
365
  const didActivateMatchingShortcut = (
@@ -261,16 +368,14 @@ const didActivateMatchingShortcut = (
261
368
  keyval: number,
262
369
  modifiers: number,
263
370
  ): boolean => {
264
- const count = controller.getNItems();
371
+ const matches = matchingShortcuts(controller, widget, keyval, modifiers);
265
372
 
266
- for (let j = 0; j < count; j++) {
267
- const shortcut = controller.getItem(j);
373
+ for (const [index, match] of matches.entries()) {
374
+ const flags = (index === matches.length - 1 ? Gtk.ShortcutActionFlags.EXCLUSIVE : 0) as Gtk.ShortcutActionFlags;
268
375
 
269
- if (!(shortcut instanceof Gtk.Shortcut)) {
270
- continue;
271
- }
376
+ if (didRunShortcutAction(match.shortcut, match.host, flags)) {
377
+ lastActivatedShortcuts.set(controller, match.index);
272
378
 
273
- if (didActivateShortcut(shortcut, widget, keyval, modifiers)) {
274
379
  return true;
275
380
  }
276
381
  }
@@ -314,33 +419,169 @@ const didDispatchAccelsOnWindow = (window: Gtk.Window, keyval: number, modifiers
314
419
  return false;
315
420
  };
316
421
 
317
- const didDispatchApplicationAccels = (widget: Gtk.Widget, keyval: number, modifiers: number): boolean => {
318
- const root = widget.getRoot();
422
+ const didDispatchApplicationAccels = (target: Gtk.Widget, keyval: number, modifiers: number): boolean => {
423
+ const root = target.getRoot();
424
+
425
+ return (
426
+ root instanceof Gtk.Window &&
427
+ receivesEventsFor(root, Gtk.PropagationLimit.SAME_NATIVE, target) &&
428
+ didDispatchAccelsOnWindow(root, keyval, modifiers)
429
+ );
430
+ };
319
431
 
320
- return root instanceof Gtk.Window && didDispatchAccelsOnWindow(root, keyval, modifiers);
432
+ const SEARCH_BAR_CAPTURE_NAME = "gtk-search-bar-capture";
433
+ const ENTRY_COMPLETION_CLASS = "entry-completion";
434
+ const FILE_CHOOSER_ENTRY_TYPE_NAME = "GtkFileChooserEntry";
435
+ const CELL_EDITABLE_TYPE_NAME = "GtkCellEditableWidget";
436
+
437
+ /**
438
+ * GTK has no public getter for a text view's own key controller (`gtk_text_view_get_key_controller` is private) and
439
+ * lists controllers newest first, so the last key controller is the one the text view added when it was created.
440
+ * Controllers that subclasses such as GtkSourceView add read the current event, which emulated keys lack.
441
+ */
442
+ const isTextViewKeyController = (controller: Gtk.EventController, widget: Gtk.Widget): boolean =>
443
+ controllersOn(widget)
444
+ .filter((current) => current instanceof Gtk.EventControllerKey)
445
+ .toArray()
446
+ .at(-1) === controller;
447
+
448
+ const isTextViewLibraryController = (controller: Gtk.EventController, widget: Gtk.Widget): boolean =>
449
+ widget instanceof Gtk.TextView && !isTextViewKeyController(controller, widget);
450
+
451
+ const capturesKeysOf = (candidate: Gtk.Widget, widget: Gtk.Widget): boolean =>
452
+ (candidate instanceof Gtk.SearchEntry || candidate instanceof Gtk.SearchBar) &&
453
+ candidate.getKeyCaptureWidget() === widget;
454
+
455
+ /**
456
+ * A search entry or search bar with a key capture widget adds a key controller to that widget, which forwards the
457
+ * current event to the entry. The search bar names its controller; the search entry leaves its own unnamed.
458
+ */
459
+ const isSearchKeyCapture = (controller: Gtk.EventController, widget: Gtk.Widget): boolean => {
460
+ const name = controller.getName();
461
+
462
+ return (
463
+ name === SEARCH_BAR_CAPTURE_NAME ||
464
+ (name === null &&
465
+ (capturesKeysOf(widget, widget) ||
466
+ descendants(widget).some((candidate) => capturesKeysOf(candidate, widget))))
467
+ );
321
468
  };
322
469
 
323
- const isKeyStopController = (controller: Gtk.EventController): controller is KeyStopController => {
470
+ const isFileChooserForwarder = (_controller: Gtk.EventController, widget: Gtk.Widget): boolean =>
471
+ widget instanceof Gtk.FileChooserWidget || getWidgetTypeName(widget) === FILE_CHOOSER_ENTRY_TYPE_NAME;
472
+
473
+ /**
474
+ * Widget types from other libraries whose key controllers read the current event: WebKit filters it through its input
475
+ * method and passes it to the web process.
476
+ */
477
+ const EVENT_READING_LIBRARY_TYPE_NAMES = ["WebKitWebViewBase"];
478
+
479
+ const isLibraryEventReader = (_controller: Gtk.EventController, widget: Gtk.Widget): boolean =>
480
+ EVENT_READING_LIBRARY_TYPE_NAMES.some((name) => {
481
+ const type = typeFromName(name);
482
+
483
+ return type !== TYPE_INVALID && typeIsA(getInstanceType(widget), type);
484
+ });
485
+
486
+ const isDeprecatedPopup = (widget: Gtk.Widget): boolean =>
487
+ widget instanceof Gtk.Popover &&
488
+ (widget.getParent() instanceof Gtk.ComboBox || widget.hasCssClass(ENTRY_COMPLETION_CLASS));
489
+
490
+ const isDeprecatedWidgetForwarder = (_controller: Gtk.EventController, widget: Gtk.Widget): boolean =>
491
+ widget instanceof Gtk.TreeView ||
492
+ getWidgetTypeName(widget) === CELL_EDITABLE_TYPE_NAME ||
493
+ isDeprecatedPopup(widget);
494
+
495
+ /**
496
+ * Key controllers GTK and its libraries add without a JavaScript listener whose handlers need the real key event,
497
+ * which emulated keys lack: they filter it through an input method, forward it with
498
+ * `gtk_event_controller_key_forward()` or read it directly, and log a critical without one. Emulated keys skip them;
499
+ * every other key controller receives the keys.
500
+ */
501
+ const REAL_EVENT_KEY_HANDLERS: ((controller: Gtk.EventController, widget: Gtk.Widget) => boolean)[] = [
502
+ isTextViewLibraryController,
503
+ isSearchKeyCapture,
504
+ isFileChooserForwarder,
505
+ isDeprecatedWidgetForwarder,
506
+ isLibraryEventReader,
507
+ ];
508
+
509
+ const needsRealKeyEvent = (controller: Gtk.EventController, widget: Gtk.Widget): boolean =>
510
+ REAL_EVENT_KEY_HANDLERS.some((needsEvent) => needsEvent(controller, widget));
511
+
512
+ const SOURCE_COMPLETION_CONTROLLER_NAME = "gtk-source-completion";
513
+ const SOURCE_COMPLETION_LIST_TYPE_NAME = "GtkSourceCompletionList";
514
+ const SOURCE_COMPLETION_LIST_BOX_TYPE_NAME = "GtkSourceCompletionListBox";
515
+
516
+ /**
517
+ * GtkSourceView's completion adds a key controller to the view. While the completion list shows, the controller hides
518
+ * the list on {Escape} and forwards every other key to the list's box with `gtk_event_controller_key_forward()`, where
519
+ * the box's bindings move the selection and choose the selected proposal. Emulated keys have no event to forward, so
520
+ * they take the same path through the box's own controllers.
521
+ */
522
+ const completionListFor = (controller: Gtk.EventController, widget: Gtk.Widget): Gtk.Widget | null =>
523
+ controller.getName() === SOURCE_COMPLETION_CONTROLLER_NAME
524
+ ? (children(widget).find((child) => getWidgetTypeName(child) === SOURCE_COMPLETION_LIST_TYPE_NAME) ?? null)
525
+ : null;
526
+
527
+ const isKeyStopController = (controller: Gtk.EventController, widget: Gtk.Widget): controller is KeyStopController => {
324
528
  if (controller instanceof Gtk.ShortcutController) {
325
529
  return controller.getScope() === Gtk.ShortcutScope.LOCAL;
326
530
  }
327
531
 
328
- return controller instanceof Gtk.EventControllerKey && hasSignalListener(controller, KEY_CONTROLLER_SIGNALS);
532
+ return (
533
+ controller instanceof Gtk.EventControllerKey &&
534
+ (hasSignalListener(controller, KEY_CONTROLLER_SIGNALS) || !needsRealKeyEvent(controller, widget))
535
+ );
536
+ };
537
+
538
+ const keyStopFor = (controller: Gtk.EventController, widget: Gtk.Widget): KeyStop | null => {
539
+ const completionList = completionListFor(controller, widget);
540
+
541
+ if (completionList !== null) {
542
+ return { widget, completionList };
543
+ }
544
+
545
+ return isKeyStopController(controller, widget) ? { widget, controller } : null;
329
546
  };
330
547
 
331
- const keyStopsInPhase = (widget: Gtk.Widget, phase: Gtk.PropagationPhase): KeyStop[] => {
548
+ const keyStopsInPhase = (widget: Gtk.Widget, phase: Gtk.PropagationPhase, target: Gtk.Widget): KeyStop[] => {
332
549
  const stops: KeyStop[] = [];
333
550
 
334
551
  for (const controller of controllersOn(widget)) {
335
- if (controller.getPropagationPhase() === phase && isKeyStopController(controller)) {
336
- stops.push({ widget, controller });
552
+ const stop =
553
+ controller.getPropagationPhase() === phase &&
554
+ receivesEventsFor(widget, controller.getPropagationLimit(), target)
555
+ ? keyStopFor(controller, widget)
556
+ : null;
557
+
558
+ if (stop !== null) {
559
+ stops.push(stop);
337
560
  }
338
561
  }
339
562
 
340
563
  return stops;
341
564
  };
342
565
 
566
+ const forwardedKeyStops = (target: Gtk.Widget): KeyStop[] =>
567
+ [Gtk.PropagationPhase.CAPTURE, Gtk.PropagationPhase.TARGET, Gtk.PropagationPhase.BUBBLE].flatMap((phase) =>
568
+ keyStopsInPhase(target, phase, target),
569
+ );
570
+
571
+ const focusWithin = (widget: Gtk.Widget): Gtk.Widget | null => {
572
+ const root = widget.getRoot();
573
+ const focus = root instanceof Gtk.Window ? root.getFocus() : null;
574
+
575
+ return focus?.getMapped() === true && focus.isAncestor(widget) ? focus : null;
576
+ };
577
+
343
578
  const propagationChain = (widget: Gtk.Widget): Gtk.Widget[] => {
579
+ const focus = focusWithin(widget);
580
+
581
+ if (focus !== null) {
582
+ return [focus, ...ancestors(focus)];
583
+ }
584
+
344
585
  const delegate = getEditableDelegate(widget);
345
586
  const below = delegate === null ? [] : [delegate];
346
587
 
@@ -352,13 +593,35 @@ const keyPropagationChain = (widget: Gtk.Widget): KeyStop[] => {
352
593
  const [eventWidget = widget] = chain;
353
594
 
354
595
  return [
355
- ...chain.toReversed().flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.CAPTURE)),
356
- ...keyStopsInPhase(eventWidget, Gtk.PropagationPhase.TARGET),
357
- ...chain.flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.BUBBLE)),
596
+ ...chain.toReversed().flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.CAPTURE, eventWidget)),
597
+ ...keyStopsInPhase(eventWidget, Gtk.PropagationPhase.TARGET, eventWidget),
598
+ ...chain.flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.BUBBLE, eventWidget)),
358
599
  ];
359
600
  };
360
601
 
361
- const didStopHandlePress = (stop: KeyStop, keyval: number, modifiers: Gdk.ModifierType): boolean => {
602
+ const didCompletionHandlePress = (completionList: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): boolean => {
603
+ if (!completionList.getVisible()) {
604
+ return false;
605
+ }
606
+
607
+ if (keyval === Gdk.KEY_Escape) {
608
+ completionList.setVisible(false);
609
+
610
+ return false;
611
+ }
612
+
613
+ const box = descendants(completionList).find(
614
+ (descendant) => getWidgetTypeName(descendant) === SOURCE_COMPLETION_LIST_BOX_TYPE_NAME,
615
+ );
616
+
617
+ return box !== undefined && forwardedKeyStops(box).some((stop) => didStopHandlePress(stop, keyval, modifiers));
618
+ };
619
+
620
+ function didStopHandlePress(stop: KeyStop, keyval: number, modifiers: Gdk.ModifierType): boolean {
621
+ if ("completionList" in stop) {
622
+ return didCompletionHandlePress(stop.completionList, keyval, modifiers);
623
+ }
624
+
362
625
  const { controller, widget } = stop;
363
626
 
364
627
  if (controller instanceof Gtk.ShortcutController) {
@@ -366,7 +629,7 @@ const didStopHandlePress = (stop: KeyStop, keyval: number, modifiers: Gdk.Modifi
366
629
  }
367
630
 
368
631
  return controller.emit("key-pressed", keyval, 0, modifiers);
369
- };
632
+ }
370
633
 
371
634
  const keyTargetFor = (widget: Gtk.Widget): KeyTarget => {
372
635
  const root = widget.getRoot();
@@ -382,8 +645,9 @@ const isKeyTargetInvalidated = (widget: Gtk.Widget, target: KeyTarget): boolean
382
645
 
383
646
  const didHandlePress = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): boolean => {
384
647
  const target = keyTargetFor(widget);
648
+ const [eventWidget = widget] = propagationChain(widget);
385
649
 
386
- if (didDispatchApplicationAccels(widget, keyval, modifiers)) {
650
+ if (didDispatchApplicationAccels(eventWidget, keyval, modifiers)) {
387
651
  return true;
388
652
  }
389
653
 
@@ -402,39 +666,139 @@ const didHandlePress = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.Modif
402
666
 
403
667
  const emitKeyReleased = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): void => {
404
668
  for (const stop of keyPropagationChain(widget)) {
405
- if (stop.controller instanceof Gtk.EventControllerKey) {
669
+ if ("controller" in stop && stop.controller instanceof Gtk.EventControllerKey) {
406
670
  stop.controller.emit("key-released", keyval, 0, modifiers);
407
671
  }
408
672
  }
409
673
  };
410
674
 
411
- const applyKeyPress = async (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): Promise<void> => {
675
+ const applyKeyPress = async (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): Promise<boolean> => {
412
676
  if (didHandlePress(widget, keyval, modifiers)) {
413
- return;
677
+ return true;
414
678
  }
415
679
 
416
680
  if (keyval === Gdk.KEY_Return && widget instanceof Gtk.Editable) {
417
681
  await fireEvent(widget, "activate");
682
+
683
+ return true;
418
684
  }
685
+
686
+ return false;
419
687
  };
420
688
 
421
- const applyKeyAction = async (widget: Gtk.Widget, state: UserEventState, action: KeyAction): Promise<void> => {
689
+ const isUnhandledPress = async (widget: Gtk.Widget, state: UserEventState, action: KeyAction): Promise<boolean> => {
422
690
  updateModifierState(state, action);
423
691
 
424
- if (action.isPress) {
425
- await applyKeyPress(widget, action.keyval, state.modifierState);
692
+ if (!action.isPress) {
693
+ emitKeyReleased(widget, action.keyval, state.modifierState);
426
694
 
427
- return;
695
+ return false;
428
696
  }
429
697
 
430
- emitKeyReleased(widget, action.keyval, state.modifierState);
698
+ const isHandled = await applyKeyPress(widget, action.keyval, state.modifierState);
699
+
700
+ return !isHandled && MODIFIER_KEYVAL_TO_MASK[action.keyval] === undefined;
431
701
  };
432
702
 
433
- const keyboard = (state: UserEventState, widget: Gtk.Widget, input: string): Promise<void> =>
434
- wrapEvent(widget, async () => {
435
- for (const action of parseKeyboardInput(input)) {
436
- await applyKeyAction(widget, state, action);
703
+ /**
704
+ * Returns a function that resolves the widget keys meant for `widget` reach now: the widget, or the widget outside it
705
+ * that focus has moved to since, as after a {Tab}.
706
+ */
707
+ const followKeyFocus = (widget: Gtk.Widget): (() => Gtk.Widget) => {
708
+ const start = keyTargetFor(widget);
709
+
710
+ return () => {
711
+ const { root, focus } = keyTargetFor(widget);
712
+
713
+ return root === start.root && focus !== null && focus !== start.focus && !focus.isAncestor(widget)
714
+ ? focus
715
+ : widget;
716
+ };
717
+ };
718
+
719
+ /** Applies the key actions and returns the keyvals of the presses, other than modifiers, that nothing handled. */
720
+ const applyKeyActions = async (widget: Gtk.Widget, state: UserEventState, actions: KeyAction[]): Promise<number[]> => {
721
+ const keyWidget = followKeyFocus(widget);
722
+ const unhandled: number[] = [];
723
+
724
+ for (const action of actions) {
725
+ if (await isUnhandledPress(keyWidget(), state, action)) {
726
+ unhandled.push(action.keyval);
437
727
  }
728
+ }
729
+
730
+ return unhandled;
731
+ };
732
+
733
+ const keyLabel = (keyval: number): string => {
734
+ const name = Object.keys(KEY_MAP).find((key) => KEY_MAP[key] === keyval);
735
+ const character = Gdk.keyvalToUnicode(keyval);
736
+
737
+ if (name !== undefined) {
738
+ return `{${name}}`;
739
+ }
740
+
741
+ return character === 0 ? `{${Gdk.keyvalName(keyval) ?? String(keyval)}}` : String.fromCodePoint(character);
742
+ };
743
+
744
+ const canTakeFocus = (widget: Gtk.Widget): boolean => widget.getFocusable() && widget.getCanFocus();
745
+
746
+ /**
747
+ * Focuses a target whose window has its focus elsewhere, as a user does before typing, so the window's own bindings
748
+ * such as {Space} and {Enter} activating the focus widget act on the target. An entry that delegates its focus to its
749
+ * text, such as a search entry, takes it there without selecting the text, as a click does. A widget that passes its
750
+ * focus to a child, such as a menu button to its button, focuses that child.
751
+ */
752
+ const focusKeyTarget = (widget: Gtk.Widget): void => {
753
+ if (focusWithin(widget) !== null) {
754
+ return;
755
+ }
756
+
757
+ const delegate = getEditableDelegate(widget);
758
+
759
+ if (delegate instanceof Gtk.Text && canTakeFocus(delegate)) {
760
+ delegate.grabFocusWithoutSelecting();
761
+ } else {
762
+ widget.grabFocus();
763
+ }
764
+ };
765
+
766
+ /**
767
+ * Sends the key sequence and returns the keys, as `keyboard` input would name them, whose presses nothing on the
768
+ * widget, its focused descendant or their ancestors handled.
769
+ */
770
+ const keyboard = async (state: UserEventState, widget: Gtk.Widget, input: string): Promise<string[]> => {
771
+ let unhandled: number[] = [];
772
+
773
+ await wrapEvent(widget, async () => {
774
+ focusKeyTarget(widget);
775
+ unhandled = await applyKeyActions(widget, state, parseKeyboardInput(input));
438
776
  });
439
777
 
440
- export { tab, keyboard, type TabOptions };
778
+ return [...new Set(unhandled.map((keyval) => keyLabel(keyval)))];
779
+ };
780
+
781
+ const focusedKeyWidget = (): Gtk.Widget => {
782
+ const window = activeToplevel() ?? mappedToplevels()[0];
783
+
784
+ if (window === undefined) {
785
+ throw usageError(new Error("No window is open to receive the keys"));
786
+ }
787
+
788
+ const focus = window.getFocus();
789
+
790
+ return focus?.getMapped() ? focus : window;
791
+ };
792
+
793
+ export {
794
+ applyKeyActions,
795
+ characterActions,
796
+ focusedKeyWidget,
797
+ followKeyFocus,
798
+ heldModifiers,
799
+ keyboard,
800
+ parseTypedInput,
801
+ tab,
802
+ type TabOptions,
803
+ type TypedStep,
804
+ };