drawnui-react 0.1.0-preview.8 → 1.0.1

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 (117) hide show
  1. package/README.md +229 -22
  2. package/SKIPPED.md +38 -29
  3. package/dist/controls/AnimatedFramesRenderer.d.ts +9 -0
  4. package/dist/controls/AnimatedFramesRenderer.js +18 -1
  5. package/dist/controls/DrawnGame.d.ts +44 -0
  6. package/dist/controls/DrawnGame.js +82 -0
  7. package/dist/controls/GridStructure.d.ts +25 -3
  8. package/dist/controls/GridStructure.js +81 -5
  9. package/dist/controls/SkiaBackdrop.d.ts +8 -2
  10. package/dist/controls/SkiaBackdrop.js +42 -14
  11. package/dist/controls/SkiaButton.d.ts +17 -3
  12. package/dist/controls/SkiaButton.js +26 -8
  13. package/dist/controls/SkiaCarousel.d.ts +22 -3
  14. package/dist/controls/SkiaCarousel.js +85 -9
  15. package/dist/controls/SkiaCheckbox.d.ts +0 -1
  16. package/dist/controls/SkiaCheckbox.js +2 -7
  17. package/dist/controls/SkiaDrawer.d.ts +15 -0
  18. package/dist/controls/SkiaDrawer.js +42 -7
  19. package/dist/controls/SkiaEditor.d.ts +51 -2
  20. package/dist/controls/SkiaEditor.js +292 -58
  21. package/dist/controls/SkiaGif.d.ts +7 -0
  22. package/dist/controls/SkiaGif.js +25 -4
  23. package/dist/controls/SkiaImage.d.ts +10 -0
  24. package/dist/controls/SkiaImage.js +80 -19
  25. package/dist/controls/SkiaLabel.d.ts +200 -11
  26. package/dist/controls/SkiaLabel.js +925 -93
  27. package/dist/controls/SkiaLabelFps.d.ts +12 -0
  28. package/dist/controls/SkiaLabelFps.js +35 -0
  29. package/dist/controls/SkiaLayout.d.ts +89 -9
  30. package/dist/controls/SkiaLayout.js +397 -23
  31. package/dist/controls/SkiaLottie.js +5 -3
  32. package/dist/controls/SkiaProgress.d.ts +2 -1
  33. package/dist/controls/SkiaProgress.js +8 -5
  34. package/dist/controls/SkiaRadioButton.d.ts +2 -0
  35. package/dist/controls/SkiaRadioButton.js +16 -4
  36. package/dist/controls/SkiaRichLabel.d.ts +1 -1
  37. package/dist/controls/SkiaRichLabel.js +11 -1
  38. package/dist/controls/SkiaScroll.d.ts +86 -7
  39. package/dist/controls/SkiaScroll.js +361 -40
  40. package/dist/controls/SkiaScrollBar.d.ts +27 -0
  41. package/dist/controls/SkiaScrollBar.js +51 -2
  42. package/dist/controls/SkiaShaderCarousel.d.ts +11 -1
  43. package/dist/controls/SkiaShaderCarousel.js +27 -5
  44. package/dist/controls/SkiaShape.d.ts +15 -2
  45. package/dist/controls/SkiaShape.js +57 -10
  46. package/dist/controls/SkiaShell.d.ts +282 -0
  47. package/dist/controls/SkiaShell.js +972 -0
  48. package/dist/controls/SkiaSlider.d.ts +20 -2
  49. package/dist/controls/SkiaSlider.js +61 -7
  50. package/dist/controls/SkiaSprite.d.ts +4 -0
  51. package/dist/controls/SkiaSprite.js +27 -11
  52. package/dist/controls/SkiaSvg.d.ts +4 -0
  53. package/dist/controls/SkiaSvg.js +28 -4
  54. package/dist/controls/SkiaSwitch.d.ts +0 -1
  55. package/dist/controls/SkiaSwitch.js +4 -8
  56. package/dist/controls/SkiaToggle.d.ts +25 -5
  57. package/dist/controls/SkiaToggle.js +58 -5
  58. package/dist/controls/SnappingLayout.d.ts +12 -1
  59. package/dist/controls/SnappingLayout.js +21 -2
  60. package/dist/controls/TooltipStyle.d.ts +52 -0
  61. package/dist/controls/TooltipStyle.js +88 -0
  62. package/dist/core/Accessibility.d.ts +162 -11
  63. package/dist/core/Accessibility.js +882 -22
  64. package/dist/core/Animators.d.ts +37 -1
  65. package/dist/core/Animators.js +102 -9
  66. package/dist/core/Canvas.d.ts +307 -8
  67. package/dist/core/Canvas.js +1202 -66
  68. package/dist/core/ControlStyle.d.ts +2 -0
  69. package/dist/core/ControlStyle.js +2 -0
  70. package/dist/core/DesignerActions.d.ts +32 -0
  71. package/dist/core/DesignerActions.js +68 -0
  72. package/dist/core/Easing.d.ts +12 -1
  73. package/dist/core/Easing.js +25 -1
  74. package/dist/core/Gestures.d.ts +86 -2
  75. package/dist/core/Gestures.js +184 -4
  76. package/dist/core/Hover.d.ts +51 -0
  77. package/dist/core/Hover.js +140 -0
  78. package/dist/core/ImageEffects.d.ts +2 -0
  79. package/dist/core/ImageEffects.js +2 -0
  80. package/dist/core/KeyboardManager.d.ts +40 -0
  81. package/dist/core/KeyboardManager.js +98 -8
  82. package/dist/core/PopupWindows.d.ts +94 -0
  83. package/dist/core/PopupWindows.js +207 -0
  84. package/dist/core/Props.d.ts +11 -0
  85. package/dist/core/Props.js +44 -0
  86. package/dist/core/ScrollAnimators.d.ts +6 -0
  87. package/dist/core/ScrollAnimators.js +16 -1
  88. package/dist/core/Shaping.d.ts +40 -0
  89. package/dist/core/Shaping.js +224 -0
  90. package/dist/core/SkiaControl.d.ts +306 -13
  91. package/dist/core/SkiaControl.js +681 -75
  92. package/dist/core/SkiaEffect.d.ts +29 -1
  93. package/dist/core/SkiaEffect.js +78 -16
  94. package/dist/core/SkiaImageManager.d.ts +101 -18
  95. package/dist/core/SkiaImageManager.js +318 -63
  96. package/dist/core/Styles.js +7 -1
  97. package/dist/core/Super.d.ts +33 -2
  98. package/dist/core/Super.js +58 -12
  99. package/dist/core/TextInputProxy.d.ts +31 -1
  100. package/dist/core/TextInputProxy.js +97 -15
  101. package/dist/core/Tooltips.d.ts +71 -0
  102. package/dist/core/Tooltips.js +205 -0
  103. package/dist/core/Types.d.ts +11 -1
  104. package/dist/core/Types.js +32 -1
  105. package/dist/core/ViewsAdapter.d.ts +7 -2
  106. package/dist/core/ViewsAdapter.js +10 -3
  107. package/dist/index.d.ts +8 -0
  108. package/dist/index.js +8 -0
  109. package/dist/react/SkiaShell.d.ts +8 -74
  110. package/dist/react/SkiaShell.js +163 -475
  111. package/dist/react/index.d.ts +48 -4
  112. package/dist/react/index.js +210 -43
  113. package/dist/react/reconciler.d.ts +32 -0
  114. package/dist/react/reconciler.js +71 -40
  115. package/dist/vite/index.d.ts +9 -0
  116. package/dist/vite/index.js +36 -3
  117. package/package.json +103 -5
@@ -0,0 +1,140 @@
1
+ import { SKPoint } from "./Gestures";
2
+ /**
3
+ * DrawnUI hover (drawnui-cross 6m, C# DrawnView ReportHover / CommitHover): every control under the mouse that takes
4
+ * hover (`ReceivesHover`) is hovered, a card and the button inside it alike, as CSS :hover. Decided from the hit path
5
+ * of one pointer position, by the routing rules of ProcessGestures (visible, not InputTransparent, inside, children
6
+ * not locked, nothing below a control that blocks gestures below).
7
+ * While content moves under the pointer (a scroll glide, fling or pan, a carousel or drawer transition) hover is not
8
+ * tracked and what was hovered stays; one check at the last pointer position follows when it stops, and after
9
+ * controls are added or removed, a cell is rebound, a hovered control is hidden or a scroll jumps. Leaving the canvas
10
+ * clears hover at once. Touch never hovers. No allocation on frames where nothing changes.
11
+ */
12
+ export class HoverManager {
13
+ /** The hovered controls, root first. */
14
+ Hovered = [];
15
+ next = [];
16
+ /** The controls under the pointer at the last hit test, root first, and the pointer in each one's own space. */
17
+ Path = [];
18
+ PathX = [];
19
+ PathY = [];
20
+ movers = [];
21
+ hasPointer = false;
22
+ x = 0;
23
+ y = 0;
24
+ paused = false;
25
+ check = false;
26
+ /** Picks the root under a point (canvas pixels): a popup window over the content, else the content. */
27
+ RootAt;
28
+ /** Content moves under the pointer (a scroll glide, a carousel transition): hover waits. */
29
+ get Paused() { return this.paused; }
30
+ /**
31
+ * The mouse (or a pen in range) moved to (x, y), canvas pixels, nothing pressed. The hit path is taken also while
32
+ * hover waits (the cursor follows it), hover changes only when nothing moves.
33
+ */
34
+ Move(root, x, y) {
35
+ this.hasPointer = true;
36
+ this.x = x;
37
+ this.y = y;
38
+ this.HitTest(root);
39
+ if (this.paused)
40
+ return; // content moves under the pointer: what was hovered stays, one check when it stops
41
+ this.check = false;
42
+ this.Apply(true);
43
+ }
44
+ /** Fills Path with the controls under the pointer by the routing rules of ProcessGestures. */
45
+ HitTest(root) {
46
+ const path = this.Path;
47
+ path.length = 0;
48
+ this.PathX.length = 0;
49
+ this.PathY.length = 0;
50
+ if (this.RootAt)
51
+ root = this.RootAt(this.x, this.y);
52
+ if (root && root.IsVisible && !root.InputTransparent) {
53
+ const point = root.TransformPointToLocalSpace(new SKPoint(this.x, this.y));
54
+ if (root.HitIsInside(point.X, point.Y))
55
+ root.CollectHovered(point, path, this.PathX, this.PathY);
56
+ }
57
+ }
58
+ /** The pointer left the canvas: nothing is hovered, also while content moves. */
59
+ Leave() {
60
+ this.hasPointer = false;
61
+ this.check = false;
62
+ this.Path.length = 0;
63
+ this.PathX.length = 0;
64
+ this.PathY.length = 0;
65
+ this.Apply(false);
66
+ }
67
+ /** The controls under a still pointer may have changed: checked once at the end of the frame. */
68
+ RequestCheck() { if (this.hasPointer)
69
+ this.check = true; }
70
+ /** A control that moves its content (MovesContent) while drawn: hover waits while any of them moves. */
71
+ RegisterMover(control) { if (this.movers.indexOf(control) < 0)
72
+ this.movers.push(control); }
73
+ /** End of a frame: pause while content moves, else run a pending check (at most one a frame, none while pressed). */
74
+ AfterFrame(root, pressed) {
75
+ let moving = false;
76
+ for (let i = this.movers.length - 1; i >= 0; i--) {
77
+ const m = this.movers[i];
78
+ if (m.IsDisposed || !m.Superview) {
79
+ this.movers.splice(i, 1);
80
+ continue;
81
+ }
82
+ if (!moving && m.IsVisible && m.MovesContent())
83
+ moving = true;
84
+ }
85
+ if (moving) {
86
+ this.paused = true;
87
+ return;
88
+ }
89
+ if (this.paused) {
90
+ this.paused = false;
91
+ this.check = true;
92
+ }
93
+ // a hovered control (or an ancestor) hidden or removed under a still pointer stops being hovered
94
+ for (let i = 0; i < this.Hovered.length && !this.check; i++)
95
+ if (!HoverManager.Shown(this.Hovered[i]))
96
+ this.check = true;
97
+ if (!this.check)
98
+ return;
99
+ this.check = false;
100
+ if (!this.hasPointer || pressed)
101
+ return;
102
+ this.HitTest(root);
103
+ this.Apply(true);
104
+ }
105
+ static Shown(control) {
106
+ if (control.IsDisposed || !control.Superview)
107
+ return false;
108
+ for (let c = control; c; c = c.Parent)
109
+ if (!c.IsVisible)
110
+ return false;
111
+ return true;
112
+ }
113
+ /**
114
+ * The hovered set becomes the controls of the hit path that take hover (`fromPath`), or none: the ones that left get
115
+ * HoverChanged(false), then the new ones (true).
116
+ */
117
+ Apply(fromPath) {
118
+ const now = this.Hovered, next = this.next;
119
+ next.length = 0;
120
+ if (fromPath)
121
+ for (const c of this.Path)
122
+ if (c.ReceivesHover)
123
+ next.push(c);
124
+ if (now.length === next.length) {
125
+ let same = true;
126
+ for (let i = 0; i < now.length && same; i++)
127
+ same = now[i] === next[i];
128
+ if (same)
129
+ return;
130
+ }
131
+ const was = now.splice(0);
132
+ now.push(...next);
133
+ for (const c of was)
134
+ if (now.indexOf(c) < 0)
135
+ c.ApplyHover(false);
136
+ for (const c of now)
137
+ if (was.indexOf(c) < 0)
138
+ c.ApplyHover(true);
139
+ }
140
+ }
@@ -14,6 +14,8 @@ export declare const SkiaImageEffects: {
14
14
  Darken(amount: number): ColorFilter;
15
15
  Lighten(amount: number): ColorFilter;
16
16
  Grayscale(): ColorFilter;
17
+ /** C# Grayscale2: the luminance weights the Grayscale effect uses (BlackAndWhite keeps Grayscale's). */
18
+ Grayscale2(): ColorFilter;
17
19
  Pastel(): ColorFilter;
18
20
  Sepia(): ColorFilter;
19
21
  InvertColors(): ColorFilter;
@@ -12,6 +12,8 @@ export const SkiaImageEffects = {
12
12
  Darken(amount) { const a = -amount / 255; return SkiaImageEffects.Matrix([1, 0, 0, 0, a, 0, 1, 0, 0, a, 0, 0, 1, 0, a, 0, 0, 0, 1, 0]); },
13
13
  Lighten(amount) { const a = amount / 255; return SkiaImageEffects.Matrix([1, 0, 0, 0, a, 0, 1, 0, 0, a, 0, 0, 1, 0, a, 0, 0, 0, 1, 0]); },
14
14
  Grayscale() { return SkiaImageEffects.Matrix([0.2989, 0.587, 0.114, 0, 0, 0.2989, 0.587, 0.114, 0, 0, 0.2989, 0.587, 0.114, 0, 0, 0, 0, 0, 1, 0]); },
15
+ /** C# Grayscale2: the luminance weights the Grayscale effect uses (BlackAndWhite keeps Grayscale's). */
16
+ Grayscale2() { return SkiaImageEffects.Matrix([0.21, 0.72, 0.07, 0, 0, 0.21, 0.72, 0.07, 0, 0, 0.21, 0.72, 0.07, 0, 0, 0, 0, 0, 1, 0]); },
15
17
  Pastel() { return SkiaImageEffects.Matrix([0.75, 0.25, 0.25, 0, 0, 0.25, 0.75, 0.25, 0, 0, 0.25, 0.25, 0.75, 0, 0, 0, 0, 0, 1, 0]); },
16
18
  Sepia() { return SkiaImageEffects.Matrix([0.393, 0.769, 0.189, 0, 0, 0.349, 0.686, 0.168, 0, 0, 0.272, 0.534, 0.131, 0, 0, 0, 0, 0, 1, 0]); },
17
19
  InvertColors() { return SkiaImageEffects.Matrix([-1, 0, 0, 0, 1, 0, -1, 0, 0, 1, 0, 0, -1, 0, 1, 0, 0, 0, 1, 0]); },
@@ -5,6 +5,11 @@
5
5
  * subscribes while focused. Characters typed into the hidden TextInputProxy textarea (IME, soft keyboard) arrive
6
6
  * as input events there, so its key events are not forwarded as KeyChar here (a React-port addition over
7
7
  * DrawnUi.Blazor, which has no DOM text input).
8
+ *
9
+ * One key routing order on every head (drawnui-cross 6az, C# KeyboardManager.IsKeyHandled): the canvas keyboard
10
+ * navigation gets a key first (an accessibility overlay node: activation, group arrows, slider steps; then the canvas'
11
+ * own handlers: Escape closing a popup window, arrows starting navigation), then the app's KeyDown handlers run with
12
+ * `IsKeyHandled` telling whether navigation used it. Tab belongs to the browser.
8
13
  */
9
14
  export type InputKey = string;
10
15
  export declare class KeyboardManager {
@@ -13,13 +18,48 @@ export declare class KeyboardManager {
13
18
  private static readonly keyDown;
14
19
  private static readonly keyUp;
15
20
  private static readonly keyChar;
21
+ private static readonly navigation;
22
+ private static usedKey?;
23
+ private static deferred?;
24
+ /**
25
+ * True while KeyDown handlers run for a key the canvas keyboard navigation already used: Enter / Space activated the
26
+ * focused control, an arrow moved inside a list or a menu or stepped a slider, Escape closed a popup window. An app
27
+ * handler that would act on the same key (a menu that opens on Enter) skips it, so one press does one thing.
28
+ */
29
+ static IsKeyHandled: boolean;
30
+ /** For the accessibility overlay and canvas navigation: `key` was used, the KeyDown raised next for it reports IsKeyHandled. */
31
+ static MarkKeyHandled(key: InputKey): void;
16
32
  static get IsShiftPressed(): boolean;
17
33
  static get IsControlPressed(): boolean;
34
+ /** True while the keydown handlers run: a popup opened now was opened from the keyboard (Alt+F). */
35
+ static IsDispatchingKeyDown: boolean;
18
36
  static get IsAltPressed(): boolean;
19
37
  /** C# AttachToKeyboardAsync / JS attachGlobalKeyboard: idempotent. */
20
38
  static AttachToKeyboard(): void;
39
+ /**
40
+ * The modifier state follows each key event's own flags: a modifier pressed while the page had no focus (its keydown
41
+ * went elsewhere) still counts, and one released elsewhere stops counting.
42
+ */
43
+ private static SyncModifiers;
44
+ private static RaiseKeyDown;
45
+ /** For the canvas: keyboard navigation that runs before the app's KeyDown handlers; returns true when it used the key. */
46
+ static SubscribeNavigation(handler: (key: InputKey, e: KeyboardEvent) => boolean): void;
47
+ static UnsubscribeNavigation(handler: (key: InputKey, e: KeyboardEvent) => boolean): void;
21
48
  static Subscribe(down: (key: InputKey, e: KeyboardEvent) => void, char: (ch: string, e: KeyboardEvent) => void, up?: (key: InputKey, e: KeyboardEvent) => void): void;
22
49
  static Unsubscribe(down: (key: InputKey, e: KeyboardEvent) => void, char: (ch: string, e: KeyboardEvent) => void, up?: (key: InputKey, e: KeyboardEvent) => void): void;
50
+ /**
51
+ * Hooks observe keys, they never take them from the element that owns them (C# 845b26e9, GitHub #231). True when a
52
+ * focused DOM element is the key's target: a page input, textarea, select, button, link or contenteditable, an
53
+ * accessibility overlay node, the drawn editor's hidden textarea. False when the key goes to the page itself (body,
54
+ * html) or the canvas. A handler that acts on keys (edits, moves, calls preventDefault) skips owned keys, except
55
+ * those of its own element.
56
+ */
57
+ static IsOwnedByElement(e: KeyboardEvent): boolean;
58
+ /**
59
+ * The key belongs to the input method (an IME composition, a soft keyboard): isComposing, keyCode 229, key "Process"
60
+ * or "Unidentified". Its result arrives as input events (TextInputProxy), never act on the key itself.
61
+ */
62
+ static IsInputMethodKey(e: KeyboardEvent): boolean;
23
63
  /** JS blurExternalTextInput: a page text input outside the canvas would keep receiving the keys. */
24
64
  static BlurExternalTextInput(): void;
25
65
  }
@@ -4,8 +4,21 @@ export class KeyboardManager {
4
4
  static keyDown = new Set();
5
5
  static keyUp = new Set();
6
6
  static keyChar = new Set();
7
+ static navigation = new Set();
8
+ static usedKey;
9
+ static deferred;
10
+ /**
11
+ * True while KeyDown handlers run for a key the canvas keyboard navigation already used: Enter / Space activated the
12
+ * focused control, an arrow moved inside a list or a menu or stepped a slider, Escape closed a popup window. An app
13
+ * handler that would act on the same key (a menu that opens on Enter) skips it, so one press does one thing.
14
+ */
15
+ static IsKeyHandled = false;
16
+ /** For the accessibility overlay and canvas navigation: `key` was used, the KeyDown raised next for it reports IsKeyHandled. */
17
+ static MarkKeyHandled(key) { KeyboardManager.usedKey = key; }
7
18
  static get IsShiftPressed() { return KeyboardManager.pressed.has("ShiftLeft") || KeyboardManager.pressed.has("ShiftRight"); }
8
19
  static get IsControlPressed() { return KeyboardManager.pressed.has("ControlLeft") || KeyboardManager.pressed.has("ControlRight") || KeyboardManager.pressed.has("MetaLeft") || KeyboardManager.pressed.has("MetaRight"); }
20
+ /** True while the keydown handlers run: a popup opened now was opened from the keyboard (Alt+F). */
21
+ static IsDispatchingKeyDown = false;
9
22
  static get IsAltPressed() { return KeyboardManager.pressed.has("AltLeft") || KeyboardManager.pressed.has("AltRight"); }
10
23
  /** C# AttachToKeyboardAsync / JS attachGlobalKeyboard: idempotent. */
11
24
  static AttachToKeyboard() {
@@ -15,23 +28,80 @@ export class KeyboardManager {
15
28
  // inside a cross-frame iframe the embedding page keeps DOM focus: a pointerdown pulls it to this window
16
29
  window.addEventListener("pointerdown", () => window.focus(), true);
17
30
  window.addEventListener("keydown", (e) => {
18
- const code = e.code || "";
19
- KeyboardManager.pressed.add(code);
20
- for (const h of [...KeyboardManager.keyDown])
21
- h(code, e);
22
- const proxied = e.target?.dataset?.drawnuiTextInput === "1"; // TextInputProxy: characters come through its input events
23
- if (!proxied && e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey)
24
- for (const h of [...KeyboardManager.keyChar])
25
- h(e.key, e);
31
+ KeyboardManager.SyncModifiers(e);
32
+ KeyboardManager.pressed.add(e.code || "");
33
+ // a key for an overlay node: its keydown (the canvas navigation) runs first, the key is raised when it bubbles here
34
+ if (e.target?.dataset?.a11yId) {
35
+ KeyboardManager.deferred = e;
36
+ return;
37
+ }
38
+ KeyboardManager.RaiseKeyDown(e);
26
39
  }, true);
40
+ window.addEventListener("keydown", (e) => {
41
+ if (KeyboardManager.deferred !== e)
42
+ return;
43
+ KeyboardManager.deferred = undefined;
44
+ KeyboardManager.RaiseKeyDown(e);
45
+ });
27
46
  window.addEventListener("keyup", (e) => {
28
47
  const code = e.code || "";
48
+ KeyboardManager.SyncModifiers(e);
29
49
  KeyboardManager.pressed.delete(code);
50
+ if (KeyboardManager.usedKey === code)
51
+ KeyboardManager.usedKey = undefined;
30
52
  for (const h of [...KeyboardManager.keyUp])
31
53
  h(code, e);
32
54
  }, true);
33
55
  window.addEventListener("blur", () => KeyboardManager.pressed.clear());
34
56
  }
57
+ /**
58
+ * The modifier state follows each key event's own flags: a modifier pressed while the page had no focus (its keydown
59
+ * went elsewhere) still counts, and one released elsewhere stops counting.
60
+ */
61
+ static SyncModifiers(e) {
62
+ const sync = (down, left, right) => {
63
+ const p = KeyboardManager.pressed;
64
+ if (!down) {
65
+ p.delete(left);
66
+ p.delete(right);
67
+ }
68
+ else if (!p.has(left) && !p.has(right))
69
+ p.add(left);
70
+ };
71
+ sync(!!e.altKey, "AltLeft", "AltRight");
72
+ sync(!!e.ctrlKey, "ControlLeft", "ControlRight");
73
+ sync(!!e.shiftKey, "ShiftLeft", "ShiftRight");
74
+ sync(!!e.metaKey, "MetaLeft", "MetaRight");
75
+ }
76
+ static RaiseKeyDown(e) {
77
+ const code = e.code || "";
78
+ let used = KeyboardManager.usedKey === code;
79
+ KeyboardManager.usedKey = undefined;
80
+ KeyboardManager.IsDispatchingKeyDown = true;
81
+ try {
82
+ // the canvas' own navigation (a popup window's Escape, arrows starting navigation), then the app with the result
83
+ if (!used)
84
+ for (const h of [...KeyboardManager.navigation])
85
+ if (h(code, e)) {
86
+ used = true;
87
+ break;
88
+ }
89
+ KeyboardManager.IsKeyHandled = used;
90
+ for (const h of [...KeyboardManager.keyDown])
91
+ h(code, e);
92
+ }
93
+ finally {
94
+ KeyboardManager.IsDispatchingKeyDown = false;
95
+ KeyboardManager.IsKeyHandled = false;
96
+ }
97
+ const proxied = e.target?.dataset?.drawnuiTextInput === "1"; // TextInputProxy: characters come through its input events
98
+ if (!proxied && e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey)
99
+ for (const h of [...KeyboardManager.keyChar])
100
+ h(e.key, e);
101
+ }
102
+ /** For the canvas: keyboard navigation that runs before the app's KeyDown handlers; returns true when it used the key. */
103
+ static SubscribeNavigation(handler) { KeyboardManager.AttachToKeyboard(); KeyboardManager.navigation.add(handler); }
104
+ static UnsubscribeNavigation(handler) { KeyboardManager.navigation.delete(handler); }
35
105
  static Subscribe(down, char, up) {
36
106
  KeyboardManager.AttachToKeyboard();
37
107
  KeyboardManager.keyDown.add(down);
@@ -45,6 +115,26 @@ export class KeyboardManager {
45
115
  if (up)
46
116
  KeyboardManager.keyUp.delete(up);
47
117
  }
118
+ /**
119
+ * Hooks observe keys, they never take them from the element that owns them (C# 845b26e9, GitHub #231). True when a
120
+ * focused DOM element is the key's target: a page input, textarea, select, button, link or contenteditable, an
121
+ * accessibility overlay node, the drawn editor's hidden textarea. False when the key goes to the page itself (body,
122
+ * html) or the canvas. A handler that acts on keys (edits, moves, calls preventDefault) skips owned keys, except
123
+ * those of its own element.
124
+ */
125
+ static IsOwnedByElement(e) {
126
+ const target = e.target;
127
+ const tag = target?.tagName;
128
+ // the canvas host holds the DOM focus when no drawn control has it: its keys are the app's, as the canvas' own
129
+ return !!tag && tag !== "BODY" && tag !== "HTML" && tag !== "CANVAS" && target?.dataset?.drawnuiHost !== "1";
130
+ }
131
+ /**
132
+ * The key belongs to the input method (an IME composition, a soft keyboard): isComposing, keyCode 229, key "Process"
133
+ * or "Unidentified". Its result arrives as input events (TextInputProxy), never act on the key itself.
134
+ */
135
+ static IsInputMethodKey(e) {
136
+ return e.isComposing || e.keyCode === 229 || e.key === "Process" || e.key === "Unidentified";
137
+ }
48
138
  /** JS blurExternalTextInput: a page text input outside the canvas would keep receiving the keys. */
49
139
  static BlurExternalTextInput() {
50
140
  const a = typeof document !== "undefined" ? document.activeElement : null;
@@ -0,0 +1,94 @@
1
+ import type { DrawingContext, SkiaControl } from "./SkiaControl";
2
+ import { SKPoint } from "./Gestures";
3
+ import { SKRect } from "./Types";
4
+ import { type SkiaAccessibilityManager } from "./Accessibility";
5
+ /** A popup window opened with `Canvas.OpenPopupWindow` (drawnui-cross 6at, C# PopupId, Rust PopupId). */
6
+ export type PopupId = number;
7
+ /** Where a popup window opens from, in canvas points, or a control (C# PopupAnchor, Rust PopupAnchor). */
8
+ export declare class PopupAnchor {
9
+ /** The point or the rect, in canvas points (empty for a control). */
10
+ readonly Rect: SKRect;
11
+ /** A point: the popup's top-left corner is there; where the canvas ends it opens to the left of the point, or above it. */
12
+ readonly IsPoint: boolean;
13
+ /** A control: the popup opens below the rect it is drawn at now, as for a rect. */
14
+ readonly Control?: SkiaControl | undefined;
15
+ private constructor();
16
+ /** A point (where a right click was), in canvas points. */
17
+ static AtPoint(point: SKPoint): PopupAnchor;
18
+ /** A rect (the button a dropdown belongs to), in canvas points: below it, left edges together; where the canvas ends, above it or right edges together. */
19
+ static AtRect(rect: SKRect): PopupAnchor;
20
+ /** A control: as a rect, with the rect it is drawn at now (moved, scaled, scrolled). */
21
+ static AtControl(control: SkiaControl): PopupAnchor;
22
+ /** A point, a rect or a control as an anchor. */
23
+ static From(anchor: PopupAnchor | SKPoint | SKRect | SkiaControl | {
24
+ X: number;
25
+ Y: number;
26
+ }): PopupAnchor;
27
+ /** The anchor in canvas pixels. */
28
+ Pixels(scale: number): SKRect;
29
+ }
30
+ /** How a popup window behaves; the defaults: sized to its content, closed by a press outside it, Escape or the app. */
31
+ export interface PopupWindowOptions {
32
+ /** Its size in points; unset: the size its content measures to. Never larger than the canvas. */
33
+ Size?: {
34
+ Width: number;
35
+ Height: number;
36
+ };
37
+ /** It also closes once the mouse has stayed outside it (or off the canvas) for 300 ms. */
38
+ CloseOnPointerLeave?: boolean;
39
+ /** Drawn on the canvas, inside it: always so in the browser (accepted for the same API as the other engines). */
40
+ OnCanvas?: boolean;
41
+ /** A window of its own drawn on the CPU (desktop engines); no effect in the browser. */
42
+ Cpu?: boolean;
43
+ /** Shows only, as a tooltip: takes no input, has no accessibility node, Escape and a press outside leave it open. */
44
+ Passive?: boolean;
45
+ }
46
+ /** The canvas side the layer needs. */
47
+ interface PopupHostCanvas {
48
+ Update(): void;
49
+ DisposeObject(o: {
50
+ Dispose(): void;
51
+ }): void;
52
+ RaisePopupWindowClosed(id: PopupId): void;
53
+ readonly AccessibilityManager: SkiaAccessibilityManager;
54
+ }
55
+ /**
56
+ * The popup windows of one canvas (C# PopupLayer): drawn over everything, given input first, closed by a press outside
57
+ * (the rest of that press goes nowhere, as with native menus).
58
+ */
59
+ export declare class PopupLayer {
60
+ private readonly canvas;
61
+ private readonly popups;
62
+ private nextId;
63
+ /** A press outside closed the popups: the rest of that press goes nowhere. */
64
+ SwallowPress: boolean;
65
+ constructor(canvas: PopupHostCanvas);
66
+ get HasAny(): boolean;
67
+ /** The root controls of the open popup windows, bottom to top (a new array per call). */
68
+ get Roots(): SkiaControl[];
69
+ get HasInteractive(): boolean;
70
+ /** The content of the top popup window that takes input. */
71
+ get TopInteractive(): SkiaControl | undefined;
72
+ IsOpen(id: PopupId): boolean;
73
+ Open(root: SkiaControl, anchor: PopupAnchor, options: PopupWindowOptions, superview: SkiaControl["_superview"]): PopupId;
74
+ Close(id: PopupId): void;
75
+ CloseTop(): boolean;
76
+ CloseAll(passiveToo: boolean): void;
77
+ private Remove;
78
+ private static IsInside;
79
+ /** Tab stays inside the top popup window that takes input (a menu, a dropdown) while it is open. */
80
+ private UpdateKeyboardScope;
81
+ /** The top popup window taking input whose content is under the point (canvas pixels). */
82
+ At(x: number, y: number): SkiaControl | undefined;
83
+ /** The pointer is at (x, y) (undefined: off the canvas): popups closing on pointer leave close after 300 ms outside. */
84
+ PointerAt(x: number | undefined, y: number | undefined): void;
85
+ /**
86
+ * Where a popup of `w` x `h` goes for `anchor` (pixels) inside `bounds`: at the anchor's corner, flipped to its other side
87
+ * where it does not fit, then kept inside (C# PopupLayer.Place, Rust place_popup).
88
+ */
89
+ static Place(anchor: SKRect, point: boolean, w: number, h: number, bounds: SKRect): SKRect;
90
+ /** Measures, places and draws every popup over the content, inside `bounds` (canvas pixels). */
91
+ Draw(ctx: DrawingContext, bounds: SKRect): void;
92
+ Dispose(): void;
93
+ }
94
+ export {};
@@ -0,0 +1,207 @@
1
+ import { SKPoint } from "./Gestures";
2
+ import { SKRect } from "./Types";
3
+ import { KeyboardManager } from "./KeyboardManager";
4
+ import { Aria } from "./Accessibility";
5
+ /** Where a popup window opens from, in canvas points, or a control (C# PopupAnchor, Rust PopupAnchor). */
6
+ export class PopupAnchor {
7
+ Rect;
8
+ IsPoint;
9
+ Control;
10
+ constructor(
11
+ /** The point or the rect, in canvas points (empty for a control). */
12
+ Rect,
13
+ /** A point: the popup's top-left corner is there; where the canvas ends it opens to the left of the point, or above it. */
14
+ IsPoint,
15
+ /** A control: the popup opens below the rect it is drawn at now, as for a rect. */
16
+ Control) {
17
+ this.Rect = Rect;
18
+ this.IsPoint = IsPoint;
19
+ this.Control = Control;
20
+ }
21
+ /** A point (where a right click was), in canvas points. */
22
+ static AtPoint(point) { return new PopupAnchor(new SKRect(point.X, point.Y, point.X, point.Y), true); }
23
+ /** A rect (the button a dropdown belongs to), in canvas points: below it, left edges together; where the canvas ends, above it or right edges together. */
24
+ static AtRect(rect) { return new PopupAnchor(rect, false); }
25
+ /** A control: as a rect, with the rect it is drawn at now (moved, scaled, scrolled). */
26
+ static AtControl(control) { return new PopupAnchor(SKRect.Empty, false, control); }
27
+ /** A point, a rect or a control as an anchor. */
28
+ static From(anchor) {
29
+ if (anchor instanceof PopupAnchor)
30
+ return anchor;
31
+ if (typeof anchor.Measure === "function")
32
+ return PopupAnchor.AtControl(anchor);
33
+ if ("Right" in anchor && "Bottom" in anchor) {
34
+ const r = anchor;
35
+ return PopupAnchor.AtRect(new SKRect(r.Left, r.Top, r.Right, r.Bottom));
36
+ }
37
+ const p = anchor;
38
+ return PopupAnchor.AtPoint(new SKPoint(p.X, p.Y));
39
+ }
40
+ /** The anchor in canvas pixels. */
41
+ Pixels(scale) {
42
+ if (this.Control) {
43
+ const r = this.Control.GetAccessibilityPixelRect(); // on screen now: through caches and scroll offsets
44
+ return r.Width > 0 || r.Height > 0 ? r : this.Control.DrawingRect;
45
+ }
46
+ const r = this.Rect;
47
+ return new SKRect(r.Left * scale, r.Top * scale, r.Right * scale, r.Bottom * scale);
48
+ }
49
+ }
50
+ /**
51
+ * The popup windows of one canvas (C# PopupLayer): drawn over everything, given input first, closed by a press outside
52
+ * (the rest of that press goes nowhere, as with native menus).
53
+ */
54
+ export class PopupLayer {
55
+ canvas;
56
+ popups = [];
57
+ nextId = 0;
58
+ /** A press outside closed the popups: the rest of that press goes nowhere. */
59
+ SwallowPress = false;
60
+ constructor(canvas) {
61
+ this.canvas = canvas;
62
+ }
63
+ get HasAny() { return this.popups.length > 0; }
64
+ /** The root controls of the open popup windows, bottom to top (a new array per call). */
65
+ get Roots() { return this.popups.map((p) => p.Root); }
66
+ get HasInteractive() { return this.popups.some((p) => !p.Options.Passive); }
67
+ /** The content of the top popup window that takes input. */
68
+ get TopInteractive() { for (let i = this.popups.length - 1; i >= 0; i--)
69
+ if (!this.popups[i].Options.Passive)
70
+ return this.popups[i].Root; return undefined; }
71
+ IsOpen(id) { return this.popups.some((p) => p.Id === id); }
72
+ Open(root, anchor, options, superview) {
73
+ root.HorizontalOptions = "Start";
74
+ root.VerticalOptions = "Start";
75
+ if (options.Size) {
76
+ root.WidthRequest = options.Size.Width;
77
+ root.HeightRequest = options.Size.Height;
78
+ }
79
+ if (options.Passive) {
80
+ root.InputTransparent = true;
81
+ root.AccessibilityRole ??= Aria.RolePresentation;
82
+ }
83
+ root.Parent = undefined;
84
+ root._superview = superview;
85
+ const a11y = this.canvas.AccessibilityManager;
86
+ const popup = { Id: ++this.nextId, Root: root, Anchor: anchor, Options: options, Rect: SKRect.Empty, LeftAt: 0 };
87
+ // opened from the keyboard (Enter on a focused control, a key handler such as Alt+F): it takes the keyboard (6ao) and
88
+ // gives it back when it closes, to the control it opens from, else to where the keyboard was (unless that is inside a
89
+ // popup closing now: Left / Right in a menu bar). A popup opened with the mouse leaves the keyboard alone.
90
+ if (!options.Passive && (a11y.FocusedNode || KeyboardManager.IsDispatchingKeyDown)) {
91
+ const keyboard = a11y.FocusedNode;
92
+ popup.Opener = anchor.Control ?? (keyboard && !this.popups.some((p) => PopupLayer.IsInside(keyboard, p.Root)) ? keyboard : undefined);
93
+ a11y.FocusOpenedPopup(root);
94
+ }
95
+ this.popups.push(popup);
96
+ this.UpdateKeyboardScope();
97
+ // its nodes join the accessibility tree with its first frame (a Tab right after it opened goes into it)
98
+ a11y.ForceRebuildOnNextFrame();
99
+ return popup.Id;
100
+ }
101
+ Close(id) { const p = this.popups.find((x) => x.Id === id); if (p)
102
+ this.Remove(p); }
103
+ CloseTop() {
104
+ for (let i = this.popups.length - 1; i >= 0; i--)
105
+ if (!this.popups[i].Options.Passive) {
106
+ this.Remove(this.popups[i]);
107
+ return true;
108
+ }
109
+ return false;
110
+ }
111
+ CloseAll(passiveToo) {
112
+ for (const p of [...this.popups])
113
+ if (passiveToo || !p.Options.Passive)
114
+ this.Remove(p);
115
+ }
116
+ Remove(popup) {
117
+ const i = this.popups.indexOf(popup);
118
+ if (i < 0)
119
+ return;
120
+ this.popups.splice(i, 1);
121
+ // its nodes leave the accessibility tree with this frame, not when the disposal runs after it
122
+ const a11y = this.canvas.AccessibilityManager;
123
+ const focused = a11y.FocusedNode;
124
+ // the keyboard was in it (or nowhere): it goes back to the control the popup opened from, as native menus do
125
+ if (popup.Opener && (!focused || PopupLayer.IsInside(focused, popup.Root)))
126
+ a11y.RequestKeyboardFocus(popup.Opener);
127
+ this.UpdateKeyboardScope();
128
+ a11y.UnregisterSubtree(popup.Root);
129
+ a11y.ForceRebuildOnNextFrame();
130
+ this.canvas.DisposeObject(popup.Root);
131
+ this.canvas.Update();
132
+ this.canvas.RaisePopupWindowClosed(popup.Id);
133
+ }
134
+ static IsInside(control, root) {
135
+ for (let c = control; c; c = c.Parent)
136
+ if (c === root)
137
+ return true;
138
+ return false;
139
+ }
140
+ /** Tab stays inside the top popup window that takes input (a menu, a dropdown) while it is open. */
141
+ UpdateKeyboardScope() {
142
+ let top;
143
+ for (let i = this.popups.length - 1; i >= 0 && !top; i--)
144
+ if (!this.popups[i].Options.Passive)
145
+ top = this.popups[i].Root;
146
+ this.canvas.AccessibilityManager.KeyboardScope = top;
147
+ }
148
+ /** The top popup window taking input whose content is under the point (canvas pixels). */
149
+ At(x, y) {
150
+ for (let i = this.popups.length - 1; i >= 0; i--) {
151
+ const p = this.popups[i];
152
+ if (!p.Options.Passive && p.Root.IsVisible && p.Root.HitIsInside(x, y))
153
+ return p.Root;
154
+ }
155
+ return undefined;
156
+ }
157
+ /** The pointer is at (x, y) (undefined: off the canvas): popups closing on pointer leave close after 300 ms outside. */
158
+ PointerAt(x, y) {
159
+ const now = performance.now();
160
+ for (const p of [...this.popups]) {
161
+ if (!p.Options.CloseOnPointerLeave)
162
+ continue;
163
+ const inside = x !== undefined && y !== undefined && p.Root.HitIsInside(x, y);
164
+ if (inside) {
165
+ p.LeftAt = 0;
166
+ continue;
167
+ }
168
+ if (!p.LeftAt) {
169
+ p.LeftAt = now;
170
+ window.setTimeout(() => { if (p.LeftAt && performance.now() - p.LeftAt >= 300)
171
+ this.Remove(p); }, 310);
172
+ }
173
+ }
174
+ }
175
+ /**
176
+ * Where a popup of `w` x `h` goes for `anchor` (pixels) inside `bounds`: at the anchor's corner, flipped to its other side
177
+ * where it does not fit, then kept inside (C# PopupLayer.Place, Rust place_popup).
178
+ */
179
+ static Place(anchor, point, w, h, bounds) {
180
+ w = Math.max(0, Math.min(w, bounds.Width));
181
+ h = Math.max(0, Math.min(h, bounds.Height));
182
+ let left = anchor.Left;
183
+ if (left + w > bounds.Right)
184
+ left = point ? anchor.Left - w : anchor.Right - w;
185
+ let top = anchor.Bottom;
186
+ if (top + h > bounds.Bottom)
187
+ top = anchor.Top - h;
188
+ left = Math.round(Math.min(Math.max(left, bounds.Left), bounds.Right - w));
189
+ top = Math.round(Math.min(Math.max(top, bounds.Top), bounds.Bottom - h));
190
+ return new SKRect(left, top, left + Math.round(w), top + Math.round(h));
191
+ }
192
+ /** Measures, places and draws every popup over the content, inside `bounds` (canvas pixels). */
193
+ Draw(ctx, bounds) {
194
+ const scale = ctx.Scale;
195
+ for (const p of this.popups) {
196
+ const root = p.Root;
197
+ const size = root.Measure(bounds.Width, bounds.Height, scale).Pixels;
198
+ if (size.Width <= 0 || size.Height <= 0)
199
+ continue;
200
+ p.Rect = PopupLayer.Place(p.Anchor.Pixels(scale), p.Anchor.IsPoint, size.Width, size.Height, bounds);
201
+ root.Arrange(p.Rect, root.WidthRequest, root.HeightRequest, scale);
202
+ root.Render({ Context: ctx.Context, Destination: p.Rect, Scale: scale });
203
+ }
204
+ }
205
+ Dispose() { for (const p of this.popups.splice(0))
206
+ p.Root.Dispose(); }
207
+ }