drawnui-react 0.1.0-preview.40 → 0.1.0-preview.42
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.
- package/README.md +57 -0
- package/SKIPPED.md +2 -2
- package/dist/controls/SkiaEditor.js +5 -4
- package/dist/controls/SkiaImage.d.ts +5 -0
- package/dist/controls/SkiaImage.js +32 -5
- package/dist/controls/SkiaLabel.d.ts +17 -2
- package/dist/controls/SkiaLabel.js +38 -12
- package/dist/controls/SkiaLabelFps.d.ts +0 -3
- package/dist/controls/SkiaLabelFps.js +1 -11
- package/dist/controls/SkiaLayout.js +33 -2
- package/dist/controls/SkiaScroll.d.ts +17 -0
- package/dist/controls/SkiaScroll.js +51 -6
- package/dist/controls/SkiaShell.d.ts +282 -0
- package/dist/controls/SkiaShell.js +972 -0
- package/dist/controls/SkiaSprite.js +5 -1
- package/dist/core/Accessibility.d.ts +12 -0
- package/dist/core/Accessibility.js +62 -14
- package/dist/core/Canvas.d.ts +50 -8
- package/dist/core/Canvas.js +126 -25
- package/dist/core/DesignerActions.d.ts +32 -0
- package/dist/core/DesignerActions.js +68 -0
- package/dist/core/Gestures.d.ts +1 -0
- package/dist/core/Props.d.ts +11 -0
- package/dist/core/Props.js +44 -0
- package/dist/core/SkiaControl.d.ts +26 -3
- package/dist/core/SkiaControl.js +64 -13
- package/dist/core/SkiaEffect.d.ts +12 -1
- package/dist/core/SkiaEffect.js +35 -8
- package/dist/core/SkiaImageManager.d.ts +100 -18
- package/dist/core/SkiaImageManager.js +309 -64
- package/dist/core/Styles.js +7 -1
- package/dist/core/Super.d.ts +2 -0
- package/dist/core/Super.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/react/SkiaShell.d.ts +8 -74
- package/dist/react/SkiaShell.js +163 -514
- package/dist/react/index.d.ts +24 -1
- package/dist/react/index.js +32 -5
- package/dist/react/reconciler.d.ts +12 -0
- package/dist/react/reconciler.js +22 -46
- package/package.json +24 -2
|
@@ -121,7 +121,11 @@ export class SkiaSprite extends AnimatedFramesRenderer {
|
|
|
121
121
|
const cached = SkiaSprite.CachedSpriteSheets.get(source);
|
|
122
122
|
if (cached)
|
|
123
123
|
return cached;
|
|
124
|
-
const image = await SkiaImageManager.Instance.
|
|
124
|
+
const image = await SkiaImageManager.Instance.LoadImageManagedAsync(source);
|
|
125
|
+
if (!image)
|
|
126
|
+
throw new Error(`DrawnUi: cannot load sprite sheet '${source}'`);
|
|
127
|
+
// the sheets cache holds it for good (the image manager never deletes an image somebody holds)
|
|
128
|
+
SkiaImageManager.Instance.Hold(image);
|
|
125
129
|
SkiaSprite.CachedSpriteSheets.set(source, image);
|
|
126
130
|
return image;
|
|
127
131
|
}
|
|
@@ -174,8 +174,15 @@ export declare class SkiaAccessibilityManager {
|
|
|
174
174
|
private focusRequest?;
|
|
175
175
|
private popupFocusRoot?;
|
|
176
176
|
private focusAnyInside;
|
|
177
|
+
private popupFocusDeadline;
|
|
177
178
|
/** The keyboard goes to this control at the end of the next frame (a popup closed: back to what opened it). */
|
|
178
179
|
RequestKeyboardFocus(control: SkiaControl): void;
|
|
180
|
+
/**
|
|
181
|
+
* How long a keyboard focus request waits for its control to take input again (drawnui-cross 6bs): an app that shows
|
|
182
|
+
* the opener of a dialog again only once the dialog is gone (a HUD hidden under it) still gets the keyboard back there.
|
|
183
|
+
*/
|
|
184
|
+
static FocusRequestWaitMs: number;
|
|
185
|
+
private focusRequestDeadline;
|
|
179
186
|
private appFocus?;
|
|
180
187
|
private appFocusDeadline;
|
|
181
188
|
private appFocusThisFrame;
|
|
@@ -203,6 +210,11 @@ export declare class SkiaAccessibilityManager {
|
|
|
203
210
|
set KeyboardScope(v: SkiaControl | undefined);
|
|
204
211
|
/** The modal that accessibility is limited to: the topmost `AccessibilityModal` control that shows. Undefined when none. */
|
|
205
212
|
ActiveModal?: SkiaControl;
|
|
213
|
+
/**
|
|
214
|
+
* The node the overlay names the active modal after (drawnui-cross 6bs): the modal's own when it has a name, else the
|
|
215
|
+
* first named dialog inside it, so a nameless popup host around a dialog reads as that one dialog, not as two.
|
|
216
|
+
*/
|
|
217
|
+
get ActiveModalNode(): AccessibilityNode | undefined;
|
|
206
218
|
private readonly modalReturns;
|
|
207
219
|
/** A control behind the active modal (not in it, not in a popup window over it): no focus for navigation. */
|
|
208
220
|
IsOutsideModal(control: SkiaControl): boolean;
|
|
@@ -262,9 +262,20 @@ export class SkiaAccessibilityManager {
|
|
|
262
262
|
focusRequest;
|
|
263
263
|
popupFocusRoot;
|
|
264
264
|
focusAnyInside = false;
|
|
265
|
+
popupFocusDeadline = 0;
|
|
265
266
|
/** The keyboard goes to this control at the end of the next frame (a popup closed: back to what opened it). */
|
|
266
|
-
RequestKeyboardFocus(control) {
|
|
267
|
-
this.
|
|
267
|
+
RequestKeyboardFocus(control) {
|
|
268
|
+
if (this.appFocusThisFrame)
|
|
269
|
+
return;
|
|
270
|
+
this.focusRequest = control;
|
|
271
|
+
this.focusRequestDeadline = performance.now() + SkiaAccessibilityManager.FocusRequestWaitMs;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* How long a keyboard focus request waits for its control to take input again (drawnui-cross 6bs): an app that shows
|
|
275
|
+
* the opener of a dialog again only once the dialog is gone (a HUD hidden under it) still gets the keyboard back there.
|
|
276
|
+
*/
|
|
277
|
+
static FocusRequestWaitMs = 1000;
|
|
278
|
+
focusRequestDeadline = 0;
|
|
268
279
|
appFocus;
|
|
269
280
|
appFocusDeadline = 0;
|
|
270
281
|
appFocusThisFrame = false; // until the frame end: the app said where the focus goes, the library's moves give way
|
|
@@ -313,6 +324,19 @@ export class SkiaAccessibilityManager {
|
|
|
313
324
|
// ---- modal accessibility scope (drawnui-cross 6av, C# AccessibilityModal) ----
|
|
314
325
|
/** The modal that accessibility is limited to: the topmost `AccessibilityModal` control that shows. Undefined when none. */
|
|
315
326
|
ActiveModal;
|
|
327
|
+
/**
|
|
328
|
+
* The node the overlay names the active modal after (drawnui-cross 6bs): the modal's own when it has a name, else the
|
|
329
|
+
* first named dialog inside it, so a nameless popup host around a dialog reads as that one dialog, not as two.
|
|
330
|
+
*/
|
|
331
|
+
get ActiveModalNode() {
|
|
332
|
+
const modal = this.ActiveModal;
|
|
333
|
+
if (!modal)
|
|
334
|
+
return undefined;
|
|
335
|
+
const own = this.Snapshot.find((n) => n.Source === modal);
|
|
336
|
+
if (own?.Label)
|
|
337
|
+
return own;
|
|
338
|
+
return this.Snapshot.find((n) => (n.Role === Aria.RoleDialog || n.Role === Aria.RoleAlertDialog) && !!n.Label && n.Source !== modal && IsInside(n.Source, modal)) ?? own;
|
|
339
|
+
}
|
|
316
340
|
// where the keyboard was when each modal became active, to go back there when it goes
|
|
317
341
|
modalReturns = new Map();
|
|
318
342
|
/** A control behind the active modal (not in it, not in a popup window over it): no focus for navigation. */
|
|
@@ -336,18 +360,27 @@ export class SkiaAccessibilityManager {
|
|
|
336
360
|
this.ActiveModal = next;
|
|
337
361
|
this.ForceRebuildOnNextFrame(); // readers and Tab switch to the new scope in this frame
|
|
338
362
|
const keyboard = this.FocusedNode;
|
|
339
|
-
if (next && keyboard && !IsInside(keyboard, next)) {
|
|
340
|
-
// the keyboard is in use: into the modal, and back here when it goes
|
|
341
|
-
this.modalReturns.set(next, keyboard);
|
|
342
|
-
this.popupFocusRoot = next;
|
|
343
|
-
this.focusAnyInside = true;
|
|
344
|
-
}
|
|
345
363
|
const back = previous && this.modalReturns.get(previous);
|
|
346
364
|
if (previous)
|
|
347
365
|
this.modalReturns.delete(previous);
|
|
348
366
|
// back unless the keyboard went elsewhere meanwhile; a keyboard node that left with the modal (disposed, detached) was in it
|
|
349
367
|
const leftWithIt = !!keyboard && (keyboard.IsDisposed || !keyboard.Superview);
|
|
350
|
-
|
|
368
|
+
const fromPrevious = !!back && (!keyboard || leftWithIt || IsInside(keyboard, previous));
|
|
369
|
+
if (next) {
|
|
370
|
+
// the keyboard is in use: into the modal, and back to `from` when it goes. A dialog handing over to another (How to
|
|
371
|
+
// play, then Paused) passes its own return on, in the same frame or while that return is still waiting (6bs).
|
|
372
|
+
const waiting = this.focusRequest && !this.focusRequest.IsDisposed && !IsInside(this.focusRequest, next) ? this.focusRequest : undefined;
|
|
373
|
+
const from = fromPrevious ? back : waiting ?? (keyboard && !leftWithIt && !IsInside(keyboard, next) ? keyboard : undefined);
|
|
374
|
+
if (from) {
|
|
375
|
+
this.modalReturns.set(next, from);
|
|
376
|
+
this.popupFocusRoot = next;
|
|
377
|
+
this.focusAnyInside = true;
|
|
378
|
+
this.popupFocusDeadline = performance.now() + SkiaAccessibilityManager.FocusRequestWaitMs;
|
|
379
|
+
if (from === this.focusRequest)
|
|
380
|
+
this.focusRequest = undefined;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (fromPrevious && (!next || IsInside(back, next)))
|
|
351
384
|
this.RequestKeyboardFocus(back);
|
|
352
385
|
}
|
|
353
386
|
/** The Tab stop after (or before) `from` in reading order; inside a KeyboardScope it goes round, never leaving. */
|
|
@@ -367,8 +400,8 @@ export class SkiaAccessibilityManager {
|
|
|
367
400
|
* A popup window just opened: at the end of the frame that draws it, the keyboard goes to its arrow-key group's current
|
|
368
401
|
* choice (a checked radio, menuitemradio, option or tab), else to its first interactive item, however it was opened.
|
|
369
402
|
*/
|
|
370
|
-
FocusOpenedPopup(root) { this.popupFocusRoot = root; this.focusAnyInside = false; }
|
|
371
|
-
ProcessFocusRequests(scale, w, h) {
|
|
403
|
+
FocusOpenedPopup(root) { this.popupFocusRoot = root; this.focusAnyInside = false; this.popupFocusDeadline = performance.now() + SkiaAccessibilityManager.FocusRequestWaitMs; }
|
|
404
|
+
ProcessFocusRequests(scale, w, h, requestFrame) {
|
|
372
405
|
const request = this.focusRequest, root = this.popupFocusRoot;
|
|
373
406
|
this.focusRequest = this.popupFocusRoot = undefined;
|
|
374
407
|
if (root && !root.IsDisposed && root.Superview) {
|
|
@@ -384,9 +417,23 @@ export class SkiaAccessibilityManager {
|
|
|
384
417
|
this.FocusFromKeyboard(target.Source, scale, w, h);
|
|
385
418
|
return;
|
|
386
419
|
}
|
|
420
|
+
// a modal with nothing to take input yet (its buttons show a moment later): looked at again on the next frames, for a moment
|
|
421
|
+
if (this.focusAnyInside && performance.now() < this.popupFocusDeadline) {
|
|
422
|
+
this.popupFocusRoot = root;
|
|
423
|
+
this.focusRequest ??= request;
|
|
424
|
+
requestFrame();
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
387
427
|
}
|
|
388
|
-
if (request
|
|
428
|
+
if (!request || request.IsDisposed || !request.Superview)
|
|
429
|
+
return;
|
|
430
|
+
if (request.AccessibilityCanInteract)
|
|
389
431
|
this.FocusFromKeyboard(request, scale, w, h);
|
|
432
|
+
// not taking input yet (shown again only after a dialog closed): asked again on the next frames, for a moment
|
|
433
|
+
else if (performance.now() < this.focusRequestDeadline) {
|
|
434
|
+
this.focusRequest = request;
|
|
435
|
+
requestFrame();
|
|
436
|
+
}
|
|
390
437
|
}
|
|
391
438
|
/** Frame end: focus the requested item once it is drawn, skipping items that cannot take input. */
|
|
392
439
|
ProcessGroupFocus(scale, w, h, requestFrame) {
|
|
@@ -483,7 +530,7 @@ export class SkiaAccessibilityManager {
|
|
|
483
530
|
if (this.appFocus)
|
|
484
531
|
this.ProcessAppFocus(scale, canvasWidthPx, canvasHeightPx, requestFrame);
|
|
485
532
|
if (this.focusRequest || this.popupFocusRoot)
|
|
486
|
-
this.ProcessFocusRequests(scale, canvasWidthPx, canvasHeightPx);
|
|
533
|
+
this.ProcessFocusRequests(scale, canvasWidthPx, canvasHeightPx, requestFrame);
|
|
487
534
|
this.appFocusThisFrame = false;
|
|
488
535
|
if (this.nodes.size === 0 && this.Snapshot.length === 0)
|
|
489
536
|
return;
|
|
@@ -513,7 +560,8 @@ export class SkiaAccessibilityManager {
|
|
|
513
560
|
n.OnAccessibilityUnregistered();
|
|
514
561
|
continue;
|
|
515
562
|
} // detached from the tree
|
|
516
|
-
|
|
563
|
+
// at opacity 0 a control is not seen, so it is not read either (DrawnUi.Net 4bd759e8)
|
|
564
|
+
if (!n.IsVisible || n.Opacity <= 0 || !n.IsAccessibilityElement || n.AccessibilityRole === Aria.RolePresentation)
|
|
517
565
|
continue;
|
|
518
566
|
if (modal && this.IsOutsideModal(n))
|
|
519
567
|
continue; // a modal is shown: only its nodes, and those of popup windows over it
|
package/dist/core/Canvas.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ import { ContextMenuEventArgs, type GesturesMode, SKPoint, SkiaGesturesParameter
|
|
|
13
13
|
* Frames are drawn only after Update() (invalidation), never continuously.
|
|
14
14
|
* Gestures are accumulated and processed in order at the START of the next frame, like DrawnUi.
|
|
15
15
|
*/
|
|
16
|
+
/** The system's light / dark appearance (DrawnUi.Rust SystemTheme). */
|
|
17
|
+
export type SystemTheme = "Light" | "Dark";
|
|
16
18
|
export declare class Canvas {
|
|
17
19
|
readonly Element: HTMLCanvasElement;
|
|
18
20
|
/** Points a pointer may travel between Down and Up and still count as a tap (AppoMobi TouchEffect default). */
|
|
@@ -43,6 +45,20 @@ export declare class Canvas {
|
|
|
43
45
|
WillFirstTimeDraw?: (sender: Canvas, context: DrawingContext["Context"]) => void;
|
|
44
46
|
/** DrawnView.WasDrawn: raised after every frame has been drawn (the C# event passes no context). */
|
|
45
47
|
WasDrawn?: (sender: Canvas) => void;
|
|
48
|
+
/**
|
|
49
|
+
* The system's light / dark appearance (DrawnUi.Rust `system_theme`): undefined until the host learns it, which it
|
|
50
|
+
* does when the canvas is created (`prefers-color-scheme`), then it follows every change.
|
|
51
|
+
*/
|
|
52
|
+
get SystemTheme(): SystemTheme | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Raised once when the host learns the system theme (set it in the Canvas init / the `<Canvas>` prop to get that first
|
|
55
|
+
* value), then once per change; a repeated value is not raised. No handler, no call; it never asks for a frame.
|
|
56
|
+
*/
|
|
57
|
+
SystemThemeChanged?: (theme: SystemTheme) => void;
|
|
58
|
+
private systemTheme?;
|
|
59
|
+
private themeQuery?;
|
|
60
|
+
private readonly onThemeQuery;
|
|
61
|
+
private SetSystemTheme;
|
|
46
62
|
/** Registry + rate-limited snapshot of accessible controls, rendered by the DOM overlay (DrawnUi AccessibilityManager). */
|
|
47
63
|
readonly AccessibilityManager: SkiaAccessibilityManager;
|
|
48
64
|
/** Mouse hover of the controls (drawnui-cross 6m): ReceivesHover / IsHovered / HoverChanged. */
|
|
@@ -129,13 +145,25 @@ export declare class Canvas {
|
|
|
129
145
|
private frameId;
|
|
130
146
|
private disposed;
|
|
131
147
|
private readonly observer;
|
|
148
|
+
/**
|
|
149
|
+
* The device pixels the element covers, as the browser reports them (ResizeObserver device-pixel-content-box). At a
|
|
150
|
+
* 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
|
|
151
|
+
* px), and the browser would then resample the whole canvas, softening every glyph.
|
|
152
|
+
*/
|
|
153
|
+
private devicePixels?;
|
|
154
|
+
/** The element's CSS width when the device-pixel box was reported. */
|
|
155
|
+
private devicePixelsCss;
|
|
132
156
|
/** The WebGL context is lost: no frames until the browser restores it. */
|
|
133
157
|
private contextLost;
|
|
134
158
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
159
|
+
* The GPU context generation, unique across canvases: a new one when this canvas is made and when its WebGL context
|
|
160
|
+
* is restored. Every GPU object made under another one (Image caches, picture caches that may replay them, kept
|
|
161
|
+
* composite surfaces, effect textures; also those a control brought from another canvas) is remade at its next draw
|
|
162
|
+
* (DrawnUi.Rust Gpu::epoch). A texture of another WebGL context draws nothing.
|
|
137
163
|
*/
|
|
138
164
|
GpuEpoch: number;
|
|
165
|
+
private static gpuEpochs;
|
|
166
|
+
private static NextGpuEpoch;
|
|
139
167
|
/** `init` runs before the first frame is drawn: the place to attach `WillFirstTimeDraw` / `WasDrawn` for that frame. */
|
|
140
168
|
constructor(Element: HTMLCanvasElement, init?: (canvas: Canvas) => void);
|
|
141
169
|
/** Request a redraw (full measure + arrange + render) on the next animation frame. */
|
|
@@ -150,15 +178,26 @@ export declare class Canvas {
|
|
|
150
178
|
* redrawn content lands in the same paint and no blank frame is ever shown while dragging.
|
|
151
179
|
*/
|
|
152
180
|
private OnResized;
|
|
153
|
-
/**
|
|
181
|
+
/**
|
|
182
|
+
* Registers the WebGL context without `WEBGL_polygon_mode` (drawnui-cross 6bt): Emscripten turns on every extension it
|
|
183
|
+
* knows while it registers a context, nothing here draws with that one, and WebKit warns "non-portable extension
|
|
184
|
+
* enabled" for it. The lookup is hidden only for this synchronous call (creating the context here first would change
|
|
185
|
+
* the attributes CanvasKit asks for).
|
|
186
|
+
*/
|
|
187
|
+
private static WithoutPolygonMode;
|
|
188
|
+
/**
|
|
189
|
+
* WebGL context lost: keep the browser restoring it (preventDefault) and stop the frames. The old Skia context is
|
|
190
|
+
* abandoned here, while the context is lost: WebGL ignores every call on a lost context without a word, so the GL
|
|
191
|
+
* deletes Skia makes while it lets go of its objects stay silent (done after the restore, they reach the new
|
|
192
|
+
* generation and every one logs "object does not belong to this context"). CanvasKit has no `abandonContext`;
|
|
193
|
+
* `releaseResourcesAndAbandonContext` is its only way, and after it freeing an old image or surface makes no GL call.
|
|
194
|
+
*/
|
|
154
195
|
private readonly onContextLost;
|
|
155
196
|
/**
|
|
156
197
|
* WebGL context restored (as DrawnUi.Rust and the C# Wasm / Blazor heads): every GL object of the lost generation is
|
|
157
|
-
* gone
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* GL call. Then the same WebGL object is registered again (the old handle deleted first, which clears the canvas'
|
|
161
|
-
* context object) with a new GrContext and surface; GpuEpoch makes the old generation's GPU objects remake themselves.
|
|
198
|
+
* gone, and the old Skia context was abandoned when it was lost. The same WebGL object is registered again (the old
|
|
199
|
+
* handle deleted first, which clears the canvas' context object) with a new GrContext and surface; GpuEpoch makes the
|
|
200
|
+
* old generation's GPU objects remake themselves.
|
|
162
201
|
*/
|
|
163
202
|
private readonly onContextRestored;
|
|
164
203
|
/** Draws one frame immediately (outside the rAF loop) and presents it. */
|
|
@@ -180,6 +219,9 @@ export declare class Canvas {
|
|
|
180
219
|
* focus, a game in it stops getting keys (CanvasLeaveWatchers). Runs once per removal, never per frame.
|
|
181
220
|
*/
|
|
182
221
|
ControlLeft(root: SkiaControl, disposed: boolean): void;
|
|
222
|
+
private static readonly reportedAnimators;
|
|
223
|
+
/** An animator's callback threw: logged once for that animator, the frame goes on. */
|
|
224
|
+
private static ReportAnimatorError;
|
|
183
225
|
/** Ticks every registered animator once; returns how many ran. */
|
|
184
226
|
protected ExecuteAnimators(frameTimeNanos: number): number;
|
|
185
227
|
private wakeTimer;
|
package/dist/core/Canvas.js
CHANGED
|
@@ -7,12 +7,6 @@ import { PopupAnchor, PopupLayer } from "./PopupWindows";
|
|
|
7
7
|
import { KeyboardManager } from "./KeyboardManager";
|
|
8
8
|
import { Colors, SKRect } from "./Types";
|
|
9
9
|
import { ContextMenuEventArgs, GestureEventProcessingInfo, MultitouchTracker, PointerData, SKPoint, SkiaGesturesParameters, TouchActionEventArgs, } from "./Gestures";
|
|
10
|
-
/**
|
|
11
|
-
* Mirrors DrawnUi Canvas (DrawnView): hosts one Content control on an HTML canvas element,
|
|
12
|
-
* owns RenderingScale (devicePixelRatio), the surface, the on-demand frame loop and raw input.
|
|
13
|
-
* Frames are drawn only after Update() (invalidation), never continuously.
|
|
14
|
-
* Gestures are accumulated and processed in order at the START of the next frame, like DrawnUi.
|
|
15
|
-
*/
|
|
16
10
|
export class Canvas {
|
|
17
11
|
Element;
|
|
18
12
|
/** Points a pointer may travel between Down and Up and still count as a tap (AppoMobi TouchEffect default). */
|
|
@@ -43,6 +37,25 @@ export class Canvas {
|
|
|
43
37
|
WillFirstTimeDraw;
|
|
44
38
|
/** DrawnView.WasDrawn: raised after every frame has been drawn (the C# event passes no context). */
|
|
45
39
|
WasDrawn;
|
|
40
|
+
/**
|
|
41
|
+
* The system's light / dark appearance (DrawnUi.Rust `system_theme`): undefined until the host learns it, which it
|
|
42
|
+
* does when the canvas is created (`prefers-color-scheme`), then it follows every change.
|
|
43
|
+
*/
|
|
44
|
+
get SystemTheme() { return this.systemTheme; }
|
|
45
|
+
/**
|
|
46
|
+
* Raised once when the host learns the system theme (set it in the Canvas init / the `<Canvas>` prop to get that first
|
|
47
|
+
* value), then once per change; a repeated value is not raised. No handler, no call; it never asks for a frame.
|
|
48
|
+
*/
|
|
49
|
+
SystemThemeChanged;
|
|
50
|
+
systemTheme;
|
|
51
|
+
themeQuery;
|
|
52
|
+
onThemeQuery = () => this.SetSystemTheme(this.themeQuery.matches ? "Dark" : "Light");
|
|
53
|
+
SetSystemTheme(theme) {
|
|
54
|
+
if (theme === this.systemTheme)
|
|
55
|
+
return;
|
|
56
|
+
this.systemTheme = theme;
|
|
57
|
+
this.SystemThemeChanged?.(theme);
|
|
58
|
+
}
|
|
46
59
|
/** Registry + rate-limited snapshot of accessible controls, rendered by the DOM overlay (DrawnUi AccessibilityManager). */
|
|
47
60
|
AccessibilityManager = new SkiaAccessibilityManager();
|
|
48
61
|
/** Mouse hover of the controls (drawnui-cross 6m): ReceivesHover / IsHovered / HoverChanged. */
|
|
@@ -180,26 +193,57 @@ export class Canvas {
|
|
|
180
193
|
frameId = 0;
|
|
181
194
|
disposed = false;
|
|
182
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;
|
|
183
204
|
/** The WebGL context is lost: no frames until the browser restores it. */
|
|
184
205
|
contextLost = false;
|
|
185
206
|
/**
|
|
186
|
-
*
|
|
187
|
-
*
|
|
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.
|
|
188
211
|
*/
|
|
189
|
-
GpuEpoch =
|
|
212
|
+
GpuEpoch = Canvas.NextGpuEpoch();
|
|
213
|
+
static gpuEpochs = 0;
|
|
214
|
+
static NextGpuEpoch() { return ++Canvas.gpuEpochs; }
|
|
190
215
|
/** `init` runs before the first frame is drawn: the place to attach `WillFirstTimeDraw` / `WasDrawn` for that frame. */
|
|
191
216
|
constructor(Element, init) {
|
|
192
217
|
this.Element = Element;
|
|
193
218
|
if (!Super.CK)
|
|
194
219
|
throw new Error("DrawnUi: call Super.UseDrawnUi()...BuildAsync() before creating a Canvas");
|
|
195
220
|
init?.(this);
|
|
221
|
+
if (typeof window.matchMedia === "function") {
|
|
222
|
+
this.themeQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
223
|
+
this.themeQuery.addEventListener?.("change", this.onThemeQuery);
|
|
224
|
+
this.onThemeQuery();
|
|
225
|
+
}
|
|
196
226
|
Element.addEventListener("webglcontextlost", this.onContextLost);
|
|
197
227
|
Element.addEventListener("webglcontextrestored", this.onContextRestored);
|
|
198
228
|
window.addEventListener("blur", this.onWindowInactive);
|
|
199
229
|
window.addEventListener("focus", this.onWindowActive);
|
|
200
230
|
this.Hover.RootAt = (x, y) => this.popupLayer?.At(x, y) ?? this.content;
|
|
201
|
-
this.observer = new ResizeObserver(() =>
|
|
202
|
-
|
|
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
|
+
}
|
|
203
247
|
this.OnResized();
|
|
204
248
|
}
|
|
205
249
|
/** Request a redraw (full measure + arrange + render) on the next animation frame. */
|
|
@@ -231,7 +275,7 @@ export class Canvas {
|
|
|
231
275
|
const CK = Super.CK;
|
|
232
276
|
if (this.RenderingMode === "Accelerated") {
|
|
233
277
|
if (!this.grContext) {
|
|
234
|
-
this.glHandle = CK.GetWebGLContext(this.Element);
|
|
278
|
+
this.glHandle = Canvas.WithoutPolygonMode(() => CK.GetWebGLContext(this.Element));
|
|
235
279
|
this.grContext = (this.glHandle ? CK.MakeWebGLContext(this.glHandle) : null) ?? undefined;
|
|
236
280
|
}
|
|
237
281
|
if (this.grContext) {
|
|
@@ -248,9 +292,16 @@ export class Canvas {
|
|
|
248
292
|
* redrawn content lands in the same paint and no blank frame is ever shown while dragging.
|
|
249
293
|
*/
|
|
250
294
|
OnResized() {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
295
|
+
let dpr = window.devicePixelRatio || 1;
|
|
296
|
+
// the device-pixel box can disagree with devicePixelRatio (DPR emulation in DevTools or Playwright, some zoom cases):
|
|
297
|
+
// past 2% the scale follows the box, so the layout keeps the element's CSS size (DrawnUi.Rust e7ff239)
|
|
298
|
+
if (this.devicePixels && this.devicePixelsCss > 0) {
|
|
299
|
+
const boxScale = this.devicePixels[0] / this.devicePixelsCss;
|
|
300
|
+
if (Math.abs(boxScale - dpr) > dpr * 0.02)
|
|
301
|
+
dpr = boxScale;
|
|
302
|
+
}
|
|
303
|
+
const w = Math.max(1, this.devicePixels?.[0] ?? Math.round(this.Element.clientWidth * dpr));
|
|
304
|
+
const h = Math.max(1, this.devicePixels?.[1] ?? Math.round(this.Element.clientHeight * dpr));
|
|
254
305
|
if (this.surface && this.Element.width === w && this.Element.height === h && this.RenderingScale === dpr)
|
|
255
306
|
return;
|
|
256
307
|
this.RenderingScale = dpr;
|
|
@@ -264,7 +315,31 @@ export class Canvas {
|
|
|
264
315
|
throw new Error("DrawnUi: cannot create surface");
|
|
265
316
|
this.DrawNow();
|
|
266
317
|
}
|
|
267
|
-
/**
|
|
318
|
+
/**
|
|
319
|
+
* Registers the WebGL context without `WEBGL_polygon_mode` (drawnui-cross 6bt): Emscripten turns on every extension it
|
|
320
|
+
* knows while it registers a context, nothing here draws with that one, and WebKit warns "non-portable extension
|
|
321
|
+
* enabled" for it. The lookup is hidden only for this synchronous call (creating the context here first would change
|
|
322
|
+
* the attributes CanvasKit asks for).
|
|
323
|
+
*/
|
|
324
|
+
static WithoutPolygonMode(make) {
|
|
325
|
+
const prototypes = [globalThis.WebGL2RenderingContext?.prototype, globalThis.WebGLRenderingContext?.prototype]
|
|
326
|
+
.filter((p) => !!p);
|
|
327
|
+
const originals = prototypes.map((p) => p.getExtension);
|
|
328
|
+
prototypes.forEach((p, i) => { p.getExtension = function (name) { return name === "WEBGL_polygon_mode" ? null : originals[i].call(this, name); }; });
|
|
329
|
+
try {
|
|
330
|
+
return make();
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
prototypes.forEach((p, i) => { p.getExtension = originals[i]; });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* WebGL context lost: keep the browser restoring it (preventDefault) and stop the frames. The old Skia context is
|
|
338
|
+
* abandoned here, while the context is lost: WebGL ignores every call on a lost context without a word, so the GL
|
|
339
|
+
* deletes Skia makes while it lets go of its objects stay silent (done after the restore, they reach the new
|
|
340
|
+
* generation and every one logs "object does not belong to this context"). CanvasKit has no `abandonContext`;
|
|
341
|
+
* `releaseResourcesAndAbandonContext` is its only way, and after it freeing an old image or surface makes no GL call.
|
|
342
|
+
*/
|
|
268
343
|
onContextLost = (e) => {
|
|
269
344
|
e.preventDefault();
|
|
270
345
|
this.contextLost = true;
|
|
@@ -272,20 +347,24 @@ export class Canvas {
|
|
|
272
347
|
cancelAnimationFrame(this.frameId);
|
|
273
348
|
this.frameId = 0;
|
|
274
349
|
}
|
|
350
|
+
if (this.grContext)
|
|
351
|
+
this.grContext.releaseResourcesAndAbandonContext();
|
|
352
|
+
this.surface?.dispose();
|
|
353
|
+
this.surface = undefined;
|
|
354
|
+
this.grContext?.delete();
|
|
355
|
+
this.grContext = undefined;
|
|
275
356
|
};
|
|
276
357
|
/**
|
|
277
358
|
* WebGL context restored (as DrawnUi.Rust and the C# Wasm / Blazor heads): every GL object of the lost generation is
|
|
278
|
-
* gone
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* GL call. Then the same WebGL object is registered again (the old handle deleted first, which clears the canvas'
|
|
282
|
-
* context object) with a new GrContext and surface; GpuEpoch makes the old generation's GPU objects remake themselves.
|
|
359
|
+
* gone, and the old Skia context was abandoned when it was lost. The same WebGL object is registered again (the old
|
|
360
|
+
* handle deleted first, which clears the canvas' context object) with a new GrContext and surface; GpuEpoch makes the
|
|
361
|
+
* old generation's GPU objects remake themselves.
|
|
283
362
|
*/
|
|
284
363
|
onContextRestored = () => {
|
|
285
364
|
if (this.disposed)
|
|
286
365
|
return;
|
|
287
366
|
if (this.grContext)
|
|
288
|
-
this.grContext.releaseResourcesAndAbandonContext();
|
|
367
|
+
this.grContext.releaseResourcesAndAbandonContext(); // lost before this Canvas listened
|
|
289
368
|
this.surface?.dispose();
|
|
290
369
|
this.surface = undefined;
|
|
291
370
|
this.grContext?.delete();
|
|
@@ -295,7 +374,7 @@ export class Canvas {
|
|
|
295
374
|
this.glHandle = undefined;
|
|
296
375
|
}
|
|
297
376
|
this.contextLost = false;
|
|
298
|
-
this.GpuEpoch
|
|
377
|
+
this.GpuEpoch = Canvas.NextGpuEpoch();
|
|
299
378
|
this.surface = this.CreateSurface(this.Element.width, this.Element.height);
|
|
300
379
|
this.DrawNow();
|
|
301
380
|
};
|
|
@@ -408,6 +487,14 @@ export class Canvas {
|
|
|
408
487
|
if (inside(c))
|
|
409
488
|
c.OnLeftCanvas();
|
|
410
489
|
}
|
|
490
|
+
static reportedAnimators = new WeakSet();
|
|
491
|
+
/** An animator's callback threw: logged once for that animator, the frame goes on. */
|
|
492
|
+
static ReportAnimatorError(a, e) {
|
|
493
|
+
if (Canvas.reportedAnimators.has(a))
|
|
494
|
+
return;
|
|
495
|
+
Canvas.reportedAnimators.add(a);
|
|
496
|
+
console.error("DrawnUi: an animator's callback threw; the other animators and the frame go on", e);
|
|
497
|
+
}
|
|
411
498
|
/** Ticks every registered animator once; returns how many ran. */
|
|
412
499
|
ExecuteAnimators(frameTimeNanos) {
|
|
413
500
|
let executed = 0, wake = Infinity;
|
|
@@ -434,7 +521,13 @@ export class Canvas {
|
|
|
434
521
|
wake = Math.min(wake, a.SleepUntilNanos);
|
|
435
522
|
continue;
|
|
436
523
|
} // nothing changes yet
|
|
437
|
-
|
|
524
|
+
// one animator whose callback throws must not stop the others or the frame (DrawnUi.Net catches the pass, Super.Log)
|
|
525
|
+
try {
|
|
526
|
+
a.TickFrame(frameTimeNanos);
|
|
527
|
+
}
|
|
528
|
+
catch (e) {
|
|
529
|
+
Canvas.ReportAnimatorError(a, e);
|
|
530
|
+
}
|
|
438
531
|
executed++;
|
|
439
532
|
if (a.SleepUntilNanos > frameTimeNanos)
|
|
440
533
|
wake = Math.min(wake, a.SleepUntilNanos);
|
|
@@ -519,6 +612,8 @@ export class Canvas {
|
|
|
519
612
|
}
|
|
520
613
|
this.Element.removeEventListener("webglcontextlost", this.onContextLost);
|
|
521
614
|
this.Element.removeEventListener("webglcontextrestored", this.onContextRestored);
|
|
615
|
+
this.themeQuery?.removeEventListener?.("change", this.onThemeQuery);
|
|
616
|
+
this.SystemThemeChanged = undefined;
|
|
522
617
|
window.removeEventListener("blur", this.onWindowInactive);
|
|
523
618
|
window.removeEventListener("focus", this.onWindowActive);
|
|
524
619
|
this.WindowActiveChanged.clear();
|
|
@@ -530,7 +625,12 @@ export class Canvas {
|
|
|
530
625
|
this.observer.disconnect();
|
|
531
626
|
this.content?.Dispose();
|
|
532
627
|
this.Content = undefined;
|
|
628
|
+
this.DrainDisposeQueue(); // what the last frames handed over for deletion goes while the context lives
|
|
533
629
|
this.ReleaseSurface();
|
|
630
|
+
// GPU objects that outlive this canvas (a control the app keeps and shows on another canvas) are freed later, under
|
|
631
|
+
// another WebGL context: the abandoned Skia context makes no GL call when they go (as DrawnUi.Net Cells 81b89f1a)
|
|
632
|
+
if (this.grContext && !this.contextLost)
|
|
633
|
+
this.grContext.releaseResourcesAndAbandonContext();
|
|
534
634
|
this.grContext?.delete();
|
|
535
635
|
this.grContext = undefined;
|
|
536
636
|
if (this.glHandle) {
|
|
@@ -712,7 +812,8 @@ export class Canvas {
|
|
|
712
812
|
// horizontal events alone (C# ba03cb20)
|
|
713
813
|
const factor = e.deltaMode === 1 ? 40 : e.deltaMode === 2 ? 800 : 1;
|
|
714
814
|
const horizontal = Math.abs(e.deltaX) > Math.abs(e.deltaY);
|
|
715
|
-
|
|
815
|
+
// IsZoom: Ctrl / Cmd + wheel, or a touchpad pinch (browsers send it as a Ctrl wheel), as AppoMobi WheelEventArgs.IsZoom
|
|
816
|
+
args.Wheel = { Delta: (horizontal ? e.deltaX : e.deltaY) * factor, IsHorizontal: horizontal, IsZoom: e.ctrlKey || e.metaKey };
|
|
716
817
|
this.tooltip.CloseOnInput();
|
|
717
818
|
if (this.gestures === "Lock") {
|
|
718
819
|
e.preventDefault();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actions an app declares for the DrawnUI Designer (C# DrawnUi.Draw.DesignerActions): named states it can jump to without
|
|
3
|
+
* being played to them (a level, a game state, a dialog). The designer lists them under the app's screens and runs one on
|
|
4
|
+
* a click; AI agents run them through the designer's MCP (`list_actions`, `run_action`).
|
|
5
|
+
*
|
|
6
|
+
* A path names the action and its groups, slash-separated: "Levels/Level 3" shows as "Level 3" under "Levels". Nothing
|
|
7
|
+
* here runs unless a designer is attached: the list only keeps the names and their callbacks.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* for (let level = 1; level <= 20; level++) designerAction(`Levels/Level ${level}`, () => game.startLevel(level));
|
|
11
|
+
* designerAction("Dialogs/Options", () => game.showOptions());
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class DesignerActions {
|
|
15
|
+
private static readonly items;
|
|
16
|
+
/** Raised after the list changed (an action added, replaced or removed); the designer agent sends the new list. */
|
|
17
|
+
static readonly Changed: Set<() => void>;
|
|
18
|
+
private static RaiseChanged;
|
|
19
|
+
/**
|
|
20
|
+
* Adds an action, or replaces the one with the same path (keeping its place). Returns the function that removes it
|
|
21
|
+
* again (an action a screen offers while it is shown); it removes only this registration, not a later one of the path.
|
|
22
|
+
*/
|
|
23
|
+
static Add(path: string, run: () => void): () => void;
|
|
24
|
+
/** Removes the action with this path, if there is one. */
|
|
25
|
+
static Remove(path: string): void;
|
|
26
|
+
/** The declared actions' paths, in the order they were added. */
|
|
27
|
+
static get Paths(): string[];
|
|
28
|
+
/** Runs the action with this path; false when there is no such action. */
|
|
29
|
+
static Run(path: string): boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Declares a Designer action (DesignerActions.Add); returns the function that removes it. Usable outside React. */
|
|
32
|
+
export declare function designerAction(path: string, run: () => void): () => void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actions an app declares for the DrawnUI Designer (C# DrawnUi.Draw.DesignerActions): named states it can jump to without
|
|
3
|
+
* being played to them (a level, a game state, a dialog). The designer lists them under the app's screens and runs one on
|
|
4
|
+
* a click; AI agents run them through the designer's MCP (`list_actions`, `run_action`).
|
|
5
|
+
*
|
|
6
|
+
* A path names the action and its groups, slash-separated: "Levels/Level 3" shows as "Level 3" under "Levels". Nothing
|
|
7
|
+
* here runs unless a designer is attached: the list only keeps the names and their callbacks.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* for (let level = 1; level <= 20; level++) designerAction(`Levels/Level ${level}`, () => game.startLevel(level));
|
|
11
|
+
* designerAction("Dialogs/Options", () => game.showOptions());
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export class DesignerActions {
|
|
15
|
+
static items = [];
|
|
16
|
+
/** Raised after the list changed (an action added, replaced or removed); the designer agent sends the new list. */
|
|
17
|
+
static Changed = new Set();
|
|
18
|
+
static RaiseChanged() { for (const h of [...DesignerActions.Changed])
|
|
19
|
+
h(); }
|
|
20
|
+
/**
|
|
21
|
+
* Adds an action, or replaces the one with the same path (keeping its place). Returns the function that removes it
|
|
22
|
+
* again (an action a screen offers while it is shown); it removes only this registration, not a later one of the path.
|
|
23
|
+
*/
|
|
24
|
+
static Add(path, run) {
|
|
25
|
+
if (!path || !path.trim())
|
|
26
|
+
throw new Error("DesignerActions: an action needs a path");
|
|
27
|
+
if (typeof run !== "function")
|
|
28
|
+
throw new Error("DesignerActions: an action needs a function");
|
|
29
|
+
const items = DesignerActions.items, at = items.findIndex((x) => x.Path === path);
|
|
30
|
+
const item = { Path: path, Run: run };
|
|
31
|
+
if (at >= 0)
|
|
32
|
+
items[at] = item;
|
|
33
|
+
else
|
|
34
|
+
items.push(item);
|
|
35
|
+
DesignerActions.RaiseChanged();
|
|
36
|
+
let done = false;
|
|
37
|
+
return () => {
|
|
38
|
+
if (done)
|
|
39
|
+
return;
|
|
40
|
+
done = true;
|
|
41
|
+
const i = DesignerActions.items.indexOf(item);
|
|
42
|
+
if (i < 0)
|
|
43
|
+
return;
|
|
44
|
+
DesignerActions.items.splice(i, 1);
|
|
45
|
+
DesignerActions.RaiseChanged();
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Removes the action with this path, if there is one. */
|
|
49
|
+
static Remove(path) {
|
|
50
|
+
const i = DesignerActions.items.findIndex((x) => x.Path === path);
|
|
51
|
+
if (i < 0)
|
|
52
|
+
return;
|
|
53
|
+
DesignerActions.items.splice(i, 1);
|
|
54
|
+
DesignerActions.RaiseChanged();
|
|
55
|
+
}
|
|
56
|
+
/** The declared actions' paths, in the order they were added. */
|
|
57
|
+
static get Paths() { return DesignerActions.items.map((x) => x.Path); }
|
|
58
|
+
/** Runs the action with this path; false when there is no such action. */
|
|
59
|
+
static Run(path) {
|
|
60
|
+
const item = DesignerActions.items.find((x) => x.Path === path);
|
|
61
|
+
if (!item)
|
|
62
|
+
return false;
|
|
63
|
+
item.Run();
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Declares a Designer action (DesignerActions.Add); returns the function that removes it. Usable outside React. */
|
|
68
|
+
export function designerAction(path, run) { return DesignerActions.Add(path, run); }
|
package/dist/core/Gestures.d.ts
CHANGED
|
@@ -126,6 +126,7 @@ export declare class TouchActionEventArgs {
|
|
|
126
126
|
Wheel: {
|
|
127
127
|
Delta: number;
|
|
128
128
|
IsHorizontal?: boolean;
|
|
129
|
+
IsZoom?: boolean;
|
|
129
130
|
};
|
|
130
131
|
/**
|
|
131
132
|
* AppoMobi TouchActionEventArgs.Handled: set by a control that actually used the event. The canvas prevents the
|