@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,11 +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 { getEditableDelegate } from "../editable.js";
6
7
  import { fireEvent } from "../fire-event.js";
7
- import { ancestors, descendants } from "../traversal.js";
8
+ import { ancestors, children, descendants } from "../traversal.js";
9
+ import { receivesEventsFor } from "./controller.js";
8
10
  import { wrapEvent } from "./event-wrapper.js";
11
+ import { usageError } from "../usage-error.js";
12
+ import { getWidgetTypeName } from "../widget-getters.js";
13
+ import { activeToplevel, mappedToplevels } from "../window-state.js";
14
+ const lastActivatedShortcuts = new WeakMap();
9
15
  const KEY_MAP = {
10
16
  Enter: Gdk.KEY_Return,
11
17
  Tab: Gdk.KEY_Tab,
@@ -39,6 +45,7 @@ const KEY_MAP = {
39
45
  F12: Gdk.KEY_F12,
40
46
  };
41
47
  const KEY_CONTROLLER_SIGNALS = ["key-pressed", "key-released"];
48
+ const LITERAL_BRACE = "{{";
42
49
  const MODIFIER_KEYVAL_TO_MASK = {
43
50
  [Gdk.KEY_Shift_L]: Gdk.ModifierType.SHIFT_MASK,
44
51
  [Gdk.KEY_Shift_R]: Gdk.ModifierType.SHIFT_MASK,
@@ -49,6 +56,14 @@ const MODIFIER_KEYVAL_TO_MASK = {
49
56
  [Gdk.KEY_Meta_L]: Gdk.ModifierType.META_MASK,
50
57
  [Gdk.KEY_Meta_R]: Gdk.ModifierType.META_MASK,
51
58
  };
59
+ const MODIFIER_NAMES = [
60
+ ["Control", Gdk.ModifierType.CONTROL_MASK],
61
+ ["Shift", Gdk.ModifierType.SHIFT_MASK],
62
+ ["Alt", Gdk.ModifierType.ALT_MASK],
63
+ ["Meta", Gdk.ModifierType.META_MASK],
64
+ ];
65
+ /** The modifier keys pressed and not yet released, named as `keyboard` input names them. */
66
+ const heldModifiers = (state) => MODIFIER_NAMES.filter(([, mask]) => (state.modifierState & mask) !== 0).map(([name]) => name);
52
67
  /** Moves focus within the widget's root. */
53
68
  const tab = (widget, options) => wrapEvent(widget, () => {
54
69
  const direction = options?.isShiftHeld ? Gtk.DirectionType.TAB_BACKWARD : Gtk.DirectionType.TAB_FORWARD;
@@ -57,6 +72,10 @@ const tab = (widget, options) => wrapEvent(widget, () => {
57
72
  root.childFocus(direction);
58
73
  }
59
74
  });
75
+ const characterKeyval = (keyName) => {
76
+ const [character, ...rest] = Array.from(keyName);
77
+ return character === undefined || rest.length > 0 ? undefined : Gdk.unicodeToKeyval(character.codePointAt(0) ?? 0);
78
+ };
60
79
  const parseKeyToken = (token) => {
61
80
  let keyName = token;
62
81
  let isPress = true;
@@ -69,9 +88,9 @@ const parseKeyToken = (token) => {
69
88
  keyName = keyName.slice(0, -1);
70
89
  isRelease = false;
71
90
  }
72
- const keyval = KEY_MAP[keyName];
91
+ const keyval = KEY_MAP[keyName] ?? characterKeyval(keyName);
73
92
  if (keyval === undefined) {
74
- throw new Error(`Unknown key: {${keyName}}`);
93
+ throw usageError(new Error(`Unknown key: {${keyName}}`));
75
94
  }
76
95
  return { keyval, isPress, isRelease };
77
96
  };
@@ -115,6 +134,34 @@ const parseKeyboardInput = (input) => {
115
134
  }
116
135
  return actions;
117
136
  };
137
+ const characterActions = (text) => Array.from(text, (character) => parseCharAt(character, 0).actions).flat();
138
+ const parseTypedCharAt = (input, i) => {
139
+ const text = String.fromCodePoint(input.codePointAt(i) ?? 0);
140
+ return { step: { text }, next: i + text.length };
141
+ };
142
+ const parseTypedStepAt = (input, i) => {
143
+ if (input.startsWith(LITERAL_BRACE, i)) {
144
+ return { step: { text: "{" }, next: i + LITERAL_BRACE.length };
145
+ }
146
+ if (input[i] !== "{") {
147
+ return parseTypedCharAt(input, i);
148
+ }
149
+ const brace = parseBraceAt(input, i);
150
+ if (brace === null) {
151
+ throw usageError(new Error(`Unclosed key descriptor at ${String(i)} in "${input}"; type a literal { as {{`));
152
+ }
153
+ return { step: { actions: brace.actions }, next: brace.next };
154
+ };
155
+ const parseTypedInput = (input) => {
156
+ const steps = [];
157
+ let i = 0;
158
+ while (i < input.length) {
159
+ const { step, next } = parseTypedStepAt(input, i);
160
+ steps.push(step);
161
+ i = next;
162
+ }
163
+ return steps;
164
+ };
118
165
  const updateModifierState = (state, action) => {
119
166
  const mask = MODIFIER_KEYVAL_TO_MASK[action.keyval];
120
167
  if (!mask) {
@@ -127,13 +174,17 @@ const updateModifierState = (state, action) => {
127
174
  state.modifierState &= ~mask;
128
175
  }
129
176
  };
130
- const isTriggerMatch = (trigger, keyval, modifiers) => {
177
+ const isTriggerMatch = (trigger, keyval, modifiers, areMnemonicsEnabled) => {
131
178
  if (trigger instanceof Gtk.KeyvalTrigger) {
132
179
  return trigger.getKeyval() === keyval && trigger.getModifiers() === modifiers;
133
180
  }
181
+ if (trigger instanceof Gtk.MnemonicTrigger) {
182
+ const mnemonicKeyval = keyval === Gdk.KEY_ISO_Left_Tab ? Gdk.KEY_Tab : Gdk.keyvalToLower(keyval);
183
+ return areMnemonicsEnabled && trigger.getKeyval() === mnemonicKeyval;
184
+ }
134
185
  if (trigger instanceof Gtk.AlternativeTrigger) {
135
- return (isTriggerMatch(trigger.getFirst(), keyval, modifiers) ||
136
- isTriggerMatch(trigger.getSecond(), keyval, modifiers));
186
+ return (isTriggerMatch(trigger.getFirst(), keyval, modifiers, areMnemonicsEnabled) ||
187
+ isTriggerMatch(trigger.getSecond(), keyval, modifiers, areMnemonicsEnabled));
137
188
  }
138
189
  return false;
139
190
  };
@@ -167,29 +218,49 @@ const managedShortcutHost = (manager, shortcut) => {
167
218
  }
168
219
  return null;
169
220
  };
170
- const isMovingFocus = (action) => action instanceof Gtk.SignalAction && action.getSignalName() === "move-focus";
171
221
  const isShortcutLive = (host) => host.isSensitive() && host.getMapped();
172
- const didRunShortcutAction = (shortcut, host) => {
222
+ const didRunShortcutAction = (shortcut, host, flags) => {
173
223
  const action = shortcut.getAction();
174
- if (action === null || isMovingFocus(action) || !isShortcutLive(host)) {
224
+ if (action === null || !isShortcutLive(host)) {
175
225
  return false;
176
226
  }
177
- return action.activate(0, host, shortcut.getArguments());
227
+ return action.activate(flags, host, shortcut.getArguments());
178
228
  };
179
- const didActivateShortcut = (shortcut, widget, keyval, modifiers) => {
180
- if (!isTriggerMatch(shortcut.getTrigger(), keyval, modifiers)) {
181
- return false;
182
- }
183
- return didRunShortcutAction(shortcut, managedShortcutHost(widget, shortcut) ?? widget);
229
+ const mnemonicModifiersFor = (widget, keyval, modifiers) => {
230
+ const display = widget.getDisplay();
231
+ const [, keys] = display.mapKeyval(keyval);
232
+ const key = keys[0];
233
+ const consumed = key === undefined ? 0 : display.translateKey(key.keycode, modifiers, key.group)[4];
234
+ return modifiers & ~consumed & Gtk.acceleratorGetDefaultModMask();
184
235
  };
185
- const didActivateMatchingShortcut = (controller, widget, keyval, modifiers) => {
236
+ const matchingShortcuts = (controller, widget, keyval, modifiers) => {
186
237
  const count = controller.getNItems();
238
+ if (count === 0) {
239
+ return [];
240
+ }
241
+ const areMnemonicsEnabled = mnemonicModifiersFor(widget, keyval, modifiers) === controller.getMnemonicsModifiers();
242
+ const offset = areMnemonicsEnabled ? (lastActivatedShortcuts.get(controller) ?? 0) + 1 : 0;
243
+ const matches = [];
187
244
  for (let j = 0; j < count; j++) {
188
- const shortcut = controller.getItem(j);
189
- if (!(shortcut instanceof Gtk.Shortcut)) {
245
+ const index = (offset + j) % count;
246
+ const shortcut = controller.getItem(index);
247
+ if (!(shortcut instanceof Gtk.Shortcut) ||
248
+ !isTriggerMatch(shortcut.getTrigger(), keyval, modifiers, areMnemonicsEnabled)) {
190
249
  continue;
191
250
  }
192
- if (didActivateShortcut(shortcut, widget, keyval, modifiers)) {
251
+ const host = managedShortcutHost(widget, shortcut) ?? widget;
252
+ if (isShortcutLive(host)) {
253
+ matches.push({ shortcut, host, index });
254
+ }
255
+ }
256
+ return matches;
257
+ };
258
+ const didActivateMatchingShortcut = (controller, widget, keyval, modifiers) => {
259
+ const matches = matchingShortcuts(controller, widget, keyval, modifiers);
260
+ for (const [index, match] of matches.entries()) {
261
+ const flags = (index === matches.length - 1 ? Gtk.ShortcutActionFlags.EXCLUSIVE : 0);
262
+ if (didRunShortcutAction(match.shortcut, match.host, flags)) {
263
+ lastActivatedShortcuts.set(controller, match.index);
193
264
  return true;
194
265
  }
195
266
  }
@@ -216,26 +287,118 @@ const didDispatchAccelsOnWindow = (window, keyval, modifiers) => {
216
287
  }
217
288
  return false;
218
289
  };
219
- const didDispatchApplicationAccels = (widget, keyval, modifiers) => {
220
- const root = widget.getRoot();
221
- return root instanceof Gtk.Window && didDispatchAccelsOnWindow(root, keyval, modifiers);
290
+ const didDispatchApplicationAccels = (target, keyval, modifiers) => {
291
+ const root = target.getRoot();
292
+ return (root instanceof Gtk.Window &&
293
+ receivesEventsFor(root, Gtk.PropagationLimit.SAME_NATIVE, target) &&
294
+ didDispatchAccelsOnWindow(root, keyval, modifiers));
295
+ };
296
+ const SEARCH_BAR_CAPTURE_NAME = "gtk-search-bar-capture";
297
+ const ENTRY_COMPLETION_CLASS = "entry-completion";
298
+ const FILE_CHOOSER_ENTRY_TYPE_NAME = "GtkFileChooserEntry";
299
+ const CELL_EDITABLE_TYPE_NAME = "GtkCellEditableWidget";
300
+ /**
301
+ * GTK has no public getter for a text view's own key controller (`gtk_text_view_get_key_controller` is private) and
302
+ * lists controllers newest first, so the last key controller is the one the text view added when it was created.
303
+ * Controllers that subclasses such as GtkSourceView add read the current event, which emulated keys lack.
304
+ */
305
+ const isTextViewKeyController = (controller, widget) => controllersOn(widget)
306
+ .filter((current) => current instanceof Gtk.EventControllerKey)
307
+ .toArray()
308
+ .at(-1) === controller;
309
+ const isTextViewLibraryController = (controller, widget) => widget instanceof Gtk.TextView && !isTextViewKeyController(controller, widget);
310
+ const capturesKeysOf = (candidate, widget) => (candidate instanceof Gtk.SearchEntry || candidate instanceof Gtk.SearchBar) &&
311
+ candidate.getKeyCaptureWidget() === widget;
312
+ /**
313
+ * A search entry or search bar with a key capture widget adds a key controller to that widget, which forwards the
314
+ * current event to the entry. The search bar names its controller; the search entry leaves its own unnamed.
315
+ */
316
+ const isSearchKeyCapture = (controller, widget) => {
317
+ const name = controller.getName();
318
+ return (name === SEARCH_BAR_CAPTURE_NAME ||
319
+ (name === null &&
320
+ (capturesKeysOf(widget, widget) ||
321
+ descendants(widget).some((candidate) => capturesKeysOf(candidate, widget)))));
222
322
  };
223
- const isKeyStopController = (controller) => {
323
+ const isFileChooserForwarder = (_controller, widget) => widget instanceof Gtk.FileChooserWidget || getWidgetTypeName(widget) === FILE_CHOOSER_ENTRY_TYPE_NAME;
324
+ /**
325
+ * Widget types from other libraries whose key controllers read the current event: WebKit filters it through its input
326
+ * method and passes it to the web process.
327
+ */
328
+ const EVENT_READING_LIBRARY_TYPE_NAMES = ["WebKitWebViewBase"];
329
+ const isLibraryEventReader = (_controller, widget) => EVENT_READING_LIBRARY_TYPE_NAMES.some((name) => {
330
+ const type = typeFromName(name);
331
+ return type !== TYPE_INVALID && typeIsA(getInstanceType(widget), type);
332
+ });
333
+ const isDeprecatedPopup = (widget) => widget instanceof Gtk.Popover &&
334
+ (widget.getParent() instanceof Gtk.ComboBox || widget.hasCssClass(ENTRY_COMPLETION_CLASS));
335
+ const isDeprecatedWidgetForwarder = (_controller, widget) => widget instanceof Gtk.TreeView ||
336
+ getWidgetTypeName(widget) === CELL_EDITABLE_TYPE_NAME ||
337
+ isDeprecatedPopup(widget);
338
+ /**
339
+ * Key controllers GTK and its libraries add without a JavaScript listener whose handlers need the real key event,
340
+ * which emulated keys lack: they filter it through an input method, forward it with
341
+ * `gtk_event_controller_key_forward()` or read it directly, and log a critical without one. Emulated keys skip them;
342
+ * every other key controller receives the keys.
343
+ */
344
+ const REAL_EVENT_KEY_HANDLERS = [
345
+ isTextViewLibraryController,
346
+ isSearchKeyCapture,
347
+ isFileChooserForwarder,
348
+ isDeprecatedWidgetForwarder,
349
+ isLibraryEventReader,
350
+ ];
351
+ const needsRealKeyEvent = (controller, widget) => REAL_EVENT_KEY_HANDLERS.some((needsEvent) => needsEvent(controller, widget));
352
+ const SOURCE_COMPLETION_CONTROLLER_NAME = "gtk-source-completion";
353
+ const SOURCE_COMPLETION_LIST_TYPE_NAME = "GtkSourceCompletionList";
354
+ const SOURCE_COMPLETION_LIST_BOX_TYPE_NAME = "GtkSourceCompletionListBox";
355
+ /**
356
+ * GtkSourceView's completion adds a key controller to the view. While the completion list shows, the controller hides
357
+ * the list on {Escape} and forwards every other key to the list's box with `gtk_event_controller_key_forward()`, where
358
+ * the box's bindings move the selection and choose the selected proposal. Emulated keys have no event to forward, so
359
+ * they take the same path through the box's own controllers.
360
+ */
361
+ const completionListFor = (controller, widget) => controller.getName() === SOURCE_COMPLETION_CONTROLLER_NAME
362
+ ? (children(widget).find((child) => getWidgetTypeName(child) === SOURCE_COMPLETION_LIST_TYPE_NAME) ?? null)
363
+ : null;
364
+ const isKeyStopController = (controller, widget) => {
224
365
  if (controller instanceof Gtk.ShortcutController) {
225
366
  return controller.getScope() === Gtk.ShortcutScope.LOCAL;
226
367
  }
227
- return controller instanceof Gtk.EventControllerKey && hasSignalListener(controller, KEY_CONTROLLER_SIGNALS);
368
+ return (controller instanceof Gtk.EventControllerKey &&
369
+ (hasSignalListener(controller, KEY_CONTROLLER_SIGNALS) || !needsRealKeyEvent(controller, widget)));
370
+ };
371
+ const keyStopFor = (controller, widget) => {
372
+ const completionList = completionListFor(controller, widget);
373
+ if (completionList !== null) {
374
+ return { widget, completionList };
375
+ }
376
+ return isKeyStopController(controller, widget) ? { widget, controller } : null;
228
377
  };
229
- const keyStopsInPhase = (widget, phase) => {
378
+ const keyStopsInPhase = (widget, phase, target) => {
230
379
  const stops = [];
231
380
  for (const controller of controllersOn(widget)) {
232
- if (controller.getPropagationPhase() === phase && isKeyStopController(controller)) {
233
- stops.push({ widget, controller });
381
+ const stop = controller.getPropagationPhase() === phase &&
382
+ receivesEventsFor(widget, controller.getPropagationLimit(), target)
383
+ ? keyStopFor(controller, widget)
384
+ : null;
385
+ if (stop !== null) {
386
+ stops.push(stop);
234
387
  }
235
388
  }
236
389
  return stops;
237
390
  };
391
+ const forwardedKeyStops = (target) => [Gtk.PropagationPhase.CAPTURE, Gtk.PropagationPhase.TARGET, Gtk.PropagationPhase.BUBBLE].flatMap((phase) => keyStopsInPhase(target, phase, target));
392
+ const focusWithin = (widget) => {
393
+ const root = widget.getRoot();
394
+ const focus = root instanceof Gtk.Window ? root.getFocus() : null;
395
+ return focus?.getMapped() === true && focus.isAncestor(widget) ? focus : null;
396
+ };
238
397
  const propagationChain = (widget) => {
398
+ const focus = focusWithin(widget);
399
+ if (focus !== null) {
400
+ return [focus, ...ancestors(focus)];
401
+ }
239
402
  const delegate = getEditableDelegate(widget);
240
403
  const below = delegate === null ? [] : [delegate];
241
404
  return [...below, widget, ...ancestors(widget)];
@@ -244,18 +407,32 @@ const keyPropagationChain = (widget) => {
244
407
  const chain = propagationChain(widget);
245
408
  const [eventWidget = widget] = chain;
246
409
  return [
247
- ...chain.toReversed().flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.CAPTURE)),
248
- ...keyStopsInPhase(eventWidget, Gtk.PropagationPhase.TARGET),
249
- ...chain.flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.BUBBLE)),
410
+ ...chain.toReversed().flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.CAPTURE, eventWidget)),
411
+ ...keyStopsInPhase(eventWidget, Gtk.PropagationPhase.TARGET, eventWidget),
412
+ ...chain.flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.BUBBLE, eventWidget)),
250
413
  ];
251
414
  };
252
- const didStopHandlePress = (stop, keyval, modifiers) => {
415
+ const didCompletionHandlePress = (completionList, keyval, modifiers) => {
416
+ if (!completionList.getVisible()) {
417
+ return false;
418
+ }
419
+ if (keyval === Gdk.KEY_Escape) {
420
+ completionList.setVisible(false);
421
+ return false;
422
+ }
423
+ const box = descendants(completionList).find((descendant) => getWidgetTypeName(descendant) === SOURCE_COMPLETION_LIST_BOX_TYPE_NAME);
424
+ return box !== undefined && forwardedKeyStops(box).some((stop) => didStopHandlePress(stop, keyval, modifiers));
425
+ };
426
+ function didStopHandlePress(stop, keyval, modifiers) {
427
+ if ("completionList" in stop) {
428
+ return didCompletionHandlePress(stop.completionList, keyval, modifiers);
429
+ }
253
430
  const { controller, widget } = stop;
254
431
  if (controller instanceof Gtk.ShortcutController) {
255
432
  return didActivateMatchingShortcut(controller, widget, keyval, modifiers);
256
433
  }
257
434
  return controller.emit("key-pressed", keyval, 0, modifiers);
258
- };
435
+ }
259
436
  const keyTargetFor = (widget) => {
260
437
  const root = widget.getRoot();
261
438
  return { root, focus: root instanceof Gtk.Window ? root.getFocus() : null };
@@ -266,7 +443,8 @@ const isKeyTargetInvalidated = (widget, target) => {
266
443
  };
267
444
  const didHandlePress = (widget, keyval, modifiers) => {
268
445
  const target = keyTargetFor(widget);
269
- if (didDispatchApplicationAccels(widget, keyval, modifiers)) {
446
+ const [eventWidget = widget] = propagationChain(widget);
447
+ if (didDispatchApplicationAccels(eventWidget, keyval, modifiers)) {
270
448
  return true;
271
449
  }
272
450
  if (isKeyTargetInvalidated(widget, target)) {
@@ -281,31 +459,100 @@ const didHandlePress = (widget, keyval, modifiers) => {
281
459
  };
282
460
  const emitKeyReleased = (widget, keyval, modifiers) => {
283
461
  for (const stop of keyPropagationChain(widget)) {
284
- if (stop.controller instanceof Gtk.EventControllerKey) {
462
+ if ("controller" in stop && stop.controller instanceof Gtk.EventControllerKey) {
285
463
  stop.controller.emit("key-released", keyval, 0, modifiers);
286
464
  }
287
465
  }
288
466
  };
289
467
  const applyKeyPress = async (widget, keyval, modifiers) => {
290
468
  if (didHandlePress(widget, keyval, modifiers)) {
291
- return;
469
+ return true;
292
470
  }
293
471
  if (keyval === Gdk.KEY_Return && widget instanceof Gtk.Editable) {
294
472
  await fireEvent(widget, "activate");
473
+ return true;
295
474
  }
475
+ return false;
296
476
  };
297
- const applyKeyAction = async (widget, state, action) => {
477
+ const isUnhandledPress = async (widget, state, action) => {
298
478
  updateModifierState(state, action);
299
- if (action.isPress) {
300
- await applyKeyPress(widget, action.keyval, state.modifierState);
479
+ if (!action.isPress) {
480
+ emitKeyReleased(widget, action.keyval, state.modifierState);
481
+ return false;
482
+ }
483
+ const isHandled = await applyKeyPress(widget, action.keyval, state.modifierState);
484
+ return !isHandled && MODIFIER_KEYVAL_TO_MASK[action.keyval] === undefined;
485
+ };
486
+ /**
487
+ * Returns a function that resolves the widget keys meant for `widget` reach now: the widget, or the widget outside it
488
+ * that focus has moved to since, as after a {Tab}.
489
+ */
490
+ const followKeyFocus = (widget) => {
491
+ const start = keyTargetFor(widget);
492
+ return () => {
493
+ const { root, focus } = keyTargetFor(widget);
494
+ return root === start.root && focus !== null && focus !== start.focus && !focus.isAncestor(widget)
495
+ ? focus
496
+ : widget;
497
+ };
498
+ };
499
+ /** Applies the key actions and returns the keyvals of the presses, other than modifiers, that nothing handled. */
500
+ const applyKeyActions = async (widget, state, actions) => {
501
+ const keyWidget = followKeyFocus(widget);
502
+ const unhandled = [];
503
+ for (const action of actions) {
504
+ if (await isUnhandledPress(keyWidget(), state, action)) {
505
+ unhandled.push(action.keyval);
506
+ }
507
+ }
508
+ return unhandled;
509
+ };
510
+ const keyLabel = (keyval) => {
511
+ const name = Object.keys(KEY_MAP).find((key) => KEY_MAP[key] === keyval);
512
+ const character = Gdk.keyvalToUnicode(keyval);
513
+ if (name !== undefined) {
514
+ return `{${name}}`;
515
+ }
516
+ return character === 0 ? `{${Gdk.keyvalName(keyval) ?? String(keyval)}}` : String.fromCodePoint(character);
517
+ };
518
+ const canTakeFocus = (widget) => widget.getFocusable() && widget.getCanFocus();
519
+ /**
520
+ * Focuses a target whose window has its focus elsewhere, as a user does before typing, so the window's own bindings
521
+ * such as {Space} and {Enter} activating the focus widget act on the target. An entry that delegates its focus to its
522
+ * text, such as a search entry, takes it there without selecting the text, as a click does. A widget that passes its
523
+ * focus to a child, such as a menu button to its button, focuses that child.
524
+ */
525
+ const focusKeyTarget = (widget) => {
526
+ if (focusWithin(widget) !== null) {
301
527
  return;
302
528
  }
303
- emitKeyReleased(widget, action.keyval, state.modifierState);
529
+ const delegate = getEditableDelegate(widget);
530
+ if (delegate instanceof Gtk.Text && canTakeFocus(delegate)) {
531
+ delegate.grabFocusWithoutSelecting();
532
+ }
533
+ else {
534
+ widget.grabFocus();
535
+ }
304
536
  };
305
- const keyboard = (state, widget, input) => wrapEvent(widget, async () => {
306
- for (const action of parseKeyboardInput(input)) {
307
- await applyKeyAction(widget, state, action);
537
+ /**
538
+ * Sends the key sequence and returns the keys, as `keyboard` input would name them, whose presses nothing on the
539
+ * widget, its focused descendant or their ancestors handled.
540
+ */
541
+ const keyboard = async (state, widget, input) => {
542
+ let unhandled = [];
543
+ await wrapEvent(widget, async () => {
544
+ focusKeyTarget(widget);
545
+ unhandled = await applyKeyActions(widget, state, parseKeyboardInput(input));
546
+ });
547
+ return [...new Set(unhandled.map((keyval) => keyLabel(keyval)))];
548
+ };
549
+ const focusedKeyWidget = () => {
550
+ const window = activeToplevel() ?? mappedToplevels()[0];
551
+ if (window === undefined) {
552
+ throw usageError(new Error("No window is open to receive the keys"));
308
553
  }
309
- });
310
- export { tab, keyboard };
554
+ const focus = window.getFocus();
555
+ return focus?.getMapped() ? focus : window;
556
+ };
557
+ export { applyKeyActions, characterActions, focusedKeyWidget, followKeyFocus, heldModifiers, keyboard, parseTypedInput, tab, };
311
558
  //# sourceMappingURL=keyboard.js.map