@gtkx/testing 2.0.0-beta.12 → 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 (148) hide show
  1. package/dist/act.d.ts +7 -1
  2. package/dist/act.d.ts.map +1 -1
  3. package/dist/act.js +54 -4
  4. package/dist/act.js.map +1 -1
  5. package/dist/bound-queries.d.ts +1 -1
  6. package/dist/bound-queries.d.ts.map +1 -1
  7. package/dist/bound-queries.js.map +1 -1
  8. package/dist/cleanup-registry.d.ts.map +1 -1
  9. package/dist/cleanup-registry.js +4 -1
  10. package/dist/cleanup-registry.js.map +1 -1
  11. package/dist/fire-event.d.ts.map +1 -1
  12. package/dist/fire-event.js +11 -2
  13. package/dist/fire-event.js.map +1 -1
  14. package/dist/harness-window.d.ts +8 -2
  15. package/dist/harness-window.d.ts.map +1 -1
  16. package/dist/harness-window.js +28 -5
  17. package/dist/harness-window.js.map +1 -1
  18. package/dist/internal.d.ts +5 -0
  19. package/dist/internal.d.ts.map +1 -1
  20. package/dist/internal.js +5 -0
  21. package/dist/internal.js.map +1 -1
  22. package/dist/matchers.d.ts +4 -1
  23. package/dist/matchers.d.ts.map +1 -1
  24. package/dist/matchers.js +29 -9
  25. package/dist/matchers.js.map +1 -1
  26. package/dist/pretty-widget.d.ts +4 -2
  27. package/dist/pretty-widget.d.ts.map +1 -1
  28. package/dist/pretty-widget.js +7 -5
  29. package/dist/pretty-widget.js.map +1 -1
  30. package/dist/queries.d.ts +2 -1
  31. package/dist/queries.d.ts.map +1 -1
  32. package/dist/queries.js +19 -6
  33. package/dist/queries.js.map +1 -1
  34. package/dist/register-test-hooks.js +8 -0
  35. package/dist/register-test-hooks.js.map +1 -1
  36. package/dist/render.d.ts +14 -6
  37. package/dist/render.d.ts.map +1 -1
  38. package/dist/render.js +46 -31
  39. package/dist/render.js.map +1 -1
  40. package/dist/role-helpers.js.map +1 -1
  41. package/dist/types.d.ts +10 -2
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/types.js.map +1 -1
  44. package/dist/usage-error.d.ts +9 -0
  45. package/dist/usage-error.d.ts.map +1 -0
  46. package/dist/usage-error.js +13 -0
  47. package/dist/usage-error.js.map +1 -0
  48. package/dist/user-event/adjustment.d.ts.map +1 -1
  49. package/dist/user-event/adjustment.js +6 -5
  50. package/dist/user-event/adjustment.js.map +1 -1
  51. package/dist/user-event/click.d.ts +15 -7
  52. package/dist/user-event/click.d.ts.map +1 -1
  53. package/dist/user-event/click.js +165 -43
  54. package/dist/user-event/click.js.map +1 -1
  55. package/dist/user-event/controller.d.ts +10 -2
  56. package/dist/user-event/controller.d.ts.map +1 -1
  57. package/dist/user-event/controller.js +25 -3
  58. package/dist/user-event/controller.js.map +1 -1
  59. package/dist/user-event/dispatch.d.ts +1 -1
  60. package/dist/user-event/dispatch.d.ts.map +1 -1
  61. package/dist/user-event/dispatch.js +9 -6
  62. package/dist/user-event/dispatch.js.map +1 -1
  63. package/dist/user-event/editable-label-click.d.ts +5 -0
  64. package/dist/user-event/editable-label-click.d.ts.map +1 -0
  65. package/dist/user-event/editable-label-click.js +12 -0
  66. package/dist/user-event/editable-label-click.js.map +1 -0
  67. package/dist/user-event/event-wrapper.d.ts +11 -1
  68. package/dist/user-event/event-wrapper.d.ts.map +1 -1
  69. package/dist/user-event/event-wrapper.js +66 -12
  70. package/dist/user-event/event-wrapper.js.map +1 -1
  71. package/dist/user-event/gesture.d.ts.map +1 -1
  72. package/dist/user-event/gesture.js +67 -27
  73. package/dist/user-event/gesture.js.map +1 -1
  74. package/dist/user-event/index.d.ts +55 -18
  75. package/dist/user-event/index.d.ts.map +1 -1
  76. package/dist/user-event/index.js +42 -8
  77. package/dist/user-event/index.js.map +1 -1
  78. package/dist/user-event/keyboard.d.ts +27 -2
  79. package/dist/user-event/keyboard.d.ts.map +1 -1
  80. package/dist/user-event/keyboard.js +251 -30
  81. package/dist/user-event/keyboard.js.map +1 -1
  82. package/dist/user-event/list-row-click.d.ts +2 -1
  83. package/dist/user-event/list-row-click.d.ts.map +1 -1
  84. package/dist/user-event/list-row-click.js +10 -6
  85. package/dist/user-event/list-row-click.js.map +1 -1
  86. package/dist/user-event/native-click.d.ts +2 -1
  87. package/dist/user-event/native-click.d.ts.map +1 -1
  88. package/dist/user-event/native-click.js +16 -2
  89. package/dist/user-event/native-click.js.map +1 -1
  90. package/dist/user-event/pointer.d.ts +5 -3
  91. package/dist/user-event/pointer.d.ts.map +1 -1
  92. package/dist/user-event/pointer.js +72 -27
  93. package/dist/user-event/pointer.js.map +1 -1
  94. package/dist/user-event/selection.d.ts.map +1 -1
  95. package/dist/user-event/selection.js +46 -15
  96. package/dist/user-event/selection.js.map +1 -1
  97. package/dist/user-event/state.d.ts +2 -1
  98. package/dist/user-event/state.d.ts.map +1 -1
  99. package/dist/user-event/state.js +1 -1
  100. package/dist/user-event/state.js.map +1 -1
  101. package/dist/user-event/tab-view-click.d.ts +8 -0
  102. package/dist/user-event/tab-view-click.d.ts.map +1 -0
  103. package/dist/user-event/tab-view-click.js +37 -0
  104. package/dist/user-event/tab-view-click.js.map +1 -0
  105. package/dist/user-event/text.d.ts +3 -2
  106. package/dist/user-event/text.d.ts.map +1 -1
  107. package/dist/user-event/text.js +34 -14
  108. package/dist/user-event/text.js.map +1 -1
  109. package/dist/widget-accessible-properties.d.ts +13 -3
  110. package/dist/widget-accessible-properties.d.ts.map +1 -1
  111. package/dist/widget-accessible-properties.js +56 -17
  112. package/dist/widget-accessible-properties.js.map +1 -1
  113. package/dist/widget-target.d.ts.map +1 -1
  114. package/dist/widget-target.js +2 -1
  115. package/dist/widget-target.js.map +1 -1
  116. package/package.json +13 -8
  117. package/src/act.ts +70 -6
  118. package/src/bound-queries.ts +1 -1
  119. package/src/cleanup-registry.ts +5 -1
  120. package/src/fire-event.ts +17 -2
  121. package/src/harness-window.ts +37 -6
  122. package/src/internal.ts +5 -0
  123. package/src/matchers.ts +43 -9
  124. package/src/pretty-widget.ts +8 -5
  125. package/src/queries.ts +43 -10
  126. package/src/register-test-hooks.ts +11 -0
  127. package/src/render.tsx +56 -35
  128. package/src/role-helpers.ts +1 -1
  129. package/src/types.ts +11 -2
  130. package/src/usage-error.ts +16 -0
  131. package/src/user-event/adjustment.ts +11 -6
  132. package/src/user-event/click.ts +227 -47
  133. package/src/user-event/controller.ts +48 -3
  134. package/src/user-event/dispatch.ts +17 -7
  135. package/src/user-event/editable-label-click.ts +17 -0
  136. package/src/user-event/event-wrapper.ts +105 -14
  137. package/src/user-event/gesture.ts +101 -31
  138. package/src/user-event/index.ts +104 -36
  139. package/src/user-event/keyboard.ts +358 -35
  140. package/src/user-event/list-row-click.ts +12 -6
  141. package/src/user-event/native-click.ts +22 -3
  142. package/src/user-event/pointer.ts +101 -35
  143. package/src/user-event/selection.ts +71 -15
  144. package/src/user-event/state.ts +4 -2
  145. package/src/user-event/tab-view-click.ts +59 -0
  146. package/src/user-event/text.ts +39 -12
  147. package/src/widget-accessible-properties.ts +79 -18
  148. package/src/widget-target.ts +2 -1
@@ -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,8 +21,12 @@ 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 };
22
31
  type MatchingShortcut = { shortcut: Gtk.Shortcut; host: Gtk.Widget; index: number };
23
32
 
@@ -57,6 +66,7 @@ const KEY_MAP: Record<string, number> = {
57
66
  };
58
67
 
59
68
  const KEY_CONTROLLER_SIGNALS = ["key-pressed", "key-released"];
69
+ const LITERAL_BRACE = "{{";
60
70
 
61
71
  const MODIFIER_KEYVAL_TO_MASK: Record<number, number> = {
62
72
  [Gdk.KEY_Shift_L]: Gdk.ModifierType.SHIFT_MASK,
@@ -69,6 +79,17 @@ const MODIFIER_KEYVAL_TO_MASK: Record<number, number> = {
69
79
  [Gdk.KEY_Meta_R]: Gdk.ModifierType.META_MASK,
70
80
  };
71
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
+
72
93
  /** Moves focus within the widget's root. */
73
94
  const tab = (widget: Gtk.Widget, options?: TabOptions): Promise<void> =>
74
95
  wrapEvent(widget, () => {
@@ -80,6 +101,12 @@ const tab = (widget: Gtk.Widget, options?: TabOptions): Promise<void> =>
80
101
  }
81
102
  });
82
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
+
83
110
  const parseKeyToken = (token: string): { keyval: number; isPress: boolean; isRelease: boolean } => {
84
111
  let keyName = token;
85
112
  let isPress = true;
@@ -93,10 +120,10 @@ const parseKeyToken = (token: string): { keyval: number; isPress: boolean; isRel
93
120
  isRelease = false;
94
121
  }
95
122
 
96
- const keyval = KEY_MAP[keyName];
123
+ const keyval = KEY_MAP[keyName] ?? characterKeyval(keyName);
97
124
 
98
125
  if (keyval === undefined) {
99
- throw new Error(`Unknown key: {${keyName}}`);
126
+ throw usageError(new Error(`Unknown key: {${keyName}}`));
100
127
  }
101
128
 
102
129
  return { keyval, isPress, isRelease };
@@ -157,6 +184,46 @@ const parseKeyboardInput = (input: string): KeyAction[] => {
157
184
  return actions;
158
185
  };
159
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
+
160
227
  const updateModifierState = (state: UserEventState, action: KeyAction): void => {
161
228
  const mask = MODIFIER_KEYVAL_TO_MASK[action.keyval];
162
229
 
@@ -237,15 +304,12 @@ const managedShortcutHost = (manager: Gtk.Widget, shortcut: Gtk.Shortcut): Gtk.W
237
304
  return null;
238
305
  };
239
306
 
240
- const isMovingFocus = (action: Gtk.ShortcutAction): boolean =>
241
- action instanceof Gtk.SignalAction && action.getSignalName() === "move-focus";
242
-
243
307
  const isShortcutLive = (host: Gtk.Widget): boolean => host.isSensitive() && host.getMapped();
244
308
 
245
309
  const didRunShortcutAction = (shortcut: Gtk.Shortcut, host: Gtk.Widget, flags: Gtk.ShortcutActionFlags): boolean => {
246
310
  const action = shortcut.getAction();
247
311
 
248
- if (action === null || isMovingFocus(action) || !isShortcutLive(host)) {
312
+ if (action === null || !isShortcutLive(host)) {
249
313
  return false;
250
314
  }
251
315
 
@@ -355,33 +419,169 @@ const didDispatchAccelsOnWindow = (window: Gtk.Window, keyval: number, modifiers
355
419
  return false;
356
420
  };
357
421
 
358
- const didDispatchApplicationAccels = (widget: Gtk.Widget, keyval: number, modifiers: number): boolean => {
359
- const root = widget.getRoot();
422
+ const didDispatchApplicationAccels = (target: Gtk.Widget, keyval: number, modifiers: number): boolean => {
423
+ const root = target.getRoot();
360
424
 
361
- return root instanceof Gtk.Window && didDispatchAccelsOnWindow(root, keyval, modifiers);
425
+ return (
426
+ root instanceof Gtk.Window &&
427
+ receivesEventsFor(root, Gtk.PropagationLimit.SAME_NATIVE, target) &&
428
+ didDispatchAccelsOnWindow(root, keyval, modifiers)
429
+ );
362
430
  };
363
431
 
364
- const isKeyStopController = (controller: Gtk.EventController): controller is KeyStopController => {
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
+ );
468
+ };
469
+
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 => {
365
528
  if (controller instanceof Gtk.ShortcutController) {
366
529
  return controller.getScope() === Gtk.ShortcutScope.LOCAL;
367
530
  }
368
531
 
369
- 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
+ );
370
536
  };
371
537
 
372
- const keyStopsInPhase = (widget: Gtk.Widget, phase: Gtk.PropagationPhase): KeyStop[] => {
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;
546
+ };
547
+
548
+ const keyStopsInPhase = (widget: Gtk.Widget, phase: Gtk.PropagationPhase, target: Gtk.Widget): KeyStop[] => {
373
549
  const stops: KeyStop[] = [];
374
550
 
375
551
  for (const controller of controllersOn(widget)) {
376
- if (controller.getPropagationPhase() === phase && isKeyStopController(controller)) {
377
- 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);
378
560
  }
379
561
  }
380
562
 
381
563
  return stops;
382
564
  };
383
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
+
384
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
+
385
585
  const delegate = getEditableDelegate(widget);
386
586
  const below = delegate === null ? [] : [delegate];
387
587
 
@@ -393,13 +593,35 @@ const keyPropagationChain = (widget: Gtk.Widget): KeyStop[] => {
393
593
  const [eventWidget = widget] = chain;
394
594
 
395
595
  return [
396
- ...chain.toReversed().flatMap((current) => keyStopsInPhase(current, Gtk.PropagationPhase.CAPTURE)),
397
- ...keyStopsInPhase(eventWidget, Gtk.PropagationPhase.TARGET),
398
- ...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)),
399
599
  ];
400
600
  };
401
601
 
402
- 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
+
403
625
  const { controller, widget } = stop;
404
626
 
405
627
  if (controller instanceof Gtk.ShortcutController) {
@@ -407,7 +629,7 @@ const didStopHandlePress = (stop: KeyStop, keyval: number, modifiers: Gdk.Modifi
407
629
  }
408
630
 
409
631
  return controller.emit("key-pressed", keyval, 0, modifiers);
410
- };
632
+ }
411
633
 
412
634
  const keyTargetFor = (widget: Gtk.Widget): KeyTarget => {
413
635
  const root = widget.getRoot();
@@ -423,8 +645,9 @@ const isKeyTargetInvalidated = (widget: Gtk.Widget, target: KeyTarget): boolean
423
645
 
424
646
  const didHandlePress = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): boolean => {
425
647
  const target = keyTargetFor(widget);
648
+ const [eventWidget = widget] = propagationChain(widget);
426
649
 
427
- if (didDispatchApplicationAccels(widget, keyval, modifiers)) {
650
+ if (didDispatchApplicationAccels(eventWidget, keyval, modifiers)) {
428
651
  return true;
429
652
  }
430
653
 
@@ -443,39 +666,139 @@ const didHandlePress = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.Modif
443
666
 
444
667
  const emitKeyReleased = (widget: Gtk.Widget, keyval: number, modifiers: Gdk.ModifierType): void => {
445
668
  for (const stop of keyPropagationChain(widget)) {
446
- if (stop.controller instanceof Gtk.EventControllerKey) {
669
+ if ("controller" in stop && stop.controller instanceof Gtk.EventControllerKey) {
447
670
  stop.controller.emit("key-released", keyval, 0, modifiers);
448
671
  }
449
672
  }
450
673
  };
451
674
 
452
- 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> => {
453
676
  if (didHandlePress(widget, keyval, modifiers)) {
454
- return;
677
+ return true;
455
678
  }
456
679
 
457
680
  if (keyval === Gdk.KEY_Return && widget instanceof Gtk.Editable) {
458
681
  await fireEvent(widget, "activate");
682
+
683
+ return true;
459
684
  }
685
+
686
+ return false;
460
687
  };
461
688
 
462
- 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> => {
463
690
  updateModifierState(state, action);
464
691
 
465
- if (action.isPress) {
466
- await applyKeyPress(widget, action.keyval, state.modifierState);
692
+ if (!action.isPress) {
693
+ emitKeyReleased(widget, action.keyval, state.modifierState);
467
694
 
468
- return;
695
+ return false;
469
696
  }
470
697
 
471
- 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;
472
701
  };
473
702
 
474
- const keyboard = (state: UserEventState, widget: Gtk.Widget, input: string): Promise<void> =>
475
- wrapEvent(widget, async () => {
476
- for (const action of parseKeyboardInput(input)) {
477
- 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);
478
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));
479
776
  });
480
777
 
481
- 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
+ };
@@ -1,3 +1,4 @@
1
+ import * as Gdk from "@gtkx/gi/gdk";
1
2
  import * as GLib from "@gtkx/gi/glib";
2
3
  import * as Gtk from "@gtkx/gi/gtk";
3
4
  import { ancestors } from "../traversal.js";
@@ -23,31 +24,36 @@ const isViewSingleClickActivating = (row: Gtk.Widget): boolean => {
23
24
  return view !== null && (callBooleanGetter(view, SINGLE_CLICK_ACTIVATE_GETTER) ?? false);
24
25
  };
25
26
 
26
- const selectRow = (row: Gtk.Widget): void => {
27
+ const hasModifier = (modifiers: Gdk.ModifierType, mask: Gdk.ModifierType): boolean => (modifiers & mask) !== 0;
28
+
29
+ const selectRow = (row: Gtk.Widget, modifiers: Gdk.ModifierType): void => {
30
+ const modify = hasModifier(modifiers, Gdk.ModifierType.CONTROL_MASK);
31
+ const extend = hasModifier(modifiers, Gdk.ModifierType.SHIFT_MASK);
32
+
27
33
  row.activateAction(
28
34
  SELECT_ACTION,
29
- GLib.Variant.newTuple([GLib.Variant.newBoolean(false), GLib.Variant.newBoolean(false)]),
35
+ GLib.Variant.newTuple([GLib.Variant.newBoolean(modify), GLib.Variant.newBoolean(extend)]),
30
36
  );
31
37
  };
32
38
 
33
39
  const isRowActivatedByPress = (row: Gtk.Widget, press: number): boolean =>
34
40
  press === DOUBLE_CLICK_PRESS || isViewSingleClickActivating(row);
35
41
 
36
- const applyRowPress = (row: Gtk.Widget, press: number): void => {
42
+ const applyRowPress = (row: Gtk.Widget, press: number, modifiers: Gdk.ModifierType): void => {
37
43
  if (row.getFocusOnClick()) {
38
44
  row.grabFocus();
39
45
  }
40
46
 
41
- selectRow(row);
47
+ selectRow(row, modifiers);
42
48
 
43
49
  if (isRowActivatedByPress(row, press)) {
44
50
  Gtk.Widget.prototype.activate.call(row);
45
51
  }
46
52
  };
47
53
 
48
- const applyRowClick = (row: Gtk.Widget, nPress: number): void => {
54
+ const applyRowClick = (row: Gtk.Widget, nPress: number, modifiers: Gdk.ModifierType): void => {
49
55
  for (let press = 1; press <= nPress; press++) {
50
- applyRowPress(row, press);
56
+ applyRowPress(row, press, modifiers);
51
57
  }
52
58
  };
53
59
 
@@ -1,13 +1,17 @@
1
+ import type * as Gdk from "@gtkx/gi/gdk";
1
2
  import * as Gtk from "@gtkx/gi/gtk";
2
3
  import { getWidgetTypeName } from "../widget-getters.js";
3
4
  import { applyHeaderClick } from "./column-header-click.js";
5
+ import { applyEditableLabelClick, isEditableLabelText } from "./editable-label-click.js";
4
6
  import { applyRowClick } from "./list-row-click.js";
5
7
  import { applyTabClick, isNotebookTab } from "./notebook-tab-click.js";
8
+ import { applyTabViewClick, isTabViewTab } from "./tab-view-click.js";
6
9
 
7
- type NativeClick = (widget: Gtk.Widget, nPress: number) => void;
10
+ type NativeClick = (widget: Gtk.Widget, nPress: number, modifiers: Gdk.ModifierType) => void;
8
11
 
9
12
  const COLUMN_ROW_TYPE_NAME = "GtkColumnViewRowWidget";
10
- const ROW_TYPE_NAMES: Set<string> = new Set([COLUMN_ROW_TYPE_NAME, "GtkListItemWidget"]);
13
+ const LIST_ITEM_TYPE_NAME = "GtkListItemWidget";
14
+ const ROW_TYPE_NAMES: Set<string> = new Set([COLUMN_ROW_TYPE_NAME, LIST_ITEM_TYPE_NAME]);
11
15
  const CELL_TYPE_NAME = "GtkColumnViewCellWidget";
12
16
  const TITLE_TYPE_NAME = "GtkColumnViewTitle";
13
17
  const TOGGLE_EXPAND_ACTION = "listitem.toggle-expand";
@@ -15,8 +19,15 @@ const TOGGLE_EXPAND_ACTION = "listitem.toggle-expand";
15
19
  const isHeaderRow = (widget: Gtk.Widget): boolean =>
16
20
  getWidgetTypeName(widget) === COLUMN_ROW_TYPE_NAME && widget.getParent() instanceof Gtk.ColumnView;
17
21
 
22
+ /**
23
+ * A list item widget outside a list view, such as the one showing a drop-down's selection, is neither selectable nor
24
+ * activatable, so GTK denies its click gesture and the press reaches its ancestors.
25
+ */
26
+ const isDetachedListItem = (widget: Gtk.Widget): boolean =>
27
+ getWidgetTypeName(widget) === LIST_ITEM_TYPE_NAME && !(widget.getParent() instanceof Gtk.ListBase);
28
+
18
29
  const isClickTransparent = (widget: Gtk.Widget): boolean =>
19
- getWidgetTypeName(widget) === CELL_TYPE_NAME || isHeaderRow(widget);
30
+ getWidgetTypeName(widget) === CELL_TYPE_NAME || isHeaderRow(widget) || isDetachedListItem(widget);
20
31
 
21
32
  const isExpanderToggling = (widget: Gtk.Widget): boolean =>
22
33
  widget instanceof Gtk.TreeExpander && widget.getListRow()?.isExpandable() === true && !widget.getHideExpander();
@@ -35,6 +46,10 @@ const nativeClickFor = (widget: Gtk.Widget, isClicked: boolean): NativeClick | n
35
46
  return toggleExpander;
36
47
  }
37
48
 
49
+ if (isClicked && isEditableLabelText(widget)) {
50
+ return applyEditableLabelClick;
51
+ }
52
+
38
53
  if (isColumnHeader(widget)) {
39
54
  return applyHeaderClick;
40
55
  }
@@ -43,6 +58,10 @@ const nativeClickFor = (widget: Gtk.Widget, isClicked: boolean): NativeClick | n
43
58
  return applyTabClick;
44
59
  }
45
60
 
61
+ if (isTabViewTab(widget)) {
62
+ return applyTabViewClick;
63
+ }
64
+
46
65
  return isFactoryRow(widget) ? applyRowClick : null;
47
66
  };
48
67