drawnui-react 0.1.0-preview.41 → 0.1.0-preview.43

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 (51) hide show
  1. package/README.md +89 -7
  2. package/SKIPPED.md +7 -7
  3. package/dist/controls/GridStructure.d.ts +9 -1
  4. package/dist/controls/GridStructure.js +41 -1
  5. package/dist/controls/SkiaEditor.d.ts +4 -1
  6. package/dist/controls/SkiaEditor.js +25 -6
  7. package/dist/controls/SkiaImage.d.ts +5 -0
  8. package/dist/controls/SkiaImage.js +32 -5
  9. package/dist/controls/SkiaLabel.d.ts +85 -4
  10. package/dist/controls/SkiaLabel.js +406 -20
  11. package/dist/controls/SkiaLabelFps.d.ts +0 -3
  12. package/dist/controls/SkiaLabelFps.js +1 -11
  13. package/dist/controls/SkiaLayout.js +33 -2
  14. package/dist/controls/SkiaScroll.d.ts +51 -2
  15. package/dist/controls/SkiaScroll.js +150 -9
  16. package/dist/controls/SkiaShape.d.ts +2 -0
  17. package/dist/controls/SkiaShape.js +7 -0
  18. package/dist/controls/SkiaShell.d.ts +282 -0
  19. package/dist/controls/SkiaShell.js +972 -0
  20. package/dist/controls/SkiaSprite.js +5 -1
  21. package/dist/core/Accessibility.d.ts +30 -13
  22. package/dist/core/Accessibility.js +237 -43
  23. package/dist/core/Canvas.d.ts +122 -14
  24. package/dist/core/Canvas.js +585 -47
  25. package/dist/core/Gestures.d.ts +11 -1
  26. package/dist/core/Gestures.js +12 -1
  27. package/dist/core/PopupWindows.js +2 -0
  28. package/dist/core/Props.d.ts +11 -0
  29. package/dist/core/Props.js +44 -0
  30. package/dist/core/ScrollAnimators.d.ts +6 -0
  31. package/dist/core/ScrollAnimators.js +7 -1
  32. package/dist/core/SkiaControl.d.ts +24 -4
  33. package/dist/core/SkiaControl.js +59 -13
  34. package/dist/core/SkiaEffect.d.ts +12 -1
  35. package/dist/core/SkiaEffect.js +35 -8
  36. package/dist/core/SkiaImageManager.d.ts +101 -18
  37. package/dist/core/SkiaImageManager.js +318 -63
  38. package/dist/core/Styles.js +7 -1
  39. package/dist/core/Super.d.ts +4 -0
  40. package/dist/core/Super.js +4 -9
  41. package/dist/core/TextInputProxy.d.ts +31 -1
  42. package/dist/core/TextInputProxy.js +97 -15
  43. package/dist/index.d.ts +1 -0
  44. package/dist/index.js +1 -0
  45. package/dist/react/SkiaShell.d.ts +8 -74
  46. package/dist/react/SkiaShell.js +163 -514
  47. package/dist/react/index.d.ts +6 -1
  48. package/dist/react/index.js +51 -40
  49. package/dist/react/reconciler.d.ts +12 -0
  50. package/dist/react/reconciler.js +22 -46
  51. package/package.json +37 -4
@@ -5,17 +5,17 @@ import { HoverManager } from "./Hover";
5
5
  import { TooltipController } from "./Tooltips";
6
6
  import { PopupAnchor, PopupLayer } from "./PopupWindows";
7
7
  import { KeyboardManager } from "./KeyboardManager";
8
+ import { TouchEffect } from "./Gestures";
9
+ import { TextInputProxy } from "./TextInputProxy";
8
10
  import { Colors, SKRect } from "./Types";
9
11
  import { ContextMenuEventArgs, GestureEventProcessingInfo, MultitouchTracker, PointerData, SKPoint, SkiaGesturesParameters, TouchActionEventArgs, } from "./Gestures";
10
12
  export class Canvas {
11
13
  Element;
12
14
  /** Points a pointer may travel between Down and Up and still count as a tap (AppoMobi TouchEffect default). */
13
15
  static TappedCancelMoveThresholdPoints = 16;
14
- /**
15
- * A press held this long without moving past TappedCancelMoveThresholdPoints is a LongPressing, at the press point
16
- * (AppoMobi TouchEffect, DrawnUi.Rust LONG_PRESS_MS); its release sends no Tapped. A timer, no frames while waiting.
17
- */
18
- static LongPressTimeMs = 1500;
16
+ /** Alias of TouchEffect.LongPressTimeMsDefault: how long a press is held still to be a LongPressing. */
17
+ static get LongPressTimeMs() { return TouchEffect.LongPressTimeMsDefault; }
18
+ static set LongPressTimeMs(ms) { TouchEffect.LongPressTimeMsDefault = ms; }
19
19
  BackgroundColor = Colors.Transparent;
20
20
  /** Accelerated = WebGL surface, Default = software. Read once at first frame. */
21
21
  RenderingMode = "Accelerated";
@@ -84,7 +84,7 @@ export class Canvas {
84
84
  * tool drawing an overlay on `Surface.getCanvas()`). Made on first use: a canvas without listeners does no work.
85
85
  */
86
86
  get WasDrawnListeners() { return this.wasDrawnListeners ??= new Set(); }
87
- raiseWasDrawn = (f) => f(this);
87
+ raiseWasDrawn = (f) => Canvas.Guard("a WasDrawn listener", f, () => f(this));
88
88
  /**
89
89
  * Shows `content` over everything, opening from `anchor` the way native menus do: at a point (a right click, canvas
90
90
  * points), or below a rect or a control (a dropdown), flipped to the other side where it does not fit. It closes with
@@ -193,13 +193,25 @@ export class Canvas {
193
193
  frameId = 0;
194
194
  disposed = false;
195
195
  observer;
196
+ /**
197
+ * The device pixels the element covers, as the browser reports them (ResizeObserver device-pixel-content-box). At a
198
+ * fractional display scale the rounded CSS size can be a pixel off (701.3 x 333.3 CSS px at 1.25 cover 877 x 416 device
199
+ * px), and the browser would then resample the whole canvas, softening every glyph.
200
+ */
201
+ devicePixels;
202
+ /** The element's CSS width when the device-pixel box was reported. */
203
+ devicePixelsCss = 0;
196
204
  /** The WebGL context is lost: no frames until the browser restores it. */
197
205
  contextLost = false;
198
206
  /**
199
- * Bumped when the WebGL context was restored: every GPU object made before (Image caches, picture caches that may
200
- * replay them, kept composite surfaces, effect textures) is remade at its next draw (DrawnUi.Rust Gpu::epoch).
207
+ * The GPU context generation, unique across canvases: a new one when this canvas is made and when its WebGL context
208
+ * is restored. Every GPU object made under another one (Image caches, picture caches that may replay them, kept
209
+ * composite surfaces, effect textures; also those a control brought from another canvas) is remade at its next draw
210
+ * (DrawnUi.Rust Gpu::epoch). A texture of another WebGL context draws nothing.
201
211
  */
202
- GpuEpoch = 0;
212
+ GpuEpoch = Canvas.NextGpuEpoch();
213
+ static gpuEpochs = 0;
214
+ static NextGpuEpoch() { return ++Canvas.gpuEpochs; }
203
215
  /** `init` runs before the first frame is drawn: the place to attach `WillFirstTimeDraw` / `WasDrawn` for that frame. */
204
216
  constructor(Element, init) {
205
217
  this.Element = Element;
@@ -216,8 +228,22 @@ export class Canvas {
216
228
  window.addEventListener("blur", this.onWindowInactive);
217
229
  window.addEventListener("focus", this.onWindowActive);
218
230
  this.Hover.RootAt = (x, y) => this.popupLayer?.At(x, y) ?? this.content;
219
- this.observer = new ResizeObserver(() => this.OnResized());
220
- this.observer.observe(Element);
231
+ this.observer = new ResizeObserver((entries) => {
232
+ const entry = entries[entries.length - 1];
233
+ const box = entry?.devicePixelContentBoxSize?.[0];
234
+ if (box) {
235
+ this.devicePixels = [box.inlineSize, box.blockSize];
236
+ this.devicePixelsCss = entry.contentBoxSize?.[0]?.inlineSize ?? entry.contentRect.width;
237
+ }
238
+ this.OnResized();
239
+ });
240
+ // browsers without the device-pixel box (Safari) take the rounded size
241
+ try {
242
+ this.observer.observe(Element, { box: "device-pixel-content-box" });
243
+ }
244
+ catch {
245
+ this.observer.observe(Element);
246
+ }
221
247
  this.OnResized();
222
248
  }
223
249
  /** Request a redraw (full measure + arrange + render) on the next animation frame. */
@@ -227,11 +253,8 @@ export class Canvas {
227
253
  const surface = this.surface;
228
254
  this.frameId = surface.requestAnimationFrame((c) => {
229
255
  this.frameId = 0;
230
- if (this.surface === surface && !this.disposed) {
231
- this.Draw(c);
232
- surface.flush();
233
- this.DrainDisposeQueue();
234
- }
256
+ if (this.surface === surface && !this.disposed)
257
+ this.DrawGuarded(c, surface);
235
258
  });
236
259
  }
237
260
  /** Deletes the current surface; a pending frame on it is cancelled first (drawing on a deleted surface faults). */
@@ -249,7 +272,7 @@ export class Canvas {
249
272
  const CK = Super.CK;
250
273
  if (this.RenderingMode === "Accelerated") {
251
274
  if (!this.grContext) {
252
- this.glHandle = CK.GetWebGLContext(this.Element);
275
+ this.glHandle = Canvas.WithoutPolygonMode(() => CK.GetWebGLContext(this.Element));
253
276
  this.grContext = (this.glHandle ? CK.MakeWebGLContext(this.glHandle) : null) ?? undefined;
254
277
  }
255
278
  if (this.grContext) {
@@ -266,9 +289,16 @@ export class Canvas {
266
289
  * redrawn content lands in the same paint and no blank frame is ever shown while dragging.
267
290
  */
268
291
  OnResized() {
269
- const dpr = window.devicePixelRatio || 1;
270
- const w = Math.max(1, Math.round(this.Element.clientWidth * dpr));
271
- const h = Math.max(1, Math.round(this.Element.clientHeight * dpr));
292
+ let dpr = window.devicePixelRatio || 1;
293
+ // the device-pixel box can disagree with devicePixelRatio (DPR emulation in DevTools or Playwright, some zoom cases):
294
+ // past 2% the scale follows the box, so the layout keeps the element's CSS size (DrawnUi.Rust e7ff239)
295
+ if (this.devicePixels && this.devicePixelsCss > 0) {
296
+ const boxScale = this.devicePixels[0] / this.devicePixelsCss;
297
+ if (Math.abs(boxScale - dpr) > dpr * 0.02)
298
+ dpr = boxScale;
299
+ }
300
+ const w = Math.max(1, this.devicePixels?.[0] ?? Math.round(this.Element.clientWidth * dpr));
301
+ const h = Math.max(1, this.devicePixels?.[1] ?? Math.round(this.Element.clientHeight * dpr));
272
302
  if (this.surface && this.Element.width === w && this.Element.height === h && this.RenderingScale === dpr)
273
303
  return;
274
304
  this.RenderingScale = dpr;
@@ -282,7 +312,31 @@ export class Canvas {
282
312
  throw new Error("DrawnUi: cannot create surface");
283
313
  this.DrawNow();
284
314
  }
285
- /** WebGL context lost: keep the browser restoring it (preventDefault) and stop the frames. */
315
+ /**
316
+ * Registers the WebGL context without `WEBGL_polygon_mode` (drawnui-cross 6bt): Emscripten turns on every extension it
317
+ * knows while it registers a context, nothing here draws with that one, and WebKit warns "non-portable extension
318
+ * enabled" for it. The lookup is hidden only for this synchronous call (creating the context here first would change
319
+ * the attributes CanvasKit asks for).
320
+ */
321
+ static WithoutPolygonMode(make) {
322
+ const prototypes = [globalThis.WebGL2RenderingContext?.prototype, globalThis.WebGLRenderingContext?.prototype]
323
+ .filter((p) => !!p);
324
+ const originals = prototypes.map((p) => p.getExtension);
325
+ prototypes.forEach((p, i) => { p.getExtension = function (name) { return name === "WEBGL_polygon_mode" ? null : originals[i].call(this, name); }; });
326
+ try {
327
+ return make();
328
+ }
329
+ finally {
330
+ prototypes.forEach((p, i) => { p.getExtension = originals[i]; });
331
+ }
332
+ }
333
+ /**
334
+ * WebGL context lost: keep the browser restoring it (preventDefault) and stop the frames. The old Skia context is
335
+ * abandoned here, while the context is lost: WebGL ignores every call on a lost context without a word, so the GL
336
+ * deletes Skia makes while it lets go of its objects stay silent (done after the restore, they reach the new
337
+ * generation and every one logs "object does not belong to this context"). CanvasKit has no `abandonContext`;
338
+ * `releaseResourcesAndAbandonContext` is its only way, and after it freeing an old image or surface makes no GL call.
339
+ */
286
340
  onContextLost = (e) => {
287
341
  e.preventDefault();
288
342
  this.contextLost = true;
@@ -290,20 +344,24 @@ export class Canvas {
290
344
  cancelAnimationFrame(this.frameId);
291
345
  this.frameId = 0;
292
346
  }
347
+ if (this.grContext)
348
+ this.grContext.releaseResourcesAndAbandonContext();
349
+ this.surface?.dispose();
350
+ this.surface = undefined;
351
+ this.grContext?.delete();
352
+ this.grContext = undefined;
293
353
  };
294
354
  /**
295
355
  * WebGL context restored (as DrawnUi.Rust and the C# Wasm / Blazor heads): every GL object of the lost generation is
296
- * gone. The old Skia context is abandoned first, while its own GL handle is current and before the new context makes
297
- * any object, so its GL calls reach only lost-generation objects. CanvasKit exposes no `abandonContext`;
298
- * `releaseResourcesAndAbandonContext` is its only way to abandon, and after it freeing an old image or surface makes no
299
- * GL call. Then the same WebGL object is registered again (the old handle deleted first, which clears the canvas'
300
- * context object) with a new GrContext and surface; GpuEpoch makes the old generation's GPU objects remake themselves.
356
+ * gone, and the old Skia context was abandoned when it was lost. The same WebGL object is registered again (the old
357
+ * handle deleted first, which clears the canvas' context object) with a new GrContext and surface; GpuEpoch makes the
358
+ * old generation's GPU objects remake themselves.
301
359
  */
302
360
  onContextRestored = () => {
303
361
  if (this.disposed)
304
362
  return;
305
363
  if (this.grContext)
306
- this.grContext.releaseResourcesAndAbandonContext();
364
+ this.grContext.releaseResourcesAndAbandonContext(); // lost before this Canvas listened
307
365
  this.surface?.dispose();
308
366
  this.surface = undefined;
309
367
  this.grContext?.delete();
@@ -313,7 +371,7 @@ export class Canvas {
313
371
  this.glHandle = undefined;
314
372
  }
315
373
  this.contextLost = false;
316
- this.GpuEpoch++;
374
+ this.GpuEpoch = Canvas.NextGpuEpoch();
317
375
  this.surface = this.CreateSurface(this.Element.width, this.Element.height);
318
376
  this.DrawNow();
319
377
  };
@@ -322,13 +380,12 @@ export class Canvas {
322
380
  const surface = this.surface;
323
381
  if (!surface || this.disposed || this.contextLost)
324
382
  return;
325
- this.Draw(surface.getCanvas());
326
- surface.flush();
327
- this.DrainDisposeQueue();
383
+ this.DrawGuarded(surface.getCanvas(), surface);
328
384
  }
329
385
  Draw(canvas) {
330
386
  const started = performance.now();
331
387
  this.ProcessPendingGestures();
388
+ this.ProcessTextSelectionFrame();
332
389
  // Animators run on the frame's vsync time (inside a rAF callback document.timeline.currentTime is the frame
333
390
  // timestamp), not on when this callback happened to start: callback starts drift by several ms around vsync
334
391
  // (measured 12..23 ms between frames shown 16.7 ms apart), and a game loop moving by speed * delta then
@@ -337,8 +394,8 @@ export class Canvas {
337
394
  this.FrameTimeNanos = Math.round(frameTime * 1_000_000);
338
395
  const executed = this.ExecuteAnimators(Math.round(frameTime * 1_000_000));
339
396
  const canRender = this.CanRender;
340
- if (canRender && !this.WasRendered)
341
- this.WillFirstTimeDraw?.(this, { Canvas: canvas, Surface: this.surface });
397
+ if (canRender && !this.WasRendered && this.WillFirstTimeDraw)
398
+ Canvas.Guard("WillFirstTimeDraw", this.WillFirstTimeDraw, () => this.WillFirstTimeDraw(this, { Canvas: canvas, Surface: this.surface }));
342
399
  canvas.clear(Super.ParseColor(this.BackgroundColor));
343
400
  const root = this.content;
344
401
  if (root) {
@@ -354,6 +411,7 @@ export class Canvas {
354
411
  this.popupLayer.Draw(top, top.Destination);
355
412
  this.tooltipController?.Draw(top, top.Destination);
356
413
  this.AccessibilityManager.OnFrameEnd(this.RenderingScale, this.Element.width, this.Element.height, () => this.Update());
414
+ TextInputProxy.AfterFrame(this);
357
415
  this.Hover.AfterFrame(this.content, this.activeTouchIds.size > 0);
358
416
  if (this.Hover.Paused)
359
417
  this.tooltipController?.Reset(); // content moves under the pointer
@@ -368,7 +426,8 @@ export class Canvas {
368
426
  if (executed > 0)
369
427
  this.Update(); // animators running: keep frames coming
370
428
  // C# order: WasDrawn at the end of the frame, then the frame counts as rendered
371
- this.WasDrawn?.(this);
429
+ if (this.WasDrawn)
430
+ Canvas.Guard("WasDrawn", this.WasDrawn, () => this.WasDrawn(this));
372
431
  const listeners = this.wasDrawnListeners;
373
432
  if (listeners !== undefined && listeners.size > 0)
374
433
  listeners.forEach(this.raiseWasDrawn);
@@ -428,6 +487,46 @@ export class Canvas {
428
487
  }
429
488
  static reportedAnimators = new WeakSet();
430
489
  /** An animator's callback threw: logged once for that animator, the frame goes on. */
490
+ /**
491
+ * One exception never stops the frame loop (drawnui-cross 6ch, DrawnUi.Net 3db4eba3): app code the frame runs (a
492
+ * listener, a gesture handler) is called through this; a throw is logged once per source and the frame goes on.
493
+ */
494
+ static Guard(what, source, call) {
495
+ try {
496
+ return call();
497
+ }
498
+ catch (e) {
499
+ if (!Canvas.reportedSources.has(source)) {
500
+ Canvas.reportedSources.add(source);
501
+ console.error(`DrawnUi: ${what} threw; the frame goes on`, e);
502
+ }
503
+ return undefined;
504
+ }
505
+ }
506
+ static reportedSources = new WeakSet();
507
+ /**
508
+ * Draws a frame and always shows it: a throw inside it (app drawing code) is logged once, the canvas state is reset and
509
+ * the frame loop goes on (a running animation or game keeps asking for frames).
510
+ */
511
+ DrawGuarded(canvas, surface) {
512
+ try {
513
+ this.Draw(canvas);
514
+ }
515
+ catch (e) {
516
+ canvas.restoreToCount(1);
517
+ if (!Canvas.reportedFrameError) {
518
+ Canvas.reportedFrameError = true;
519
+ console.error("DrawnUi: drawing a frame threw; the next frames go on", e);
520
+ }
521
+ if (this.AnimatingControls.size > 0)
522
+ this.Update();
523
+ }
524
+ finally {
525
+ surface.flush();
526
+ this.DrainDisposeQueue();
527
+ }
528
+ }
529
+ static reportedFrameError = false;
431
530
  static ReportAnimatorError(a, e) {
432
531
  if (Canvas.reportedAnimators.has(a))
433
532
  return;
@@ -564,7 +663,12 @@ export class Canvas {
564
663
  this.observer.disconnect();
565
664
  this.content?.Dispose();
566
665
  this.Content = undefined;
666
+ this.DrainDisposeQueue(); // what the last frames handed over for deletion goes while the context lives
567
667
  this.ReleaseSurface();
668
+ // GPU objects that outlive this canvas (a control the app keeps and shows on another canvas) are freed later, under
669
+ // another WebGL context: the abandoned Skia context makes no GL call when they go (as DrawnUi.Net Cells 81b89f1a)
670
+ if (this.grContext && !this.contextLost)
671
+ this.grContext.releaseResourcesAndAbandonContext();
568
672
  this.grContext?.delete();
569
673
  this.grContext = undefined;
570
674
  if (this.glHandle) {
@@ -588,6 +692,163 @@ export class Canvas {
588
692
  pointerDownArgs = new Map();
589
693
  previousTouchArgs = new Map();
590
694
  pendingGestures = [];
695
+ // ---- page text selection with the mouse, as in browsers (port of C# DrawnView.TextSelection, drawnui-cross 6cc) ----
696
+ // A main-button drag that starts on no interactive control selects the selectable labels (AccessibilityTextSelectable)
697
+ // it crosses, in reading order, the first and last partly. Idle cost: one check per gesture and per frame.
698
+ textDrag;
699
+ pageSelection = [];
700
+ pageSelectionKeys = false;
701
+ /**
702
+ * The text selected on the page: the labels a mouse drag selected, joined with new lines, in page order; else the
703
+ * selection of the label in focus. Empty when nothing is selected.
704
+ */
705
+ get SelectedText() {
706
+ if (this.pageSelection.length > 0)
707
+ return this.pageSelection.map((l) => l.SelectedText).filter((t) => t).join("\n");
708
+ return this.focusedChild?.SelectedText ?? "";
709
+ }
710
+ /** True while a mouse drag selection holds page text: the page, not a label, owns Ctrl+C. */
711
+ get OwnsTextSelectionKeys() { return this.pageSelection.some((l) => l.SelectionLength > 0); }
712
+ /** Puts SelectedText on the clipboard through Super.SetClipboardText. False when nothing is selected. */
713
+ CopyTextSelection() {
714
+ const text = this.SelectedText;
715
+ if (!text)
716
+ return false;
717
+ Super.SetClipboardText(text);
718
+ return true;
719
+ }
720
+ /** Drops the page text selection (every label a drag selected) and the selection of the label in focus. */
721
+ ClearTextSelection() {
722
+ for (const label of this.pageSelection)
723
+ label.ClearSelection();
724
+ this.pageSelection.length = 0;
725
+ this.focusedChild?.ClearSelection?.();
726
+ this.AttachPageSelectionKeys(false);
727
+ }
728
+ /**
729
+ * Before a gesture goes to the controls: a mouse drag that is selecting text keeps its moves (a scroll under it does
730
+ * not scroll), a main-button press drops the previous page selection. True when the gesture was used here.
731
+ */
732
+ PreviewTextSelectionGesture(args) {
733
+ const mouseMain = args.Event.Pointer?.DeviceType === "Mouse" && args.Event.Pointer.Button === "Left";
734
+ const drag = this.textDrag;
735
+ if (args.Type === "Down" && mouseMain) {
736
+ this.textDrag = undefined;
737
+ // a press on an open popup window (the menu that copies the selection) keeps it, as browsers keep a selection
738
+ // while their context menu is used
739
+ const onPopup = this.popups.HasInteractive && !!this.popups.At(args.Event.Location.X, args.Event.Location.Y);
740
+ if (this.pageSelection.length > 0 && !onPopup)
741
+ this.ClearTextSelection();
742
+ }
743
+ else if (args.Type === "Panning" && drag) {
744
+ drag.X = args.Event.Location.X;
745
+ drag.Y = args.Event.Location.Y;
746
+ drag.Moved = true;
747
+ this.UpdateTextDrag(drag);
748
+ this.Update();
749
+ return true;
750
+ }
751
+ else if (args.Type === "Up" && drag) {
752
+ if (drag.Moved)
753
+ this.UpdateTextDrag(drag);
754
+ this.textDrag = undefined;
755
+ }
756
+ return false;
757
+ }
758
+ /**
759
+ * After a mouse main-button press went to the controls: when it landed on no interactive control (empty space, a
760
+ * scroll's empty area, a selectable label), a drag from here selects text. Not inside a scroll that holds no
761
+ * selectable text: that one keeps scrolling by mouse drag.
762
+ */
763
+ AfterTextSelectionDown(args, consumed) {
764
+ if (args.Type !== "Down" || args.Event.Pointer?.DeviceType !== "Mouse" || args.Event.Pointer.Button !== "Left")
765
+ return;
766
+ if (consumed && !IsScroll(consumed) && !IsSelectableLabel(consumed))
767
+ return;
768
+ if (consumed && IsScroll(consumed) && consumed.ScrollBarOwnsGesture)
769
+ return; // a press on the scroll bar drags the bar
770
+ const collected = [];
771
+ if (this.content)
772
+ CollectSelectable(this.content, collected);
773
+ if (collected.length === 0)
774
+ return;
775
+ const labels = ReadingOrder(collected);
776
+ const x = args.Event.Location.X, y = args.Event.Location.Y;
777
+ const scroll = (consumed && IsScroll(consumed) ? consumed : undefined) ?? ScrollOf(consumed ?? undefined) ?? ScrollAt(labels, x, y);
778
+ if (scroll && !labels.some((l) => IsInside(l, scroll)))
779
+ return;
780
+ const [label, index] = Locate(labels, x, y);
781
+ if (!label)
782
+ return;
783
+ this.textDrag = { Labels: labels, StartLabel: label, StartIndex: index, Scroll: scroll, X: x, Y: y, Moved: false };
784
+ }
785
+ /** Frame start: while a drag holds the pointer past a scroll's top or bottom edge, the scroll follows it. */
786
+ ProcessTextSelectionFrame() {
787
+ const drag = this.textDrag;
788
+ if (!drag?.Scroll || !drag.Moved)
789
+ return;
790
+ const view = drag.Scroll.GetAccessibilityPixelRect();
791
+ if (view.Width <= 0 || view.Height <= 0)
792
+ return;
793
+ const past = drag.Y > view.Bottom ? drag.Y - view.Bottom : drag.Y < view.Top ? drag.Y - view.Top : 0;
794
+ if (past === 0)
795
+ return;
796
+ // faster the farther past the edge, as browsers do
797
+ const step = Math.min(Math.max(Math.abs(past) * 0.25, 2), 40) / this.RenderingScale * Math.sign(past);
798
+ drag.Scroll.ScrollTo(drag.Scroll.ViewportOffsetX, drag.Scroll.ViewportOffsetY - step, 0, true);
799
+ this.UpdateTextDrag(drag);
800
+ this.Update();
801
+ }
802
+ UpdateTextDrag(drag) {
803
+ const [endLabel, endIndex] = Locate(drag.Labels, drag.X, drag.Y);
804
+ if (!endLabel)
805
+ return;
806
+ const a = drag.Labels.indexOf(drag.StartLabel), b = drag.Labels.indexOf(endLabel);
807
+ if (a < 0 || b < 0)
808
+ return;
809
+ const [fromLabel, fromIndex, toLabel, toIndex] = a < b || (a === b && drag.StartIndex <= endIndex)
810
+ ? [a, drag.StartIndex, b, endIndex] : [b, endIndex, a, drag.StartIndex];
811
+ for (const label of this.pageSelection) {
812
+ const at = drag.Labels.indexOf(label);
813
+ if (at < fromLabel || at > toLabel)
814
+ label.ClearSelection();
815
+ }
816
+ this.pageSelection.length = 0;
817
+ for (let i = fromLabel; i <= toLabel; i++) {
818
+ const label = drag.Labels[i];
819
+ const start = i === fromLabel ? fromIndex : 0, end = i === toLabel ? toIndex : label.SelectableLength;
820
+ label.Select(start, Math.max(0, end - start));
821
+ this.pageSelection.push(label);
822
+ }
823
+ this.AttachPageSelectionKeys(this.OwnsTextSelectionKeys);
824
+ }
825
+ AttachPageSelectionKeys(attach) {
826
+ if (attach === this.pageSelectionKeys)
827
+ return;
828
+ this.pageSelectionKeys = attach;
829
+ if (attach)
830
+ KeyboardManager.Subscribe(this.onPageSelectionKey, Canvas.noChar);
831
+ else
832
+ KeyboardManager.Unsubscribe(this.onPageSelectionKey, Canvas.noChar);
833
+ }
834
+ static noChar = () => { };
835
+ onPageSelectionKey = (key, e) => {
836
+ // Esc keeps the selection, as in browsers: a click elsewhere drops it
837
+ if (KeyboardManager.IsOwnedByElement(e) || !KeyboardManager.IsControlPressed)
838
+ return;
839
+ if (key === "KeyC")
840
+ this.CopyTextSelection();
841
+ else if (key === "KeyA" && IsSelectableLabel(this.focusedChild)) {
842
+ // as for one label: Ctrl+A selects all of the label in focus
843
+ e.preventDefault();
844
+ const focused = this.focusedChild;
845
+ for (const label of this.pageSelection)
846
+ label.ClearSelection();
847
+ this.pageSelection.length = 0;
848
+ this.AttachPageSelectionKeys(false);
849
+ focused.SelectAll();
850
+ }
851
+ };
591
852
  onPointer = (e) => {
592
853
  const type = e.type === "pointerdown" ? "Pressed" :
593
854
  e.type === "pointermove" ? "Moved" :
@@ -622,6 +883,7 @@ export class Canvas {
622
883
  }
623
884
  const rect = this.Element.getBoundingClientRect();
624
885
  const args = new TouchActionEventArgs();
886
+ args.Timestamp = EventTime(e);
625
887
  args.Id = e.pointerId;
626
888
  args.Type = type;
627
889
  args.Scale = this.RenderingScale;
@@ -639,6 +901,16 @@ export class Canvas {
639
901
  this.claimedTouches.delete(e.pointerId);
640
902
  this.UpdateTouchAction();
641
903
  }
904
+ // a click on the focused field gives its input DOM focus again (desktop typing and IME); on touch that is done in the
905
+ // touchend (onTouchEnd), the only event iOS opens the soft keyboard from
906
+ if (type === "Released" && e.pointerType === "mouse")
907
+ TextInputProxy.FocusFromTap(e.clientX, e.clientY);
908
+ // the touch taps the browser delivered, for a double tap whose second tap it kept (onDoubleClick)
909
+ if (type === "Released" && e.pointerType !== "mouse" && !this.replayingTap) {
910
+ this.touchUps.push(performance.now());
911
+ if (this.touchUps.length > 2)
912
+ this.touchUps.shift();
913
+ }
642
914
  };
643
915
  /** The mouse left the canvas: hover ends at once, also while content moves. */
644
916
  onPointerLeave = (e) => {
@@ -650,6 +922,99 @@ export class Canvas {
650
922
  this.popups.PointerAt(undefined, undefined);
651
923
  };
652
924
  preventTouch = (e) => e.preventDefault();
925
+ // ---- the on-screen keyboard (C# DrawnView.Keyboard, drawnui-cross 6cj: SkiaScroll.AdaptToKeyboardAuto, off by default) ----
926
+ /** Points of this canvas, up from its bottom, that the on-screen keyboard covers (0 when none). */
927
+ KeyboardCovered = 0;
928
+ keyboardScroll;
929
+ onViewportResize = () => this.ApplyKeyboard();
930
+ /**
931
+ * Measures the part the keyboard covers (the visual viewport shrinks when it opens) and, when the focused text field sits
932
+ * in a scroll with AdaptToKeyboardAuto, hands that scroll the field and the covered height; the scroll it adapted lets go
933
+ * when the keyboard closes or the field moves to another scroll. Runs on viewport resizes and focus changes only.
934
+ */
935
+ ApplyKeyboard() {
936
+ const vv = typeof window !== "undefined" ? window.visualViewport : undefined;
937
+ if (!vv)
938
+ return;
939
+ const layoutH = window.innerHeight, visibleBottom = vv.offsetTop + vv.height;
940
+ // a keyboard shrinks the visual viewport against the layout viewport (a page zoom scales it instead)
941
+ const keyboard = layoutH - vv.height > 80 && Math.abs((vv.scale ?? 1) - 1) < 0.01;
942
+ const b = this.Element.getBoundingClientRect();
943
+ const covered = keyboard ? Math.max(0, Math.min(b.bottom, layoutH) - visibleBottom) : 0;
944
+ this.KeyboardCovered = covered;
945
+ let scroll;
946
+ const field = this.focusedChild;
947
+ if (covered > 0 && field?.IsFocused && field.CursorPosition !== undefined) {
948
+ for (let p = field.Parent; p && !scroll; p = p.Parent)
949
+ if (p.AdaptToKeyboardAuto)
950
+ scroll = p;
951
+ if (scroll) {
952
+ scroll.AdaptToKeyboardFor = field;
953
+ scroll.AdaptToKeyboardSize = covered;
954
+ }
955
+ }
956
+ // keyboard up, no focused field (between two fields): the adapted scroll keeps its room
957
+ if (covered > 0 && !scroll)
958
+ return;
959
+ if (this.keyboardScroll && this.keyboardScroll !== scroll) {
960
+ this.keyboardScroll.AdaptToKeyboardFor = undefined;
961
+ this.keyboardScroll.AdaptToKeyboardSize = 0;
962
+ }
963
+ this.keyboardScroll = scroll;
964
+ }
965
+ /** When the last touch releases came (ms), at most two. */
966
+ touchUps = [];
967
+ replayingTap = false;
968
+ /**
969
+ * iOS Safari (measured on an iPhone, iOS 26.6.1) takes the second tap of a double tap for itself: it sends no pointer or
970
+ * touch event for it, only click, click and dblclick. A dblclick that follows ONE touch release (the other browsers
971
+ * deliver both taps first) brings that tap back as a touch tap at its place, so a double tap reaches the controls (a 3D
972
+ * viewer's reset).
973
+ */
974
+ onDoubleClick = (e) => {
975
+ const now = performance.now(), recent = this.touchUps.filter((t) => now - t < 700);
976
+ this.touchUps.length = 0;
977
+ if (recent.length !== 1)
978
+ return;
979
+ const pointerId = 2_000_000 + Math.floor(now);
980
+ // the second tap happened just before the dblclick: dated by it, not by when a frame handles it
981
+ const at = { clientX: e.clientX, clientY: e.clientY, pointerId, pointerType: "touch", button: 0, isTrusted: true, timeStamp: e.timeStamp };
982
+ this.replayingTap = true;
983
+ try {
984
+ this.onPointer({ ...at, type: "pointerdown", buttons: 1 });
985
+ this.onPointer({ ...at, type: "pointerup", buttons: 0 });
986
+ }
987
+ finally {
988
+ this.replayingTap = false;
989
+ }
990
+ };
991
+ /**
992
+ * The end of a touch is the user gesture iOS opens the soft keyboard from (a focus() in pointerup does not open it): the
993
+ * press's gestures run now, inside it, so a field they focus focuses its input here, and a tap on the focused field gives
994
+ * its input focus again.
995
+ */
996
+ /** A finger or a pen is down on the canvas: a text field focused now takes its input focus in the touchend (onTouchEnd). */
997
+ get TouchPressed() {
998
+ for (const a of this.pointerDownArgs.values())
999
+ if (a.Pointer && a.Pointer.DeviceType !== "Mouse")
1000
+ return true;
1001
+ return false;
1002
+ }
1003
+ onTouchEnd = (e) => {
1004
+ TextInputProxy.TouchGesture = true;
1005
+ try {
1006
+ this.ProcessPendingGestures();
1007
+ }
1008
+ finally {
1009
+ TextInputProxy.TouchGesture = false;
1010
+ }
1011
+ const t = e.changedTouches[0];
1012
+ // the input has focus now: no compatibility mouse events or click follow, whose focus would go to the canvas host
1013
+ // and take the keyboard away again
1014
+ if (t && TextInputProxy.FocusFromTap(t.clientX, t.clientY, true) && e.cancelable)
1015
+ e.preventDefault();
1016
+ TextInputProxy.AfterTouchEnd();
1017
+ };
653
1018
  /** Gestures="Enabled": touch / pen pointers whose Down a control used (see OnTouchAction). */
654
1019
  claimedTouches = new Set();
655
1020
  /** Gestures="Enabled": while a claimed touch is down the page may not take its drag. */
@@ -659,20 +1024,38 @@ export class Canvas {
659
1024
  ContextMenu;
660
1025
  /** DOM contextmenu (right click / long press / Menu key) -> ContextMenuEventArgs routed through the tree like a tap. */
661
1026
  onContextMenu = (e) => {
662
- const rect = this.Element.getBoundingClientRect();
663
- const scale = this.RenderingScale;
664
- const x = e.clientX - rect.left, y = e.clientY - rect.top;
665
1027
  const pointerType = e.pointerType;
666
1028
  const source = pointerType === "touch" || pointerType === "pen" ? "touch" : pointerType === "mouse" || e.button === 2 ? "mouse" : "keyboard";
667
- const args = new ContextMenuEventArgs(new SKPoint(x, y), new SKPoint(x * scale, y * scale), source, e);
1029
+ // a long press: the browser's own contextmenu (Android Chrome) or the press's long press raises it, once (6cb); a
1030
+ // browser menu that comes first runs the long press before it (the word under the finger is selected first, C#
1031
+ // SkipRepeatedTouchContextMenu)
1032
+ if (source === "touch")
1033
+ this.LongPressNow(e.pointerId);
1034
+ // the press the menu comes from first, in the order it happened (the browser sends contextmenu at the press on macOS
1035
+ // and Linux, after the release on Windows): processed later, its press would close a menu that opens now
1036
+ else
1037
+ this.ProcessPendingGestures();
1038
+ const handled = source === "touch" ? this.RaiseTouchContextMenu(e.clientX, e.clientY, e) : this.RaiseContextMenu(e.clientX, e.clientY, source, e);
1039
+ if (handled)
1040
+ e.preventDefault();
1041
+ // a menu opened while the button is still down: the rest of that press goes nowhere (its release would tap the item
1042
+ // under it)
1043
+ if (source !== "touch" && this.activeTouchIds.size > 0 && this.popups.HasInteractive)
1044
+ this.popups.SwallowPress = true;
1045
+ };
1046
+ /** A context-menu request at a point of the page (client CSS px), routed through the tree like a tap; true when one took it. */
1047
+ RaiseContextMenu(clientX, clientY, source, native) {
1048
+ const rect = this.Element.getBoundingClientRect();
1049
+ const scale = this.RenderingScale;
1050
+ const x = clientX - rect.left, y = clientY - rect.top;
1051
+ const args = new ContextMenuEventArgs(new SKPoint(x, y), new SKPoint(x * scale, y * scale), source, native);
668
1052
  this.tooltip.CloseOnInput();
669
1053
  // over a popup window its content is asked; elsewhere the popups close and the request goes on, so a new menu can
670
1054
  // open there (C# PopupLayer ContextMenu)
671
1055
  const popupRoot = this.popups.HasInteractive ? this.popups.At(args.Pixels.X, args.Pixels.Y) : undefined;
672
1056
  if (popupRoot) {
673
1057
  popupRoot.ProcessContextMenu(args.Pixels, args);
674
- e.preventDefault();
675
- return;
1058
+ return true;
676
1059
  }
677
1060
  if (this.popups.HasInteractive) {
678
1061
  this.popups.CloseAll(false);
@@ -681,9 +1064,31 @@ export class Canvas {
681
1064
  let handled = this.content?.ProcessContextMenu(args.Pixels, args) ?? false;
682
1065
  if (!handled && this.ContextMenu)
683
1066
  handled = this.ContextMenu(this, args) === true;
684
- if (handled)
685
- e.preventDefault();
686
- };
1067
+ return handled;
1068
+ }
1069
+ // ---- a long press on touch raises the context menu (drawnui-cross 6cb) ----
1070
+ /** The touch or pen press going on: its context menu is raised once, by the browser's contextmenu or after its long press. */
1071
+ touchMenu;
1072
+ /** The long press of a touch press was just queued: its context menu follows the LongPressing (client CSS px). */
1073
+ touchMenuDue;
1074
+ /** A touch or pen press starts (on the canvas, or on selectable text of the overlay): its context menu is still to come. */
1075
+ StartTouchPress() { this.touchMenu = { Raised: false, Handled: false }; }
1076
+ /**
1077
+ * The context menu of the touch press now (Source "touch"), raised once per press whoever asks first: the browser's own
1078
+ * contextmenu (Android Chrome fires one on a long press) or the press's long press (iOS Safari fires none). True when a
1079
+ * handler took it: the browser's own menu is then suppressed where it can be.
1080
+ */
1081
+ RaiseTouchContextMenu(clientX, clientY, native) {
1082
+ const press = this.touchMenu ??= { Raised: false, Handled: false };
1083
+ if (press.Raised)
1084
+ return press.Handled;
1085
+ press.Raised = true;
1086
+ press.Handled = this.RaiseContextMenu(clientX, clientY, "touch", native ?? new MouseEvent("contextmenu", { clientX, clientY }));
1087
+ // the app showed its own menu: the drawn Copy button of a touch text selection steps aside, the selection stays
1088
+ if (press.Handled)
1089
+ this.focusedChild?.HideCopyButton?.();
1090
+ return press.Handled;
1091
+ }
687
1092
  /**
688
1093
  * The Menu key, Shift+F10 or a screen reader's context-menu command on the control in keyboard focus (the DOM
689
1094
  * `contextmenu` of its overlay node): its context menu opens at its center, from it or the first control above it with
@@ -737,6 +1142,7 @@ export class Canvas {
737
1142
  onWheel = (e) => {
738
1143
  const rect = this.Element.getBoundingClientRect();
739
1144
  const args = new TouchActionEventArgs();
1145
+ args.Timestamp = EventTime(e);
740
1146
  args.Id = -1;
741
1147
  args.Type = "Wheel";
742
1148
  args.Scale = this.RenderingScale;
@@ -746,7 +1152,8 @@ export class Canvas {
746
1152
  // horizontal events alone (C# ba03cb20)
747
1153
  const factor = e.deltaMode === 1 ? 40 : e.deltaMode === 2 ? 800 : 1;
748
1154
  const horizontal = Math.abs(e.deltaX) > Math.abs(e.deltaY);
749
- args.Wheel = { Delta: (horizontal ? e.deltaX : e.deltaY) * factor, IsHorizontal: horizontal };
1155
+ // IsZoom: Ctrl / Cmd + wheel, or a touchpad pinch (browsers send it as a Ctrl wheel), as AppoMobi WheelEventArgs.IsZoom
1156
+ args.Wheel = { Delta: (horizontal ? e.deltaX : e.deltaY) * factor, IsHorizontal: horizontal, IsZoom: e.ctrlKey || e.metaKey };
750
1157
  this.tooltip.CloseOnInput();
751
1158
  if (this.gestures === "Lock") {
752
1159
  e.preventDefault();
@@ -765,7 +1172,8 @@ export class Canvas {
765
1172
  ProcessGestureNow(args, result) {
766
1173
  this.ProcessPendingGestures();
767
1174
  const root = this.content;
768
- const consumed = root ? this.ProcessGestures(root, SkiaGesturesParameters.Create(result, args)) : null;
1175
+ const gesture = SkiaGesturesParameters.Create(result, args);
1176
+ const consumed = root ? Canvas.Guard("a gesture handler", root, () => this.ProcessGestures(root, gesture)) ?? null : null;
769
1177
  this.Update();
770
1178
  return !!consumed && (args.Handled || !consumed.BlockGesturesBelow);
771
1179
  }
@@ -824,6 +1232,9 @@ export class Canvas {
824
1232
  el.addEventListener(t, this.onPointer);
825
1233
  el.addEventListener("pointerleave", this.onPointerLeave);
826
1234
  el.addEventListener("contextmenu", this.onContextMenu);
1235
+ el.addEventListener("touchend", this.onTouchEnd);
1236
+ el.addEventListener("dblclick", this.onDoubleClick);
1237
+ window.visualViewport?.addEventListener("resize", this.onViewportResize);
827
1238
  el.addEventListener("wheel", this.onWheel, { passive: false });
828
1239
  window.addEventListener("blur", this.onWindowBlur);
829
1240
  if (this.gestures === "Lock") {
@@ -853,6 +1264,10 @@ export class Canvas {
853
1264
  el.removeEventListener(t, this.onPointer);
854
1265
  el.removeEventListener("pointerleave", this.onPointerLeave);
855
1266
  el.removeEventListener("contextmenu", this.onContextMenu);
1267
+ el.removeEventListener("touchend", this.onTouchEnd);
1268
+ el.removeEventListener("dblclick", this.onDoubleClick);
1269
+ window.visualViewport?.removeEventListener("resize", this.onViewportResize);
1270
+ this.textDrag = undefined;
856
1271
  el.removeEventListener("wheel", this.onWheel);
857
1272
  window.removeEventListener("blur", this.onWindowBlur);
858
1273
  el.removeEventListener("touchmove", this.preventTouch);
@@ -879,6 +1294,8 @@ export class Canvas {
879
1294
  return;
880
1295
  const id = args.Id;
881
1296
  if (args.Type === "Pressed") {
1297
+ if (args.Pointer && args.Pointer.DeviceType !== "Mouse" && this.activeTouchIds.size === 0)
1298
+ this.StartTouchPress();
882
1299
  this.ArmLongPress(id, args);
883
1300
  // the fingers for pinch and turn: the first restarts them, a second one makes the press no tap
884
1301
  if (this.activeTouchIds.size === 0) {
@@ -961,10 +1378,30 @@ export class Canvas {
961
1378
  this.longPressed.add(id);
962
1379
  const args = new TouchActionEventArgs();
963
1380
  Object.assign(args, down);
1381
+ args.Timestamp = performance.now();
964
1382
  args.Location = down.StartingLocation ?? down.Location;
965
1383
  this.OnGestureEvent(args, "LongPressing");
1384
+ // on touch the context menu follows the long press (6cb), at the press point
1385
+ if (down.Pointer && down.Pointer.DeviceType !== "Mouse") {
1386
+ const rect = this.Element.getBoundingClientRect(), scale = Math.max(0.1, this.RenderingScale);
1387
+ this.touchMenuDue = { X: rect.left + args.Location.X / scale, Y: rect.top + args.Location.Y / scale };
1388
+ }
966
1389
  }, Canvas.LongPressTimeMs));
967
1390
  }
1391
+ /** The long press of a held touch, now (the browser's context menu came first): queued gestures first, then it; its timer is off. */
1392
+ LongPressNow(pointerId) {
1393
+ const id = this.longPressTimers.has(pointerId) ? pointerId : [...this.longPressTimers.keys()][0];
1394
+ const down = id === undefined ? undefined : this.pointerDownArgs.get(id);
1395
+ if (id === undefined || !down || !this.activeTouchIds.has(id))
1396
+ return;
1397
+ this.DisarmLongPress(id);
1398
+ this.longPressed.add(id);
1399
+ const args = new TouchActionEventArgs();
1400
+ Object.assign(args, down);
1401
+ args.Timestamp = performance.now();
1402
+ args.Location = down.StartingLocation ?? down.Location;
1403
+ this.ProcessGestureNow(args, "LongPressing");
1404
+ }
968
1405
  DisarmLongPress(id) {
969
1406
  const t = this.longPressTimers.get(id);
970
1407
  if (t) {
@@ -984,7 +1421,12 @@ export class Canvas {
984
1421
  if (!root)
985
1422
  return;
986
1423
  for (const args of batch)
987
- this.ProcessGestures(root, args);
1424
+ Canvas.Guard("a gesture handler", root, () => this.ProcessGestures(root, args));
1425
+ const due = this.touchMenuDue;
1426
+ if (due) {
1427
+ this.touchMenuDue = undefined;
1428
+ this.RaiseTouchContextMenu(due.X, due.Y);
1429
+ }
988
1430
  }
989
1431
  /**
990
1432
  * The control that consumed the current gesture keeps it until it lets go (DrawnUi Canvas.HadInput). Without this a
@@ -1070,6 +1512,9 @@ export class Canvas {
1070
1512
  this.focusedChild = value;
1071
1513
  if (previous && !previous.IsDisposed)
1072
1514
  previous.SetFrameworkFocus(false);
1515
+ // the keyboard is up: the newly focused field goes above it too
1516
+ if (this.KeyboardCovered > 0)
1517
+ setTimeout(() => this.ApplyKeyboard(), 50);
1073
1518
  }
1074
1519
  finally {
1075
1520
  this.reportingFocus = false;
@@ -1081,7 +1526,12 @@ export class Canvas {
1081
1526
  this.focusAtDown = this.focusedChild;
1082
1527
  this.tapNoFocus = false;
1083
1528
  }
1529
+ // a mouse drag selecting page text keeps its moves: no scroll under it scrolls (the pointer is captured, so the
1530
+ // moves keep coming once it leaves the canvas and the scroll follows it past its edge)
1531
+ if (this.PreviewTextSelectionGesture(args))
1532
+ return null;
1084
1533
  const consumed = this.RouteGestures(root, args);
1534
+ this.AfterTextSelectionDown(args, consumed);
1085
1535
  // a control that claimed the focus during this gesture (an editor on its press) keeps it; a press that closed the
1086
1536
  // popup windows moves nothing
1087
1537
  if (args.Type === "Tapped" && !this.tapNoFocus && this.focusedChild === this.focusAtDown)
@@ -1144,8 +1594,96 @@ export class Canvas {
1144
1594
  }
1145
1595
  }
1146
1596
  const consumed = root.ProcessGestures(args, info());
1147
- if (!wheel)
1597
+ // a mouse move never makes an owner (DrawnUi.Net 62608ca7): a focused field takes every hover, and as the owner it
1598
+ // would get the next release of a press that began elsewhere
1599
+ if (!wheel && args.Type !== "Pointer")
1148
1600
  this.gestureOwner = consumed && args.Type !== "Up" ? consumed : null;
1149
1601
  return consumed;
1150
1602
  }
1151
1603
  }
1604
+ const IsScroll = (c) => !!c && "ScrollTo" in c && "ViewportOffsetY" in c;
1605
+ /**
1606
+ * When a browser event happened, on the performance.now() clock (its timeStamp; now when the browser gives none or a time
1607
+ * from another clock): a slow frame holds events back, and gestures are dated by when they happened.
1608
+ */
1609
+ function EventTime(e) {
1610
+ const now = performance.now(), t = e.timeStamp;
1611
+ return typeof t === "number" && t > 0 && t <= now + 1 ? t : now;
1612
+ }
1613
+ const IsSelectableLabel = (c) => !!c && c.AccessibilityTextSelectable && "TextIndexAtCanvas" in c;
1614
+ /**
1615
+ * Where a canvas point falls in the labels' order: inside a drawn label, its text index there; before a label (above it,
1616
+ * or left of it on its line), that label's start; past them all, the end of the last drawn one.
1617
+ */
1618
+ function Locate(labels, x, y) {
1619
+ let last;
1620
+ for (const label of labels) {
1621
+ const r = label.GetAccessibilityPixelRect();
1622
+ if (r.Width <= 0 || r.Height <= 0)
1623
+ continue;
1624
+ if (x >= r.Left && x < r.Right && y >= r.Top && y < r.Bottom)
1625
+ return [label, label.TextIndexAtCanvas(x, y)];
1626
+ if (y < r.Top || (y <= r.Bottom && x < r.Left))
1627
+ return [label, 0];
1628
+ last = label;
1629
+ }
1630
+ return last ? [last, last.SelectableLength] : [undefined, 0];
1631
+ }
1632
+ /**
1633
+ * The labels in reading order, as they are seen (the tree order is not: a page in two columns lists the left column's
1634
+ * labels first, and a drag in the right column would then start at a label below it): by top, labels whose tops are within
1635
+ * half the smaller height forming one line, read left to right.
1636
+ */
1637
+ function ReadingOrder(labels) {
1638
+ const rects = new Map(labels.map((l) => [l, l.GetAccessibilityPixelRect()]));
1639
+ const sorted = [...labels].sort((a, b) => rects.get(a).Top - rects.get(b).Top || rects.get(a).Left - rects.get(b).Left);
1640
+ for (let start = 0, i = 1; i <= sorted.length; i++) {
1641
+ if (i < sorted.length) {
1642
+ const first = rects.get(sorted[start]), next = rects.get(sorted[i]);
1643
+ if (next.Top - first.Top < Math.min(first.Height, next.Height) / 2)
1644
+ continue;
1645
+ }
1646
+ if (i - start > 1) {
1647
+ const line = sorted.slice(start, i).sort((a, b) => rects.get(a).Left - rects.get(b).Left);
1648
+ for (let k = 0; k < line.length; k++)
1649
+ sorted[start + k] = line[k];
1650
+ }
1651
+ start = i;
1652
+ }
1653
+ return sorted;
1654
+ }
1655
+ function CollectSelectable(control, into) {
1656
+ if (!control.IsVisible || control.IsDisposed)
1657
+ return;
1658
+ if (IsSelectableLabel(control))
1659
+ into.push(control);
1660
+ const views = "Views" in control ? control.Views : undefined;
1661
+ const content = views ? undefined : control.Content;
1662
+ for (const child of views ?? (content ? [content] : []))
1663
+ CollectSelectable(child, into);
1664
+ }
1665
+ function ScrollOf(control) {
1666
+ for (let p = control?.Parent; p; p = p.Parent)
1667
+ if (IsScroll(p))
1668
+ return p;
1669
+ return undefined;
1670
+ }
1671
+ /** The innermost drawn scroll around a selectable label whose viewport holds the point. */
1672
+ function ScrollAt(labels, x, y) {
1673
+ for (const label of labels) {
1674
+ for (let p = label.Parent; p; p = p.Parent) {
1675
+ if (!IsScroll(p))
1676
+ continue;
1677
+ const r = p.GetAccessibilityPixelRect();
1678
+ if (x >= r.Left && x < r.Right && y >= r.Top && y < r.Bottom)
1679
+ return p;
1680
+ }
1681
+ }
1682
+ return undefined;
1683
+ }
1684
+ function IsInside(control, ancestor) {
1685
+ for (let p = control.Parent; p; p = p.Parent)
1686
+ if (p === ancestor)
1687
+ return true;
1688
+ return false;
1689
+ }