drawnui-react 0.1.0-preview.42 → 0.1.0-preview.44

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.
@@ -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";
@@ -36,6 +37,10 @@ export class SkiaLabel extends SkiaControl {
36
37
  Spans = [];
37
38
  /** Set to `Aria.RoleText` to expose every label to screen readers (React extension; C# is opt-in per control). */
38
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";
39
44
  /** Like C# OnTextInternalChanged: the spoken label is the text (or the joined spans) unless AccessibilityLabel is set. */
40
45
  DefaultAccessibilityLabel() {
41
46
  return this.Spans.length > 0 ? this.Spans.map((s) => s.Text).join("") : this.text || undefined;
@@ -55,7 +60,8 @@ export class SkiaLabel extends SkiaControl {
55
60
  this.UpdateDraw();
56
61
  } }
57
62
  get Text() { return this.text; }
58
- 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); }
59
65
  get FontSize() { return this.fontSize; }
60
66
  set FontSize(v) { this.Set("fontSize", v); }
61
67
  get TextColor() { return this.textColor; }
@@ -810,13 +816,6 @@ export class SkiaLabel extends SkiaControl {
810
816
  }
811
817
  return out;
812
818
  }
813
- /** Lines as CSS text for the accessibility overlay (AccessibilityTextSelectable): font alias chain, weight and px size. */
814
- GetAccessibilityTextLines(scale) {
815
- const family = [this.fontFamily || Super.DefaultFontAlias, ...this.fontFamilyFallback.split(",").map((f) => f.trim()).filter(Boolean)].filter(Boolean).join(", ");
816
- const weight = this.fontWeight > 0 ? this.fontWeight : this.fontAttributes === "Bold" || this.fontAttributes === "BoldItalic" ? 600 : 400;
817
- const size = this.fontSize;
818
- 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 }));
819
- }
820
819
  /** Laid-out lines with their text index range (a wrapped-away space or line break sits between two lines). */
821
820
  GetLineBoxes() {
822
821
  const geo = this.LineGeometry();
@@ -1028,9 +1027,16 @@ export class SkiaLabel extends SkiaControl {
1028
1027
  paint.delete();
1029
1028
  strokePaint?.delete();
1030
1029
  shadowPaint?.delete();
1030
+ if (this.AccessibilityTextSelectable && this.selLength > 0)
1031
+ this.DrawTextSelection(ctx);
1031
1032
  }
1032
1033
  // ---- span taps (port of C# SkiaLabel.ProcessGestures) ----
1033
1034
  ProcessGestures(args, apply) {
1035
+ if (this.AccessibilityTextSelectable) {
1036
+ const consumed = this.ProcessTextSelection(args, apply);
1037
+ if (consumed)
1038
+ return consumed;
1039
+ }
1034
1040
  if (args.Type === "Tapped" && this.Spans.length > 0) {
1035
1041
  const x = apply.MappedLocation.X + apply.ChildOffset.X - this.DrawingRect.Left;
1036
1042
  const y = apply.MappedLocation.Y + apply.ChildOffset.Y - this.DrawingRect.Top;
@@ -1058,4 +1064,358 @@ export class SkiaLabel extends SkiaControl {
1058
1064
  span.FireTap(new ControlTappedEventArgs(this, args, apply));
1059
1065
  return this;
1060
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
+ }
1061
1421
  }
@@ -2,10 +2,9 @@ import type { DrawingContext } from "../core/SkiaControl";
2
2
  import { SkiaLabel } from "./SkiaLabel";
3
3
  /**
4
4
  * Mirrors DrawnUi SkiaLabelFps: a label showing the canvas FPS, drawn above its siblings, input-transparent, never
5
- * re-laying out its parent. Divergence: C# sets Text on every draw, and each new value asks for another frame, so
6
- * the counter keeps an idle canvas redrawing. Here the value changes only on frames that running animators already
7
- * request (scroll inertia, transitions, Lottie, game loops): the counter never asks for a frame itself, and an idle
8
- * canvas keeps the last value measured. Not ported: ForceRefresh, Format, MonoForDigits.
5
+ * re-laying out its parent. It shows the value on every frame it draws and asks for the next one, so while it is on
6
+ * screen the canvas keeps drawing and the number is the frame rate the canvas reaches (as DrawnUi.Net and Rust). Not
7
+ * ported: ForceRefresh, Format, MonoForDigits.
9
8
  */
10
9
  export declare class SkiaLabelFps extends SkiaLabel {
11
10
  constructor();
@@ -2,10 +2,9 @@ import { Aria } from "../core/Accessibility";
2
2
  import { SkiaLabel } from "./SkiaLabel";
3
3
  /**
4
4
  * Mirrors DrawnUi SkiaLabelFps: a label showing the canvas FPS, drawn above its siblings, input-transparent, never
5
- * re-laying out its parent. Divergence: C# sets Text on every draw, and each new value asks for another frame, so
6
- * the counter keeps an idle canvas redrawing. Here the value changes only on frames that running animators already
7
- * request (scroll inertia, transitions, Lottie, game loops): the counter never asks for a frame itself, and an idle
8
- * canvas keeps the last value measured. Not ported: ForceRefresh, Format, MonoForDigits.
5
+ * re-laying out its parent. It shows the value on every frame it draws and asks for the next one, so while it is on
6
+ * screen the canvas keeps drawing and the number is the frame rate the canvas reaches (as DrawnUi.Net and Rust). Not
7
+ * ported: ForceRefresh, Format, MonoForDigits.
9
8
  */
10
9
  export class SkiaLabelFps extends SkiaLabel {
11
10
  constructor() {
@@ -22,9 +21,14 @@ export class SkiaLabelFps extends SkiaLabel {
22
21
  }
23
22
  Render(ctx) {
24
23
  const view = this.Superview;
25
- // the frame this asks for is requested anyway by the running animators
26
- if (view && view.AnimatingControls.size > 0)
27
- this.Text = `FPS ${view.FPS.toFixed(1).padStart(4, "0")}`;
24
+ // as SkiaControl.Render: a hidden or transparent label draws nothing and asks for nothing
25
+ if (view && this.IsVisible && this.Opacity > 0 && !this.IsDisposed) {
26
+ const shown = `FPS ${view.FPS.toFixed(1).padStart(4, "0")}`;
27
+ if (shown !== this.Text)
28
+ this.Text = shown;
29
+ // the next frame (C#'s ever-changing value asks for it the same way): the counter shows what the canvas reaches
30
+ view.Update();
31
+ }
28
32
  // parent-independent: it measures itself again in its slot as it draws (SkiaControl.Render)
29
33
  super.Render(ctx);
30
34
  }
@@ -197,6 +197,35 @@ export declare class SkiaScroll extends SkiaControl {
197
197
  /** Content gets an infinite constraint along the scroll axis; the scroll itself takes the box it is given. */
198
198
  protected MeasureAbsolute(widthConstraint: number, heightConstraint: number, scale: number): ScaledSize;
199
199
  protected OnLayoutChanged(): void;
200
+ /**
201
+ * When true, the canvas keeps the focused text field of this scroll above the on-screen keyboard by itself (it sets
202
+ * AdaptToKeyboardFor and AdaptToKeyboardSize), and while the keyboard is up the part of the viewport it covers is room at
203
+ * the content end, so the last fields rise above it too. Off by default: an app that wires those two itself keeps doing so.
204
+ */
205
+ AdaptToKeyboardAuto: boolean;
206
+ /** Points kept between the bottom of AdaptToKeyboardFor and the keyboard's top (8 by default). */
207
+ AdaptToKeyboardGap: number;
208
+ private adaptFor?;
209
+ private adaptSize;
210
+ private keyboardInset;
211
+ private adaptPending;
212
+ /** The field to keep above the keyboard (set by the app, or by the canvas with AdaptToKeyboardAuto). */
213
+ get AdaptToKeyboardFor(): SkiaControl | undefined;
214
+ set AdaptToKeyboardFor(v: SkiaControl | undefined);
215
+ /** Points of the canvas, from its bottom, the keyboard covers (0 = no keyboard). */
216
+ get AdaptToKeyboardSize(): number;
217
+ set AdaptToKeyboardSize(v: number);
218
+ /**
219
+ * After the keyboard or the focused field changed: scrolls so the field shows above the keyboard. A field already
220
+ * visible, or the keyboard closing, leaves the page where it is (the keyboard closing settles a page scrolled into the
221
+ * keyboard's room at the content end). One adaptation for the field and the size set together.
222
+ */
223
+ AdaptToKeyboard(): void;
224
+ /** The keyboard's top in canvas points, from AdaptToKeyboardSize; NaN without one. */
225
+ private KeyboardTopPts;
226
+ private UpdateKeyboardInset;
227
+ /** Where the scroll must go so the field's bottom sits AdaptToKeyboardGap above the keyboard; NaN = nothing to move. */
228
+ CalculateNeededScrollForKeyboard(): number;
200
229
  /** Places Content at the current offset; called on layout and every frame while the offset moves. */
201
230
  private ArrangeContent;
202
231
  /** Header: sticky = pinned at the viewport start, else scrolled at HeaderParallaxRatio of the offset; footer after the content. */
@@ -211,9 +240,10 @@ export declare class SkiaScroll extends SkiaControl {
211
240
  /** Scroll to an offset in points; maxSpeedSecs > 0 animates along the deceleration curve. */
212
241
  /**
213
242
  * Scrolls every SkiaScroll ancestor of `control` so its DrawingRect lies inside the viewport (React extension,
214
- * used by the accessibility overlay when keyboard focus lands on an off-screen node). Offsets are points.
243
+ * used by the accessibility overlay when keyboard focus lands on an off-screen node). Offsets are points. Returns the
244
+ * scrolls that move.
215
245
  */
216
- static EnsureVisible(control: SkiaControl, maxTimeSecs?: number, paddingPts?: number): void;
246
+ static EnsureVisible(control: SkiaControl, maxTimeSecs?: number, paddingPts?: number): SkiaScroll[];
217
247
  /** Destination of the running ScrollTo per axis: the deceleration curve stops within its threshold, the exact value is applied when it finishes. */
218
248
  private scrollToTargetX;
219
249
  private scrollToTargetY;
@@ -245,6 +275,8 @@ export declare class SkiaScroll extends SkiaControl {
245
275
  private ResetPan;
246
276
  private draggedScrollBar?;
247
277
  private scrollBarOwnsGesture;
278
+ /** The gesture going on started on a draggable scroll bar: it is the bar's (a page text selection does not start there). */
279
+ get ScrollBarOwnsGesture(): boolean;
248
280
  /**
249
281
  * Thumb drag and track press on a ScrollBar / ScrollBarHorizontal with IsDraggable set (C# ProcessScrollBarGestures).
250
282
  * A gesture that starts on the bar belongs to it until the next down: the content neither pans nor gets the tap.
@@ -589,7 +589,8 @@ export class SkiaScroll extends SkiaControl {
589
589
  const viewportW = this.DrawingRect.Width / scale, viewportH = this.DrawingRect.Height / scale;
590
590
  const extra = this.HeaderExtentPts + this.FooterExtentPts;
591
591
  const width = Math.max(0, this.ContentSize.Units.Width + (this.Orientation === "Horizontal" ? extra : 0) - viewportW);
592
- const height = Math.max(0, this.ContentSize.Units.Height + (this.Orientation !== "Horizontal" ? extra : 0) - viewportH);
592
+ // with the keyboard up (AdaptToKeyboardAuto), its cover of the viewport is room at the content end
593
+ const height = Math.max(0, this.ContentSize.Units.Height + (this.Orientation !== "Horizontal" ? extra + this.keyboardInset : 0) - viewportH);
593
594
  this.ContentOffsetBounds = new SKRect(-width, -height, 0, 0);
594
595
  // C# InitializeViewport: pull an offset the new bounds no longer contain back inside them, here where
595
596
  // the bounds change. Shrinking content (fewer items) otherwise leaves the viewport parked past its end
@@ -603,6 +604,94 @@ export class SkiaScroll extends SkiaControl {
603
604
  this.ArrangeContent();
604
605
  this.CheckLoadMore();
605
606
  }
607
+ // ---- the on-screen keyboard (C# SkiaScroll ScrollViewKeyboardAwareBehavior, drawnui-cross 6cj) ----
608
+ /**
609
+ * When true, the canvas keeps the focused text field of this scroll above the on-screen keyboard by itself (it sets
610
+ * AdaptToKeyboardFor and AdaptToKeyboardSize), and while the keyboard is up the part of the viewport it covers is room at
611
+ * the content end, so the last fields rise above it too. Off by default: an app that wires those two itself keeps doing so.
612
+ */
613
+ AdaptToKeyboardAuto = false;
614
+ /** Points kept between the bottom of AdaptToKeyboardFor and the keyboard's top (8 by default). */
615
+ AdaptToKeyboardGap = 8;
616
+ adaptFor;
617
+ adaptSize = 0;
618
+ keyboardInset = 0;
619
+ adaptPending = false;
620
+ /** The field to keep above the keyboard (set by the app, or by the canvas with AdaptToKeyboardAuto). */
621
+ get AdaptToKeyboardFor() { return this.adaptFor; }
622
+ set AdaptToKeyboardFor(v) { if (this.adaptFor !== v) {
623
+ this.adaptFor = v;
624
+ this.AdaptToKeyboard();
625
+ } }
626
+ /** Points of the canvas, from its bottom, the keyboard covers (0 = no keyboard). */
627
+ get AdaptToKeyboardSize() { return this.adaptSize; }
628
+ set AdaptToKeyboardSize(v) { if (this.adaptSize !== v) {
629
+ this.adaptSize = v;
630
+ this.AdaptToKeyboard();
631
+ } }
632
+ /**
633
+ * After the keyboard or the focused field changed: scrolls so the field shows above the keyboard. A field already
634
+ * visible, or the keyboard closing, leaves the page where it is (the keyboard closing settles a page scrolled into the
635
+ * keyboard's room at the content end). One adaptation for the field and the size set together.
636
+ */
637
+ AdaptToKeyboard() {
638
+ if (this.adaptPending)
639
+ return;
640
+ this.adaptPending = true;
641
+ setTimeout(() => {
642
+ this.adaptPending = false;
643
+ if (this.IsDisposed)
644
+ return;
645
+ this.UpdateKeyboardInset();
646
+ const to = this.CalculateNeededScrollForKeyboard();
647
+ if (!Number.isNaN(to)) {
648
+ this.ViewportOffsetY = to;
649
+ this.Repaint();
650
+ }
651
+ }, 150);
652
+ }
653
+ /** The keyboard's top in canvas points, from AdaptToKeyboardSize; NaN without one. */
654
+ KeyboardTopPts() {
655
+ const canvas = this.Superview;
656
+ if (!canvas || this.adaptSize <= 0)
657
+ return Number.NaN;
658
+ return canvas.Element.height / (this.RenderingScale || 1) - this.adaptSize;
659
+ }
660
+ UpdateKeyboardInset() {
661
+ let inset = 0;
662
+ const top = this.KeyboardTopPts();
663
+ if (this.AdaptToKeyboardAuto && !Number.isNaN(top) && this.Orientation === "Vertical") {
664
+ const covered = this.GetAccessibilityPixelRect().Bottom / (this.RenderingScale || 1) - top;
665
+ // the gap too: the last field rests that far above the keyboard
666
+ if (covered > 0)
667
+ inset = covered + Math.max(0, this.AdaptToKeyboardGap);
668
+ }
669
+ if (Math.abs(inset - this.keyboardInset) < 0.5)
670
+ return;
671
+ const shrinking = inset < this.keyboardInset;
672
+ this.keyboardInset = inset;
673
+ const viewportH = this.DrawingRect.Height / (this.RenderingScale || 1);
674
+ const height = Math.max(0, this.ContentSize.Units.Height + this.HeaderExtentPts + this.FooterExtentPts + inset - viewportH);
675
+ this.ContentOffsetBounds = new SKRect(this.ContentOffsetBounds.Left, -height, this.ContentOffsetBounds.Right, 0);
676
+ // the keyboard closed: content past the new end settles at the end, animated, as native scroll views do
677
+ if (shrinking && this.offsetY < -height && !this.IsUserPanning)
678
+ this.ScrollTo(this.offsetX, -height, this.AutoScrollingSpeedMs / 1000, true);
679
+ this.Repaint();
680
+ }
681
+ /** Where the scroll must go so the field's bottom sits AdaptToKeyboardGap above the keyboard; NaN = nothing to move. */
682
+ CalculateNeededScrollForKeyboard() {
683
+ const field = this.adaptFor, top = this.KeyboardTopPts();
684
+ if (!field || Number.isNaN(top) || !this.Superview)
685
+ return Number.NaN;
686
+ this.StopScrolling();
687
+ const k = this.RenderingScale || 1;
688
+ const f = field.GetAccessibilityPixelRect(), viewport = this.GetAccessibilityPixelRect();
689
+ if (f.Width <= 0 || f.Height <= 0)
690
+ return Number.NaN;
691
+ const visibleBottom = Math.min(viewport.Bottom, top * k);
692
+ const need = (f.Bottom - visibleBottom) / k + this.AdaptToKeyboardGap;
693
+ return need > 0 ? this.offsetY - need : Number.NaN;
694
+ }
606
695
  /** Places Content at the current offset; called on layout and every frame while the offset moves. */
607
696
  ArrangeContent() {
608
697
  const c = this.content;
@@ -719,9 +808,11 @@ export class SkiaScroll extends SkiaControl {
719
808
  /** Scroll to an offset in points; maxSpeedSecs > 0 animates along the deceleration curve. */
720
809
  /**
721
810
  * Scrolls every SkiaScroll ancestor of `control` so its DrawingRect lies inside the viewport (React extension,
722
- * used by the accessibility overlay when keyboard focus lands on an off-screen node). Offsets are points.
811
+ * used by the accessibility overlay when keyboard focus lands on an off-screen node). Offsets are points. Returns the
812
+ * scrolls that move.
723
813
  */
724
814
  static EnsureVisible(control, maxTimeSecs = 0.25, paddingPts = 8) {
815
+ const moved = [];
725
816
  let p = control.Parent;
726
817
  while (p) {
727
818
  if (p instanceof SkiaScroll) {
@@ -736,11 +827,14 @@ export class SkiaScroll extends SkiaControl {
736
827
  dx = (v.Left - r.Left) / scale + paddingPts;
737
828
  else if (r.Right > v.Right)
738
829
  dx = -((r.Right - v.Right) / scale + paddingPts);
739
- if (dx !== 0 || dy !== 0)
830
+ if (dx !== 0 || dy !== 0) {
740
831
  p.ScrollTo(p.offsetX + dx, p.offsetY + dy, maxTimeSecs);
832
+ moved.push(p);
833
+ }
741
834
  }
742
835
  p = p.Parent;
743
836
  }
837
+ return moved;
744
838
  }
745
839
  /** Destination of the running ScrollTo per axis: the deceleration curve stops within its threshold, the exact value is applied when it finishes. */
746
840
  scrollToTargetX = null;
@@ -923,6 +1017,8 @@ export class SkiaScroll extends SkiaControl {
923
1017
  }
924
1018
  draggedScrollBar;
925
1019
  scrollBarOwnsGesture = false;
1020
+ /** The gesture going on started on a draggable scroll bar: it is the bar's (a page text selection does not start there). */
1021
+ get ScrollBarOwnsGesture() { return this.scrollBarOwnsGesture; }
926
1022
  /**
927
1023
  * Thumb drag and track press on a ScrollBar / ScrollBarHorizontal with IsDraggable set (C# ProcessScrollBarGestures).
928
1024
  * A gesture that starts on the bar belongs to it until the next down: the content neither pans nor gets the tap.