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
@@ -45,6 +45,9 @@ export declare class SkiaLabel extends SkiaControl {
45
45
  private lineSpacing;
46
46
  private lineHeight;
47
47
  private paragraphSpacing;
48
+ private lineHeightUniform;
49
+ /** The height every line takes under LineHeightUniform: the tallest laid-out line's (C# MeasuredLineHeight). */
50
+ private uniformLineHeight;
48
51
  private characterSpacing;
49
52
  private textTransform;
50
53
  private padding;
@@ -52,6 +55,10 @@ export declare class SkiaLabel extends SkiaControl {
52
55
  readonly Spans: TextSpan[];
53
56
  /** Set to `Aria.RoleText` to expose every label to screen readers (React extension; C# is opt-in per control). */
54
57
  static DefaultAccessibilityRole?: string;
58
+ /** Highlight drawn over selected text (AccessibilityTextSelectable), for every label (C# SkiaLabel.TextSelectionColor). */
59
+ static TextSelectionColor: Color;
60
+ /** Caption of the Copy button shown over a touch selection (C# SkiaLabel.CopyButtonText). */
61
+ static CopyButtonText: string;
55
62
  /** Like C# OnTextInternalChanged: the spoken label is the text (or the joined spans) unless AccessibilityLabel is set. */
56
63
  protected DefaultAccessibilityLabel(): string | undefined;
57
64
  constructor();
@@ -90,11 +97,14 @@ export declare class SkiaLabel extends SkiaControl {
90
97
  get LineHeight(): number;
91
98
  set LineHeight(v: number);
92
99
  /**
93
- * Space above every paragraph after the first, in lines (C# ParagraphSpacing; 0 here as DrawnUi.Rust, C# 0.25). A
100
+ * Space above every paragraph after the first, in lines of the label's font (C# ParagraphSpacing, default 0.25). A
94
101
  * paragraph starts after "\n"; U+2028 breaks the line inside a paragraph.
95
102
  */
96
103
  get ParagraphSpacing(): number;
97
104
  set ParagraphSpacing(v: number);
105
+ /** LineHeightUniform: true = every line as tall as the tallest one; false (default in every engine) = each line its own height. */
106
+ get LineHeightUniform(): boolean;
107
+ set LineHeightUniform(v: boolean);
98
108
  /**
99
109
  * C# CharacterSpacing: 1 = the font's own spacing; every glyph is followed by (CharacterSpacing - 1) points more
100
110
  * (less below 1), the line measures and wraps at the width it draws (C# 7cf1007c, DrawnUi.Rust label).
@@ -202,8 +212,17 @@ export declare class SkiaLabel extends SkiaControl {
202
212
  * in logical order, reversed in a right-to-left run.
203
213
  */
204
214
  private static PlaceRuns;
215
+ /** A line's own height: its spans' largest ascent plus largest descent, times LineHeight, rounded once to whole pixels. */
216
+ private OwnLineHeight;
205
217
  private LineHeightPx;
206
- /** ParagraphSpacing above a line that starts a paragraph (after the first): that many of its lines, with their spacing. */
218
+ /** C# GetSpaceBetweenLines: the label font's Leading times LineSpacing when it has one, else (LineSpacing - 1) lines. */
219
+ private LineGap;
220
+ /**
221
+ * As DrawnUi.Net: the baseline sits at the line's bottom minus its descent, the extra space of LineHeight split above and
222
+ * below the text (a line of lh pixels holds lh / LineHeight of text).
223
+ */
224
+ private BaselineIn;
225
+ /** ParagraphSpacing above a line that starts a paragraph (after the first): that many lines of the label's font, with their gap. */
207
226
  private ParagraphSpace;
208
227
  private BlockHeight;
209
228
  /** Pixels of ink outside the lines' advance boxes, at their left and right ends (side bearings, italic slant). */
@@ -234,8 +253,6 @@ export declare class SkiaLabel extends SkiaControl {
234
253
  get MeasuredLineHeight(): number;
235
254
  /** Line origins relative to DrawingRect, same alignment math as Paint. */
236
255
  private LineGeometry;
237
- /** Lines as CSS text for the accessibility overlay (AccessibilityTextSelectable): font alias chain, weight and px size. */
238
- GetAccessibilityTextLines(scale: number): import("../core/Accessibility").AccessibilityTextLine[];
239
256
  /** Laid-out lines with their text index range (a wrapped-away space or line break sits between two lines). */
240
257
  GetLineBoxes(): LineBox[];
241
258
  /** Every code point of the laid-out text with its box (editor caret / selection / hit testing). */
@@ -267,4 +284,68 @@ export declare class SkiaLabel extends SkiaControl {
267
284
  WantsPointerCursor(x: number, y: number): boolean;
268
285
  /** Return null to not consume the tap. */
269
286
  protected OnSpanTapped(span: TextSpan, args: SkiaGesturesParameters, apply: GestureEventProcessingInfo): SkiaControl | null;
287
+ get AccessibilityTextSelectable(): boolean;
288
+ /**
289
+ * The text can be selected and copied, drawn by the label (C# SkiaLabel.AccessibilityTextSelectable). Mouse: drag, double
290
+ * click for a word. Touch: long press picks a word, keep the finger down and drag to extend, a Copy button appears.
291
+ * Ctrl+C (Cmd+C) copies and Ctrl+A selects all while the label holds the selection; a click elsewhere drops it. The label
292
+ * takes the mouse press for this, so it no longer reaches controls under it. Free when off. Copies through
293
+ * Super.SetClipboardText.
294
+ */
295
+ set AccessibilityTextSelectable(v: boolean);
296
+ private selStart;
297
+ private selLength;
298
+ private anchorStart;
299
+ private anchorEnd;
300
+ private selecting;
301
+ private touchSelection;
302
+ private copyPressed;
303
+ private selectionKeysAttached;
304
+ private lastClickMs;
305
+ private lastClickX;
306
+ private lastClickY;
307
+ /** The drawn Copy button of a touch selection, relative to DrawingRect (pixels); empty when none. */
308
+ private copyButton;
309
+ private selectionMap?;
310
+ /** Start of the selection in the selection's source: the label's text when the lines are taken from it verbatim, else the drawn lines joined. */
311
+ get SelectionStart(): number;
312
+ /** Length of the selection, 0 when nothing is selected. */
313
+ get SelectionLength(): number;
314
+ /** The selected text, empty when nothing is selected. */
315
+ get SelectedText(): string;
316
+ /** Selects a range of the text (see SelectionStart for what the indexes count). */
317
+ Select(start: number, length: number): void;
318
+ /** Selects all of the text. */
319
+ SelectAll(): void;
320
+ /** Drops the selection. */
321
+ ClearSelection(): void;
322
+ /** The app showed its own context menu for a touch selection: the drawn Copy button steps aside, the selection stays. */
323
+ HideCopyButton(): void;
324
+ /** Puts the selected text on the clipboard through Super.SetClipboardText. False when nothing is selected. */
325
+ CopySelection(): boolean;
326
+ /** A selectable label takes the canvas focus while text is selected and drops the selection when the focus moves elsewhere. */
327
+ SetFrameworkFocus(focus: boolean): boolean;
328
+ protected OnDisposing(): void;
329
+ private AttachSelectionKeys;
330
+ private static readonly noChar;
331
+ private readonly onSelectionKey;
332
+ private ClaimSelectionFocus;
333
+ /**
334
+ * The drawn lines mapped onto a source string: the label's text when every line is found in it in order (spaces
335
+ * dropped at wraps are then copied too), else the lines joined, a new line between paragraphs. Each line keeps its
336
+ * caret slots (x of each text index, relative to DrawingRect) and its glyph boxes.
337
+ */
338
+ private SelectionMap;
339
+ /** Text index under a point relative to DrawingRect (pixels). */
340
+ private IndexAt;
341
+ private WordAt;
342
+ /** Text index under a canvas point (pixels), for a selection a page drag makes across labels. */
343
+ TextIndexAtCanvas(x: number, y: number): number;
344
+ /** Length of the text a selection counts in (see SelectionStart). */
345
+ get SelectableLength(): number;
346
+ private SelectBetween;
347
+ /** Pointer handling of AccessibilityTextSelectable; null lets the gesture go on as usual. */
348
+ protected ProcessTextSelection(args: SkiaGesturesParameters, apply: GestureEventProcessingInfo): SkiaControl | null;
349
+ /** Draws the selection over the lines just drawn, and the Copy button of a touch selection. */
350
+ protected DrawTextSelection(ctx: DrawingContext): void;
270
351
  }
@@ -1,5 +1,6 @@
1
1
  import { SkiaControl } from "../core/SkiaControl";
2
2
  import { ControlTappedEventArgs } from "../core/Gestures";
3
+ import { KeyboardManager } from "../core/KeyboardManager";
3
4
  import { Super } from "../core/Super";
4
5
  import { Colors, ScaledSize, SKRect, Thickness, } from "../core/Types";
5
6
  import { TextSpan } from "./TextSpan";
@@ -25,7 +26,10 @@ export class SkiaLabel extends SkiaControl {
25
26
  verticalTextAlignment = "Start";
26
27
  lineSpacing = 1;
27
28
  lineHeight = 1;
28
- paragraphSpacing = 0;
29
+ paragraphSpacing = 0.25;
30
+ lineHeightUniform = false;
31
+ /** The height every line takes under LineHeightUniform: the tallest laid-out line's (C# MeasuredLineHeight). */
32
+ uniformLineHeight = 0;
29
33
  characterSpacing = 1;
30
34
  textTransform = "None";
31
35
  padding = Thickness.Zero;
@@ -33,6 +37,10 @@ export class SkiaLabel extends SkiaControl {
33
37
  Spans = [];
34
38
  /** Set to `Aria.RoleText` to expose every label to screen readers (React extension; C# is opt-in per control). */
35
39
  static DefaultAccessibilityRole;
40
+ /** Highlight drawn over selected text (AccessibilityTextSelectable), for every label (C# SkiaLabel.TextSelectionColor). */
41
+ static TextSelectionColor = "rgba(13,110,253,0.353)";
42
+ /** Caption of the Copy button shown over a touch selection (C# SkiaLabel.CopyButtonText). */
43
+ static CopyButtonText = "Copy";
36
44
  /** Like C# OnTextInternalChanged: the spoken label is the text (or the joined spans) unless AccessibilityLabel is set. */
37
45
  DefaultAccessibilityLabel() {
38
46
  return this.Spans.length > 0 ? this.Spans.map((s) => s.Text).join("") : this.text || undefined;
@@ -52,7 +60,8 @@ export class SkiaLabel extends SkiaControl {
52
60
  this.UpdateDraw();
53
61
  } }
54
62
  get Text() { return this.text; }
55
- set Text(v) { this.Set("text", v); }
63
+ set Text(v) { if (v !== this.text && this.selLength > 0)
64
+ this.ClearSelection(); this.Set("text", v); }
56
65
  get FontSize() { return this.fontSize; }
57
66
  set FontSize(v) { this.Set("fontSize", v); }
58
67
  get TextColor() { return this.textColor; }
@@ -83,11 +92,14 @@ export class SkiaLabel extends SkiaControl {
83
92
  get LineHeight() { return this.lineHeight; }
84
93
  set LineHeight(v) { this.Set("lineHeight", v); }
85
94
  /**
86
- * Space above every paragraph after the first, in lines (C# ParagraphSpacing; 0 here as DrawnUi.Rust, C# 0.25). A
95
+ * Space above every paragraph after the first, in lines of the label's font (C# ParagraphSpacing, default 0.25). A
87
96
  * paragraph starts after "\n"; U+2028 breaks the line inside a paragraph.
88
97
  */
89
98
  get ParagraphSpacing() { return this.paragraphSpacing; }
90
99
  set ParagraphSpacing(v) { this.Set("paragraphSpacing", v); }
100
+ /** LineHeightUniform: true = every line as tall as the tallest one; false (default in every engine) = each line its own height. */
101
+ get LineHeightUniform() { return this.lineHeightUniform; }
102
+ set LineHeightUniform(v) { this.Set("lineHeightUniform", v); }
91
103
  /**
92
104
  * C# CharacterSpacing: 1 = the font's own spacing; every glyph is followed by (CharacterSpacing - 1) points more
93
105
  * (less below 1), the line measures and wraps at the width it draws (C# 7cf1007c, DrawnUi.Rust label).
@@ -144,7 +156,7 @@ export class SkiaLabel extends SkiaControl {
144
156
  const main = Super.GetFont(family, weight, italic, sizePx);
145
157
  const m = main.getMetrics();
146
158
  f = {
147
- Key: key, Main: main, SizePx: sizePx, Ascent: -m.ascent, Descent: m.descent,
159
+ Key: key, Main: main, SizePx: sizePx, Ascent: -m.ascent, Descent: m.descent, Leading: m.leading,
148
160
  Fallbacks: this.fontFamilyFallback
149
161
  ? this.fontFamilyFallback.split(",").map((a) => a.trim()).filter(Boolean).map((a) => Super.GetFont(a, weight, italic, sizePx))
150
162
  : [],
@@ -497,13 +509,13 @@ export class SkiaLabel extends SkiaControl {
497
509
  if (isFinite(maxHeight)) {
498
510
  let used = 0, fit = 0;
499
511
  for (let i = 0; i < out.length; i++) {
500
- const lh = this.LineHeightPx(out[i]);
512
+ const lh = this.OwnLineHeight(out[i]);
501
513
  // the block height of the first i + 1 lines: spacing after every line but the last, paragraph space above
502
514
  const total = used + lh + (i > 0 ? this.ParagraphSpace(out[i]) : 0);
503
515
  if (total > maxHeight && fit > 0)
504
516
  break;
505
517
  fit++;
506
- used = total - lh + lh * this.lineSpacing;
518
+ used = total + this.LineGap(lh);
507
519
  }
508
520
  kept = Math.min(kept, fit);
509
521
  }
@@ -642,14 +654,31 @@ export class SkiaLabel extends SkiaControl {
642
654
  x += runs[k].Width;
643
655
  }
644
656
  }
645
- LineHeightPx(line) { return (line.Ascent + line.Descent) * this.lineHeight; }
646
- /** ParagraphSpacing above a line that starts a paragraph (after the first): that many of its lines, with their spacing. */
647
- ParagraphSpace(line) { return line.Para && this.paragraphSpacing ? this.LineHeightPx(line) * this.lineSpacing * this.paragraphSpacing : 0; }
657
+ /** A line's own height: its spans' largest ascent plus largest descent, times LineHeight, rounded once to whole pixels. */
658
+ OwnLineHeight(line) { return Math.round((line.Ascent + line.Descent) * this.lineHeight); }
659
+ LineHeightPx(line) { return this.lineHeightUniform ? this.uniformLineHeight : this.OwnLineHeight(line); }
660
+ /** C# GetSpaceBetweenLines: the label font's Leading times LineSpacing when it has one, else (LineSpacing - 1) lines. */
661
+ LineGap(lh) {
662
+ const leading = this.mainFonts?.Leading ?? 0;
663
+ return leading > 0 ? leading * this.lineSpacing : lh * (this.lineSpacing - 1);
664
+ }
665
+ /**
666
+ * As DrawnUi.Net: the baseline sits at the line's bottom minus its descent, the extra space of LineHeight split above and
667
+ * below the text (a line of lh pixels holds lh / LineHeight of text).
668
+ */
669
+ BaselineIn(line, lh) { return lh - (lh - lh / this.lineHeight) / 2 - line.Descent; }
670
+ /** ParagraphSpacing above a line that starts a paragraph (after the first): that many lines of the label's font, with their gap. */
671
+ ParagraphSpace(line) {
672
+ if (!line.Para || !this.paragraphSpacing)
673
+ return 0;
674
+ const f = this.mainFonts, lh = Math.round((f.Ascent + f.Descent) * this.lineHeight);
675
+ return (lh + this.LineGap(lh)) * this.paragraphSpacing;
676
+ }
648
677
  BlockHeight() {
649
678
  let h = 0;
650
679
  for (let i = 0; i < this.lines.length; i++) {
651
680
  const lh = this.LineHeightPx(this.lines[i]);
652
- h += (i < this.lines.length - 1 ? lh * this.lineSpacing : lh) + this.ParagraphSpace(this.lines[i]);
681
+ h += lh + (i < this.lines.length - 1 ? this.LineGap(lh) : 0) + this.ParagraphSpace(this.lines[i]);
653
682
  }
654
683
  return h;
655
684
  }
@@ -727,6 +756,9 @@ export class SkiaLabel extends SkiaControl {
727
756
  const r = this.reusable;
728
757
  if (this.NeedMeasure || !r || r.Scale !== scale || r.Spacing !== glyphSpacing || roomW < r.Width - 0.01 || roomH < r.Height - 0.01) {
729
758
  this.lines = this.text || this.Spans.length > 0 ? this.LayoutLines(roomW, scale, roomH) : [];
759
+ this.uniformLineHeight = 0;
760
+ for (const l of this.lines)
761
+ this.uniformLineHeight = Math.max(this.uniformLineHeight, this.OwnLineHeight(l));
730
762
  this.ShapeLines(this.lines);
731
763
  // ink past the lines' ends (a "j" hook, an italic slant) is drawing room for caches; widths unchanged
732
764
  const [inkLeft, inkRight] = this.MeasureInkOverhang();
@@ -780,17 +812,10 @@ export class SkiaLabel extends SkiaControl {
780
812
  let x = this.LineX(line, left, right, lineW);
781
813
  x += extra.Stroke / 2;
782
814
  out.push({ x, y, w: line.Width, h: lh, text: line.Runs.map((r) => r.Text).join("") });
783
- y += lh * this.lineSpacing;
815
+ y += lh + this.LineGap(lh);
784
816
  }
785
817
  return out;
786
818
  }
787
- /** Lines as CSS text for the accessibility overlay (AccessibilityTextSelectable): font alias chain, weight and px size. */
788
- GetAccessibilityTextLines(scale) {
789
- const family = [this.fontFamily || Super.DefaultFontAlias, ...this.fontFamilyFallback.split(",").map((f) => f.trim()).filter(Boolean)].filter(Boolean).join(", ");
790
- const weight = this.fontWeight > 0 ? this.fontWeight : this.fontAttributes === "Bold" || this.fontAttributes === "BoldItalic" ? 600 : 400;
791
- const size = this.fontSize;
792
- return this.LineGeometry().map((g) => ({ Text: g.text, Left: g.x / scale, Top: g.y / scale, Width: g.w / scale, Height: g.h / scale, FontFamily: family, FontWeight: weight, FontSize: size }));
793
- }
794
819
  /** Laid-out lines with their text index range (a wrapped-away space or line break sits between two lines). */
795
820
  GetLineBoxes() {
796
821
  const geo = this.LineGeometry();
@@ -952,7 +977,7 @@ export class SkiaLabel extends SkiaControl {
952
977
  const lineW = line.Width + extra.W;
953
978
  let x = this.LineX(line, left, right, lineW);
954
979
  x += extra.Stroke / 2;
955
- const baseline = y + line.Ascent;
980
+ const baseline = y + this.BaselineIn(line, lh);
956
981
  // C# paintDefault gets the gradient over rectDraw, or over line.Bounds per line (GradientByLines)
957
982
  const gradientRect = gradient ? (this.GradientByLines ? new SKRect(x, y, x + line.Width, y + lh) : textRect) : undefined;
958
983
  if (strokePaint && this.StrokeGradient)
@@ -996,15 +1021,22 @@ export class SkiaLabel extends SkiaControl {
996
1021
  }
997
1022
  x += run.Width;
998
1023
  }
999
- y += lh * this.lineSpacing;
1024
+ y += lh + this.LineGap(lh);
1000
1025
  }
1001
1026
  for (const paint of paints.values())
1002
1027
  paint.delete();
1003
1028
  strokePaint?.delete();
1004
1029
  shadowPaint?.delete();
1030
+ if (this.AccessibilityTextSelectable && this.selLength > 0)
1031
+ this.DrawTextSelection(ctx);
1005
1032
  }
1006
1033
  // ---- span taps (port of C# SkiaLabel.ProcessGestures) ----
1007
1034
  ProcessGestures(args, apply) {
1035
+ if (this.AccessibilityTextSelectable) {
1036
+ const consumed = this.ProcessTextSelection(args, apply);
1037
+ if (consumed)
1038
+ return consumed;
1039
+ }
1008
1040
  if (args.Type === "Tapped" && this.Spans.length > 0) {
1009
1041
  const x = apply.MappedLocation.X + apply.ChildOffset.X - this.DrawingRect.Left;
1010
1042
  const y = apply.MappedLocation.Y + apply.ChildOffset.Y - this.DrawingRect.Top;
@@ -1032,4 +1064,358 @@ export class SkiaLabel extends SkiaControl {
1032
1064
  span.FireTap(new ControlTappedEventArgs(this, args, apply));
1033
1065
  return this;
1034
1066
  }
1067
+ // ---- text selection (port of C# SkiaLabel TEXT SELECTION, drawnui-cross 6cb / 6cc) ----
1068
+ get AccessibilityTextSelectable() { return super.AccessibilityTextSelectable; }
1069
+ /**
1070
+ * The text can be selected and copied, drawn by the label (C# SkiaLabel.AccessibilityTextSelectable). Mouse: drag, double
1071
+ * click for a word. Touch: long press picks a word, keep the finger down and drag to extend, a Copy button appears.
1072
+ * Ctrl+C (Cmd+C) copies and Ctrl+A selects all while the label holds the selection; a click elsewhere drops it. The label
1073
+ * takes the mouse press for this, so it no longer reaches controls under it. Free when off. Copies through
1074
+ * Super.SetClipboardText.
1075
+ */
1076
+ set AccessibilityTextSelectable(v) { super.AccessibilityTextSelectable = v; if (!v)
1077
+ this.ClearSelection(); }
1078
+ selStart = 0;
1079
+ selLength = 0;
1080
+ anchorStart = 0;
1081
+ anchorEnd = 0;
1082
+ selecting = false;
1083
+ touchSelection = false;
1084
+ copyPressed = false;
1085
+ selectionKeysAttached = false;
1086
+ lastClickMs = 0;
1087
+ lastClickX = 0;
1088
+ lastClickY = 0;
1089
+ /** The drawn Copy button of a touch selection, relative to DrawingRect (pixels); empty when none. */
1090
+ copyButton = new SKRect(0, 0, 0, 0);
1091
+ selectionMap;
1092
+ /** Start of the selection in the selection's source: the label's text when the lines are taken from it verbatim, else the drawn lines joined. */
1093
+ get SelectionStart() { return this.selStart; }
1094
+ /** Length of the selection, 0 when nothing is selected. */
1095
+ get SelectionLength() { return this.selLength; }
1096
+ /** The selected text, empty when nothing is selected. */
1097
+ get SelectedText() {
1098
+ if (this.selLength <= 0)
1099
+ return "";
1100
+ const source = this.SelectionMap().Source;
1101
+ const start = Math.min(Math.max(this.selStart, 0), source.length);
1102
+ return source.substr(start, Math.min(Math.max(this.selLength, 0), source.length - start));
1103
+ }
1104
+ /** Selects a range of the text (see SelectionStart for what the indexes count). */
1105
+ Select(start, length) {
1106
+ const n = this.SelectionMap().Source.length;
1107
+ start = Math.min(Math.max(start, 0), n);
1108
+ length = Math.min(Math.max(length, 0), n - start);
1109
+ if (start === this.selStart && length === this.selLength)
1110
+ return;
1111
+ this.selStart = start;
1112
+ this.selLength = length;
1113
+ this.UpdateDraw();
1114
+ }
1115
+ /** Selects all of the text. */
1116
+ SelectAll() { this.Select(0, this.SelectionMap().Source.length); }
1117
+ /** Drops the selection. */
1118
+ ClearSelection() {
1119
+ this.selecting = false;
1120
+ this.copyPressed = false;
1121
+ if (this.selLength === 0 && !this.touchSelection)
1122
+ return;
1123
+ this.selLength = 0;
1124
+ this.touchSelection = false;
1125
+ this.UpdateDraw();
1126
+ }
1127
+ /** The app showed its own context menu for a touch selection: the drawn Copy button steps aside, the selection stays. */
1128
+ HideCopyButton() {
1129
+ if (!this.touchSelection)
1130
+ return;
1131
+ this.touchSelection = false;
1132
+ this.copyButton = new SKRect(0, 0, 0, 0);
1133
+ this.UpdateDraw();
1134
+ }
1135
+ /** Puts the selected text on the clipboard through Super.SetClipboardText. False when nothing is selected. */
1136
+ CopySelection() {
1137
+ const text = this.SelectedText;
1138
+ if (!text)
1139
+ return false;
1140
+ Super.SetClipboardText(text);
1141
+ return true;
1142
+ }
1143
+ /** A selectable label takes the canvas focus while text is selected and drops the selection when the focus moves elsewhere. */
1144
+ SetFrameworkFocus(focus) {
1145
+ if (!this.AccessibilityTextSelectable)
1146
+ return super.SetFrameworkFocus(focus);
1147
+ if (!focus)
1148
+ this.ClearSelection();
1149
+ this.AttachSelectionKeys(focus);
1150
+ return true;
1151
+ }
1152
+ OnDisposing() { this.AttachSelectionKeys(false); super.OnDisposing(); }
1153
+ AttachSelectionKeys(attach) {
1154
+ if (attach === this.selectionKeysAttached)
1155
+ return;
1156
+ this.selectionKeysAttached = attach;
1157
+ if (attach)
1158
+ KeyboardManager.Subscribe(this.onSelectionKey, SkiaLabel.noChar);
1159
+ else
1160
+ KeyboardManager.Unsubscribe(this.onSelectionKey, SkiaLabel.noChar);
1161
+ }
1162
+ static noChar = () => { };
1163
+ onSelectionKey = (key, e) => {
1164
+ // Ctrl+C in a page input or a focused element is that element's copy, not this selection's
1165
+ if (KeyboardManager.IsOwnedByElement(e))
1166
+ return;
1167
+ // a drag selected page text: the page copies it
1168
+ if (this.Superview?.OwnsTextSelectionKeys)
1169
+ return;
1170
+ if (!KeyboardManager.IsControlPressed)
1171
+ return;
1172
+ if (key === "KeyC")
1173
+ this.CopySelection();
1174
+ else if (key === "KeyA") {
1175
+ e.preventDefault();
1176
+ this.SelectAll();
1177
+ }
1178
+ };
1179
+ ClaimSelectionFocus() { this.Superview?.ReportFocus(this, this); this.AttachSelectionKeys(true); }
1180
+ /**
1181
+ * The drawn lines mapped onto a source string: the label's text when every line is found in it in order (spaces
1182
+ * dropped at wraps are then copied too), else the lines joined, a new line between paragraphs. Each line keeps its
1183
+ * caret slots (x of each text index, relative to DrawingRect) and its glyph boxes.
1184
+ */
1185
+ SelectionMap() {
1186
+ const d = this.DrawingRect;
1187
+ const key = `${this.LayoutVersion}|${d.Width}|${d.Height}|${this.text}`;
1188
+ if (this.selectionMap?.Key === key)
1189
+ return this.selectionMap;
1190
+ const boxes = this.GetLineBoxes(), glyphs = this.GetGlyphBoxes();
1191
+ const texts = this.lines.map((l) => l.Runs.map((r) => r.Text).join(""));
1192
+ const starts = [];
1193
+ let source = this.text ?? "", position = 0, verbatim = true;
1194
+ for (const t of texts) {
1195
+ const at = source.indexOf(t, position);
1196
+ if (at < 0) {
1197
+ verbatim = false;
1198
+ break;
1199
+ }
1200
+ starts.push(at);
1201
+ position = at + t.length;
1202
+ }
1203
+ if (!verbatim) {
1204
+ starts.length = 0;
1205
+ source = "";
1206
+ texts.forEach((t, i) => { if (i > 0 && this.lines[i].Para)
1207
+ source += "\n"; starts.push(source.length); source += t; });
1208
+ }
1209
+ const lines = boxes.map((b, i) => {
1210
+ const own = glyphs.filter((g) => g.Line === i);
1211
+ const rtl = own.some((g) => g.Rtl);
1212
+ const slots = own.map((g) => ({ Offset: g.Index - b.Start, X: g.Rtl ? g.Left + g.Width : g.Left }));
1213
+ slots.push({ Offset: texts[i].length, X: this.lines[i].Rtl ? b.Left : b.Left + b.Width });
1214
+ return { Start: starts[i] ?? 0, Length: texts[i].length, Left: b.Left, Right: b.Left + b.Width, Top: b.Top, Bottom: b.Top + b.Height, Rtl: rtl, Slots: slots,
1215
+ Boxes: own.map((g) => ({ Offset: g.Index - b.Start, Length: g.Length, Left: g.Left, Right: g.Left + g.Width })) };
1216
+ });
1217
+ this.selectionMap = { Key: key, Source: source, Lines: lines };
1218
+ return this.selectionMap;
1219
+ }
1220
+ /** Text index under a point relative to DrawingRect (pixels). */
1221
+ IndexAt(x, y) {
1222
+ const lines = this.SelectionMap().Lines;
1223
+ if (lines.length === 0)
1224
+ return 0;
1225
+ const line = lines.find((l) => y <= l.Bottom) ?? lines[lines.length - 1];
1226
+ let best = 0, bestDistance = Infinity;
1227
+ for (const s of line.Slots) {
1228
+ const dx = Math.abs(s.X - x);
1229
+ if (dx < bestDistance) {
1230
+ bestDistance = dx;
1231
+ best = s.Offset;
1232
+ }
1233
+ }
1234
+ return line.Start + best;
1235
+ }
1236
+ WordAt(index) {
1237
+ const text = this.SelectionMap().Source;
1238
+ if (text.length === 0)
1239
+ return [0, 0];
1240
+ index = Math.min(Math.max(index, 0), text.length - 1);
1241
+ const isWord = (c) => /[\p{L}\p{N}_']/u.test(c);
1242
+ if (!isWord(text[index]) && index > 0 && isWord(text[index - 1]))
1243
+ index--;
1244
+ if (!isWord(text[index]))
1245
+ return [index, index + 1];
1246
+ let start = index;
1247
+ while (start > 0 && isWord(text[start - 1]))
1248
+ start--;
1249
+ let end = index + 1;
1250
+ while (end < text.length && isWord(text[end]))
1251
+ end++;
1252
+ return [start, end];
1253
+ }
1254
+ /** Text index under a canvas point (pixels), for a selection a page drag makes across labels. */
1255
+ TextIndexAtCanvas(x, y) {
1256
+ const r = this.GetAccessibilityPixelRect();
1257
+ if (r.Width <= 0 || r.Height <= 0)
1258
+ return 0;
1259
+ return this.IndexAt(x - r.Left, y - r.Top);
1260
+ }
1261
+ /** Length of the text a selection counts in (see SelectionStart). */
1262
+ get SelectableLength() { return this.SelectionMap().Source.length; }
1263
+ SelectBetween(index) {
1264
+ const start = Math.min(this.anchorStart, index), end = Math.max(this.anchorEnd, index);
1265
+ this.Select(start, end - start);
1266
+ }
1267
+ /** Pointer handling of AccessibilityTextSelectable; null lets the gesture go on as usual. */
1268
+ ProcessTextSelection(args, apply) {
1269
+ const x = apply.MappedLocation.X + apply.ChildOffset.X - this.DrawingRect.Left;
1270
+ const y = apply.MappedLocation.Y + apply.ChildOffset.Y - this.DrawingRect.Top;
1271
+ const pointer = args.Event.Pointer;
1272
+ const mouse = pointer?.DeviceType === "Mouse";
1273
+ const onCopy = () => { const b = this.copyButton; return x >= b.Left && x < b.Right && y >= b.Top && y < b.Bottom; };
1274
+ switch (args.Type) {
1275
+ case "Down": {
1276
+ if (this.touchSelection && this.selLength > 0 && onCopy()) {
1277
+ this.copyPressed = true;
1278
+ return this;
1279
+ }
1280
+ if (!mouse)
1281
+ return null; // touch: a long press selects, a drag keeps scrolling
1282
+ // only the main button selects: a right click keeps the selection for its context menu
1283
+ if (pointer && pointer.Button !== "Left")
1284
+ return this.selLength > 0 ? this : null;
1285
+ const index = this.IndexAt(x, y);
1286
+ // when the click happened (stamped at arrival), not when a frame got to process it
1287
+ const now = args.Event.Timestamp;
1288
+ const second = now - this.lastClickMs < 450 && Math.hypot(x - this.lastClickX, y - this.lastClickY) < 8 * this.RenderingScale;
1289
+ this.lastClickMs = second ? 0 : now;
1290
+ this.lastClickX = x;
1291
+ this.lastClickY = y;
1292
+ [this.anchorStart, this.anchorEnd] = second ? this.WordAt(index) : [index, index];
1293
+ this.touchSelection = false;
1294
+ this.selecting = true;
1295
+ this.Select(this.anchorStart, this.anchorEnd - this.anchorStart);
1296
+ this.ClaimSelectionFocus();
1297
+ return this;
1298
+ }
1299
+ case "LongPressing": {
1300
+ if (mouse)
1301
+ return this.selecting ? this : null;
1302
+ const pressed = this.IndexAt(x, y);
1303
+ // a long press on the selection keeps it, for the context menu that follows
1304
+ if (this.selLength > 0 && pressed >= this.selStart && pressed < this.selStart + this.selLength) {
1305
+ [this.anchorStart, this.anchorEnd] = [this.selStart, this.selStart + this.selLength];
1306
+ this.touchSelection = true;
1307
+ this.selecting = true;
1308
+ this.ClaimSelectionFocus();
1309
+ this.UpdateDraw();
1310
+ return this;
1311
+ }
1312
+ [this.anchorStart, this.anchorEnd] = this.WordAt(pressed);
1313
+ this.touchSelection = true;
1314
+ this.selecting = true;
1315
+ this.Select(this.anchorStart, this.anchorEnd - this.anchorStart);
1316
+ this.ClaimSelectionFocus();
1317
+ this.UpdateDraw();
1318
+ return this;
1319
+ }
1320
+ case "Panning":
1321
+ if (!this.selecting)
1322
+ return null;
1323
+ this.SelectBetween(this.IndexAt(x, y));
1324
+ return this;
1325
+ case "Up":
1326
+ if (this.copyPressed) {
1327
+ this.copyPressed = false;
1328
+ if (onCopy()) {
1329
+ this.CopySelection();
1330
+ this.ClearSelection();
1331
+ }
1332
+ return this;
1333
+ }
1334
+ if (this.selecting) {
1335
+ this.selecting = false;
1336
+ return this;
1337
+ }
1338
+ return null;
1339
+ case "Tapped":
1340
+ if (this.copyPressed)
1341
+ return this; // the tap on the Copy button: its Up (after the tap) copies
1342
+ if (this.selLength <= 0)
1343
+ return null; // nothing selected: span links and Tapped still get the tap
1344
+ if (!mouse)
1345
+ this.ClearSelection(); // a tap drops a touch selection
1346
+ // a click that selected (double click) is the label's: unconsumed, the canvas would read it as a tap on empty
1347
+ // space and take the focus, and with it the selection
1348
+ return this;
1349
+ }
1350
+ return null;
1351
+ }
1352
+ /** Draws the selection over the lines just drawn, and the Copy button of a touch selection. */
1353
+ DrawTextSelection(ctx) {
1354
+ const CK = Super.CK, map = this.SelectionMap(), canvas = ctx.Context.Canvas, d = ctx.Destination, scale = ctx.Scale;
1355
+ const paint = new CK.Paint();
1356
+ paint.setAntiAlias(true);
1357
+ paint.setColor(Super.ParseColor(SkiaLabel.TextSelectionColor));
1358
+ const start = this.selStart, end = this.selStart + this.selLength;
1359
+ let first, last;
1360
+ map.Lines.forEach((line, i) => {
1361
+ const lineEnd = line.Start + line.Length;
1362
+ const a = Math.max(start, line.Start), b = Math.min(end, lineEnd);
1363
+ const continues = end > lineEnd && i < map.Lines.length - 1;
1364
+ if (a > b || (a === b && !continues))
1365
+ return;
1366
+ let rect;
1367
+ if (line.Rtl) {
1368
+ // right-to-left text: the selected characters' boxes, blocks across a direction change
1369
+ for (const g of line.Boxes) {
1370
+ if (g.Offset < a - line.Start || g.Offset >= b - line.Start)
1371
+ continue;
1372
+ const block = new SKRect(d.Left + g.Left, d.Top + line.Top, d.Left + g.Right, d.Top + line.Bottom);
1373
+ canvas.drawRect(CK.LTRBRect(block.Left, block.Top, block.Right, block.Bottom), paint);
1374
+ rect = rect ? new SKRect(Math.min(rect.Left, block.Left), block.Top, Math.max(rect.Right, block.Right), block.Bottom) : block;
1375
+ }
1376
+ if (!rect)
1377
+ return;
1378
+ }
1379
+ else {
1380
+ const slotX = (offset) => line.Slots.find((s) => s.Offset >= offset)?.X ?? line.Right;
1381
+ const x0 = slotX(a - line.Start), x1 = continues && b === lineEnd ? line.Right : slotX(b - line.Start);
1382
+ rect = new SKRect(d.Left + x0, d.Top + line.Top, d.Left + Math.max(x1, x0 + scale), d.Top + line.Bottom);
1383
+ canvas.drawRect(CK.LTRBRect(rect.Left, rect.Top, rect.Right, rect.Bottom), paint);
1384
+ }
1385
+ first ??= rect;
1386
+ last = rect;
1387
+ });
1388
+ this.copyButton = new SKRect(0, 0, 0, 0);
1389
+ if (!this.touchSelection || !first || !last) {
1390
+ paint.delete();
1391
+ return;
1392
+ }
1393
+ // the Copy button inside the label (its cache clips and its hit box is the label): above the selection, else below
1394
+ // it, else beside it on the same line, overlapping only when none fits
1395
+ const font = Super.GetFont(this.fontFamily || Super.DefaultFontAlias, 400, false, 14 * scale);
1396
+ const caption = SkiaLabel.CopyButtonText;
1397
+ const textWidth = font.getGlyphWidths(font.getGlyphIDs(caption)).reduce((s, w) => s + w, 0);
1398
+ const height = 32 * scale, width = textWidth + 28 * scale, gap = 6 * scale;
1399
+ const clamp = (v, lo, hi) => Math.min(Math.max(v, lo), Math.max(lo, hi));
1400
+ const centered = clamp((first.Left + first.Right) / 2 - width / 2, d.Left, d.Right - width);
1401
+ const middle = clamp((first.Top + first.Bottom) / 2 - height / 2, d.Top, d.Bottom - height);
1402
+ let bx, by;
1403
+ if (first.Top - gap - height >= d.Top)
1404
+ [bx, by] = [centered, first.Top - gap - height];
1405
+ else if (last.Bottom + gap + height <= d.Bottom)
1406
+ [bx, by] = [centered, last.Bottom + gap];
1407
+ else if (first.Right + gap + width <= d.Right)
1408
+ [bx, by] = [first.Right + gap, middle];
1409
+ else if (first.Left - gap - width >= d.Left)
1410
+ [bx, by] = [first.Left - gap - width, middle];
1411
+ else
1412
+ [bx, by] = [centered, Math.max(d.Top, d.Bottom - height)];
1413
+ paint.setColor(CK.Color(33, 37, 41, 235 / 255));
1414
+ canvas.drawRRect(CK.RRectXY(CK.LTRBRect(bx, by, bx + width, by + height), height / 2, height / 2), paint);
1415
+ paint.setColor(CK.WHITE);
1416
+ const m = font.getMetrics();
1417
+ canvas.drawText(caption, bx + width / 2 - textWidth / 2, by + height / 2 - (m.ascent + m.descent) / 2, paint, font);
1418
+ paint.delete();
1419
+ this.copyButton = new SKRect(bx - d.Left, by - d.Top, bx + width - d.Left, by + height - d.Top);
1420
+ }
1035
1421
  }