drawnui-react 0.1.0-preview.35 → 0.1.0-preview.36

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 (40) hide show
  1. package/SKIPPED.md +2 -0
  2. package/dist/controls/SkiaButton.js +2 -2
  3. package/dist/controls/SkiaCheckbox.js +2 -1
  4. package/dist/controls/SkiaEditor.d.ts +4 -0
  5. package/dist/controls/SkiaEditor.js +16 -4
  6. package/dist/controls/SkiaLabel.d.ts +2 -0
  7. package/dist/controls/SkiaLabel.js +2 -0
  8. package/dist/controls/SkiaLayout.d.ts +2 -0
  9. package/dist/controls/SkiaLayout.js +2 -0
  10. package/dist/controls/SkiaProgress.js +2 -2
  11. package/dist/controls/SkiaRadioButton.js +2 -1
  12. package/dist/controls/SkiaSlider.js +2 -2
  13. package/dist/controls/SkiaSwitch.js +4 -2
  14. package/dist/controls/TooltipStyle.d.ts +52 -0
  15. package/dist/controls/TooltipStyle.js +88 -0
  16. package/dist/core/Accessibility.d.ts +18 -0
  17. package/dist/core/Accessibility.js +75 -3
  18. package/dist/core/Canvas.d.ts +60 -3
  19. package/dist/core/Canvas.js +185 -5
  20. package/dist/core/ControlStyle.d.ts +2 -0
  21. package/dist/core/ControlStyle.js +2 -0
  22. package/dist/core/Hover.d.ts +4 -0
  23. package/dist/core/Hover.js +6 -0
  24. package/dist/core/KeyboardManager.d.ts +7 -0
  25. package/dist/core/KeyboardManager.js +31 -2
  26. package/dist/core/PopupWindows.d.ts +92 -0
  27. package/dist/core/PopupWindows.js +203 -0
  28. package/dist/core/SkiaControl.d.ts +20 -0
  29. package/dist/core/SkiaControl.js +33 -0
  30. package/dist/core/Super.d.ts +7 -0
  31. package/dist/core/Super.js +7 -0
  32. package/dist/core/Tooltips.d.ts +69 -0
  33. package/dist/core/Tooltips.js +195 -0
  34. package/dist/index.d.ts +3 -0
  35. package/dist/index.js +3 -0
  36. package/dist/react/index.d.ts +13 -0
  37. package/dist/react/index.js +32 -0
  38. package/dist/react/reconciler.d.ts +2 -0
  39. package/dist/react/reconciler.js +5 -0
  40. package/package.json +2 -1
@@ -1,6 +1,9 @@
1
1
  import { Super } from "./Super";
2
2
  import { SkiaAccessibilityManager } from "./Accessibility";
3
3
  import { HoverManager } from "./Hover";
4
+ import { TooltipController } from "./Tooltips";
5
+ import { PopupAnchor, PopupLayer } from "./PopupWindows";
6
+ import { KeyboardManager } from "./KeyboardManager";
4
7
  import { Colors, SKRect } from "./Types";
5
8
  import { ContextMenuEventArgs, GestureEventProcessingInfo, PointerData, SKPoint, SkiaGesturesParameters, TouchActionEventArgs, } from "./Gestures";
6
9
  /**
@@ -43,6 +46,69 @@ export class Canvas {
43
46
  AccessibilityManager = new SkiaAccessibilityManager();
44
47
  /** Mouse hover of the controls (drawnui-cross 6m): ReceivesHover / IsHovered / HoverChanged. */
45
48
  Hover = new HoverManager();
49
+ // ---- tooltips (drawnui-cross 6ar) ----
50
+ tooltipController;
51
+ /** Made on first use: a canvas that never shows a tooltip allocates nothing for it. */
52
+ get tooltip() { return this.tooltipController ??= new TooltipController(this); }
53
+ /** Look and timing of the tooltips on this canvas; unset uses `Super.TooltipStyle`. */
54
+ TooltipStyle;
55
+ get TooltipStyleUsed() { return this.TooltipStyle ?? Super.TooltipStyle; }
56
+ /** The text of the tooltip shown now, undefined when none is shown. */
57
+ get ShownTooltip() { return this.tooltip.ShownText; }
58
+ // ---- popup windows (drawnui-cross 6at, the DrawnUi.Rust / .NET API) ----
59
+ popupLayer;
60
+ /** Made on first use, like the tooltip. */
61
+ get popups() { return this.popupLayer ??= new PopupLayer(this); }
62
+ popupKeysHooked = false;
63
+ /** Raised when a popup window closed: picked, a press outside, Escape, the wheel, window blur or ClosePopupWindow. */
64
+ PopupWindowClosed = new Set();
65
+ /**
66
+ * Shows `content` over everything, opening from `anchor` the way native menus do: at a point (a right click, canvas
67
+ * points), or below a rect or a control (a dropdown), flipped to the other side where it does not fit. It closes with
68
+ * a press outside it, Escape, the wheel outside it, window blur, `ClosePopupWindow` and the options.
69
+ */
70
+ OpenPopupWindow(content, anchor, options = {}) {
71
+ const id = this.popups.Open(content, PopupAnchor.From(anchor), options, this);
72
+ this.HookPopupKeys(true);
73
+ if (!options.Passive)
74
+ this.tooltip.Reset(); // a menu opening closes the tooltip
75
+ this.Hover.RequestCheck();
76
+ this.Update();
77
+ return id;
78
+ }
79
+ /** Closes the popup window (nothing when it is closed already). */
80
+ ClosePopupWindow(id) { this.popups.Close(id); this.AfterPopupsChanged(); }
81
+ /** Closes every popup window. */
82
+ ClosePopupWindows() { this.popups.CloseAll(true); this.AfterPopupsChanged(); }
83
+ /** The popup window is still open. */
84
+ IsPopupWindowOpen(id) { return this.popups.IsOpen(id); }
85
+ /** True while a popup window that takes input is open. */
86
+ get HasPopupWindows() { return this.popups.HasInteractive; }
87
+ /** The content of the top popup window that takes input. */
88
+ get TopPopupWindow() { return this.popups.TopInteractive; }
89
+ /** The window stopped being active (blur): popup windows and the tooltip close, as native menus do. */
90
+ OnWindowDeactivated() { this.ClosePopupWindows(); this.tooltip.CloseOnInput(); }
91
+ RaisePopupWindowClosed(id) { for (const h of [...this.PopupWindowClosed])
92
+ h(id); }
93
+ AfterPopupsChanged() {
94
+ if (!this.popups.HasAny)
95
+ this.HookPopupKeys(false);
96
+ this.Hover.RequestCheck();
97
+ this.Update();
98
+ }
99
+ onPopupKey = (key) => { if (key === "Escape" && this.popups.CloseTop())
100
+ this.AfterPopupsChanged(); };
101
+ onPopupChar = () => { };
102
+ HookPopupKeys(on) {
103
+ if (on === this.popupKeysHooked)
104
+ return;
105
+ this.popupKeysHooked = on;
106
+ if (on)
107
+ KeyboardManager.Subscribe(this.onPopupKey, this.onPopupChar);
108
+ else
109
+ KeyboardManager.Unsubscribe(this.onPopupKey, this.onPopupChar);
110
+ }
111
+ onWindowBlur = () => this.OnWindowDeactivated();
46
112
  content;
47
113
  get Content() { return this.content; }
48
114
  set Content(value) {
@@ -78,6 +144,7 @@ export class Canvas {
78
144
  init?.(this);
79
145
  Element.addEventListener("webglcontextlost", this.onContextLost);
80
146
  Element.addEventListener("webglcontextrestored", this.onContextRestored);
147
+ this.Hover.RootAt = (x, y) => this.popupLayer?.At(x, y) ?? this.content;
81
148
  this.observer = new ResizeObserver(() => this.OnResized());
82
149
  this.observer.observe(Element);
83
150
  this.OnResized();
@@ -209,8 +276,15 @@ export class Canvas {
209
276
  root.Arrange(new SKRect(0, 0, w, h), root.WidthRequest, root.HeightRequest, scale);
210
277
  root.Render({ Context: { Canvas: canvas, Surface: this.surface }, Destination: new SKRect(0, 0, w, h), Scale: scale });
211
278
  }
279
+ // popup windows, then the tooltip, over everything
280
+ const top = { Context: { Canvas: canvas, Surface: this.surface }, Destination: new SKRect(0, 0, this.Element.width, this.Element.height), Scale: this.RenderingScale };
281
+ if (this.popupLayer?.HasAny)
282
+ this.popupLayer.Draw(top, top.Destination);
283
+ this.tooltipController?.Draw(top, top.Destination);
212
284
  this.AccessibilityManager.OnFrameEnd(this.RenderingScale, this.Element.width, this.Element.height, () => this.Update());
213
285
  this.Hover.AfterFrame(this.content, this.activeTouchIds.size > 0);
286
+ if (this.Hover.Paused)
287
+ this.tooltipController?.Reset(); // content moves under the pointer
214
288
  const now = performance.now();
215
289
  this.FrameTime = now - started;
216
290
  this.FrameIndex++;
@@ -357,6 +431,9 @@ export class Canvas {
357
431
  this.Element.removeEventListener("webglcontextlost", this.onContextLost);
358
432
  this.Element.removeEventListener("webglcontextrestored", this.onContextRestored);
359
433
  this.Gestures = "Disabled";
434
+ this.tooltipController?.Dispose();
435
+ this.popupLayer?.Dispose();
436
+ this.HookPopupKeys(false);
360
437
  this.observer.disconnect();
361
438
  this.content?.Dispose();
362
439
  this.Content = undefined;
@@ -394,8 +471,13 @@ export class Canvas {
394
471
  if (type === "Moved" && !this.activeTouchIds.has(e.pointerId)) {
395
472
  if (e.pointerType === "touch")
396
473
  return; // touch never hovers
397
- if (this.activeTouchIds.size === 0)
398
- this.Hover.Move(this.content, e.offsetX * this.RenderingScale, e.offsetY * this.RenderingScale);
474
+ const hx = e.offsetX * this.RenderingScale, hy = e.offsetY * this.RenderingScale;
475
+ if (this.activeTouchIds.size === 0) {
476
+ this.Hover.Move(this.content, hx, hy);
477
+ this.tooltip.OnPointerMove(this.Hover.Path, hx, hy);
478
+ }
479
+ if (this.popups.HasAny)
480
+ this.popups.PointerAt(hx, hy);
399
481
  if (e.pointerType === "mouse")
400
482
  this.UpdateCursor();
401
483
  return;
@@ -403,6 +485,8 @@ export class Canvas {
403
485
  if ((type === "Released" || type === "Cancelled") && !this.activeTouchIds.has(e.pointerId))
404
486
  return; // Up of a pointer that never pressed here
405
487
  // Capture so Up outside the element still arrives; throws for synthetic events (tests) — harmless.
488
+ if (type === "Pressed")
489
+ this.tooltip.CloseOnInput();
406
490
  if (type === "Pressed") {
407
491
  try {
408
492
  this.Element.setPointerCapture(e.pointerId);
@@ -430,8 +514,14 @@ export class Canvas {
430
514
  }
431
515
  };
432
516
  /** The mouse left the canvas: hover ends at once, also while content moves. */
433
- onPointerLeave = (e) => { if (e.pointerType !== "touch")
434
- this.Hover.Leave(); };
517
+ onPointerLeave = (e) => {
518
+ if (e.pointerType === "touch")
519
+ return;
520
+ this.Hover.Leave();
521
+ this.tooltip.Reset();
522
+ if (this.popups.HasAny)
523
+ this.popups.PointerAt(undefined, undefined);
524
+ };
435
525
  preventTouch = (e) => e.preventDefault();
436
526
  /** Gestures="Enabled": touch / pen pointers whose Down a control used (see OnTouchAction). */
437
527
  claimedTouches = new Set();
@@ -448,6 +538,19 @@ export class Canvas {
448
538
  const pointerType = e.pointerType;
449
539
  const source = pointerType === "touch" || pointerType === "pen" ? "touch" : pointerType === "mouse" || e.button === 2 ? "mouse" : "keyboard";
450
540
  const args = new ContextMenuEventArgs(new SKPoint(x, y), new SKPoint(x * scale, y * scale), source, e);
541
+ this.tooltip.CloseOnInput();
542
+ // over a popup window its content is asked; elsewhere the popups close and the request goes on, so a new menu can
543
+ // open there (C# PopupLayer ContextMenu)
544
+ const popupRoot = this.popups.HasInteractive ? this.popups.At(args.Pixels.X, args.Pixels.Y) : undefined;
545
+ if (popupRoot) {
546
+ popupRoot.ProcessContextMenu(args.Pixels, args);
547
+ e.preventDefault();
548
+ return;
549
+ }
550
+ if (this.popups.HasInteractive) {
551
+ this.popups.CloseAll(false);
552
+ this.AfterPopupsChanged();
553
+ }
451
554
  let handled = this.content?.ProcessContextMenu(args.Pixels, args) ?? false;
452
555
  if (!handled && this.ContextMenu)
453
556
  handled = this.ContextMenu(this, args) === true;
@@ -496,6 +599,7 @@ export class Canvas {
496
599
  const factor = e.deltaMode === 1 ? 40 : e.deltaMode === 2 ? 800 : 1;
497
600
  const horizontal = Math.abs(e.deltaX) > Math.abs(e.deltaY);
498
601
  args.Wheel = { Delta: (horizontal ? e.deltaX : e.deltaY) * factor, IsHorizontal: horizontal };
602
+ this.tooltip.CloseOnInput();
499
603
  if (this.gestures === "Lock") {
500
604
  e.preventDefault();
501
605
  this.OnGestureEvent(args, "Wheel");
@@ -573,6 +677,7 @@ export class Canvas {
573
677
  el.addEventListener("pointerleave", this.onPointerLeave);
574
678
  el.addEventListener("contextmenu", this.onContextMenu);
575
679
  el.addEventListener("wheel", this.onWheel, { passive: false });
680
+ window.addEventListener("blur", this.onWindowBlur);
576
681
  if (this.gestures === "Lock") {
577
682
  el.addEventListener("touchmove", this.preventTouch, { passive: false });
578
683
  // the canvas owns every touch (DrawnUi.Web applyGestureStyle, DrawnUi.Rust host): no iOS callout or text
@@ -601,6 +706,7 @@ export class Canvas {
601
706
  el.removeEventListener("pointerleave", this.onPointerLeave);
602
707
  el.removeEventListener("contextmenu", this.onContextMenu);
603
708
  el.removeEventListener("wheel", this.onWheel);
709
+ window.removeEventListener("blur", this.onWindowBlur);
604
710
  el.removeEventListener("touchmove", this.preventTouch);
605
711
  el.removeEventListener("touchmove", this.preventClaimedTouch);
606
712
  if (this.pageOverscroll) {
@@ -721,9 +827,66 @@ export class Canvas {
721
827
  static IsSavedGesture(type) {
722
828
  return type === "Panning" || type === "Wheel" || type === "Up";
723
829
  }
724
- /** Entry into the control tree, same shape as DrawnUi Canvas.ProcessGestures. */
830
+ // ---- focus (C# DrawnView FocusedChild / ReportFocus, drawnui-cross 6ap) ----
831
+ focusedChild;
832
+ focusAtDown;
833
+ tapNoFocus = false;
834
+ reportingFocus = false;
835
+ /** The control that has the canvas focus (an editor while it edits, a CanBeFocused control that was tapped). */
836
+ get FocusedChild() { return this.focusedChild; }
837
+ /**
838
+ * Moves the focus. The new control is asked first and the focus moves only when it takes it (a tap on a button leaves an
839
+ * editor focused); undefined clears it. Nothing moves while the focused control has LockFocus. `setter` is a control
840
+ * that focused itself already (an editor on its press) or the focused control letting go itself (Escape, submit).
841
+ */
842
+ ReportFocus(value, setter) {
843
+ if (value === this.focusedChild || this.reportingFocus)
844
+ return;
845
+ if (this.focusedChild?.LockFocus && setter !== this.focusedChild)
846
+ return;
847
+ this.reportingFocus = true;
848
+ try {
849
+ if (value && value !== setter && !value.SetFrameworkFocus(true))
850
+ return;
851
+ const previous = this.focusedChild;
852
+ this.focusedChild = value;
853
+ if (previous && !previous.IsDisposed)
854
+ previous.SetFrameworkFocus(false);
855
+ }
856
+ finally {
857
+ this.reportingFocus = false;
858
+ }
859
+ }
860
+ /** Entry into the control tree; on a completed tap the focus follows it (C# Canvas: focus only on Tapped). */
725
861
  ProcessGestures(root, args) {
862
+ if (args.Type === "Down") {
863
+ this.focusAtDown = this.focusedChild;
864
+ this.tapNoFocus = false;
865
+ }
866
+ const consumed = this.RouteGestures(root, args);
867
+ // a control that claimed the focus during this gesture (an editor on its press) keeps it; a press that closed the
868
+ // popup windows moves nothing
869
+ if (args.Type === "Tapped" && !this.tapNoFocus && this.focusedChild === this.focusAtDown)
870
+ this.ReportFocus(consumed ?? undefined);
871
+ return consumed;
872
+ }
873
+ /** Same shape as DrawnUi Canvas.ProcessGestures. */
874
+ RouteGestures(root, args) {
726
875
  const info = () => new GestureEventProcessingInfo(args.Event.Location, SKPoint.Empty, SKPoint.Empty, null);
876
+ const popups = this.popups, at = args.Event.Location;
877
+ // the rest of a press that closed the popup windows goes nowhere (native menus)
878
+ if (args.Type === "Down")
879
+ popups.SwallowPress = false;
880
+ if (popups.SwallowPress) {
881
+ if (args.Type === "Up")
882
+ popups.SwallowPress = false;
883
+ return null;
884
+ }
885
+ // the wheel outside the popup windows closes them (what they opened from is about to move), before any owner takes it
886
+ if (args.Type === "Wheel" && popups.HasInteractive && !popups.At(at.X, at.Y)) {
887
+ popups.CloseAll(false);
888
+ this.AfterPopupsChanged();
889
+ }
727
890
  // the wheel goes to the press owner only while a press is held; between presses it goes by position, and it
728
891
  // never makes an owner: a wheel over a second scroll moves that one, not the last one the wheel moved
729
892
  // (DrawnUi.Rust input_router "the wheel goes to the owner of the press first"; C# keeps the last wheel consumer)
@@ -745,6 +908,23 @@ export class Canvas {
745
908
  if (!wheel)
746
909
  this.gestureOwner = null;
747
910
  }
911
+ // over a popup window its controls take the gesture and nothing below sees it; a press elsewhere closes them
912
+ if (popups.HasInteractive) {
913
+ const popupRoot = popups.At(at.X, at.Y);
914
+ if (popupRoot) {
915
+ const used = popupRoot.ProcessGestures(args, info()) ?? popupRoot;
916
+ if (!wheel)
917
+ this.gestureOwner = args.Type !== "Up" ? used : null;
918
+ return used;
919
+ }
920
+ if (args.Type === "Down") {
921
+ this.tapNoFocus = true;
922
+ popups.CloseAll(false);
923
+ popups.SwallowPress = true;
924
+ this.AfterPopupsChanged();
925
+ return null;
926
+ }
927
+ }
748
928
  const consumed = root.ProcessGestures(args, info());
749
929
  if (!wheel)
750
930
  this.gestureOwner = consumed && args.Type !== "Up" ? consumed : null;
@@ -1,5 +1,7 @@
1
1
  /** DrawnUi PrebuiltControlStyle: the look a styled control builds its default content with. */
2
2
  export type PrebuiltControlStyle = "Unset" | "Platform" | "Cupertino" | "Material" | "Material3" | "Windows";
3
+ /** Accent of the default look (controls without a ControlStyle): blue #3B82F6, the DrawnUI Designer's and Agent Signals' accent (C# DefaultStyle.Accent). */
4
+ export declare const DefaultAccent = "#3B82F6";
3
5
  /** Resolved look: `Platform` picks from the browser's OS (C# picks from the running platform). */
4
6
  export type ResolvedControlStyle = Exclude<PrebuiltControlStyle, "Platform">;
5
7
  /** C# UsingControlStyle: Platform -> iOS/macOS = Cupertino, Android = Material, Windows = Windows, else the DrawnUi default look. */
@@ -1,3 +1,5 @@
1
+ /** Accent of the default look (controls without a ControlStyle): blue #3B82F6, the DrawnUI Designer's and Agent Signals' accent (C# DefaultStyle.Accent). */
2
+ export const DefaultAccent = "#3B82F6";
1
3
  let platformStyle;
2
4
  /** C# UsingControlStyle: Platform -> iOS/macOS = Cupertino, Android = Material, Windows = Windows, else the DrawnUi default look. */
3
5
  export function ResolveControlStyle(style) {
@@ -23,6 +23,10 @@ export declare class HoverManager {
23
23
  private y;
24
24
  private paused;
25
25
  private check;
26
+ /** Picks the root under a point (canvas pixels): a popup window over the content, else the content. */
27
+ RootAt?: (x: number, y: number) => SkiaControl | undefined;
28
+ /** Content moves under the pointer (a scroll glide, a carousel transition): hover waits. */
29
+ get Paused(): boolean;
26
30
  /**
27
31
  * The mouse (or a pen in range) moved to (x, y), canvas pixels, nothing pressed. The hit path is taken also while
28
32
  * hover waits (the cursor follows it), hover changes only when nothing moves.
@@ -23,6 +23,10 @@ export class HoverManager {
23
23
  y = 0;
24
24
  paused = false;
25
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; }
26
30
  /**
27
31
  * The mouse (or a pen in range) moved to (x, y), canvas pixels, nothing pressed. The hit path is taken also while
28
32
  * hover waits (the cursor follows it), hover changes only when nothing moves.
@@ -43,6 +47,8 @@ export class HoverManager {
43
47
  path.length = 0;
44
48
  this.PathX.length = 0;
45
49
  this.PathY.length = 0;
50
+ if (this.RootAt)
51
+ root = this.RootAt(this.x, this.y);
46
52
  if (root && root.IsVisible && !root.InputTransparent) {
47
53
  const point = root.TransformPointToLocalSpace(new SKPoint(this.x, this.y));
48
54
  if (root.HitIsInside(point.X, point.Y))
@@ -15,9 +15,16 @@ export declare class KeyboardManager {
15
15
  private static readonly keyChar;
16
16
  static get IsShiftPressed(): boolean;
17
17
  static get IsControlPressed(): boolean;
18
+ /** True while the keydown handlers run: a popup opened now was opened from the keyboard (Alt+F). */
19
+ static IsDispatchingKeyDown: boolean;
18
20
  static get IsAltPressed(): boolean;
19
21
  /** C# AttachToKeyboardAsync / JS attachGlobalKeyboard: idempotent. */
20
22
  static AttachToKeyboard(): void;
23
+ /**
24
+ * The modifier state follows each key event's own flags: a modifier pressed while the page had no focus (its keydown
25
+ * went elsewhere) still counts, and one released elsewhere stops counting.
26
+ */
27
+ private static SyncModifiers;
21
28
  static Subscribe(down: (key: InputKey, e: KeyboardEvent) => void, char: (ch: string, e: KeyboardEvent) => void, up?: (key: InputKey, e: KeyboardEvent) => void): void;
22
29
  static Unsubscribe(down: (key: InputKey, e: KeyboardEvent) => void, char: (ch: string, e: KeyboardEvent) => void, up?: (key: InputKey, e: KeyboardEvent) => void): void;
23
30
  /**
@@ -6,6 +6,8 @@ export class KeyboardManager {
6
6
  static keyChar = new Set();
7
7
  static get IsShiftPressed() { return KeyboardManager.pressed.has("ShiftLeft") || KeyboardManager.pressed.has("ShiftRight"); }
8
8
  static get IsControlPressed() { return KeyboardManager.pressed.has("ControlLeft") || KeyboardManager.pressed.has("ControlRight") || KeyboardManager.pressed.has("MetaLeft") || KeyboardManager.pressed.has("MetaRight"); }
9
+ /** True while the keydown handlers run: a popup opened now was opened from the keyboard (Alt+F). */
10
+ static IsDispatchingKeyDown = false;
9
11
  static get IsAltPressed() { return KeyboardManager.pressed.has("AltLeft") || KeyboardManager.pressed.has("AltRight"); }
10
12
  /** C# AttachToKeyboardAsync / JS attachGlobalKeyboard: idempotent. */
11
13
  static AttachToKeyboard() {
@@ -16,9 +18,16 @@ export class KeyboardManager {
16
18
  window.addEventListener("pointerdown", () => window.focus(), true);
17
19
  window.addEventListener("keydown", (e) => {
18
20
  const code = e.code || "";
21
+ KeyboardManager.SyncModifiers(e);
19
22
  KeyboardManager.pressed.add(code);
20
- for (const h of [...KeyboardManager.keyDown])
21
- h(code, e);
23
+ KeyboardManager.IsDispatchingKeyDown = true;
24
+ try {
25
+ for (const h of [...KeyboardManager.keyDown])
26
+ h(code, e);
27
+ }
28
+ finally {
29
+ KeyboardManager.IsDispatchingKeyDown = false;
30
+ }
22
31
  const proxied = e.target?.dataset?.drawnuiTextInput === "1"; // TextInputProxy: characters come through its input events
23
32
  if (!proxied && e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey)
24
33
  for (const h of [...KeyboardManager.keyChar])
@@ -26,12 +35,32 @@ export class KeyboardManager {
26
35
  }, true);
27
36
  window.addEventListener("keyup", (e) => {
28
37
  const code = e.code || "";
38
+ KeyboardManager.SyncModifiers(e);
29
39
  KeyboardManager.pressed.delete(code);
30
40
  for (const h of [...KeyboardManager.keyUp])
31
41
  h(code, e);
32
42
  }, true);
33
43
  window.addEventListener("blur", () => KeyboardManager.pressed.clear());
34
44
  }
45
+ /**
46
+ * The modifier state follows each key event's own flags: a modifier pressed while the page had no focus (its keydown
47
+ * went elsewhere) still counts, and one released elsewhere stops counting.
48
+ */
49
+ static SyncModifiers(e) {
50
+ const sync = (down, left, right) => {
51
+ const p = KeyboardManager.pressed;
52
+ if (!down) {
53
+ p.delete(left);
54
+ p.delete(right);
55
+ }
56
+ else if (!p.has(left) && !p.has(right))
57
+ p.add(left);
58
+ };
59
+ sync(!!e.altKey, "AltLeft", "AltRight");
60
+ sync(!!e.ctrlKey, "ControlLeft", "ControlRight");
61
+ sync(!!e.shiftKey, "ShiftLeft", "ShiftRight");
62
+ sync(!!e.metaKey, "MetaLeft", "MetaRight");
63
+ }
35
64
  static Subscribe(down, char, up) {
36
65
  KeyboardManager.AttachToKeyboard();
37
66
  KeyboardManager.keyDown.add(down);
@@ -0,0 +1,92 @@
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
+ get HasInteractive(): boolean;
68
+ /** The content of the top popup window that takes input. */
69
+ get TopInteractive(): SkiaControl | undefined;
70
+ IsOpen(id: PopupId): boolean;
71
+ Open(root: SkiaControl, anchor: PopupAnchor, options: PopupWindowOptions, superview: SkiaControl["_superview"]): PopupId;
72
+ Close(id: PopupId): void;
73
+ CloseTop(): boolean;
74
+ CloseAll(passiveToo: boolean): void;
75
+ private Remove;
76
+ private static IsInside;
77
+ /** Tab stays inside the top popup window that takes input (a menu, a dropdown) while it is open. */
78
+ private UpdateKeyboardScope;
79
+ /** The top popup window taking input whose content is under the point (canvas pixels). */
80
+ At(x: number, y: number): SkiaControl | undefined;
81
+ /** The pointer is at (x, y) (undefined: off the canvas): popups closing on pointer leave close after 300 ms outside. */
82
+ PointerAt(x: number | undefined, y: number | undefined): void;
83
+ /**
84
+ * Where a popup of `w` x `h` goes for `anchor` (pixels) inside `bounds`: at the anchor's corner, flipped to its other side
85
+ * where it does not fit, then kept inside (C# PopupLayer.Place, Rust place_popup).
86
+ */
87
+ static Place(anchor: SKRect, point: boolean, w: number, h: number, bounds: SKRect): SKRect;
88
+ /** Measures, places and draws every popup over the content, inside `bounds` (canvas pixels). */
89
+ Draw(ctx: DrawingContext, bounds: SKRect): void;
90
+ Dispose(): void;
91
+ }
92
+ export {};