drawnui-react 0.1.0-preview.9 → 1.0.1
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 +229 -22
- package/SKIPPED.md +37 -28
- package/dist/controls/AnimatedFramesRenderer.d.ts +9 -0
- package/dist/controls/AnimatedFramesRenderer.js +18 -1
- package/dist/controls/DrawnGame.d.ts +44 -0
- package/dist/controls/DrawnGame.js +82 -0
- package/dist/controls/GridStructure.d.ts +25 -3
- package/dist/controls/GridStructure.js +81 -5
- package/dist/controls/SkiaBackdrop.d.ts +8 -2
- package/dist/controls/SkiaBackdrop.js +42 -14
- package/dist/controls/SkiaButton.d.ts +17 -3
- package/dist/controls/SkiaButton.js +26 -8
- package/dist/controls/SkiaCarousel.d.ts +22 -3
- package/dist/controls/SkiaCarousel.js +85 -9
- package/dist/controls/SkiaCheckbox.d.ts +0 -1
- package/dist/controls/SkiaCheckbox.js +2 -7
- package/dist/controls/SkiaDrawer.d.ts +15 -0
- package/dist/controls/SkiaDrawer.js +42 -7
- package/dist/controls/SkiaEditor.d.ts +51 -2
- package/dist/controls/SkiaEditor.js +292 -58
- package/dist/controls/SkiaGif.d.ts +7 -0
- package/dist/controls/SkiaGif.js +25 -4
- package/dist/controls/SkiaImage.d.ts +10 -0
- package/dist/controls/SkiaImage.js +80 -19
- package/dist/controls/SkiaLabel.d.ts +200 -11
- package/dist/controls/SkiaLabel.js +925 -93
- package/dist/controls/SkiaLabelFps.d.ts +12 -0
- package/dist/controls/SkiaLabelFps.js +35 -0
- package/dist/controls/SkiaLayout.d.ts +89 -9
- package/dist/controls/SkiaLayout.js +397 -23
- package/dist/controls/SkiaLottie.js +5 -3
- package/dist/controls/SkiaProgress.d.ts +2 -1
- package/dist/controls/SkiaProgress.js +8 -5
- package/dist/controls/SkiaRadioButton.d.ts +2 -0
- package/dist/controls/SkiaRadioButton.js +16 -4
- package/dist/controls/SkiaRichLabel.d.ts +1 -1
- package/dist/controls/SkiaRichLabel.js +11 -1
- package/dist/controls/SkiaScroll.d.ts +86 -7
- package/dist/controls/SkiaScroll.js +361 -40
- package/dist/controls/SkiaScrollBar.d.ts +27 -0
- package/dist/controls/SkiaScrollBar.js +51 -2
- package/dist/controls/SkiaShaderCarousel.d.ts +11 -1
- package/dist/controls/SkiaShaderCarousel.js +27 -5
- package/dist/controls/SkiaShape.d.ts +15 -2
- package/dist/controls/SkiaShape.js +57 -10
- package/dist/controls/SkiaShell.d.ts +282 -0
- package/dist/controls/SkiaShell.js +972 -0
- package/dist/controls/SkiaSlider.d.ts +20 -2
- package/dist/controls/SkiaSlider.js +61 -7
- package/dist/controls/SkiaSprite.d.ts +4 -0
- package/dist/controls/SkiaSprite.js +27 -11
- package/dist/controls/SkiaSvg.d.ts +4 -0
- package/dist/controls/SkiaSvg.js +28 -4
- package/dist/controls/SkiaSwitch.d.ts +0 -1
- package/dist/controls/SkiaSwitch.js +4 -8
- package/dist/controls/SkiaToggle.d.ts +25 -5
- package/dist/controls/SkiaToggle.js +58 -5
- package/dist/controls/SnappingLayout.d.ts +12 -1
- package/dist/controls/SnappingLayout.js +21 -2
- package/dist/controls/TooltipStyle.d.ts +52 -0
- package/dist/controls/TooltipStyle.js +88 -0
- package/dist/core/Accessibility.d.ts +162 -11
- package/dist/core/Accessibility.js +882 -22
- package/dist/core/Animators.d.ts +37 -1
- package/dist/core/Animators.js +102 -9
- package/dist/core/Canvas.d.ts +307 -8
- package/dist/core/Canvas.js +1202 -66
- package/dist/core/ControlStyle.d.ts +2 -0
- package/dist/core/ControlStyle.js +2 -0
- package/dist/core/DesignerActions.d.ts +32 -0
- package/dist/core/DesignerActions.js +68 -0
- package/dist/core/Easing.d.ts +12 -1
- package/dist/core/Easing.js +25 -1
- package/dist/core/Gestures.d.ts +86 -2
- package/dist/core/Gestures.js +184 -4
- package/dist/core/Hover.d.ts +51 -0
- package/dist/core/Hover.js +140 -0
- package/dist/core/ImageEffects.d.ts +2 -0
- package/dist/core/ImageEffects.js +2 -0
- package/dist/core/KeyboardManager.d.ts +40 -0
- package/dist/core/KeyboardManager.js +98 -8
- package/dist/core/PopupWindows.d.ts +94 -0
- package/dist/core/PopupWindows.js +207 -0
- package/dist/core/Props.d.ts +11 -0
- package/dist/core/Props.js +44 -0
- package/dist/core/ScrollAnimators.d.ts +6 -0
- package/dist/core/ScrollAnimators.js +16 -1
- package/dist/core/Shaping.d.ts +40 -0
- package/dist/core/Shaping.js +224 -0
- package/dist/core/SkiaControl.d.ts +306 -13
- package/dist/core/SkiaControl.js +681 -75
- package/dist/core/SkiaEffect.d.ts +29 -1
- package/dist/core/SkiaEffect.js +78 -16
- package/dist/core/SkiaImageManager.d.ts +101 -18
- package/dist/core/SkiaImageManager.js +318 -63
- package/dist/core/Styles.js +7 -1
- package/dist/core/Super.d.ts +33 -2
- package/dist/core/Super.js +58 -12
- package/dist/core/TextInputProxy.d.ts +31 -1
- package/dist/core/TextInputProxy.js +97 -15
- package/dist/core/Tooltips.d.ts +71 -0
- package/dist/core/Tooltips.js +205 -0
- package/dist/core/Types.d.ts +11 -1
- package/dist/core/Types.js +32 -1
- package/dist/core/ViewsAdapter.d.ts +7 -2
- package/dist/core/ViewsAdapter.js +10 -3
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/react/SkiaShell.d.ts +8 -74
- package/dist/react/SkiaShell.js +163 -475
- package/dist/react/index.d.ts +48 -4
- package/dist/react/index.js +210 -43
- package/dist/react/reconciler.d.ts +32 -0
- package/dist/react/reconciler.js +71 -40
- package/dist/vite/index.d.ts +9 -0
- package/dist/vite/index.js +36 -3
- package/package.json +103 -5
|
@@ -0,0 +1,972 @@
|
|
|
1
|
+
import { Colors, Thickness } from "../core/Types";
|
|
2
|
+
import { Super } from "../core/Super";
|
|
3
|
+
import { Easing } from "../core/Easing";
|
|
4
|
+
import { ApplyProps } from "../core/Props";
|
|
5
|
+
import { SkiaGrid, SkiaLayer } from "./SkiaLayout";
|
|
6
|
+
import { SkiaLabel } from "./SkiaLabel";
|
|
7
|
+
import { SkiaRichLabel } from "./SkiaRichLabel";
|
|
8
|
+
import { SkiaButton } from "./SkiaButton";
|
|
9
|
+
import { SkiaShape } from "./SkiaShape";
|
|
10
|
+
import { SkiaBackdrop } from "./SkiaBackdrop";
|
|
11
|
+
import { SkiaDrawer } from "./SkiaDrawer";
|
|
12
|
+
/** "name?a=1&b=2" -> name + arguments (query values are strings). */
|
|
13
|
+
export function SplitRoute(route) {
|
|
14
|
+
const q = route.indexOf("?");
|
|
15
|
+
if (q < 0)
|
|
16
|
+
return { name: route, args: {} };
|
|
17
|
+
const args = {};
|
|
18
|
+
new URLSearchParams(route.slice(q + 1)).forEach((v, k) => { args[k] = v; });
|
|
19
|
+
return { name: route.slice(0, q), args };
|
|
20
|
+
}
|
|
21
|
+
/** name + arguments -> "name?a=1" (only string / number / boolean values travel in the route, the rest stays in memory). */
|
|
22
|
+
export function BuildRoute(name, args) {
|
|
23
|
+
if (!args)
|
|
24
|
+
return name;
|
|
25
|
+
const p = new URLSearchParams();
|
|
26
|
+
for (const [k, v] of Object.entries(args))
|
|
27
|
+
if (typeof v === "string" || typeof v === "number" || typeof v === "boolean")
|
|
28
|
+
p.set(k, String(v));
|
|
29
|
+
const q = p.toString();
|
|
30
|
+
return q ? `${name}?${q}` : name;
|
|
31
|
+
}
|
|
32
|
+
/** C# SkiaShell static defaults (mutable). */
|
|
33
|
+
export const ShellDefaults = {
|
|
34
|
+
ToastBackgroundColor: "#CC000000",
|
|
35
|
+
ToastTextColor: Colors.White,
|
|
36
|
+
ToastTextFont: "",
|
|
37
|
+
ToastTextSize: 16,
|
|
38
|
+
ToastTextMargins: 24,
|
|
39
|
+
/** Default overlay tint behind popups / modals. */
|
|
40
|
+
PopupBackgroundColor: "#66000000",
|
|
41
|
+
/** Backdrop blur behind popups / modals, points. */
|
|
42
|
+
PopupsBackgroundBlur: 6,
|
|
43
|
+
PopupsAnimationSpeed: 250,
|
|
44
|
+
PopupsCancelAnimationsAfterMs: 1500,
|
|
45
|
+
ZIndexModals: 1000,
|
|
46
|
+
ZIndexPopups: 2000,
|
|
47
|
+
ZIndexToasts: 3000,
|
|
48
|
+
/** Accent of the selected tab. */
|
|
49
|
+
TabSelectedColor: "#6EA8FE",
|
|
50
|
+
TabColor: "#ADB5BD",
|
|
51
|
+
};
|
|
52
|
+
const IsContentHost = (c) => typeof c.AttachTo === "function";
|
|
53
|
+
/** C# SkiaViewSwitcher.Custom easing (back-ease, side coefficient 0.55) used for tab switches. */
|
|
54
|
+
const TabsEasing = new Easing((x) => (x - 1) * (x - 1) * ((0.55 + 1) * (x - 1) + 0.55) + 1);
|
|
55
|
+
const HISTORY_KEY = "drawnui-shell";
|
|
56
|
+
/** Canvas width in points for a control (page slide distance); falls back to the window. */
|
|
57
|
+
function CanvasWidthPts(ctrl) {
|
|
58
|
+
const el = ctrl.Superview?.Element;
|
|
59
|
+
return el ? el.clientWidth : window.innerWidth;
|
|
60
|
+
}
|
|
61
|
+
/** Resolves once pred() is truthy or after timeout ms. */
|
|
62
|
+
async function WaitFor(pred, timeout = 1000) {
|
|
63
|
+
const started = performance.now();
|
|
64
|
+
for (;;) {
|
|
65
|
+
const v = pred();
|
|
66
|
+
if (v)
|
|
67
|
+
return v;
|
|
68
|
+
if (performance.now() - started > timeout)
|
|
69
|
+
return undefined;
|
|
70
|
+
await new Promise((r) => setTimeout(r, 16));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function AnimateWithTimeout(promise, ms = ShellDefaults.PopupsCancelAnimationsAfterMs) {
|
|
74
|
+
return Promise.race([promise.catch(() => undefined), new Promise((r) => setTimeout(r, ms))]).then(() => undefined);
|
|
75
|
+
}
|
|
76
|
+
const thicknesses = new Map();
|
|
77
|
+
/** One Thickness per value, so an unchanged margin is not a change. */
|
|
78
|
+
function T(l, t, r, b) {
|
|
79
|
+
const key = `${l},${t},${r},${b}`;
|
|
80
|
+
let v = thicknesses.get(key);
|
|
81
|
+
if (!v) {
|
|
82
|
+
v = new Thickness(l, t, r, b);
|
|
83
|
+
thicknesses.set(key, v);
|
|
84
|
+
}
|
|
85
|
+
return v;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* DrawnUi SkiaShell: a root page (or tabs) plus per-tab stacks of routed pages with a nav bar (pages slide in / out
|
|
89
|
+
* like SkiaViewSwitcher), popups (blurred + dimmed backdrop, scale + fade, tap outside to close), modals (full-screen
|
|
90
|
+
* SkiaDrawer from the bottom, optionally draggable), toasts (bottom banner, slide + fade, auto close). With
|
|
91
|
+
* `UseBrowserHistory` pages live in the URL hash and the browser back button behaves like C# GoBack (popup, then
|
|
92
|
+
* modal, then page). Content is a control, a function making one, or a ShellContentHost.
|
|
93
|
+
*/
|
|
94
|
+
export class SkiaShell extends SkiaLayer {
|
|
95
|
+
constructor() {
|
|
96
|
+
super();
|
|
97
|
+
this.VerticalOptions = "Fill";
|
|
98
|
+
}
|
|
99
|
+
// ---- properties (C# names) ----
|
|
100
|
+
/** Page factories by route name; a factory receives the route's arguments. */
|
|
101
|
+
Routes = {};
|
|
102
|
+
/** Page push / pop slide duration in ms (C# SkiaViewSwitcher.PagesAnimationSpeed). */
|
|
103
|
+
PagesAnimationSpeed = 200;
|
|
104
|
+
/** Slide + fade between tabs (C# SkiaViewSwitcher.AnimateTabs). */
|
|
105
|
+
AnimateTabs = false;
|
|
106
|
+
/** Tab switch duration in ms (C# TabsAnimationSpeed). */
|
|
107
|
+
TabsAnimationSpeed = 150;
|
|
108
|
+
/** Before a page / popup / modal is pushed or popped; set e.Cancel = true to stop it (C# Navigating). */
|
|
109
|
+
Navigating;
|
|
110
|
+
/** After a page / popup / modal went upfront or was removed (C# Navigated). */
|
|
111
|
+
Navigated;
|
|
112
|
+
/** The current route changed (C# RouteChanged). */
|
|
113
|
+
RouteChanged;
|
|
114
|
+
titles;
|
|
115
|
+
/** Title shown in the nav bar per route name; defaults to the route name. */
|
|
116
|
+
get Titles() { return this.titles; }
|
|
117
|
+
set Titles(v) { this.titles = v; this.RenderChrome(); }
|
|
118
|
+
navBarHeight = 56;
|
|
119
|
+
/** Nav bar height in points when a page is open. */
|
|
120
|
+
get NavBarHeight() { return this.navBarHeight; }
|
|
121
|
+
set NavBarHeight(v) { this.navBarHeight = v; this.RenderChrome(); }
|
|
122
|
+
navBarColor = "#212529";
|
|
123
|
+
get NavBarColor() { return this.navBarColor; }
|
|
124
|
+
set NavBarColor(v) { this.navBarColor = v; this.RenderChrome(); }
|
|
125
|
+
pageBackgroundColor = "#212529";
|
|
126
|
+
/** Opaque background of pushed pages (they slide over the page below). */
|
|
127
|
+
get PageBackgroundColor() { return this.pageBackgroundColor; }
|
|
128
|
+
set PageBackgroundColor(v) { this.pageBackgroundColor = v; this.RenderChrome(); }
|
|
129
|
+
tabBarHeight = 56;
|
|
130
|
+
get TabBarHeight() { return this.tabBarHeight; }
|
|
131
|
+
set TabBarHeight(v) { this.tabBarHeight = v; this.RenderChrome(); }
|
|
132
|
+
tabBarColor = "#212529";
|
|
133
|
+
get TabBarColor() { return this.tabBarColor; }
|
|
134
|
+
set TabBarColor(v) { this.tabBarColor = v; this.RenderChrome(); }
|
|
135
|
+
insets;
|
|
136
|
+
/** Safe-area insets (points) reserved above the nav bar and below the tab bar; default Super.Insets (C# Super.Insets). */
|
|
137
|
+
get Insets() { return this.insets; }
|
|
138
|
+
set Insets(v) { this.insets = v; this.RenderChrome(); }
|
|
139
|
+
tabs;
|
|
140
|
+
/** Bottom tab bar: one root route per tab, per-tab navigation stacks (C# SkiaViewSwitcher tabs). */
|
|
141
|
+
get Tabs() { return this.tabs; }
|
|
142
|
+
set Tabs(v) {
|
|
143
|
+
const before = this.tabs?.map((t) => t.route).join("\n");
|
|
144
|
+
this.tabs = v;
|
|
145
|
+
if (this.containers.length && before !== v?.map((t) => t.route).join("\n"))
|
|
146
|
+
this.RebuildContainers();
|
|
147
|
+
else
|
|
148
|
+
this.RenderChrome();
|
|
149
|
+
}
|
|
150
|
+
useBrowserHistory = true;
|
|
151
|
+
/** Mirror pages in the URL hash (#/route/route) and honour the browser back button (React extension, default true). */
|
|
152
|
+
get UseBrowserHistory() { return this.useBrowserHistory; }
|
|
153
|
+
set UseBrowserHistory(v) {
|
|
154
|
+
if (v === this.useBrowserHistory)
|
|
155
|
+
return;
|
|
156
|
+
this.useBrowserHistory = v;
|
|
157
|
+
if (this.started) {
|
|
158
|
+
this.StopHistory();
|
|
159
|
+
this.StartHistory();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
rootContent;
|
|
163
|
+
/** The root content, shown when nothing is pushed and no Tabs are set (C# RootContent). */
|
|
164
|
+
get RootContent() { return this.rootContent; }
|
|
165
|
+
set RootContent(v) {
|
|
166
|
+
if (v === this.rootContent)
|
|
167
|
+
return;
|
|
168
|
+
const old = this.rootContent;
|
|
169
|
+
this.rootContent = v;
|
|
170
|
+
const built = this.containers.length > 0;
|
|
171
|
+
this.EnsureContainers();
|
|
172
|
+
if (built && !this.HasTabs)
|
|
173
|
+
this.SetRoot(0, old, v);
|
|
174
|
+
}
|
|
175
|
+
// ---- state ----
|
|
176
|
+
containers = [];
|
|
177
|
+
tabRoots = [];
|
|
178
|
+
stacks = [];
|
|
179
|
+
popups = [];
|
|
180
|
+
modals = [];
|
|
181
|
+
toasts = [];
|
|
182
|
+
tabBar;
|
|
183
|
+
tabCells = [];
|
|
184
|
+
selected = 0;
|
|
185
|
+
/** The page sliding out during a pop. */
|
|
186
|
+
leaving;
|
|
187
|
+
/** Pages sliding in: the page below stays visible meanwhile. */
|
|
188
|
+
transitions = 0;
|
|
189
|
+
/** The tab root leaving during an animated switch (C# PreviousVisibleView) and the slide direction. */
|
|
190
|
+
tabTransition;
|
|
191
|
+
nextId = 1;
|
|
192
|
+
lastRoute;
|
|
193
|
+
started = false;
|
|
194
|
+
unsubscribeInsets;
|
|
195
|
+
/** What each chrome control was last given, so a new declaration changes only what differs (as React's props). */
|
|
196
|
+
declared = new WeakMap();
|
|
197
|
+
/** Bumped whenever what the navigation reports changed (stack, overlays, tab). */
|
|
198
|
+
NavVersion = 0;
|
|
199
|
+
/** Called after the stack, the overlays or the selected tab changed. */
|
|
200
|
+
OnChanged() { }
|
|
201
|
+
/** The root of tab `index` (C#: built from its route). */
|
|
202
|
+
CreateTabRoot(index) {
|
|
203
|
+
const route = this.tabs?.[index]?.route ?? "";
|
|
204
|
+
const { name, args } = SplitRoute(route);
|
|
205
|
+
return this.Routes[name]?.(args);
|
|
206
|
+
}
|
|
207
|
+
// ---- read (C# names) ----
|
|
208
|
+
get HasTabs() { return !!this.tabs && this.tabs.length > 0; }
|
|
209
|
+
get Stack() { return this.stacks[this.selected] ?? []; }
|
|
210
|
+
/** Index of the selected tab. */
|
|
211
|
+
get SelectedTab() { return this.selected; }
|
|
212
|
+
/** Routes below the current page of the current tab, root excluded. */
|
|
213
|
+
get NavigationStack() { return this.Stack.map((p) => p.Route); }
|
|
214
|
+
/** Current route (with its query), "" for the root content. */
|
|
215
|
+
get Route() { const s = this.Stack; return s[s.length - 1]?.Route ?? ""; }
|
|
216
|
+
/** Arguments of the current page (query + explicit). */
|
|
217
|
+
get Arguments() { const s = this.Stack; const top = s[s.length - 1]; return { ...SplitRoute(top?.Route ?? "").args, ...top?.Memory }; }
|
|
218
|
+
get PopupsCount() { return this.popups.length; }
|
|
219
|
+
/** Open modals; a modal sliding out has left the stack. */
|
|
220
|
+
get ModalsCount() { return this.modals.filter((m) => !m.left).length; }
|
|
221
|
+
get ToastsCount() { return this.toasts.length; }
|
|
222
|
+
/** Something to go back from: a popup, a modal or a pushed page. */
|
|
223
|
+
get CanGoBack() { return this.Stack.length > 0 || this.popups.length > 0 || this.ModalsCount > 0; }
|
|
224
|
+
// ---- building ----
|
|
225
|
+
/** Builds the control and sets its values as the React renderer would (styles first, then the values). */
|
|
226
|
+
Create(ctor, props) {
|
|
227
|
+
const c = new ctor();
|
|
228
|
+
c.ApplyInitialStyles(true);
|
|
229
|
+
ApplyProps(c, null, props);
|
|
230
|
+
this.declared.set(c, props);
|
|
231
|
+
return c;
|
|
232
|
+
}
|
|
233
|
+
/** New values for a control the shell made: only the differences are applied. */
|
|
234
|
+
Declare(c, props) {
|
|
235
|
+
ApplyProps(c, this.declared.get(c) ?? null, props);
|
|
236
|
+
this.declared.set(c, props);
|
|
237
|
+
}
|
|
238
|
+
Place(content, parent) {
|
|
239
|
+
if (!content)
|
|
240
|
+
return;
|
|
241
|
+
if (IsContentHost(content))
|
|
242
|
+
content.AttachTo(parent);
|
|
243
|
+
else
|
|
244
|
+
parent.InsertChild(typeof content === "function" ? content() : content);
|
|
245
|
+
}
|
|
246
|
+
/** A root replaced: the old one leaves its container (C# SetRoot), a control is not disposed. */
|
|
247
|
+
Release(content, parent) {
|
|
248
|
+
if (!content)
|
|
249
|
+
return;
|
|
250
|
+
if (IsContentHost(content))
|
|
251
|
+
content.Detach();
|
|
252
|
+
else if (typeof content !== "function")
|
|
253
|
+
parent.RemoveSubView(content);
|
|
254
|
+
}
|
|
255
|
+
/** Before the room of `content` is disposed: a host lets go of its content, a control is disposed with the room (C#). */
|
|
256
|
+
Detach(content) {
|
|
257
|
+
if (content && IsContentHost(content))
|
|
258
|
+
content.Detach();
|
|
259
|
+
}
|
|
260
|
+
Ready(content) {
|
|
261
|
+
return content && IsContentHost(content) && content.Ready ? content.Ready() : Promise.resolve();
|
|
262
|
+
}
|
|
263
|
+
get EffectiveInsets() { return this.insets ?? Super.Insets; }
|
|
264
|
+
get RootBottom() { return this.HasTabs ? this.tabBarHeight + this.EffectiveInsets.Bottom : 0; }
|
|
265
|
+
/** A tabbed shell has no RootContent to start it: it is built at the first measure. */
|
|
266
|
+
Measure(widthConstraint, heightConstraint, scale) {
|
|
267
|
+
this.EnsureContainers();
|
|
268
|
+
return super.Measure(widthConstraint, heightConstraint, scale);
|
|
269
|
+
}
|
|
270
|
+
/** One container per tab (one without tabs) holds that tab's root; pushed pages are drawn over it. */
|
|
271
|
+
EnsureContainers() {
|
|
272
|
+
if (this.containers.length || this.IsDisposed)
|
|
273
|
+
return;
|
|
274
|
+
const count = this.HasTabs ? this.tabs.length : 1;
|
|
275
|
+
for (let i = 0; i < count; i++) {
|
|
276
|
+
const c = this.Create(SkiaLayer, this.ContainerProps(i));
|
|
277
|
+
this.containers.push(c);
|
|
278
|
+
this.tabRoots.push(undefined);
|
|
279
|
+
if (!this.stacks[i])
|
|
280
|
+
this.stacks[i] = [];
|
|
281
|
+
this.InsertChild(c, this.FirstPageHost());
|
|
282
|
+
this.SetRoot(i, undefined, this.HasTabs ? this.CreateTabRoot(i) : this.rootContent);
|
|
283
|
+
}
|
|
284
|
+
if (this.HasTabs)
|
|
285
|
+
this.BuildTabBar();
|
|
286
|
+
if (!this.started) {
|
|
287
|
+
this.started = true;
|
|
288
|
+
queueMicrotask(() => this.Start());
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** The tabs changed after the shell was built: new containers, roots and tab bar; the pushed pages stay. */
|
|
292
|
+
RebuildContainers() {
|
|
293
|
+
for (let i = 0; i < this.containers.length; i++)
|
|
294
|
+
this.RemoveContainer(i);
|
|
295
|
+
this.containers.length = 0;
|
|
296
|
+
this.tabRoots.length = 0;
|
|
297
|
+
if (this.tabBar) {
|
|
298
|
+
this.RemoveSubView(this.tabBar);
|
|
299
|
+
this.tabBar.Dispose();
|
|
300
|
+
this.tabBar = undefined;
|
|
301
|
+
this.tabCells = [];
|
|
302
|
+
}
|
|
303
|
+
const count = this.HasTabs ? this.tabs.length : 1;
|
|
304
|
+
for (let t = count; t < this.stacks.length; t++)
|
|
305
|
+
for (const p of this.stacks[t])
|
|
306
|
+
this.RemovePageHost(p);
|
|
307
|
+
this.stacks.length = Math.min(this.stacks.length, count);
|
|
308
|
+
this.selected = Math.min(this.selected, count - 1);
|
|
309
|
+
this.EnsureContainers();
|
|
310
|
+
this.RenderChrome();
|
|
311
|
+
this.Notify();
|
|
312
|
+
}
|
|
313
|
+
RemoveContainer(i) {
|
|
314
|
+
const c = this.containers[i];
|
|
315
|
+
this.Detach(this.tabRoots[i]);
|
|
316
|
+
this.RemoveSubView(c);
|
|
317
|
+
c.Dispose();
|
|
318
|
+
}
|
|
319
|
+
SetRoot(tab, old, root) {
|
|
320
|
+
const c = this.containers[tab];
|
|
321
|
+
this.Release(old, c);
|
|
322
|
+
this.tabRoots[tab] = root;
|
|
323
|
+
this.Place(root, c);
|
|
324
|
+
}
|
|
325
|
+
ContainerProps(t) {
|
|
326
|
+
const selected = t === this.selected;
|
|
327
|
+
const stack = this.Stack;
|
|
328
|
+
const transitioning = this.transitions > 0 || this.leaving !== undefined;
|
|
329
|
+
const entering = this.tabTransition && selected ? { TranslationX: this.tabTransition.dir * 0.75 * (typeof window !== "undefined" ? window.innerWidth : 0), Opacity: 0.001, ZIndex: 1 } : {};
|
|
330
|
+
return {
|
|
331
|
+
VerticalOptions: "Fill", Margin: T(0, 0, 0, this.RootBottom), BackgroundColor: this.HasTabs ? this.pageBackgroundColor : undefined,
|
|
332
|
+
IsVisible: selected ? stack.length === 0 || transitioning : this.tabTransition?.from === t, ZIndex: 0, ...entering,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/** The first page host in the shell's children: tab containers go before every page. */
|
|
336
|
+
FirstPageHost(tabAfter = -1) {
|
|
337
|
+
for (let t = tabAfter + 1; t < this.stacks.length; t++)
|
|
338
|
+
if (this.stacks[t]?.length)
|
|
339
|
+
return this.stacks[t][0].Host;
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
BuildTabBar() {
|
|
343
|
+
const insets = this.EffectiveInsets;
|
|
344
|
+
const bar = this.Create(SkiaLayer, this.TabBarProps(insets));
|
|
345
|
+
bar.InsertChild(this.Create(SkiaLayer, { HeightRequest: 1, BackgroundColor: "#343A40" }));
|
|
346
|
+
const grid = this.Create(SkiaGrid, { ColumnDefinitions: this.tabs.map(() => "*").join(","), HorizontalOptions: "Fill", VerticalOptions: "Fill" });
|
|
347
|
+
this.tabCells = this.tabs.map((t, i) => {
|
|
348
|
+
const cell = this.Create(SkiaLayer, { Column: i, HorizontalOptions: "Fill", VerticalOptions: "Fill", AnimationTapped: "Ripple", Tapped: () => void this.SelectTabAsync(i), AccessibilityRole: "button", AccessibilityLabel: t.title });
|
|
349
|
+
const label = this.Create(SkiaLabel, this.TabLabelProps(t, i));
|
|
350
|
+
cell.InsertChild(label);
|
|
351
|
+
grid.InsertChild(cell);
|
|
352
|
+
return { cell, label };
|
|
353
|
+
});
|
|
354
|
+
bar.InsertChild(grid);
|
|
355
|
+
this.tabBar = bar;
|
|
356
|
+
this.InsertChild(bar);
|
|
357
|
+
this.RenderTabBar();
|
|
358
|
+
}
|
|
359
|
+
TabBarProps(insets) {
|
|
360
|
+
return { HeightRequest: this.tabBarHeight + insets.Bottom, VerticalOptions: "End", BackgroundColor: this.tabBarColor, ZIndex: ShellDefaults.ZIndexModals - 1, BlockGesturesBelow: true, Padding: T(0, 0, 0, insets.Bottom) };
|
|
361
|
+
}
|
|
362
|
+
TabLabelProps(t, i) {
|
|
363
|
+
const selected = i === this.selected;
|
|
364
|
+
return { Text: t.title, FontSize: 13, FontFamily: selected ? "FontTextBold" : undefined, TextColor: selected ? ShellDefaults.TabSelectedColor : ShellDefaults.TabColor, HorizontalOptions: "Center", VerticalOptions: "Center", AccessibilityRole: "presentation" };
|
|
365
|
+
}
|
|
366
|
+
RenderTabBar() {
|
|
367
|
+
if (!this.tabBar || !this.tabs)
|
|
368
|
+
return;
|
|
369
|
+
this.Declare(this.tabBar, this.TabBarProps(this.EffectiveInsets));
|
|
370
|
+
this.tabCells.forEach((x, i) => {
|
|
371
|
+
const t = this.tabs[i];
|
|
372
|
+
if (!t)
|
|
373
|
+
return;
|
|
374
|
+
this.Declare(x.cell, { Column: i, HorizontalOptions: "Fill", VerticalOptions: "Fill", AnimationTapped: "Ripple", Tapped: x.cell.Tapped, AccessibilityRole: "button", AccessibilityLabel: t.title });
|
|
375
|
+
this.Declare(x.label, this.TabLabelProps(t, i));
|
|
376
|
+
// the selected tab's marker: made when its tab is selected, dropped when it is not
|
|
377
|
+
if (i === this.selected && !x.marker) {
|
|
378
|
+
x.marker = this.Create(SkiaShape, { Type: "Rectangle", HeightRequest: 3, WidthRequest: 36, CornerRadius: 2, BackgroundColor: ShellDefaults.TabSelectedColor, HorizontalOptions: "Center", VerticalOptions: "End" });
|
|
379
|
+
x.cell.InsertChild(x.marker);
|
|
380
|
+
}
|
|
381
|
+
else if (i !== this.selected && x.marker) {
|
|
382
|
+
x.cell.RemoveSubView(x.marker);
|
|
383
|
+
x.marker.Dispose();
|
|
384
|
+
x.marker = undefined;
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
/** Applies the state to the chrome: containers, page hosts and their bars, tab bar. */
|
|
389
|
+
RenderChrome() {
|
|
390
|
+
if (!this.containers.length || this.IsDisposed)
|
|
391
|
+
return;
|
|
392
|
+
const insets = this.EffectiveInsets;
|
|
393
|
+
const navTop = this.navBarHeight + insets.Top;
|
|
394
|
+
const bottom = this.RootBottom;
|
|
395
|
+
const transitioning = this.transitions > 0 || this.leaving !== undefined;
|
|
396
|
+
const stack = this.Stack;
|
|
397
|
+
const topRoute = stack[stack.length - 1]?.Route ?? "";
|
|
398
|
+
this.containers.forEach((c, t) => this.Declare(c, this.ContainerProps(t)));
|
|
399
|
+
this.stacks.forEach((s, t) => {
|
|
400
|
+
for (const p of s) {
|
|
401
|
+
const visible = t === this.selected && (p.Route === topRoute || transitioning);
|
|
402
|
+
this.Declare(p.Host, { VerticalOptions: "Fill", HorizontalOptions: "Fill", Margin: T(0, 0, 0, bottom), BackgroundColor: this.pageBackgroundColor, IsVisible: visible, BlockGesturesBelow: true });
|
|
403
|
+
this.Declare(p.Body, { VerticalOptions: "Fill", Margin: T(0, navTop, 0, 0) });
|
|
404
|
+
this.Declare(p.Bar, { HeightRequest: navTop, BackgroundColor: this.navBarColor, Padding: T(0, insets.Top, 0, 0) });
|
|
405
|
+
this.Declare(p.Title, this.TitleProps(p.Route));
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
this.RenderTabBar();
|
|
409
|
+
}
|
|
410
|
+
TitleProps(route) {
|
|
411
|
+
const name = SplitRoute(route).name;
|
|
412
|
+
return { Text: this.titles?.[name] ?? name, FontSize: 18, FontFamily: "FontTextBold", TextColor: Colors.White, HorizontalOptions: "Fill", HorizontalTextAlignment: "Center", VerticalOptions: "Center", MaxLines: 1, Margin: T(96, 0, 96, 0), AccessibilityRole: "heading" };
|
|
413
|
+
}
|
|
414
|
+
// ---- notifications ----
|
|
415
|
+
Notify() { this.NavVersion++; this.OnChanged(); }
|
|
416
|
+
/** C# NotifyAndCheckCanNavigate: raises Navigating, false when cancelled. */
|
|
417
|
+
CanNavigate(route, source, view, previous) {
|
|
418
|
+
const e = { Route: route ?? "", Source: source, Cancel: false, View: view, Previous: previous };
|
|
419
|
+
this.Navigating?.(e);
|
|
420
|
+
return !e.Cancel;
|
|
421
|
+
}
|
|
422
|
+
NotifyNavigated(route, source, view) { this.Navigated?.({ Route: route ?? "", Source: source, View: view }); }
|
|
423
|
+
CheckRoute() {
|
|
424
|
+
if (!this.started)
|
|
425
|
+
return;
|
|
426
|
+
const route = this.Route;
|
|
427
|
+
if (route === this.lastRoute)
|
|
428
|
+
return;
|
|
429
|
+
this.lastRoute = route;
|
|
430
|
+
this.RouteChanged?.(route);
|
|
431
|
+
}
|
|
432
|
+
/** After the first build: the first RouteChanged, the insets, the browser history (deep link). */
|
|
433
|
+
Start() {
|
|
434
|
+
if (this.IsDisposed)
|
|
435
|
+
return;
|
|
436
|
+
this.unsubscribeInsets = Super.OnInsetsChanged(() => { if (!this.insets)
|
|
437
|
+
this.RenderChrome(); });
|
|
438
|
+
this.CheckRoute();
|
|
439
|
+
this.StartHistory();
|
|
440
|
+
}
|
|
441
|
+
OnDisposing() {
|
|
442
|
+
this.StopHistory();
|
|
443
|
+
this.unsubscribeInsets?.();
|
|
444
|
+
// content hosts let go of their content; the controls themselves are disposed with the shell's children
|
|
445
|
+
for (const c of this.tabRoots)
|
|
446
|
+
if (c && IsContentHost(c))
|
|
447
|
+
c.Detach();
|
|
448
|
+
for (const s of this.stacks)
|
|
449
|
+
for (const p of s)
|
|
450
|
+
if (p.Content && IsContentHost(p.Content))
|
|
451
|
+
p.Content.Detach();
|
|
452
|
+
for (const o of [...this.popups, ...this.modals, ...this.toasts])
|
|
453
|
+
if (o.body && IsContentHost(o.body))
|
|
454
|
+
o.body.Detach();
|
|
455
|
+
super.OnDisposing();
|
|
456
|
+
}
|
|
457
|
+
// ---- browser history: one entry per pushed page / popup / modal, popped in C# GoBack order ----
|
|
458
|
+
history = [];
|
|
459
|
+
onPop;
|
|
460
|
+
get HistoryOn() { return this.useBrowserHistory && typeof window !== "undefined"; }
|
|
461
|
+
HashFor(routes) {
|
|
462
|
+
return routes.length ? "#/" + routes.map(encodeURIComponent).join("/") : window.location.pathname + window.location.search;
|
|
463
|
+
}
|
|
464
|
+
PushHistory(kind, routes) {
|
|
465
|
+
if (!this.HistoryOn)
|
|
466
|
+
return;
|
|
467
|
+
this.history.push(kind);
|
|
468
|
+
window.history.pushState({ [HISTORY_KEY]: this.history.length }, "", kind === "page" ? this.HashFor(routes) : undefined);
|
|
469
|
+
}
|
|
470
|
+
/** A programmatic close of something that has a history entry goes through history.back(), so the URL stays in sync. */
|
|
471
|
+
BackThroughHistory(kind) {
|
|
472
|
+
if (!this.HistoryOn || this.history[this.history.length - 1] !== kind)
|
|
473
|
+
return false;
|
|
474
|
+
window.history.back();
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
ParseHash() {
|
|
478
|
+
return window.location.hash.replace(/^#\/?/, "").split("/").filter(Boolean).map(decodeURIComponent).filter((r) => !!this.Routes[SplitRoute(r).name]);
|
|
479
|
+
}
|
|
480
|
+
StopHistory() {
|
|
481
|
+
if (this.onPop)
|
|
482
|
+
window.removeEventListener("popstate", this.onPop);
|
|
483
|
+
this.onPop = undefined;
|
|
484
|
+
}
|
|
485
|
+
StartHistory() {
|
|
486
|
+
if (!this.HistoryOn || this.onPop)
|
|
487
|
+
return;
|
|
488
|
+
// initial deep link
|
|
489
|
+
const initial = this.ParseHash();
|
|
490
|
+
if (initial.length) {
|
|
491
|
+
this.ReplaceStack(this.selected, initial);
|
|
492
|
+
this.history = initial.map(() => "page");
|
|
493
|
+
window.history.replaceState({ [HISTORY_KEY]: this.history.length }, "", this.HashFor(initial));
|
|
494
|
+
}
|
|
495
|
+
// popstate: the browser popped OUR last entry (back), or moved forward to a hash we can rebuild
|
|
496
|
+
this.onPop = () => {
|
|
497
|
+
const depth = window.history.state?.[HISTORY_KEY] ?? 0;
|
|
498
|
+
const target = this.ParseHash();
|
|
499
|
+
// a Back lands on one of OUR entries: its hash is the stack it was pushed with (the first N pages, N = the page
|
|
500
|
+
// entries below it). A typed hash or a plain <a href="#/route"> makes a new entry with no state (depth 0):
|
|
501
|
+
// that is a navigation to the hash, not a Back to the root
|
|
502
|
+
const pagesBelow = this.history.slice(0, depth).filter((k) => k === "page").length;
|
|
503
|
+
const back = depth < this.history.length && target.join("/") === this.NavigationStack.slice(0, pagesBelow).join("/");
|
|
504
|
+
if (back) {
|
|
505
|
+
// back: unwind our entries down to depth
|
|
506
|
+
void (async () => {
|
|
507
|
+
while (this.history.length > depth) {
|
|
508
|
+
const kind = this.history.pop();
|
|
509
|
+
let done = true;
|
|
510
|
+
if (kind === "popup") {
|
|
511
|
+
const top = this.popups[this.popups.length - 1];
|
|
512
|
+
if (top) {
|
|
513
|
+
top.history = false;
|
|
514
|
+
done = await this.ClosePopupNow(top, true);
|
|
515
|
+
if (!done)
|
|
516
|
+
top.history = true;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
else if (kind === "modal") {
|
|
520
|
+
const top = this.modals[this.modals.length - 1];
|
|
521
|
+
if (top) {
|
|
522
|
+
top.history = false;
|
|
523
|
+
done = await this.PopModalNow(top, true);
|
|
524
|
+
if (!done)
|
|
525
|
+
top.history = true;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
else if (kind === "page")
|
|
529
|
+
done = await this.PopPage(true);
|
|
530
|
+
if (!done && kind) { // Navigating cancelled: put the browser entry back
|
|
531
|
+
this.history.push(kind);
|
|
532
|
+
window.history.pushState({ [HISTORY_KEY]: this.history.length }, "", kind === "page" ? this.HashFor(this.NavigationStack) : undefined);
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
})();
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
// forward (depth grew), or another hash = a plain <a href="#/route"> link or a typed hash:
|
|
540
|
+
// the hash names the pages, rebuild the stack without animation (overlays cannot be restored)
|
|
541
|
+
if (depth === this.history.length && target.join("/") === this.NavigationStack.join("/"))
|
|
542
|
+
return;
|
|
543
|
+
this.history = target.map(() => "page");
|
|
544
|
+
window.history.replaceState({ [HISTORY_KEY]: this.history.length }, "", this.HashFor(target));
|
|
545
|
+
this.ReplaceStack(this.selected, target);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
window.addEventListener("popstate", this.onPop);
|
|
549
|
+
}
|
|
550
|
+
// ---- pages ----
|
|
551
|
+
/** Builds a page host (content under its nav bar) for `route` in tab `tab` and adds it; it slides in from the right. */
|
|
552
|
+
CreatePage(route, memory, tab) {
|
|
553
|
+
const insets = this.EffectiveInsets;
|
|
554
|
+
const navTop = this.navBarHeight + insets.Top;
|
|
555
|
+
const host = this.Create(SkiaLayer, { VerticalOptions: "Fill", HorizontalOptions: "Fill", Margin: T(0, 0, 0, this.RootBottom), BackgroundColor: this.pageBackgroundColor, IsVisible: true, BlockGesturesBelow: true });
|
|
556
|
+
const body = this.Create(SkiaLayer, { VerticalOptions: "Fill", Margin: T(0, navTop, 0, 0) });
|
|
557
|
+
const bar = this.Create(SkiaLayer, { HeightRequest: navTop, BackgroundColor: this.navBarColor, Padding: T(0, insets.Top, 0, 0) });
|
|
558
|
+
bar.InsertChild(this.Create(SkiaButton, { Text: "‹ Back", BackgroundColor: "#00000000", TextColor: "#6EA8FE", FontSize: 16, VerticalOptions: "Center", Margin: T(8, 0, 8, 0), ApplyEffect: "Ripple", Tapped: () => void this.GoBackAsync(), AccessibilityRole: "button", AccessibilityLabel: "Back" }));
|
|
559
|
+
const title = this.Create(SkiaLabel, this.TitleProps(route));
|
|
560
|
+
bar.InsertChild(title);
|
|
561
|
+
bar.InsertChild(this.Create(SkiaButton, { Text: "Home", BackgroundColor: "#00000000", TextColor: "#6EA8FE", FontSize: 16, HorizontalOptions: "End", VerticalOptions: "Center", Margin: T(0, 0, 8, 0), ApplyEffect: "Ripple", Tapped: () => void this.PopToRootAsync(), AccessibilityRole: "button", AccessibilityLabel: "Home" }));
|
|
562
|
+
bar.InsertChild(this.Create(SkiaLayer, { HeightRequest: 1, VerticalOptions: "End", BackgroundColor: "#343A40" }));
|
|
563
|
+
const { name, args } = SplitRoute(route);
|
|
564
|
+
const factory = this.Routes[name];
|
|
565
|
+
const content = factory ? factory({ ...args, ...memory }) : undefined;
|
|
566
|
+
this.Place(content, body);
|
|
567
|
+
host.InsertChild(body);
|
|
568
|
+
host.InsertChild(bar);
|
|
569
|
+
const entry = { Route: route, Memory: memory, Host: host, Body: body, Bar: bar, Title: title, Content: content };
|
|
570
|
+
this.InsertChild(host, this.FirstPageHost(tab) ?? this.tabBar ?? this.FirstOverlay());
|
|
571
|
+
// slides in once, when added (C# SkiaViewSwitcher PushView), whether the push was animated or not
|
|
572
|
+
const speed = this.PagesAnimationSpeed;
|
|
573
|
+
if (speed > 0) {
|
|
574
|
+
host.TranslationX = CanvasWidthPts(host);
|
|
575
|
+
void host.TranslateToAsync(0, 0, speed);
|
|
576
|
+
}
|
|
577
|
+
return entry;
|
|
578
|
+
}
|
|
579
|
+
FirstOverlay() {
|
|
580
|
+
return this.modals[0]?.ctrl ?? this.popups[0]?.ctrl ?? this.toasts[0]?.ctrl;
|
|
581
|
+
}
|
|
582
|
+
RemovePageHost(p) {
|
|
583
|
+
if (p.Host.IsDisposed)
|
|
584
|
+
return;
|
|
585
|
+
this.RemoveSubView(p.Host);
|
|
586
|
+
this.Detach(p.Content);
|
|
587
|
+
p.Host.Dispose();
|
|
588
|
+
}
|
|
589
|
+
/** Tab `tab` shows `routes` (a deep link, a typed hash): pages already there at their index stay, the rest are made. */
|
|
590
|
+
ReplaceStack(tab, routes) {
|
|
591
|
+
const old = this.stacks[tab] ?? [];
|
|
592
|
+
const next = [];
|
|
593
|
+
old.forEach((p, i) => { if (routes[i] !== p.Route)
|
|
594
|
+
this.RemovePageHost(p); });
|
|
595
|
+
this.stacks[tab] = next;
|
|
596
|
+
routes.forEach((r, i) => { next.push(old[i] && old[i].Route === r ? old[i] : this.CreatePage(r, undefined, tab)); });
|
|
597
|
+
if (this.leaving && !next.includes(this.leaving))
|
|
598
|
+
this.leaving = undefined;
|
|
599
|
+
this.RenderChrome();
|
|
600
|
+
this.Notify();
|
|
601
|
+
this.CheckRoute();
|
|
602
|
+
}
|
|
603
|
+
async SlideOut(current, animated) {
|
|
604
|
+
if (!animated)
|
|
605
|
+
return;
|
|
606
|
+
const speed = this.PagesAnimationSpeed;
|
|
607
|
+
this.leaving = current;
|
|
608
|
+
this.RenderChrome();
|
|
609
|
+
await AnimateWithTimeout(current.Host.TranslateToAsync(CanvasWidthPts(current.Host), 0, speed), speed + 500);
|
|
610
|
+
this.leaving = undefined;
|
|
611
|
+
}
|
|
612
|
+
/** Pops the top page; false when Navigating cancelled it (C# NavigationSource.Pop). */
|
|
613
|
+
async PopPage(animated) {
|
|
614
|
+
const s = this.Stack;
|
|
615
|
+
const current = s[s.length - 1];
|
|
616
|
+
if (!current)
|
|
617
|
+
return false;
|
|
618
|
+
const below = s[s.length - 2];
|
|
619
|
+
const belowRoute = below?.Route ?? "";
|
|
620
|
+
if (!this.CanNavigate(belowRoute, "Pop", current.Host, current.Host))
|
|
621
|
+
return false;
|
|
622
|
+
await this.SlideOut(current, animated);
|
|
623
|
+
for (const stack of this.stacks) {
|
|
624
|
+
const i = stack.indexOf(current);
|
|
625
|
+
if (i >= 0)
|
|
626
|
+
stack.splice(i, 1);
|
|
627
|
+
}
|
|
628
|
+
this.RemovePageHost(current);
|
|
629
|
+
this.RenderChrome();
|
|
630
|
+
this.Notify();
|
|
631
|
+
this.NotifyNavigated(belowRoute, "Pop", below && !below.Host.IsDisposed ? below.Host : undefined);
|
|
632
|
+
this.CheckRoute();
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
/** Pushes a registered route; `route` may carry a query ("detail?id=42") and/or explicit arguments (C# GoToAsync(route, animate, arguments)). */
|
|
636
|
+
async GoToAsync(route, animated = true, args) {
|
|
637
|
+
const { name, args: fromRoute } = SplitRoute(route);
|
|
638
|
+
if (!this.Routes[name])
|
|
639
|
+
throw new Error(`DrawnUi: route '${name}' is not registered in SkiaShell.Routes`);
|
|
640
|
+
this.EnsureContainers();
|
|
641
|
+
const full = BuildRoute(name, { ...fromRoute, ...args });
|
|
642
|
+
const s = this.Stack;
|
|
643
|
+
if (!this.CanNavigate(full, "Push", undefined, s[s.length - 1]?.Host))
|
|
644
|
+
return;
|
|
645
|
+
const entry = this.CreatePage(full, args ? { ...fromRoute, ...args } : undefined, this.selected);
|
|
646
|
+
s.push(entry);
|
|
647
|
+
this.PushHistory("page", this.NavigationStack);
|
|
648
|
+
if (animated)
|
|
649
|
+
this.transitions++;
|
|
650
|
+
this.RenderChrome();
|
|
651
|
+
this.Notify();
|
|
652
|
+
this.CheckRoute();
|
|
653
|
+
if (animated) {
|
|
654
|
+
await new Promise((res) => setTimeout(res, this.PagesAnimationSpeed + 50));
|
|
655
|
+
this.transitions--;
|
|
656
|
+
this.RenderChrome();
|
|
657
|
+
}
|
|
658
|
+
await this.Ready(entry.Content);
|
|
659
|
+
this.NotifyNavigated(full, "Push", entry.Host.IsDisposed ? undefined : entry.Host);
|
|
660
|
+
}
|
|
661
|
+
/** C# GoBack: closes the top popup, else the top modal, else pops the page. */
|
|
662
|
+
async GoBackAsync(animated = true) {
|
|
663
|
+
if (this.popups.length) {
|
|
664
|
+
await this.ClosePopup(this.popups[this.popups.length - 1], animated);
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
if (this.modals.some((m) => !m.left)) {
|
|
668
|
+
await this.PopModalAsync(animated);
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
const count = this.Stack.length;
|
|
672
|
+
if (!count)
|
|
673
|
+
return;
|
|
674
|
+
if (this.BackThroughHistory("page")) {
|
|
675
|
+
await WaitFor(() => this.Stack.length < count, this.PagesAnimationSpeed + 800);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
await this.PopPage(animated);
|
|
679
|
+
}
|
|
680
|
+
/** Pops every page of every tab at once. */
|
|
681
|
+
async PopToRootAsync() {
|
|
682
|
+
this.leaving = undefined;
|
|
683
|
+
if (this.HistoryOn) {
|
|
684
|
+
const pages = this.history.filter((k) => k === "page").length;
|
|
685
|
+
this.history = this.history.filter((k) => k !== "page");
|
|
686
|
+
if (pages)
|
|
687
|
+
window.history.replaceState({ [HISTORY_KEY]: this.history.length }, "", this.HashFor([]));
|
|
688
|
+
}
|
|
689
|
+
for (let t = 0; t < this.stacks.length; t++) {
|
|
690
|
+
for (const p of this.stacks[t])
|
|
691
|
+
this.RemovePageHost(p);
|
|
692
|
+
this.stacks[t] = [];
|
|
693
|
+
}
|
|
694
|
+
this.RenderChrome();
|
|
695
|
+
this.Notify();
|
|
696
|
+
this.CheckRoute();
|
|
697
|
+
}
|
|
698
|
+
/** Pops every page of the current tab (C# PopTabToRoot). */
|
|
699
|
+
async PopTabToRootAsync() {
|
|
700
|
+
this.leaving = undefined;
|
|
701
|
+
for (const p of this.Stack)
|
|
702
|
+
this.RemovePageHost(p);
|
|
703
|
+
this.stacks[this.selected] = [];
|
|
704
|
+
this.RenderChrome();
|
|
705
|
+
this.Notify();
|
|
706
|
+
this.CheckRoute();
|
|
707
|
+
}
|
|
708
|
+
/** Tabs (C# SkiaViewSwitcher.SelectedIndex): each tab keeps its own page stack. */
|
|
709
|
+
async SelectTabAsync(index) {
|
|
710
|
+
if (!this.HasTabs)
|
|
711
|
+
return;
|
|
712
|
+
this.EnsureContainers();
|
|
713
|
+
const to = Math.max(0, Math.min(this.tabs.length - 1, index));
|
|
714
|
+
const from = this.selected;
|
|
715
|
+
this.leaving = undefined;
|
|
716
|
+
if (to === from) {
|
|
717
|
+
this.RenderChrome();
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
if (!this.AnimateTabs) {
|
|
721
|
+
this.selected = to;
|
|
722
|
+
this.RenderChrome();
|
|
723
|
+
this.Notify();
|
|
724
|
+
this.CheckRoute();
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
// C# SelectRightTab / SelectLeftTab: the new root slides in from 0.75 width with a fade, the old one slides out.
|
|
728
|
+
// The animations take their start values before the new root is placed off to the side, so the new root shows at
|
|
729
|
+
// once over the old one sliding out under it (the shell has always done this, kept as is)
|
|
730
|
+
const dir = to > from ? 1 : -1;
|
|
731
|
+
const next = this.containers[to], prev = this.containers[from];
|
|
732
|
+
const speed = this.TabsAnimationSpeed;
|
|
733
|
+
const w = CanvasWidthPts(next);
|
|
734
|
+
const anim = Promise.all([
|
|
735
|
+
next.TranslateToAsync(0, 0, speed, TabsEasing),
|
|
736
|
+
next.FadeToAsync(1, speed, Easing.Linear),
|
|
737
|
+
prev.TranslateToAsync(-dir * w, 0, speed, TabsEasing),
|
|
738
|
+
]);
|
|
739
|
+
this.tabTransition = { from, dir };
|
|
740
|
+
this.selected = to;
|
|
741
|
+
this.RenderChrome();
|
|
742
|
+
this.Notify();
|
|
743
|
+
this.CheckRoute();
|
|
744
|
+
await AnimateWithTimeout(anim, speed + 500);
|
|
745
|
+
// the tab left stays (its state kept): hidden first, then back in place for its next turn
|
|
746
|
+
prev.IsVisible = false;
|
|
747
|
+
prev.TranslationX = 0;
|
|
748
|
+
prev.TranslationY = 0;
|
|
749
|
+
this.tabTransition = undefined;
|
|
750
|
+
this.RenderChrome();
|
|
751
|
+
}
|
|
752
|
+
// ---- popups ----
|
|
753
|
+
async ClosePopupNow(p, animated) {
|
|
754
|
+
if (p.closing)
|
|
755
|
+
return false;
|
|
756
|
+
if (!this.CanNavigate(this.Route, "Pop", p.content, p.content))
|
|
757
|
+
return false;
|
|
758
|
+
p.closing = true;
|
|
759
|
+
if (animated && p.animated !== false && p.content) {
|
|
760
|
+
await AnimateWithTimeout(Promise.all([p.ctrl.FadeToAsync(0, ShellDefaults.PopupsAnimationSpeed), p.content.ScaleToAsync(0, 0, ShellDefaults.PopupsAnimationSpeed)]));
|
|
761
|
+
}
|
|
762
|
+
this.RemoveOverlay(this.popups, p);
|
|
763
|
+
this.NotifyNavigated(this.Route, "Pop");
|
|
764
|
+
return true;
|
|
765
|
+
}
|
|
766
|
+
async ClosePopup(p, animated) {
|
|
767
|
+
if (p.closing)
|
|
768
|
+
return;
|
|
769
|
+
if (p.history && this.BackThroughHistory("popup")) {
|
|
770
|
+
p.history = false;
|
|
771
|
+
return;
|
|
772
|
+
} // popstate closes it
|
|
773
|
+
await this.ClosePopupNow(p, animated);
|
|
774
|
+
}
|
|
775
|
+
RemoveOverlay(list, o) {
|
|
776
|
+
const i = list.indexOf(o);
|
|
777
|
+
if (i >= 0)
|
|
778
|
+
list.splice(i, 1);
|
|
779
|
+
if (!o.ctrl.IsDisposed) {
|
|
780
|
+
this.RemoveSubView(o.ctrl);
|
|
781
|
+
this.Detach(o.body);
|
|
782
|
+
o.ctrl.Dispose();
|
|
783
|
+
}
|
|
784
|
+
this.Notify();
|
|
785
|
+
}
|
|
786
|
+
/** Shows `content` centered over the page (C# OpenPopupAsync): scales in, a tap outside closes it. */
|
|
787
|
+
async OpenPopupAsync(content, options) {
|
|
788
|
+
const animated = options?.animated ?? true;
|
|
789
|
+
if (!this.CanNavigate(this.Route, "Push"))
|
|
790
|
+
return;
|
|
791
|
+
const id = this.nextId++;
|
|
792
|
+
const closeWhenBackgroundTapped = options?.closeWhenBackgroundTapped ?? true;
|
|
793
|
+
const showOverlay = options?.showOverlay ?? true;
|
|
794
|
+
const overlay = options?.backgroundColor ?? ShellDefaults.PopupBackgroundColor;
|
|
795
|
+
const entry = { id, animated, body: content };
|
|
796
|
+
// a dialog for screen readers and the keyboard while open (drawnui-cross 6av, C# 2be842b8): its outside-tap close
|
|
797
|
+
// stays, but the wrapper is no Tab stop; the backdrop is no node
|
|
798
|
+
entry.ctrl = this.Create(SkiaLayer, {
|
|
799
|
+
VerticalOptions: "Fill", HorizontalOptions: "Fill", ZIndex: ShellDefaults.ZIndexPopups + id, BlockGesturesBelow: true, Opacity: animated ? 0.1 : 1,
|
|
800
|
+
AccessibilityModal: true, AccessibilityCanInteract: false,
|
|
801
|
+
Tapped: (_, e) => {
|
|
802
|
+
// C# PopupWrapper: a tap outside the content closes it
|
|
803
|
+
const c = entry.content;
|
|
804
|
+
const l = e.ProcessingInfo.MappedLocation, o = e.ProcessingInfo.ChildOffset;
|
|
805
|
+
if (closeWhenBackgroundTapped && c && !c.HitIsInside(l.X + o.X, l.Y + o.Y))
|
|
806
|
+
void this.ClosePopup(entry, animated);
|
|
807
|
+
},
|
|
808
|
+
});
|
|
809
|
+
if (showOverlay)
|
|
810
|
+
entry.ctrl.InsertChild(this.Create(SkiaBackdrop, { Blur: ShellDefaults.PopupsBackgroundBlur, BackgroundColor: overlay, InputTransparent: true }));
|
|
811
|
+
entry.content = this.Create(SkiaLayer, { HorizontalOptions: "Center", VerticalOptions: "Center", Scale: animated ? 0.5 : 1 });
|
|
812
|
+
this.Place(content, entry.content);
|
|
813
|
+
entry.ctrl.InsertChild(entry.content);
|
|
814
|
+
this.popups.push(entry);
|
|
815
|
+
this.InsertChild(entry.ctrl);
|
|
816
|
+
this.Notify();
|
|
817
|
+
entry.history = true;
|
|
818
|
+
this.PushHistory("popup", this.NavigationStack);
|
|
819
|
+
await this.Ready(content);
|
|
820
|
+
if (animated && !entry.ctrl.IsDisposed)
|
|
821
|
+
await AnimateWithTimeout(Promise.all([entry.ctrl.FadeToAsync(1, ShellDefaults.PopupsAnimationSpeed), entry.content.ScaleToAsync(1, 1, ShellDefaults.PopupsAnimationSpeed)]));
|
|
822
|
+
this.NotifyNavigated(this.Route, "Push", entry.content);
|
|
823
|
+
}
|
|
824
|
+
/** Closes the top popup. */
|
|
825
|
+
async ClosePopupAsync(animated = true) {
|
|
826
|
+
const top = this.popups[this.popups.length - 1];
|
|
827
|
+
if (top)
|
|
828
|
+
await this.ClosePopup(top, animated);
|
|
829
|
+
}
|
|
830
|
+
/** Closes every popup, without animation. */
|
|
831
|
+
async CloseAllPopups() { for (const p of [...this.popups].reverse())
|
|
832
|
+
await this.ClosePopup(p, false); }
|
|
833
|
+
// ---- modals ----
|
|
834
|
+
/**
|
|
835
|
+
* A modal leaves the modal stack the moment its close starts, whatever started it (PopModalAsync, a swipe, IsOpen =
|
|
836
|
+
* false): ModalsCount and CanGoBack drop it and Navigated Pop is raised; its drawer takes no more pans. It stays on
|
|
837
|
+
* screen until its slide ends (drawnui-cross 6s, C# SkiaShell.LeaveModalStack).
|
|
838
|
+
*/
|
|
839
|
+
LeaveModalStack(m) {
|
|
840
|
+
if (m.left)
|
|
841
|
+
return;
|
|
842
|
+
m.left = true;
|
|
843
|
+
if (m.drawer)
|
|
844
|
+
m.drawer.RespondsToGestures = false;
|
|
845
|
+
this.Notify();
|
|
846
|
+
this.NotifyNavigated(this.Route, "Pop");
|
|
847
|
+
}
|
|
848
|
+
async PopModalNow(top, animated) {
|
|
849
|
+
if (top.closing)
|
|
850
|
+
return false;
|
|
851
|
+
if (!this.CanNavigate(this.Route, "Pop", top.drawer, top.drawer))
|
|
852
|
+
return false;
|
|
853
|
+
top.closing = true;
|
|
854
|
+
this.LeaveModalStack(top);
|
|
855
|
+
const drawer = top.drawer;
|
|
856
|
+
if (drawer && drawer.IsOpen) {
|
|
857
|
+
drawer.Animated = animated;
|
|
858
|
+
const done = new Promise((r) => { const prev = drawer.StateTransitionComplete; drawer.StateTransitionComplete = (d, o) => { prev?.(d, o); if (!o)
|
|
859
|
+
r(); }; });
|
|
860
|
+
drawer.IsOpen = false;
|
|
861
|
+
if (animated)
|
|
862
|
+
await AnimateWithTimeout(done);
|
|
863
|
+
}
|
|
864
|
+
this.RemoveOverlay(this.modals, top);
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
/** Shows `content` full screen in a drawer sliding up from the bottom (C# PushModalAsync). */
|
|
868
|
+
async PushModalAsync(content, options) {
|
|
869
|
+
const animated = options?.animated ?? true;
|
|
870
|
+
if (!this.CanNavigate(this.Route, "Push"))
|
|
871
|
+
return;
|
|
872
|
+
const id = this.nextId++;
|
|
873
|
+
const entry = { id, body: content };
|
|
874
|
+
const overlay = (options?.freezeBackground ?? true) ? ShellDefaults.PopupBackgroundColor : undefined;
|
|
875
|
+
let opened = () => { };
|
|
876
|
+
const openedPromise = new Promise((r) => { opened = r; });
|
|
877
|
+
// the user closed it (drag down / IsOpen = false): leave through history when it owns an entry, else remove
|
|
878
|
+
const userClosed = () => {
|
|
879
|
+
if (entry.closing)
|
|
880
|
+
return;
|
|
881
|
+
if (entry.history && this.BackThroughHistory("modal"))
|
|
882
|
+
entry.history = false;
|
|
883
|
+
else {
|
|
884
|
+
entry.closing = true;
|
|
885
|
+
this.RemoveOverlay(this.modals, entry);
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
// a dialog for screen readers and the keyboard while open (drawnui-cross 6av, C# 2be842b8)
|
|
889
|
+
entry.ctrl = this.Create(SkiaLayer, { VerticalOptions: "Fill", HorizontalOptions: "Fill", ZIndex: ShellDefaults.ZIndexModals + id, BlockGesturesBelow: true, AccessibilityModal: true });
|
|
890
|
+
if (overlay)
|
|
891
|
+
entry.ctrl.InsertChild(this.Create(SkiaBackdrop, { Blur: ShellDefaults.PopupsBackgroundBlur, BackgroundColor: overlay, InputTransparent: true }));
|
|
892
|
+
const drawer = this.Create(SkiaDrawer, {
|
|
893
|
+
Direction: "FromBottom", HeaderSize: 0, HorizontalOptions: "Fill", VerticalOptions: "Fill",
|
|
894
|
+
RespondsToGestures: options?.useGestures ?? false, Animated: animated, Bounces: false, BlockGesturesBelow: true, ClosingStarted: () => this.LeaveModalStack(entry),
|
|
895
|
+
StateTransitionComplete: (d, isOpen) => { if (isOpen)
|
|
896
|
+
opened();
|
|
897
|
+
else if (!d.IsOpen)
|
|
898
|
+
userClosed(); },
|
|
899
|
+
IsOpenChanged: (_, isOpen) => { if (!isOpen && !animated)
|
|
900
|
+
userClosed(); },
|
|
901
|
+
});
|
|
902
|
+
entry.drawer = drawer;
|
|
903
|
+
this.Place(content, drawer);
|
|
904
|
+
entry.ctrl.InsertChild(drawer);
|
|
905
|
+
this.modals.push(entry);
|
|
906
|
+
this.InsertChild(entry.ctrl);
|
|
907
|
+
this.Notify();
|
|
908
|
+
entry.history = true;
|
|
909
|
+
this.PushHistory("modal", this.NavigationStack);
|
|
910
|
+
await this.Ready(content);
|
|
911
|
+
await WaitFor(() => drawer.DrawingRect.Height > 0 || drawer.IsDisposed); // first draw: the drawer measured its travel
|
|
912
|
+
if (!drawer.IsDisposed) {
|
|
913
|
+
drawer.IsOpen = true;
|
|
914
|
+
if (!animated)
|
|
915
|
+
opened();
|
|
916
|
+
}
|
|
917
|
+
await AnimateWithTimeout(openedPromise);
|
|
918
|
+
this.NotifyNavigated(this.Route, "Push", drawer);
|
|
919
|
+
}
|
|
920
|
+
/** Closes the top modal. */
|
|
921
|
+
async PopModalAsync(animated = true) {
|
|
922
|
+
const top = [...this.modals].reverse().find((m) => !m.left); // a modal sliding out has left the stack
|
|
923
|
+
if (!top || top.closing)
|
|
924
|
+
return;
|
|
925
|
+
if (top.history && this.BackThroughHistory("modal")) {
|
|
926
|
+
top.history = false;
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
await this.PopModalNow(top, animated);
|
|
930
|
+
}
|
|
931
|
+
// ---- toasts ----
|
|
932
|
+
async CloseToast(t, animated) {
|
|
933
|
+
if (t.closing)
|
|
934
|
+
return;
|
|
935
|
+
t.closing = true;
|
|
936
|
+
const c = t.ctrl;
|
|
937
|
+
if (animated && !c.IsDisposed)
|
|
938
|
+
await AnimateWithTimeout(Promise.all([c.TranslateToAsync(0, c.DrawingRect.Height / c.RenderingScale, 250), c.FadeToAsync(0, 250)]));
|
|
939
|
+
this.RemoveOverlay(this.toasts, t);
|
|
940
|
+
}
|
|
941
|
+
/** Removes every toast, without animation. */
|
|
942
|
+
async CloseAllToasts() { await Promise.all([...this.toasts].map((t) => this.CloseToast(t, false))); }
|
|
943
|
+
/** Shows a banner at the bottom for `msShowTime` ms: markdown text, or any content. */
|
|
944
|
+
ShowToast(content, msShowTime = 4000) {
|
|
945
|
+
void (async () => {
|
|
946
|
+
await this.CloseAllToasts();
|
|
947
|
+
if (this.IsDisposed)
|
|
948
|
+
return;
|
|
949
|
+
const id = this.nextId++;
|
|
950
|
+
const body = typeof content === "string"
|
|
951
|
+
? this.Create(SkiaRichLabel, { Text: content, TextColor: ShellDefaults.ToastTextColor, FontFamily: ShellDefaults.ToastTextFont || undefined, FontSize: ShellDefaults.ToastTextSize, Margin: new Thickness(ShellDefaults.ToastTextMargins), HorizontalOptions: "Fill" })
|
|
952
|
+
: content;
|
|
953
|
+
const entry = { id, body };
|
|
954
|
+
entry.ctrl = this.Create(SkiaLayer, { HorizontalOptions: "Fill", VerticalOptions: "End", ZIndex: ShellDefaults.ZIndexToasts + id, BackgroundColor: ShellDefaults.ToastBackgroundColor, BlockGesturesBelow: true, UseCache: "Operations", Opacity: 0 });
|
|
955
|
+
entry.content = this.Create(SkiaLayer, { HorizontalOptions: "Fill" });
|
|
956
|
+
this.Place(body, entry.content);
|
|
957
|
+
entry.ctrl.InsertChild(entry.content);
|
|
958
|
+
this.toasts.push(entry);
|
|
959
|
+
this.InsertChild(entry.ctrl);
|
|
960
|
+
this.Notify();
|
|
961
|
+
await this.Ready(body);
|
|
962
|
+
const c = entry.ctrl;
|
|
963
|
+
await WaitFor(() => c.DrawingRect.Height > 0 || c.IsDisposed); // laid out: height known
|
|
964
|
+
if (!c.IsDisposed) {
|
|
965
|
+
c.TranslationY = c.DrawingRect.Height / c.RenderingScale;
|
|
966
|
+
void c.TranslateToAsync(0, 0, 300);
|
|
967
|
+
void c.FadeToAsync(1, 300);
|
|
968
|
+
}
|
|
969
|
+
setTimeout(() => void this.CloseToast(entry, true), msShowTime);
|
|
970
|
+
})();
|
|
971
|
+
}
|
|
972
|
+
}
|