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

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 (52) hide show
  1. package/README.md +87 -13
  2. package/SKIPPED.md +6 -5
  3. package/dist/controls/AnimatedFramesRenderer.d.ts +2 -0
  4. package/dist/controls/AnimatedFramesRenderer.js +2 -0
  5. package/dist/controls/DrawnGame.d.ts +5 -1
  6. package/dist/controls/DrawnGame.js +22 -4
  7. package/dist/controls/SkiaCarousel.d.ts +1 -1
  8. package/dist/controls/SkiaCarousel.js +36 -4
  9. package/dist/controls/SkiaDrawer.d.ts +13 -0
  10. package/dist/controls/SkiaDrawer.js +40 -7
  11. package/dist/controls/SkiaEditor.d.ts +3 -1
  12. package/dist/controls/SkiaEditor.js +5 -0
  13. package/dist/controls/SkiaGif.d.ts +4 -0
  14. package/dist/controls/SkiaGif.js +13 -2
  15. package/dist/controls/SkiaImage.d.ts +5 -0
  16. package/dist/controls/SkiaImage.js +33 -4
  17. package/dist/controls/SkiaLabel.d.ts +7 -1
  18. package/dist/controls/SkiaLabel.js +9 -3
  19. package/dist/controls/SkiaLayout.js +3 -1
  20. package/dist/controls/SkiaLottie.js +1 -1
  21. package/dist/controls/SkiaRadioButton.d.ts +2 -0
  22. package/dist/controls/SkiaRadioButton.js +10 -0
  23. package/dist/controls/SkiaScroll.js +18 -9
  24. package/dist/controls/SkiaShaderCarousel.d.ts +6 -1
  25. package/dist/controls/SkiaShaderCarousel.js +22 -5
  26. package/dist/controls/SkiaSprite.d.ts +3 -0
  27. package/dist/controls/SkiaSprite.js +18 -10
  28. package/dist/controls/SkiaSvg.d.ts +4 -0
  29. package/dist/controls/SkiaSvg.js +27 -3
  30. package/dist/controls/SnappingLayout.js +2 -1
  31. package/dist/core/Accessibility.d.ts +41 -5
  32. package/dist/core/Accessibility.js +190 -9
  33. package/dist/core/Canvas.d.ts +49 -2
  34. package/dist/core/Canvas.js +152 -11
  35. package/dist/core/Gestures.d.ts +47 -0
  36. package/dist/core/Gestures.js +93 -0
  37. package/dist/core/ImageEffects.d.ts +2 -0
  38. package/dist/core/ImageEffects.js +2 -0
  39. package/dist/core/KeyboardManager.d.ts +20 -0
  40. package/dist/core/KeyboardManager.js +58 -15
  41. package/dist/core/PopupWindows.d.ts +2 -0
  42. package/dist/core/PopupWindows.js +2 -0
  43. package/dist/core/SkiaControl.d.ts +31 -1
  44. package/dist/core/SkiaControl.js +61 -5
  45. package/dist/core/Tooltips.d.ts +2 -0
  46. package/dist/core/Tooltips.js +10 -0
  47. package/dist/react/SkiaShell.js +32 -12
  48. package/dist/react/index.d.ts +4 -1
  49. package/dist/react/index.js +34 -16
  50. package/dist/react/reconciler.d.ts +18 -0
  51. package/dist/react/reconciler.js +31 -1
  52. package/package.json +16 -2
@@ -1,3 +1,4 @@
1
+ import { CanvasLeaveWatchers } from "./SkiaControl";
1
2
  import { Super } from "./Super";
2
3
  import { SkiaAccessibilityManager } from "./Accessibility";
3
4
  import { HoverManager } from "./Hover";
@@ -5,7 +6,7 @@ import { TooltipController } from "./Tooltips";
5
6
  import { PopupAnchor, PopupLayer } from "./PopupWindows";
6
7
  import { KeyboardManager } from "./KeyboardManager";
7
8
  import { Colors, SKRect } from "./Types";
8
- import { ContextMenuEventArgs, GestureEventProcessingInfo, PointerData, SKPoint, SkiaGesturesParameters, TouchActionEventArgs, } from "./Gestures";
9
+ import { ContextMenuEventArgs, GestureEventProcessingInfo, MultitouchTracker, PointerData, SKPoint, SkiaGesturesParameters, TouchActionEventArgs, } from "./Gestures";
9
10
  /**
10
11
  * Mirrors DrawnUi Canvas (DrawnView): hosts one Content control on an HTML canvas element,
11
12
  * owns RenderingScale (devicePixelRatio), the surface, the on-demand frame loop and raw input.
@@ -62,6 +63,15 @@ export class Canvas {
62
63
  popupKeysHooked = false;
63
64
  /** Raised when a popup window closed: picked, a press outside, Escape, the wheel, window blur or ClosePopupWindow. */
64
65
  PopupWindowClosed = new Set();
66
+ /** The root controls of the open popup windows, bottom to top: a read-only view for tools (a new array per read). */
67
+ get PopupWindowRoots() { return this.popupLayer?.Roots ?? []; }
68
+ wasDrawnListeners;
69
+ /**
70
+ * Handlers run at the end of every drawn frame, after the `WasDrawn` callback and before the surface is flushed (a
71
+ * tool drawing an overlay on `Surface.getCanvas()`). Made on first use: a canvas without listeners does no work.
72
+ */
73
+ get WasDrawnListeners() { return this.wasDrawnListeners ??= new Set(); }
74
+ raiseWasDrawn = (f) => f(this);
65
75
  /**
66
76
  * Shows `content` over everything, opening from `anchor` the way native menus do: at a point (a right click, canvas
67
77
  * points), or below a rect or a control (a dropdown), flipped to the other side where it does not fit. It closes with
@@ -82,6 +92,25 @@ export class Canvas {
82
92
  ClosePopupWindows() { this.popups.CloseAll(true); this.AfterPopupsChanged(); }
83
93
  /** The popup window is still open. */
84
94
  IsPopupWindowOpen(id) { return this.popups.IsOpen(id); }
95
+ /**
96
+ * Moves the keyboard and screen-reader focus to `control` at the end of the next frame, for app code such as a custom
97
+ * menu that moves its highlight (C# DrawnView.SetAccessibilityFocus). A control not drawn yet gets it once it is, within
98
+ * 3 seconds. It wins over the library's own moves in the same frame (a closed popup window giving the keyboard back to
99
+ * its opener); undefined cancels a move still waiting. The focus ring shows only if the keyboard is in use.
100
+ */
101
+ SetAccessibilityFocus(control) { this.AccessibilityManager.SetAccessibilityFocus(control); this.Update(); }
102
+ /**
103
+ * The drawn control with the keyboard and screen-reader focus (C# KeyboardFocusNode). Set undefined to let no drawn
104
+ * control keep it: the DOM focus goes back to the canvas and the keys belong to the app; a control moves it there.
105
+ */
106
+ get KeyboardFocusNode() { return this.AccessibilityManager.FocusedNode; }
107
+ set KeyboardFocusNode(control) {
108
+ if (control)
109
+ this.AccessibilityManager.SetAccessibilityFocus(control);
110
+ else
111
+ this.AccessibilityManager.ClearKeyboardFocus();
112
+ this.Update();
113
+ }
85
114
  /** True while a popup window that takes input is open. */
86
115
  get HasPopupWindows() { return this.popups.HasInteractive; }
87
116
  /** The content of the top popup window that takes input. */
@@ -96,24 +125,29 @@ export class Canvas {
96
125
  this.Hover.RequestCheck();
97
126
  this.Update();
98
127
  }
99
- onPopupKey = (key) => { if (key === "Escape" && this.popups.CloseTop())
100
- this.AfterPopupsChanged(); };
101
- onPopupChar = () => { };
128
+ onPopupKey = (key) => {
129
+ if (key !== "Escape" || !this.popups.CloseTop())
130
+ return false;
131
+ this.AfterPopupsChanged();
132
+ return true;
133
+ };
102
134
  HookPopupKeys(on) {
103
135
  if (on === this.popupKeysHooked)
104
136
  return;
105
137
  this.popupKeysHooked = on;
106
138
  if (on)
107
- KeyboardManager.Subscribe(this.onPopupKey, this.onPopupChar);
139
+ KeyboardManager.SubscribeNavigation(this.onPopupKey);
108
140
  else
109
- KeyboardManager.Unsubscribe(this.onPopupKey, this.onPopupChar);
141
+ KeyboardManager.UnsubscribeNavigation(this.onPopupKey);
110
142
  }
111
143
  onWindowBlur = () => this.OnWindowDeactivated();
112
144
  content;
113
145
  get Content() { return this.content; }
114
146
  set Content(value) {
115
- if (this.content)
147
+ if (this.content && this.content !== value) {
148
+ this.ControlLeft(this.content, false);
116
149
  this.content._superview = undefined;
150
+ }
117
151
  this.content = value;
118
152
  if (value) {
119
153
  value.Parent = undefined;
@@ -169,7 +203,8 @@ export class Canvas {
169
203
  cancelAnimationFrame(this.frameId);
170
204
  this.frameId = 0;
171
205
  }
172
- this.surface?.delete();
206
+ // dispose, not delete: a software surface owns pixels CanvasKit allocated for it, which delete() leaves behind
207
+ this.surface?.dispose();
173
208
  this.surface = undefined;
174
209
  }
175
210
  /** On-screen surface at the element's current pixel size, reusing the GL/GrContext when accelerated. */
@@ -232,7 +267,7 @@ export class Canvas {
232
267
  return;
233
268
  if (this.grContext)
234
269
  this.grContext.releaseResourcesAndAbandonContext();
235
- this.surface?.delete();
270
+ this.surface?.dispose();
236
271
  this.surface = undefined;
237
272
  this.grContext?.delete();
238
273
  this.grContext = undefined;
@@ -292,10 +327,14 @@ export class Canvas {
292
327
  while (this.frameTimes.length && this.frameTimes[0] < now - 1000)
293
328
  this.frameTimes.shift();
294
329
  this.FPS = this.frameTimes.length;
330
+ this.ForgetGoneControls();
295
331
  if (executed > 0)
296
332
  this.Update(); // animators running: keep frames coming
297
333
  // C# order: WasDrawn at the end of the frame, then the frame counts as rendered
298
334
  this.WasDrawn?.(this);
335
+ const listeners = this.wasDrawnListeners;
336
+ if (listeners !== undefined && listeners.size > 0)
337
+ listeners.forEach(this.raiseWasDrawn);
299
338
  if (!this.WasRendered && canRender)
300
339
  this.WasRendered = true;
301
340
  }
@@ -319,6 +358,37 @@ export class Canvas {
319
358
  return true;
320
359
  }
321
360
  UnregisterAnimator(uid) { this.AnimatingControls.delete(uid); }
361
+ /**
362
+ * `root` leaves this canvas (removed from its parent, replaced as content, disposed): what runs for its subtree stops,
363
+ * as DrawnUi.Rust 44c718f. Its animators leave the frame loop: disposed, they stop; only removed, they pause and wait on
364
+ * their control (PendingUnattachedAnimators), going on from there when it is arranged on a canvas again. A focused editor in it loses the
365
+ * focus, a game in it stops getting keys (CanvasLeaveWatchers). Runs once per removal, never per frame.
366
+ */
367
+ ControlLeft(root, disposed) {
368
+ const inside = (c) => { for (let p = c; p; p = p.Parent)
369
+ if (p === root)
370
+ return true; return false; };
371
+ for (const a of this.AnimatingControls.values()) {
372
+ const control = a.Parent;
373
+ if (!control || !inside(control))
374
+ continue;
375
+ this.AnimatingControls.delete(a.Uid);
376
+ if (disposed || control.IsDisposed)
377
+ a.Stop();
378
+ else {
379
+ // paused as a hidden control's animator is: the time off the canvas is not counted when it goes on
380
+ if (!a.IsPaused) {
381
+ a.Pause();
382
+ a.PausedWhileHidden = true;
383
+ }
384
+ if (!control.PendingUnattachedAnimators?.includes(a))
385
+ (control.PendingUnattachedAnimators ??= []).push(a);
386
+ }
387
+ }
388
+ for (const c of [...CanvasLeaveWatchers])
389
+ if (inside(c))
390
+ c.OnLeftCanvas();
391
+ }
322
392
  /** Ticks every registered animator once; returns how many ran. */
323
393
  ExecuteAnimators(frameTimeNanos) {
324
394
  let executed = 0, wake = Infinity;
@@ -419,7 +489,7 @@ export class Canvas {
419
489
  }
420
490
  }
421
491
  finally {
422
- surface.delete();
492
+ surface.dispose(); // its pixels too (CK.MakeSurface allocates them)
423
493
  }
424
494
  }
425
495
  Dispose() {
@@ -431,6 +501,7 @@ export class Canvas {
431
501
  this.Element.removeEventListener("webglcontextlost", this.onContextLost);
432
502
  this.Element.removeEventListener("webglcontextrestored", this.onContextRestored);
433
503
  this.Gestures = "Disabled";
504
+ this.ArrowsStartKeyboardNavigation = false;
434
505
  this.tooltipController?.Dispose();
435
506
  this.popupLayer?.Dispose();
436
507
  this.HookPopupKeys(false);
@@ -723,6 +794,7 @@ export class Canvas {
723
794
  this.activeTouchIds.clear();
724
795
  this.pointerDownArgs.clear();
725
796
  this.previousTouchArgs.clear();
797
+ this.fingers.Reset();
726
798
  }
727
799
  /** Port of DrawnUi.Blazor Canvas.OnTouchAction: per-pointer state machine producing Down / Panning / Tapped / Up. */
728
800
  OnTouchAction(args) {
@@ -731,6 +803,17 @@ export class Canvas {
731
803
  const id = args.Id;
732
804
  if (args.Type === "Pressed") {
733
805
  this.ArmLongPress(id, args);
806
+ // the fingers for pinch and turn: the first restarts them, a second one makes the press no tap
807
+ if (this.activeTouchIds.size === 0) {
808
+ this.fingers.Restart(id, args.Location);
809
+ this.multiTouch = false;
810
+ }
811
+ else {
812
+ for (const [other, last] of this.previousTouchArgs)
813
+ this.fingers.AddMovement(other, last.Location); // their latest places
814
+ this.fingers.AddMovement(id, args.Location);
815
+ this.multiTouch = true;
816
+ }
734
817
  this.activeTouchIds.add(id);
735
818
  args.NumberOfTouches = this.activeTouchIds.size;
736
819
  args.StartingLocation = args.Location;
@@ -755,6 +838,8 @@ export class Canvas {
755
838
  const downArgs = this.pointerDownArgs.get(id);
756
839
  args.StartingLocation = downArgs ? downArgs.StartingLocation : args.Location;
757
840
  if (args.Type === "Moved") {
841
+ if (this.activeTouchIds.size > 1)
842
+ args.Manipulation = this.fingers.AddMovement(id, args.Location); // nothing for one finger
758
843
  const threshold = Canvas.TappedCancelMoveThresholdPoints * Math.max(0.1, this.RenderingScale);
759
844
  if (Math.abs(args.Distance.Total.X) >= threshold || Math.abs(args.Distance.Total.Y) >= threshold)
760
845
  this.DisarmLongPress(id);
@@ -767,7 +852,7 @@ export class Canvas {
767
852
  args.IsInContact = args.NumberOfTouches > 1;
768
853
  const longPressed = this.longPressed.delete(id);
769
854
  this.DisarmLongPress(id);
770
- if (!args.IsInContact && downArgs && args.Type === "Released" && !longPressed) {
855
+ if (!args.IsInContact && downArgs && args.Type === "Released" && !longPressed && !this.multiTouch) {
771
856
  const threshold = Canvas.TappedCancelMoveThresholdPoints * Math.max(0.1, this.RenderingScale);
772
857
  if (Math.abs(args.Distance.Total.X) < threshold && Math.abs(args.Distance.Total.Y) < threshold)
773
858
  this.OnGestureEvent(args, "Tapped");
@@ -776,8 +861,16 @@ export class Canvas {
776
861
  this.previousTouchArgs.delete(id);
777
862
  this.pointerDownArgs.delete(id);
778
863
  this.activeTouchIds.delete(id);
864
+ if (this.activeTouchIds.size === 0)
865
+ this.fingers.Reset();
866
+ else
867
+ this.fingers.Lifted(id);
779
868
  }
780
869
  }
870
+ /** The fingers down, for TouchActionEventArgs.Manipulation (pinch and turn). */
871
+ fingers = new MultitouchTracker();
872
+ /** The press had a second finger: it is no tap (AppoMobi.Blazor.Gestures). */
873
+ multiTouch = false;
781
874
  longPressTimers = new Map();
782
875
  longPressed = new Set();
783
876
  /** The long press of a new press: due LongPressTimeMs later unless the pointer moves away or lets go. */
@@ -827,11 +920,59 @@ export class Canvas {
827
920
  static IsSavedGesture(type) {
828
921
  return type === "Panning" || type === "Wheel" || type === "Up";
829
922
  }
923
+ // ---- arrows start keyboard navigation (C# 6aee5c51) ----
924
+ arrowsStart = false;
925
+ /**
926
+ * While no drawn control has keyboard focus (or only one behind the active modal), the arrow keys start keyboard
927
+ * navigation as the first Tab does: Down and Right go to the first Tab stop, Up and Left to the last, and the key is used
928
+ * for that. Once a control has focus the arrows work as usual (inside a list or a menu). Off by default, since games use
929
+ * the arrows as their own keys while nothing is focused: an app turns it on for its menu screens and off while playing.
930
+ */
931
+ get ArrowsStartKeyboardNavigation() { return this.arrowsStart; }
932
+ set ArrowsStartKeyboardNavigation(v) {
933
+ if (this.arrowsStart === v)
934
+ return;
935
+ this.arrowsStart = v;
936
+ if (v)
937
+ KeyboardManager.SubscribeNavigation(this.onArrowKey);
938
+ else
939
+ KeyboardManager.UnsubscribeNavigation(this.onArrowKey);
940
+ }
941
+ onArrowKey = (key, e) => {
942
+ const forward = key === "ArrowDown" || key === "ArrowRight" ? true : key === "ArrowUp" || key === "ArrowLeft" ? false : undefined;
943
+ // a page element, a drawn control's element or the editor's input has the keys: nothing to start
944
+ if (forward === undefined || this.disposed || KeyboardManager.IsOwnedByElement(e) || e.defaultPrevented)
945
+ return false;
946
+ const mgr = this.AccessibilityManager, focused = mgr.FocusedNode;
947
+ if (focused && !mgr.IsOutsideModal(focused))
948
+ return false;
949
+ const first = mgr.NextTabStop(undefined, forward);
950
+ if (!first)
951
+ return false;
952
+ e.preventDefault();
953
+ mgr.RequestKeyboardFocus(first);
954
+ this.Update();
955
+ return true;
956
+ };
830
957
  // ---- focus (C# DrawnView FocusedChild / ReportFocus, drawnui-cross 6ap) ----
831
958
  focusedChild;
832
959
  focusAtDown;
833
960
  tapNoFocus = false;
834
961
  reportingFocus = false;
962
+ /**
963
+ * A control that left the canvas (removed or disposed) is let go by the focus, the gesture dispatch and the tooltips,
964
+ * so the last control that took input does not keep its page alive (C# 4cb28ab6). Checked once per frame.
965
+ */
966
+ ForgetGoneControls() {
967
+ const gone = (c) => !!c && (c.IsDisposed || c.Superview !== this);
968
+ if (gone(this.focusedChild))
969
+ this.focusedChild = undefined;
970
+ if (gone(this.focusAtDown))
971
+ this.focusAtDown = undefined;
972
+ if (gone(this.gestureOwner))
973
+ this.gestureOwner = null;
974
+ this.tooltipController?.ForgetGone(gone);
975
+ }
835
976
  /** The control that has the canvas focus (an editor while it edits, a CanBeFocused control that was tapped). */
836
977
  get FocusedChild() { return this.focusedChild; }
837
978
  /**
@@ -54,6 +54,51 @@ export declare class PointerData {
54
54
  /** DOM `buttons` bitmask of the buttons held (1 left, 2 right, 4 middle, 8 back, 16 forward). */
55
55
  PressedButtons: number;
56
56
  }
57
+ /**
58
+ * AppoMobi.Gestures ManipulationInfo: what two or more fingers did since the previous move, on a Panning while they are
59
+ * down. Pinch to zoom from `Scale`, turn from `Rotation`. Pixels.
60
+ */
61
+ export declare class ManipulationInfo {
62
+ /** The middle of the fingers now, and at the previous move. */
63
+ readonly Center: SKPoint;
64
+ readonly PreviousCenter: SKPoint;
65
+ /** The fingers' spread now over the previous one, minus 1: 0.05 = 5% further apart. */
66
+ readonly Scale: number;
67
+ /** Degrees the first two fingers turned since the previous move, the short way (never a jump across ±180). */
68
+ readonly Rotation: number;
69
+ /** `Scale` and `Rotation` added up since the number of fingers last changed. */
70
+ readonly ScaleTotal: number;
71
+ readonly RotationTotal: number;
72
+ readonly TouchesCount: number;
73
+ constructor(
74
+ /** The middle of the fingers now, and at the previous move. */
75
+ Center: SKPoint, PreviousCenter: SKPoint,
76
+ /** The fingers' spread now over the previous one, minus 1: 0.05 = 5% further apart. */
77
+ Scale: number,
78
+ /** Degrees the first two fingers turned since the previous move, the short way (never a jump across ±180). */
79
+ Rotation: number,
80
+ /** `Scale` and `Rotation` added up since the number of fingers last changed. */
81
+ ScaleTotal: number, RotationTotal: number, TouchesCount: number);
82
+ }
83
+ /**
84
+ * The fingers down and what they did (AppoMobi.Gestures MultitouchTracker, after Mapsui; as DrawnUi.Rust): their middle,
85
+ * the first one's distance from it, the first two's angle. A new finger count starts the totals again.
86
+ */
87
+ export declare class MultitouchTracker {
88
+ private readonly fingers;
89
+ private state?;
90
+ private scaleTotal;
91
+ private rotationTotal;
92
+ get Count(): number;
93
+ /** The first finger came down: everything before is forgotten. */
94
+ Restart(id: number, at: SKPoint): void;
95
+ /** A finger came down or moved; what the fingers did while two or more are down and they moved. */
96
+ AddMovement(id: number, at: SKPoint): ManipulationInfo | undefined;
97
+ Lifted(id: number): void;
98
+ Reset(): void;
99
+ private Measure;
100
+ private Calculate;
101
+ }
57
102
  /** AppoMobi.Gestures TouchActionEventArgs. */
58
103
  export declare class TouchActionEventArgs {
59
104
  Id: number;
@@ -66,6 +111,8 @@ export declare class TouchActionEventArgs {
66
111
  Distance: DistanceInfo;
67
112
  NumberOfTouches: number;
68
113
  IsInContact: boolean;
114
+ /** Panning while two or more fingers are down: how they pinched and turned since the previous move. */
115
+ Manipulation?: ManipulationInfo;
69
116
  Scale: number;
70
117
  Timestamp: number;
71
118
  /** ms since the previous event of the same pointer. */
@@ -88,6 +88,97 @@ export class PointerData {
88
88
  /** DOM `buttons` bitmask of the buttons held (1 left, 2 right, 4 middle, 8 back, 16 forward). */
89
89
  PressedButtons = 0;
90
90
  }
91
+ /**
92
+ * AppoMobi.Gestures ManipulationInfo: what two or more fingers did since the previous move, on a Panning while they are
93
+ * down. Pinch to zoom from `Scale`, turn from `Rotation`. Pixels.
94
+ */
95
+ export class ManipulationInfo {
96
+ Center;
97
+ PreviousCenter;
98
+ Scale;
99
+ Rotation;
100
+ ScaleTotal;
101
+ RotationTotal;
102
+ TouchesCount;
103
+ constructor(
104
+ /** The middle of the fingers now, and at the previous move. */
105
+ Center, PreviousCenter,
106
+ /** The fingers' spread now over the previous one, minus 1: 0.05 = 5% further apart. */
107
+ Scale,
108
+ /** Degrees the first two fingers turned since the previous move, the short way (never a jump across ±180). */
109
+ Rotation,
110
+ /** `Scale` and `Rotation` added up since the number of fingers last changed. */
111
+ ScaleTotal, RotationTotal, TouchesCount) {
112
+ this.Center = Center;
113
+ this.PreviousCenter = PreviousCenter;
114
+ this.Scale = Scale;
115
+ this.Rotation = Rotation;
116
+ this.ScaleTotal = ScaleTotal;
117
+ this.RotationTotal = RotationTotal;
118
+ this.TouchesCount = TouchesCount;
119
+ }
120
+ }
121
+ /**
122
+ * The fingers down and what they did (AppoMobi.Gestures MultitouchTracker, after Mapsui; as DrawnUi.Rust): their middle,
123
+ * the first one's distance from it, the first two's angle. A new finger count starts the totals again.
124
+ */
125
+ export class MultitouchTracker {
126
+ fingers = new Map();
127
+ state;
128
+ scaleTotal = 0;
129
+ rotationTotal = 0;
130
+ get Count() { return this.fingers.size; }
131
+ /** The first finger came down: everything before is forgotten. */
132
+ Restart(id, at) {
133
+ this.fingers.clear();
134
+ this.fingers.set(id, at);
135
+ this.scaleTotal = this.rotationTotal = 0;
136
+ this.state = this.Measure();
137
+ }
138
+ /** A finger came down or moved; what the fingers did while two or more are down and they moved. */
139
+ AddMovement(id, at) {
140
+ this.fingers.set(id, at);
141
+ return this.Calculate();
142
+ }
143
+ Lifted(id) { if (this.fingers.delete(id))
144
+ this.Calculate(); }
145
+ Reset() { this.fingers.clear(); this.state = undefined; }
146
+ Measure() {
147
+ const points = [...this.fingers.values()];
148
+ if (points.length === 0)
149
+ return undefined;
150
+ const first = points[0];
151
+ if (points.length === 1)
152
+ return { Center: first, Radius: 0, Angle: 0, Count: 1 };
153
+ let x = 0, y = 0;
154
+ for (const p of points) {
155
+ x += p.X;
156
+ y += p.Y;
157
+ }
158
+ const center = new SKPoint(x / points.length, y / points.length), second = points[1];
159
+ return {
160
+ Center: center, Radius: Math.hypot(first.X - center.X, first.Y - center.Y),
161
+ Angle: (Math.atan2(second.Y - first.Y, second.X - first.X) * 180) / Math.PI, Count: points.length,
162
+ };
163
+ }
164
+ Calculate() {
165
+ const now = this.Measure(), previous = this.state;
166
+ this.state = now;
167
+ if (!now || !previous || now.Count < 2)
168
+ return undefined;
169
+ if (now.Count !== previous.Count) {
170
+ this.scaleTotal = this.rotationTotal = 0;
171
+ return undefined;
172
+ }
173
+ const scale = previous.Radius > 0 && now.Radius !== previous.Radius ? now.Radius / previous.Radius - 1 : 0;
174
+ const rotation = ((((now.Angle - previous.Angle + 540) % 360) + 360) % 360) - 180;
175
+ this.scaleTotal += scale;
176
+ this.rotationTotal += rotation;
177
+ if (now.Center.X === previous.Center.X && now.Center.Y === previous.Center.Y && now.Radius === previous.Radius && now.Angle === previous.Angle)
178
+ return undefined;
179
+ return new ManipulationInfo(now.Center, previous.Center, scale, rotation, this.scaleTotal, this.rotationTotal, now.Count);
180
+ }
181
+ }
91
182
  /** AppoMobi.Gestures TouchActionEventArgs. */
92
183
  export class TouchActionEventArgs {
93
184
  Id = 0;
@@ -100,6 +191,8 @@ export class TouchActionEventArgs {
100
191
  Distance = new DistanceInfo();
101
192
  NumberOfTouches = 1;
102
193
  IsInContact = false;
194
+ /** Panning while two or more fingers are down: how they pinched and turned since the previous move. */
195
+ Manipulation;
103
196
  Scale = 1;
104
197
  Timestamp = performance.now();
105
198
  /** ms since the previous event of the same pointer. */
@@ -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,6 +18,17 @@ 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;
18
34
  /** True while the keydown handlers run: a popup opened now was opened from the keyboard (Alt+F). */
@@ -25,6 +41,10 @@ export declare class KeyboardManager {
25
41
  * went elsewhere) still counts, and one released elsewhere stops counting.
26
42
  */
27
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;
28
48
  static Subscribe(down: (key: InputKey, e: KeyboardEvent) => void, char: (ch: string, e: KeyboardEvent) => void, up?: (key: InputKey, e: KeyboardEvent) => void): void;
29
49
  static Unsubscribe(down: (key: InputKey, e: KeyboardEvent) => void, char: (ch: string, e: KeyboardEvent) => void, up?: (key: InputKey, e: KeyboardEvent) => void): void;
30
50
  /**
@@ -4,6 +4,17 @@ 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"); }
9
20
  /** True while the keydown handlers run: a popup opened now was opened from the keyboard (Alt+F). */
@@ -17,26 +28,27 @@ export class KeyboardManager {
17
28
  // inside a cross-frame iframe the embedding page keeps DOM focus: a pointerdown pulls it to this window
18
29
  window.addEventListener("pointerdown", () => window.focus(), true);
19
30
  window.addEventListener("keydown", (e) => {
20
- const code = e.code || "";
21
31
  KeyboardManager.SyncModifiers(e);
22
- KeyboardManager.pressed.add(code);
23
- KeyboardManager.IsDispatchingKeyDown = true;
24
- try {
25
- for (const h of [...KeyboardManager.keyDown])
26
- h(code, e);
27
- }
28
- finally {
29
- KeyboardManager.IsDispatchingKeyDown = false;
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;
30
37
  }
31
- const proxied = e.target?.dataset?.drawnuiTextInput === "1"; // TextInputProxy: characters come through its input events
32
- if (!proxied && e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey)
33
- for (const h of [...KeyboardManager.keyChar])
34
- h(e.key, e);
38
+ KeyboardManager.RaiseKeyDown(e);
35
39
  }, true);
40
+ window.addEventListener("keydown", (e) => {
41
+ if (KeyboardManager.deferred !== e)
42
+ return;
43
+ KeyboardManager.deferred = undefined;
44
+ KeyboardManager.RaiseKeyDown(e);
45
+ });
36
46
  window.addEventListener("keyup", (e) => {
37
47
  const code = e.code || "";
38
48
  KeyboardManager.SyncModifiers(e);
39
49
  KeyboardManager.pressed.delete(code);
50
+ if (KeyboardManager.usedKey === code)
51
+ KeyboardManager.usedKey = undefined;
40
52
  for (const h of [...KeyboardManager.keyUp])
41
53
  h(code, e);
42
54
  }, true);
@@ -61,6 +73,35 @@ export class KeyboardManager {
61
73
  sync(!!e.shiftKey, "ShiftLeft", "ShiftRight");
62
74
  sync(!!e.metaKey, "MetaLeft", "MetaRight");
63
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); }
64
105
  static Subscribe(down, char, up) {
65
106
  KeyboardManager.AttachToKeyboard();
66
107
  KeyboardManager.keyDown.add(down);
@@ -82,8 +123,10 @@ export class KeyboardManager {
82
123
  * those of its own element.
83
124
  */
84
125
  static IsOwnedByElement(e) {
85
- const tag = e.target?.tagName;
86
- return !!tag && tag !== "BODY" && tag !== "HTML" && tag !== "CANVAS";
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";
87
130
  }
88
131
  /**
89
132
  * The key belongs to the input method (an IME composition, a soft keyboard): isComposing, keyCode 229, key "Process"
@@ -64,6 +64,8 @@ export declare class PopupLayer {
64
64
  SwallowPress: boolean;
65
65
  constructor(canvas: PopupHostCanvas);
66
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[];
67
69
  get HasInteractive(): boolean;
68
70
  /** The content of the top popup window that takes input. */
69
71
  get TopInteractive(): SkiaControl | undefined;
@@ -61,6 +61,8 @@ export class PopupLayer {
61
61
  this.canvas = canvas;
62
62
  }
63
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); }
64
66
  get HasInteractive() { return this.popups.some((p) => !p.Options.Passive); }
65
67
  /** The content of the top popup window that takes input. */
66
68
  get TopInteractive() { for (let i = this.popups.length - 1; i >= 0; i--)