@overmvp/kumi 0.0.0-stage → 0.3.0
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/LICENSE +21 -0
- package/README.md +67 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +387 -0
- package/builder/css-v1.test.mjs +361 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +127 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +435 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2336 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +381 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +83 -0
- package/builder/src/numbers.lua +76 -0
- package/builder/src/project-front.mjs +3682 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +124 -0
- package/builder/src/setup-store.lua +18 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow-reads.lua +22 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-modules.mjs +93 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +59 -0
- package/eslint.mjs +74 -0
- package/form/index.ts +526 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +26 -4
- package/router/index.ts +50 -0
- package/router/navigation.ts +22 -0
- package/router/package.json +12 -0
- package/router/router-view.ts +134 -0
- package/router/routes.ts +64 -0
- package/scripts/addon-build.mjs +364 -0
- package/scripts/addon-project.mjs +78 -0
- package/scripts/kumi-cli.mjs +105 -0
- package/scripts/link-addons.mjs +61 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +239 -0
- package/src/css-v1.ts +68 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +505 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +336 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +29 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +29 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +366 -0
- package/src/media-query.ts +144 -0
- package/src/module-ui.ts +147 -0
- package/src/module.ts +311 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +52 -0
- package/src/providers.ts +241 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +275 -0
- package/src/result.ts +64 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +63 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +9 -0
- package/state/package.json +12 -0
- package/state/setup-store-persistence.ts +206 -0
- package/state/setup-store-view.ts +10 -0
- package/state/setup-store.ts +342 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +84 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/AGENTS.md +34 -0
- package/templates/addon/Assets/Art/Corner10.tga +0 -0
- package/templates/addon/Assets/Art/Corner11.tga +0 -0
- package/templates/addon/Assets/Art/Corner12.tga +0 -0
- package/templates/addon/Assets/Art/Corner13.tga +0 -0
- package/templates/addon/Assets/Art/Corner14.tga +0 -0
- package/templates/addon/Assets/Art/Corner15.tga +0 -0
- package/templates/addon/Assets/Art/Corner16.tga +0 -0
- package/templates/addon/Assets/Art/Corner2.tga +0 -0
- package/templates/addon/Assets/Art/Corner3.tga +0 -0
- package/templates/addon/Assets/Art/Corner4.tga +0 -0
- package/templates/addon/Assets/Art/Corner5.tga +0 -0
- package/templates/addon/Assets/Art/Corner6.tga +0 -0
- package/templates/addon/Assets/Art/Corner7.tga +0 -0
- package/templates/addon/Assets/Art/Corner8.tga +0 -0
- package/templates/addon/Assets/Art/Corner9.tga +0 -0
- package/templates/addon/Assets/Art/Dot.tga +0 -0
- package/templates/addon/Assets/Art/Gear.tga +0 -0
- package/templates/addon/Assets/Art/Grip.tga +0 -0
- package/templates/addon/Assets/Art/Outline10.tga +0 -0
- package/templates/addon/Assets/Art/Outline12.tga +0 -0
- package/templates/addon/Assets/Art/Outline13.tga +0 -0
- package/templates/addon/Assets/Art/Outline14.tga +0 -0
- package/templates/addon/Assets/Art/Outline15.tga +0 -0
- package/templates/addon/Assets/Art/Outline2.tga +0 -0
- package/templates/addon/Assets/Art/Outline3.tga +0 -0
- package/templates/addon/Assets/Art/Outline5.tga +0 -0
- package/templates/addon/Assets/Art/Outline7.tga +0 -0
- package/templates/addon/Assets/Art/Outline9.tga +0 -0
- package/templates/addon/Assets/Art/Round11.tga +0 -0
- package/templates/addon/Assets/Art/Round11Border.tga +0 -0
- package/templates/addon/Assets/Art/Round4.tga +0 -0
- package/templates/addon/Assets/Art/Round4Border.tga +0 -0
- package/templates/addon/Assets/Art/Round6.tga +0 -0
- package/templates/addon/Assets/Art/Round6Border.tga +0 -0
- package/templates/addon/Assets/Art/Round8.tga +0 -0
- package/templates/addon/Assets/Art/Round8Border.tga +0 -0
- package/templates/addon/Assets/Art/Search.tga +0 -0
- package/templates/addon/CLAUDE.md +1 -0
- package/templates/addon/README.md +49 -0
- package/templates/addon/eslint.config.mjs +6 -0
- package/templates/addon/gitignore +9 -0
- package/templates/addon/kumi.config.ts +17 -0
- package/templates/addon/package.json +20 -0
- package/templates/addon/src/app/app.module.ts +18 -0
- package/templates/addon/src/app/commands.store.ts +11 -0
- package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
- package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
- package/templates/addon/src/app/main.ts +5 -0
- package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
- package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
- package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
- package/templates/addon/src/app/theme.ts +6 -0
- package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
- package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
- package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
- package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
- package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
- package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
- package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
- package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
- package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
- package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
- package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
- package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
- package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
- package/templates/addon/src/shared/api/native-art.ts +28 -0
- package/templates/addon/src/shared/assets/close.png +0 -0
- package/templates/addon/src/shared/lib/format.ts +20 -0
- package/templates/addon/src/shared/styles/_tokens.scss +12 -0
- package/templates/addon/src/shared/ui/Button.tsx +29 -0
- package/templates/addon/src/shared/ui/Choice.tsx +34 -0
- package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
- package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
- package/templates/addon/src/shared/ui/Slider.tsx +25 -0
- package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
- package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
- package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
- package/templates/addon/tsconfig.json +63 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/action-bar-layout.ts +108 -0
- package/wow/action-button.ts +219 -0
- package/wow/action-catalog.ts +98 -0
- package/wow/addon-manifest.ts +36 -0
- package/wow/animation.ts +428 -0
- package/wow/app-ui.ts +221 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/cooldown-timing.ts +24 -0
- package/wow/css-box.ts +144 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +603 -0
- package/wow/css-text.ts +399 -0
- package/wow/cursor.ts +45 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +53 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frame-timing.ts +13 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/hooks.ts +133 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +111 -0
- package/wow/layout-view.ts +663 -0
- package/wow/locale-font.ts +15 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +153 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/panel-host.ts +97 -0
- package/wow/pixel.ts +35 -0
- package/wow/plugins.ts +246 -0
- package/wow/protect.ts +48 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/protected-view.ts +103 -0
- package/wow/range-art.ts +81 -0
- package/wow/readable-api.ts +120 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +183 -0
- package/wow/saved-variables.ts +109 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/spell-cooldown.ts +36 -0
- package/wow/start-addon.ts +122 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1864 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
import type { GridTrack } from "./grid-tracks";
|
|
2
|
+
import { ownedArray, type OwnedArray, type OwnedLuaArray, type OwnedReadonlyArray, type OwnedRecord } from "./owned-collections";
|
|
3
|
+
|
|
4
|
+
type FlexAlignment = "flex-start" | "center" | "flex-end" | "stretch";
|
|
5
|
+
type Insets = number | readonly [number, number] | readonly [number, number, number]
|
|
6
|
+
| readonly [number, number, number, number];
|
|
7
|
+
/** A border-box length; a percentage resolves against the parent's content box when that is known. */
|
|
8
|
+
export type LayoutLength = number | `${number}%`;
|
|
9
|
+
type Size = LayoutLength | "auto";
|
|
10
|
+
|
|
11
|
+
export interface FlexStyle {
|
|
12
|
+
width?: Size;
|
|
13
|
+
height?: Size;
|
|
14
|
+
minWidth?: number;
|
|
15
|
+
minHeight?: number;
|
|
16
|
+
maxWidth?: number;
|
|
17
|
+
maxHeight?: number;
|
|
18
|
+
padding?: Insets;
|
|
19
|
+
margin?: Insets;
|
|
20
|
+
gap?: number;
|
|
21
|
+
flexDirection?: "row" | "column";
|
|
22
|
+
flexWrap?: "nowrap" | "wrap";
|
|
23
|
+
justifyContent?: "flex-start" | "center" | "flex-end" | "space-between";
|
|
24
|
+
alignItems?: FlexAlignment;
|
|
25
|
+
alignSelf?: FlexAlignment | "auto";
|
|
26
|
+
flexBasis?: Size;
|
|
27
|
+
flexGrow?: number;
|
|
28
|
+
flexShrink?: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type GridAlignment = "start" | "center" | "end" | "stretch";
|
|
32
|
+
|
|
33
|
+
/** CSS display. `block` stacks block-level children and gathers inline-level runs into wrapping line boxes. */
|
|
34
|
+
export type LayoutDisplay = "flex" | "grid" | "block" | "inline" | "inline-block";
|
|
35
|
+
|
|
36
|
+
/** CSS position. `relative` shifts the laid-out box; `absolute` leaves the flow and is placed in this parent's box. */
|
|
37
|
+
export type LayoutPosition = "static" | "relative" | "absolute";
|
|
38
|
+
|
|
39
|
+
export interface LayoutStyle extends Omit<FlexStyle, "alignItems"> {
|
|
40
|
+
display?: LayoutDisplay;
|
|
41
|
+
position?: LayoutPosition;
|
|
42
|
+
top?: number;
|
|
43
|
+
right?: number;
|
|
44
|
+
bottom?: number;
|
|
45
|
+
left?: number;
|
|
46
|
+
alignItems?: FlexStyle["alignItems"] | "start" | "end";
|
|
47
|
+
justifyItems?: GridAlignment;
|
|
48
|
+
gridTemplateColumns?: readonly GridTrack[];
|
|
49
|
+
gridTemplateRows?: readonly GridTrack[];
|
|
50
|
+
rowGap?: number;
|
|
51
|
+
columnGap?: number;
|
|
52
|
+
gridColumn?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface LayoutSize {
|
|
56
|
+
width: number;
|
|
57
|
+
height: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface LayoutRect extends LayoutSize {
|
|
61
|
+
x: number;
|
|
62
|
+
y: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface FlexNode {
|
|
66
|
+
style: LayoutStyle;
|
|
67
|
+
children?: readonly FlexNode[];
|
|
68
|
+
/** Content size at natural width, or at the given inner width for height measurement. */
|
|
69
|
+
measure?(this: void, width?: number): LayoutSize;
|
|
70
|
+
/** Optional minimum content width for a grid track with an automatic minimum. */
|
|
71
|
+
minContentWidth?(this: void): number | undefined;
|
|
72
|
+
fullWidth?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface FlexLayout {
|
|
76
|
+
rect: LayoutRect;
|
|
77
|
+
children: readonly FlexLayout[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type Dimension = keyof LayoutSize;
|
|
81
|
+
|
|
82
|
+
interface Axis {
|
|
83
|
+
paddingStart: number;
|
|
84
|
+
paddingEnd: number;
|
|
85
|
+
marginStart: number;
|
|
86
|
+
marginEnd: number;
|
|
87
|
+
minimum: number;
|
|
88
|
+
maximum: number;
|
|
89
|
+
specified?: number;
|
|
90
|
+
/** A percentage size, resolved against the parent's content box during placement. */
|
|
91
|
+
percent?: number;
|
|
92
|
+
preferred: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface Item {
|
|
96
|
+
source: FlexNode;
|
|
97
|
+
main: Dimension;
|
|
98
|
+
width: Axis;
|
|
99
|
+
height: Axis;
|
|
100
|
+
/** In-flow children; a block parent's inline runs are wrapped in anonymous line boxes. */
|
|
101
|
+
children?: OwnedArray<Item>;
|
|
102
|
+
/** Absolutely positioned children, placed in this item's box after its flow. */
|
|
103
|
+
absolutes?: OwnedArray<Item>;
|
|
104
|
+
grid?: Grid;
|
|
105
|
+
/** Block flow: siblings' vertical margins collapse, children neither grow nor shrink. */
|
|
106
|
+
block?: boolean;
|
|
107
|
+
/** Wrapping row: the children's indices per line, set by the width pass. */
|
|
108
|
+
lines?: OwnedArray<OwnedArray<number>>;
|
|
109
|
+
/** An anonymous line box: its children's rects are moved into the block parent's space. */
|
|
110
|
+
anonymous?: boolean;
|
|
111
|
+
result: FlexLayout;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function edges(insets: Insets = 0): readonly [number, number, number, number] {
|
|
115
|
+
if (typeof insets === "number") return [insets, insets, insets, insets];
|
|
116
|
+
const [top, right, bottom = top, left = right] = insets;
|
|
117
|
+
return [top, right, bottom, left];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The number of a `"50%"` length, or undefined for any other value. */
|
|
121
|
+
export function layoutPercent(this: void, size: unknown): number | undefined {
|
|
122
|
+
if (typeof size !== "string" || !size.endsWith("%")) return undefined;
|
|
123
|
+
const value = Number(size.slice(0, size.length - 1));
|
|
124
|
+
return Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function axis(size: Size | undefined, minimum: number | undefined, maximum: number | undefined,
|
|
128
|
+
paddingStart: number, paddingEnd: number, marginStart: number, marginEnd: number): Axis {
|
|
129
|
+
const floor = Math.max(paddingStart + paddingEnd, minimum ?? 0);
|
|
130
|
+
return {
|
|
131
|
+
paddingStart, paddingEnd, marginStart, marginEnd,
|
|
132
|
+
minimum: floor,
|
|
133
|
+
maximum: Math.max(floor, maximum ?? Infinity),
|
|
134
|
+
specified: typeof size === "number" ? size : undefined,
|
|
135
|
+
percent: layoutPercent(size),
|
|
136
|
+
preferred: 0,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function clamp(size: number, axis: Axis) {
|
|
141
|
+
return Math.max(axis.minimum, Math.min(axis.maximum, size));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function innerSize(size: number, axis: Axis) {
|
|
145
|
+
return Math.max(0, size - axis.paddingStart - axis.paddingEnd);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function margins(axis: Axis) {
|
|
149
|
+
return axis.marginStart + axis.marginEnd;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function inlineLevel(node: FlexNode) {
|
|
153
|
+
return node.style.display === "inline" || node.style.display === "inline-block";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function blockContainer(style: LayoutStyle) {
|
|
157
|
+
return style.display === "block" || style.display === "inline" || style.display === "inline-block";
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The anonymous box CSS wraps around a run of inline-level children of a block: one wrapping row, bottoms aligned. */
|
|
161
|
+
const lineBox: FlexNode = { style: { display: "flex", flexDirection: "row", flexWrap: "wrap", alignItems: "flex-end" } };
|
|
162
|
+
|
|
163
|
+
function emptyResult(): FlexLayout {
|
|
164
|
+
return { rect: { x: 0, y: 0, width: 0, height: 0 }, children: [] };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function prepare(source: FlexNode, anonymous?: OwnedArray<Item>): Item {
|
|
168
|
+
const { style } = source;
|
|
169
|
+
const [top, right, bottom, left] = edges(style.padding);
|
|
170
|
+
const [marginTop, marginRight, marginBottom, marginLeft] = edges(style.margin);
|
|
171
|
+
const block = anonymous === undefined && blockContainer(style);
|
|
172
|
+
let children = anonymous;
|
|
173
|
+
let absolutes: OwnedArray<Item> | undefined;
|
|
174
|
+
const results: FlexLayout[] = [];
|
|
175
|
+
if (anonymous === undefined && source.children !== undefined) {
|
|
176
|
+
const flow = ownedArray<Item>();
|
|
177
|
+
let run: OwnedArray<Item> | undefined;
|
|
178
|
+
for (const child of source.children as OwnedReadonlyArray<FlexNode>) {
|
|
179
|
+
const prepared = prepare(child);
|
|
180
|
+
results.push(prepared.result);
|
|
181
|
+
if (child.style.position === "absolute") {
|
|
182
|
+
(absolutes ??= ownedArray<Item>()).push(prepared);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (block && inlineLevel(child)) {
|
|
186
|
+
(run ??= ownedArray<Item>()).push(prepared);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (run !== undefined) flow.push(prepare(lineBox, run));
|
|
190
|
+
run = undefined;
|
|
191
|
+
flow.push(prepared);
|
|
192
|
+
}
|
|
193
|
+
if (run !== undefined) flow.push(prepare(lineBox, run));
|
|
194
|
+
children = flow;
|
|
195
|
+
}
|
|
196
|
+
const item: Item = {
|
|
197
|
+
source,
|
|
198
|
+
main: block || style.flexDirection === "column" ? "height" : "width",
|
|
199
|
+
width: axis(style.width, style.minWidth, style.maxWidth, left, right, marginLeft, marginRight),
|
|
200
|
+
height: axis(style.height, style.minHeight, style.maxHeight, top, bottom, marginTop, marginBottom),
|
|
201
|
+
children,
|
|
202
|
+
absolutes,
|
|
203
|
+
block: block || undefined,
|
|
204
|
+
anonymous: anonymous !== undefined || undefined,
|
|
205
|
+
grid: children !== undefined && style.display === "grid" ? prepareGrid(style, children) : undefined,
|
|
206
|
+
result: emptyResult(),
|
|
207
|
+
};
|
|
208
|
+
item.result.children = anonymous === undefined ? results : anonymous.map(child => child.result);
|
|
209
|
+
return item;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Anonymous line boxes are not in the result: move their children's rects into the block parent's space. */
|
|
213
|
+
function settleAnonymous(item: Item) {
|
|
214
|
+
for (const child of item.children ?? []) {
|
|
215
|
+
if (child.anonymous) {
|
|
216
|
+
const { x, y } = child.result.rect;
|
|
217
|
+
for (const inline of child.children ?? []) {
|
|
218
|
+
inline.result.rect.x += x;
|
|
219
|
+
inline.result.rect.y += y;
|
|
220
|
+
settleAnonymous(inline);
|
|
221
|
+
}
|
|
222
|
+
} else settleAnonymous(child);
|
|
223
|
+
}
|
|
224
|
+
for (const child of item.absolutes ?? []) settleAnonymous(child);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function flexBasis(item: Item, preferred: number) {
|
|
228
|
+
const basis = item.source.style.flexBasis;
|
|
229
|
+
return typeof basis === "number" ? basis : preferred;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function measureLeaf(item: Item, dimension: Dimension) {
|
|
233
|
+
const target = item[dimension];
|
|
234
|
+
const width = dimension === "height" ? innerSize(item.result.rect.width, item.width) : undefined;
|
|
235
|
+
const measure = item.source.measure;
|
|
236
|
+
const measured = measure === undefined ? 0 : measure(width)[dimension] ?? 0;
|
|
237
|
+
return measured + target.paddingStart + target.paddingEnd;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function measureAxis(item: Item, dimension: Dimension) {
|
|
241
|
+
const { children } = item;
|
|
242
|
+
const target = item[dimension];
|
|
243
|
+
if (children === undefined) {
|
|
244
|
+
target.preferred = target.specified ?? measureLeaf(item, dimension);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const { grid } = item;
|
|
249
|
+
if (grid !== undefined) {
|
|
250
|
+
for (const child of children) measureAxis(child, dimension);
|
|
251
|
+
measureGridTracks(grid, children, dimension);
|
|
252
|
+
target.preferred = target.specified ?? measureGridSize(grid, children, dimension) + target.paddingStart + target.paddingEnd;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const onMainAxis = item.main === dimension;
|
|
257
|
+
let content = 0;
|
|
258
|
+
for (const child of children) measureAxis(child, dimension);
|
|
259
|
+
if (onMainAxis && item.block) content = blockExtent(children, dimension, child => clamp(child[dimension].preferred, child[dimension]));
|
|
260
|
+
else if (!onMainAxis && item.lines !== undefined) content = wrappedExtent(item, children, dimension, child => child[dimension].preferred);
|
|
261
|
+
else {
|
|
262
|
+
for (const child of children) {
|
|
263
|
+
const box = onMainAxis ? automaticMinimum(child, dimension, child[dimension]) : child[dimension];
|
|
264
|
+
const base = onMainAxis ? flexBasis(child, box.preferred) : box.preferred;
|
|
265
|
+
const outer = clamp(base, box) + margins(box);
|
|
266
|
+
content = onMainAxis ? content + outer : Math.max(content, outer);
|
|
267
|
+
}
|
|
268
|
+
if (onMainAxis) content += Math.max(0, children.length - 1) * (item.source.style.gap ?? 0);
|
|
269
|
+
}
|
|
270
|
+
target.preferred = target.specified ?? Math.max(0, content) + target.paddingStart + target.paddingEnd;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** A block's children stacked with collapsed sibling margins: the larger of two touching margins applies. */
|
|
274
|
+
function blockExtent(children: OwnedReadonlyArray<Item>, dimension: Dimension, size: (this: void, child: Item) => number) {
|
|
275
|
+
let extent = 0, previous: number | undefined;
|
|
276
|
+
for (const child of children) {
|
|
277
|
+
const box = child[dimension];
|
|
278
|
+
extent += (previous === undefined ? box.marginStart : Math.max(previous, box.marginStart)) + size(child);
|
|
279
|
+
previous = box.marginEnd;
|
|
280
|
+
}
|
|
281
|
+
return extent + (previous ?? 0);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function lineGap(item: Item) {
|
|
285
|
+
return item.source.style.rowGap ?? item.source.style.gap ?? 0;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The height of a wrapping row's lines: each line as tall as its tallest item. */
|
|
289
|
+
function wrappedExtent(item: Item, children: OwnedReadonlyArray<Item>, dimension: Dimension, size: (this: void, child: Item) => number) {
|
|
290
|
+
const lines = item.lines ?? [];
|
|
291
|
+
let extent = 0;
|
|
292
|
+
for (const line of lines) {
|
|
293
|
+
let tallest = 0;
|
|
294
|
+
for (const index of line) {
|
|
295
|
+
const child = children[index];
|
|
296
|
+
tallest = Math.max(tallest, clamp(size(child), child[dimension]) + margins(child[dimension]));
|
|
297
|
+
}
|
|
298
|
+
extent += tallest;
|
|
299
|
+
}
|
|
300
|
+
return extent + Math.max(0, lines.length - 1) * lineGap(item);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function preferredSize(item: Item, dimension: Dimension, available: number) {
|
|
304
|
+
const box = item[dimension];
|
|
305
|
+
if (dimension === "width" && item.source.fullWidth === true) return Math.max(0, available - margins(box));
|
|
306
|
+
if (box.percent !== undefined) return available * box.percent / 100;
|
|
307
|
+
return box.preferred;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
interface FlexibleItem {
|
|
311
|
+
box: Axis;
|
|
312
|
+
base: number;
|
|
313
|
+
hypothetical: number;
|
|
314
|
+
grow: number;
|
|
315
|
+
shrink: number;
|
|
316
|
+
target: number;
|
|
317
|
+
violation: number;
|
|
318
|
+
frozen: boolean;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface FlexSpace {
|
|
322
|
+
available: number;
|
|
323
|
+
grow: boolean;
|
|
324
|
+
initial: number;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function remainingSpace(items: OwnedReadonlyArray<FlexibleItem>, available: number) {
|
|
328
|
+
let remaining = available;
|
|
329
|
+
for (const item of items) remaining -= item.frozen ? item.target : item.base;
|
|
330
|
+
return remaining;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function resolveWeights(items: OwnedReadonlyArray<FlexibleItem>, grow: boolean) {
|
|
334
|
+
let factors = 0, weights = 0;
|
|
335
|
+
for (const item of items) {
|
|
336
|
+
if (item.frozen) continue;
|
|
337
|
+
factors += grow ? item.grow : item.shrink;
|
|
338
|
+
weights += grow ? item.grow : item.shrink * innerSize(item.base, item.box);
|
|
339
|
+
}
|
|
340
|
+
return { factors, weights };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Return true when all remaining targets fit their bounds or settle in one clamp. */
|
|
344
|
+
function distribute(items: OwnedReadonlyArray<FlexibleItem>, space: FlexSpace) {
|
|
345
|
+
const { grow } = space;
|
|
346
|
+
const { factors, weights } = resolveWeights(items, grow);
|
|
347
|
+
if (factors === 0) return true;
|
|
348
|
+
|
|
349
|
+
let remaining = remainingSpace(items, space.available);
|
|
350
|
+
if (factors < 1) {
|
|
351
|
+
const partial = space.initial * factors;
|
|
352
|
+
if (Math.abs(partial) < Math.abs(remaining)) remaining = partial;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const delta = grow ? remaining : -Math.abs(remaining);
|
|
356
|
+
let totalViolation = 0;
|
|
357
|
+
for (const item of items) {
|
|
358
|
+
if (item.frozen) continue;
|
|
359
|
+
const weight = grow ? item.grow : item.shrink * innerSize(item.base, item.box);
|
|
360
|
+
const share = weights === 0 ? 0 : delta * (weight / weights);
|
|
361
|
+
const proposed = item.base + share;
|
|
362
|
+
item.target = clamp(proposed, item.box);
|
|
363
|
+
item.violation = item.target - proposed;
|
|
364
|
+
totalViolation += item.violation;
|
|
365
|
+
}
|
|
366
|
+
if (!Number.isFinite(totalViolation) || totalViolation === 0) return true;
|
|
367
|
+
for (const item of items) {
|
|
368
|
+
if (!item.frozen && (totalViolation > 0 ? item.violation > 0 : item.violation < 0)) item.frozen = true;
|
|
369
|
+
}
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* CSS `min-height: auto` of a flex item in a column: it does not shrink below its specified height, or below its
|
|
375
|
+
* content when it is a container (2026-11-22: a `flex: 1` field with height 48 straight in a column resolved to 0 and the
|
|
376
|
+
* next field overlapped it). Text and other leaves without a height keep no minimum here, nor does a row's width yet.
|
|
377
|
+
*/
|
|
378
|
+
function automaticMinimum(child: Item, dimension: Dimension, box: Axis): Axis {
|
|
379
|
+
if (dimension !== "height" || child.source.style.minHeight !== undefined) return box;
|
|
380
|
+
if (box.specified === undefined && child.children === undefined) return box;
|
|
381
|
+
const floor = Math.max(box.minimum, box.preferred);
|
|
382
|
+
return floor === box.minimum ? box : { ...box as OwnedRecord<Axis>, minimum: floor, maximum: Math.max(floor, box.maximum) };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function resolveMainAxis(children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number, gap: number, block = false) {
|
|
386
|
+
let available = inner - gap * Math.max(0, children.length - 1);
|
|
387
|
+
let hypotheticalTotal = 0;
|
|
388
|
+
const items = ownedArray<FlexibleItem>();
|
|
389
|
+
for (const child of children) {
|
|
390
|
+
const box = block ? child[dimension] : automaticMinimum(child, dimension, child[dimension]);
|
|
391
|
+
const base = flexBasis(child, preferredSize(child, dimension, inner));
|
|
392
|
+
const hypothetical = clamp(base, box);
|
|
393
|
+
hypotheticalTotal += hypothetical;
|
|
394
|
+
available -= margins(box);
|
|
395
|
+
items.push({
|
|
396
|
+
box, base, hypothetical,
|
|
397
|
+
// A block's children keep their size, as in CSS: no growing into free space, overflow instead of shrinking.
|
|
398
|
+
grow: block ? 0 : child.source.style.flexGrow ?? 0,
|
|
399
|
+
shrink: block ? 0 : child.source.style.flexShrink ?? 1,
|
|
400
|
+
target: hypothetical, violation: 0, frozen: false,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const grow = hypotheticalTotal < available;
|
|
405
|
+
for (const item of items) {
|
|
406
|
+
item.frozen = grow ? item.grow === 0 || item.base > item.hypothetical
|
|
407
|
+
: item.shrink === 0 || item.base < item.hypothetical;
|
|
408
|
+
}
|
|
409
|
+
const space = { available, grow, initial: remainingSpace(items, available) };
|
|
410
|
+
while (!distribute(items, space)) {
|
|
411
|
+
// Freezing at least one bound violation makes progress on every round.
|
|
412
|
+
}
|
|
413
|
+
return items;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function alignedOffset(alignment: GridAlignment | FlexStyle["justifyContent"], space: number) {
|
|
417
|
+
if (alignment === "center") return space / 2;
|
|
418
|
+
if (alignment === "flex-end" || alignment === "end") return space;
|
|
419
|
+
return 0;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function positionRun(item: Item, children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number) {
|
|
423
|
+
const { gap = 0, justifyContent } = item.source.style;
|
|
424
|
+
const sizes = resolveMainAxis(children, dimension, inner, gap);
|
|
425
|
+
let space = inner - gap * Math.max(0, children.length - 1);
|
|
426
|
+
for (const child of sizes) space -= child.target + margins(child.box);
|
|
427
|
+
let cursor = item[dimension].paddingStart + alignedOffset(justifyContent, space);
|
|
428
|
+
const between = justifyContent === "space-between" && children.length > 1
|
|
429
|
+
? Math.max(0, space) / (children.length - 1) : 0;
|
|
430
|
+
|
|
431
|
+
for (let index = 0; index < children.length; index++) {
|
|
432
|
+
const { target, box } = sizes[index];
|
|
433
|
+
cursor += box.marginStart;
|
|
434
|
+
positionAxis(children[index], dimension, target, cursor);
|
|
435
|
+
cursor += target + box.marginEnd + gap + between;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** Block flow: children top to bottom at their own size, touching sibling margins collapsed. */
|
|
440
|
+
function positionBlock(item: Item, children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number) {
|
|
441
|
+
const sizes = resolveMainAxis(children, dimension, inner, 0, true);
|
|
442
|
+
let cursor = item[dimension].paddingStart;
|
|
443
|
+
let previous: number | undefined;
|
|
444
|
+
for (let index = 0; index < children.length; index++) {
|
|
445
|
+
const { target, box } = sizes[index];
|
|
446
|
+
cursor += previous === undefined ? box.marginStart : Math.max(previous, box.marginStart);
|
|
447
|
+
positionAxis(children[index], dimension, target, cursor);
|
|
448
|
+
cursor += target;
|
|
449
|
+
previous = box.marginEnd;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** A wrapping row: break the children into lines that fit, then lay out each line as its own run. */
|
|
454
|
+
function positionWrapped(item: Item, children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number) {
|
|
455
|
+
const gap = item.source.style.gap ?? 0;
|
|
456
|
+
const lines = ownedArray<OwnedArray<number>>();
|
|
457
|
+
let line = ownedArray<number>();
|
|
458
|
+
let used = 0;
|
|
459
|
+
for (let index = 0; index < children.length; index++) {
|
|
460
|
+
const child = children[index];
|
|
461
|
+
const box = child[dimension];
|
|
462
|
+
const outer = clamp(flexBasis(child, preferredSize(child, dimension, inner)), box) + margins(box);
|
|
463
|
+
if (line.length > 0 && used + gap + outer > inner + 0.001) {
|
|
464
|
+
lines.push(line);
|
|
465
|
+
line = ownedArray<number>();
|
|
466
|
+
used = 0;
|
|
467
|
+
}
|
|
468
|
+
used += (line.length > 0 ? gap : 0) + outer;
|
|
469
|
+
line.push(index);
|
|
470
|
+
}
|
|
471
|
+
if (line.length > 0) lines.push(line);
|
|
472
|
+
item.lines = lines;
|
|
473
|
+
for (const indices of lines) positionRun(item, indices.map(index => children[index]) as OwnedArray<Item>, dimension, inner);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function positionMain(item: Item, children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number) {
|
|
477
|
+
if (item.block) positionBlock(item, children, dimension, inner);
|
|
478
|
+
else if (item.source.style.flexWrap === "wrap") positionWrapped(item, children, dimension, inner);
|
|
479
|
+
else positionRun(item, children, dimension, inner);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function positionAligned(child: Item, dimension: Dimension, start: number, inner: number,
|
|
483
|
+
alignment: GridAlignment | FlexAlignment) {
|
|
484
|
+
const box = child[dimension];
|
|
485
|
+
const available = inner - margins(box);
|
|
486
|
+
const preferred = alignment === "stretch" && box.specified === undefined && box.percent === undefined ? Math.max(0, available)
|
|
487
|
+
: preferredSize(child, dimension, inner);
|
|
488
|
+
const target = clamp(preferred, box);
|
|
489
|
+
const offset = alignedOffset(alignment, available - target);
|
|
490
|
+
positionAxis(child, dimension, target, start + box.marginStart + offset);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function crossAlignment(item: Item, child: Item): GridAlignment | FlexAlignment {
|
|
494
|
+
// A block's children fill its width unless they set one, as block boxes do.
|
|
495
|
+
if (item.block) return "stretch";
|
|
496
|
+
const self = child.source.style.alignSelf;
|
|
497
|
+
return self === undefined || self === "auto" ? item.source.style.alignItems ?? "stretch" : self;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function positionCross(item: Item, children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number) {
|
|
501
|
+
const start = item[dimension].paddingStart;
|
|
502
|
+
if (item.lines === undefined) {
|
|
503
|
+
for (const child of children) positionAligned(child, dimension, start, inner, crossAlignment(item, child));
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
let cursor = start;
|
|
507
|
+
for (const line of item.lines) {
|
|
508
|
+
let tallest = 0;
|
|
509
|
+
for (const index of line) {
|
|
510
|
+
const child = children[index];
|
|
511
|
+
tallest = Math.max(tallest, clamp(preferredSize(child, dimension, inner), child[dimension]) + margins(child[dimension]));
|
|
512
|
+
}
|
|
513
|
+
for (const index of line) positionAligned(children[index], dimension, cursor, tallest, crossAlignment(item, children[index]));
|
|
514
|
+
cursor += tallest + lineGap(item);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** Absolutely positioned children: insets from this item's box, stretched between two insets when sized auto. */
|
|
519
|
+
function positionAbsolutes(item: Item, dimension: Dimension, size: number) {
|
|
520
|
+
for (const child of item.absolutes ?? []) {
|
|
521
|
+
measureAxis(child, dimension);
|
|
522
|
+
const box = child[dimension];
|
|
523
|
+
const style = child.source.style;
|
|
524
|
+
const start = dimension === "width" ? style.left : style.top;
|
|
525
|
+
const end = dimension === "width" ? style.right : style.bottom;
|
|
526
|
+
let target = box.specified ?? (box.percent !== undefined ? size * box.percent / 100
|
|
527
|
+
: start !== undefined && end !== undefined ? size - start - end - margins(box) : box.preferred);
|
|
528
|
+
target = clamp(target, box);
|
|
529
|
+
const at = start !== undefined ? start + box.marginStart
|
|
530
|
+
: end !== undefined ? size - end - box.marginEnd - target : item[dimension].paddingStart + box.marginStart;
|
|
531
|
+
positionAxis(child, dimension, target, at);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function relativeOffset(item: Item, dimension: Dimension) {
|
|
536
|
+
const style = item.source.style;
|
|
537
|
+
if (style.position !== "relative") return 0;
|
|
538
|
+
return dimension === "width" ? style.left ?? -(style.right ?? 0) : style.top ?? -(style.bottom ?? 0);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
interface GridCell {
|
|
542
|
+
column: number;
|
|
543
|
+
row: number;
|
|
544
|
+
span: number;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
interface Track {
|
|
548
|
+
definition: GridTrack;
|
|
549
|
+
base: number;
|
|
550
|
+
start: number;
|
|
551
|
+
size: number;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
interface TrackAxis {
|
|
555
|
+
tracks: OwnedArray<Track>;
|
|
556
|
+
gap: number;
|
|
557
|
+
intrinsicUnit: number;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
interface Grid {
|
|
561
|
+
columns: TrackAxis;
|
|
562
|
+
rows: TrackAxis;
|
|
563
|
+
cells: OwnedArray<GridCell>;
|
|
564
|
+
spanning: OwnedArray<number>;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const autoTrack: GridTrack = { kind: "auto" };
|
|
568
|
+
|
|
569
|
+
function makeTrack(definition: GridTrack): Track {
|
|
570
|
+
const base = definition.kind === "fixed" ? definition.size
|
|
571
|
+
: definition.kind === "fraction" ? definition.minimum ?? 0 : 0;
|
|
572
|
+
return { definition, base, start: 0, size: 0 };
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function trackAxis(template: OwnedReadonlyArray<GridTrack> | undefined, count: number, gap: number): TrackAxis {
|
|
576
|
+
const tracks = template === undefined ? ownedArray<Track>() : template.map(track => makeTrack(track)) as OwnedArray<Track>;
|
|
577
|
+
while (tracks.length < count) tracks.push(makeTrack(autoTrack));
|
|
578
|
+
return { tracks, gap, intrinsicUnit: 0 };
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function prepareGrid(style: LayoutStyle, children: OwnedReadonlyArray<Item>): Grid {
|
|
582
|
+
const columnTracks = style.gridTemplateColumns as OwnedReadonlyArray<GridTrack> | undefined;
|
|
583
|
+
const rowTracks = style.gridTemplateRows as OwnedReadonlyArray<GridTrack> | undefined;
|
|
584
|
+
let columns = columnTracks === undefined ? 1 : Math.max(1, columnTracks.length);
|
|
585
|
+
for (const child of children) columns = Math.max(columns, child.source.style.gridColumn ?? 1);
|
|
586
|
+
|
|
587
|
+
const cells = ownedArray<GridCell>();
|
|
588
|
+
const spanning = ownedArray<number>();
|
|
589
|
+
let row = 0, column = 0;
|
|
590
|
+
for (const child of children) {
|
|
591
|
+
const span = child.source.style.gridColumn ?? 1;
|
|
592
|
+
if (column + span > columns) {
|
|
593
|
+
row++;
|
|
594
|
+
column = 0;
|
|
595
|
+
}
|
|
596
|
+
if (span > 1) spanning.push(cells.length);
|
|
597
|
+
cells.push({ column, row, span });
|
|
598
|
+
column += span;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
spanning.sort((a, b) => cells[a].span === cells[b].span ? a - b : cells[a].span - cells[b].span);
|
|
602
|
+
return {
|
|
603
|
+
columns: trackAxis(columnTracks, columns, style.columnGap ?? style.gap ?? 0),
|
|
604
|
+
rows: trackAxis(rowTracks, cells.length > 0 ? row + 1 : 0, style.rowGap ?? style.gap ?? 0),
|
|
605
|
+
cells, spanning,
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function preferredContribution(item: Item, dimension: Dimension) {
|
|
610
|
+
const box = item[dimension];
|
|
611
|
+
return clamp(box.preferred, box) + margins(box);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/** The min-content width of a box's content, as CSS computes it: wrapping text gives way, so a column of cards with
|
|
615
|
+
* long notes does not stretch its grid track to the notes' single-line width. */
|
|
616
|
+
function minContent(item: Item): number {
|
|
617
|
+
const box = item.width;
|
|
618
|
+
if (box.specified !== undefined) return box.specified;
|
|
619
|
+
const own = item.source.minContentWidth?.();
|
|
620
|
+
const { children } = item;
|
|
621
|
+
if (children === undefined) return own ?? box.preferred;
|
|
622
|
+
if (own !== undefined) return own;
|
|
623
|
+
const padding = box.paddingStart + box.paddingEnd;
|
|
624
|
+
if (children.length === 0) return padding;
|
|
625
|
+
const outer = (child: Item) => Math.max(child.width.minimum, minContent(child)) + margins(child.width);
|
|
626
|
+
if (item.grid !== undefined) {
|
|
627
|
+
const columns = [] as number[] as OwnedLuaArray<number>;
|
|
628
|
+
item.grid.cells.forEach((cell, index) => {
|
|
629
|
+
if (cell.span === 1) columns[cell.column] = Math.max(columns[cell.column] ?? 0, outer(children[index]));
|
|
630
|
+
});
|
|
631
|
+
let sum = trackGaps(item.grid.columns);
|
|
632
|
+
for (const track of item.grid.columns.tracks) sum += track.definition.kind === "fixed" ? track.definition.size : 0;
|
|
633
|
+
for (const size of columns) sum += size ?? 0;
|
|
634
|
+
return sum + padding;
|
|
635
|
+
}
|
|
636
|
+
if (item.main === "width" && item.source.style.flexWrap !== "wrap") {
|
|
637
|
+
let sum = Math.max(0, children.length - 1) * (item.source.style.gap ?? 0);
|
|
638
|
+
for (const child of children) sum += outer(child);
|
|
639
|
+
return sum + padding;
|
|
640
|
+
}
|
|
641
|
+
let widest = 0;
|
|
642
|
+
for (const child of children) widest = Math.max(widest, outer(child));
|
|
643
|
+
return widest + padding;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function minimumContribution(item: Item, dimension: Dimension) {
|
|
647
|
+
const box = item[dimension];
|
|
648
|
+
const explicitMinimum = dimension === "width" ? item.source.style.minWidth : item.source.style.minHeight;
|
|
649
|
+
if (explicitMinimum !== undefined) return box.minimum + margins(box);
|
|
650
|
+
if (dimension === "width" && box.specified === undefined) {
|
|
651
|
+
const minimum = item.children === undefined ? item.source.minContentWidth?.() : minContent(item) - box.paddingStart - box.paddingEnd;
|
|
652
|
+
if (minimum !== undefined) return clamp(minimum + box.paddingStart + box.paddingEnd, box) + margins(box);
|
|
653
|
+
}
|
|
654
|
+
return preferredContribution(item, dimension);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function growIntrinsicSpan(axis: TrackAxis, child: Item, cell: GridCell) {
|
|
658
|
+
const { column, span } = cell;
|
|
659
|
+
let size = (span - 1) * axis.gap;
|
|
660
|
+
let autos = 0, factors = 0;
|
|
661
|
+
for (let index = column; index < column + span; index++) {
|
|
662
|
+
const { base, definition } = axis.tracks[index];
|
|
663
|
+
size += base;
|
|
664
|
+
if (definition.kind === "auto") autos++;
|
|
665
|
+
else if (definition.kind === "fraction" && definition.minimum === undefined) factors += definition.factor;
|
|
666
|
+
}
|
|
667
|
+
if (autos === 0 && factors === 0) return;
|
|
668
|
+
const needed = autos > 0 ? preferredContribution(child, "width") : minimumContribution(child, "width");
|
|
669
|
+
const deficit = needed - size;
|
|
670
|
+
if (deficit <= 0) return;
|
|
671
|
+
|
|
672
|
+
for (let index = column; index < column + span; index++) {
|
|
673
|
+
const track = axis.tracks[index];
|
|
674
|
+
const { definition } = track;
|
|
675
|
+
if (autos > 0) {
|
|
676
|
+
if (definition.kind === "auto") track.base += deficit / autos;
|
|
677
|
+
} else if (definition.kind === "fraction" && definition.minimum === undefined) {
|
|
678
|
+
track.base += deficit * (definition.factor / factors);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** A smaller unit can only freeze more tracks at their minima. */
|
|
684
|
+
function fractionUnit(tracks: OwnedReadonlyArray<Track>, start: number, count: number, available: number, divisorFloor: number): number {
|
|
685
|
+
let unit = Infinity;
|
|
686
|
+
let previousCount = -1;
|
|
687
|
+
for (;;) {
|
|
688
|
+
let free = available, factors = 0, active = 0;
|
|
689
|
+
for (let index = start; index < start + count; index++) {
|
|
690
|
+
const { base, definition } = tracks[index];
|
|
691
|
+
if (definition.kind === "fraction" && definition.factor > 0 && base / definition.factor <= unit) {
|
|
692
|
+
factors += definition.factor;
|
|
693
|
+
active++;
|
|
694
|
+
} else free -= base;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if (factors === 0) return 0;
|
|
698
|
+
if (active === previousCount) return unit;
|
|
699
|
+
previousCount = active;
|
|
700
|
+
unit = Math.max(0, free) / Math.max(divisorFloor, factors);
|
|
701
|
+
if (!Number.isFinite(unit)) return unit;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function trackSize(track: Track, unit: number) {
|
|
706
|
+
const { definition, base } = track;
|
|
707
|
+
if (definition.kind !== "fraction" || definition.factor === 0) return base;
|
|
708
|
+
return Math.max(base, unit * definition.factor);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function gridAxis(grid: Grid, dimension: Dimension) {
|
|
712
|
+
return dimension === "width" ? grid.columns : grid.rows;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function measureGridTracks(grid: Grid, children: OwnedReadonlyArray<Item>, dimension: Dimension) {
|
|
716
|
+
const axis = gridAxis(grid, dimension);
|
|
717
|
+
for (let index = 0; index < children.length; index++) {
|
|
718
|
+
const { column, row, span } = grid.cells[index];
|
|
719
|
+
if (dimension === "width" && span > 1) continue;
|
|
720
|
+
const track = axis.tracks[dimension === "width" ? column : row];
|
|
721
|
+
if (track.definition.kind === "auto") {
|
|
722
|
+
track.base = Math.max(track.base, preferredContribution(children[index], dimension));
|
|
723
|
+
} else if (track.definition.kind === "fraction" && track.definition.minimum === undefined) {
|
|
724
|
+
track.base = Math.max(track.base, minimumContribution(children[index], dimension));
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
if (dimension === "width") {
|
|
728
|
+
for (const index of grid.spanning) growIntrinsicSpan(axis, children[index], grid.cells[index]);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function trackGaps(axis: TrackAxis) {
|
|
733
|
+
return Math.max(0, axis.tracks.length - 1) * axis.gap;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
function measureGridSize(grid: Grid, children: OwnedReadonlyArray<Item>, dimension: Dimension) {
|
|
737
|
+
const axis = gridAxis(grid, dimension);
|
|
738
|
+
let unit = 0;
|
|
739
|
+
for (let index = 0; index < children.length; index++) {
|
|
740
|
+
const { column, row, span } = grid.cells[index];
|
|
741
|
+
const start = dimension === "width" ? column : row;
|
|
742
|
+
const count = dimension === "width" ? span : 1;
|
|
743
|
+
const needed = preferredContribution(children[index], dimension) - (count - 1) * axis.gap;
|
|
744
|
+
unit = Math.max(unit, fractionUnit(axis.tracks, start, count, needed, 0));
|
|
745
|
+
}
|
|
746
|
+
axis.intrinsicUnit = unit;
|
|
747
|
+
let size = trackGaps(axis);
|
|
748
|
+
for (const track of axis.tracks) size += trackSize(track, unit);
|
|
749
|
+
return size;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
function positionGrid(item: Item, grid: Grid, children: OwnedReadonlyArray<Item>, dimension: Dimension, inner: number, definite: boolean) {
|
|
753
|
+
const axis = gridAxis(grid, dimension);
|
|
754
|
+
const unit = definite ? fractionUnit(axis.tracks, 0, axis.tracks.length, inner - trackGaps(axis), 1) : axis.intrinsicUnit;
|
|
755
|
+
let cursor = item[dimension].paddingStart;
|
|
756
|
+
for (const track of axis.tracks) {
|
|
757
|
+
track.start = cursor;
|
|
758
|
+
track.size = trackSize(track, unit);
|
|
759
|
+
cursor += track.size + axis.gap;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const alignment = (dimension === "width" ? item.source.style.justifyItems : item.source.style.alignItems) ?? "stretch";
|
|
763
|
+
for (let index = 0; index < children.length; index++) {
|
|
764
|
+
const child = children[index];
|
|
765
|
+
const { column, row, span } = grid.cells[index];
|
|
766
|
+
const first = axis.tracks[dimension === "width" ? column : row];
|
|
767
|
+
const last = dimension === "width" ? axis.tracks[column + span - 1] : first;
|
|
768
|
+
const cellSize = last.start + last.size - first.start;
|
|
769
|
+
positionAligned(child, dimension, first.start, cellSize, alignment);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
function positionAxis(item: Item, dimension: Dimension, assigned: number | undefined, start: number) {
|
|
775
|
+
const axis = item[dimension];
|
|
776
|
+
const size = clamp(assigned ?? axis.preferred, axis);
|
|
777
|
+
const { rect } = item.result;
|
|
778
|
+
rect[dimension] = size;
|
|
779
|
+
const at = start + relativeOffset(item, dimension);
|
|
780
|
+
if (dimension === "width") rect.x = at;
|
|
781
|
+
else rect.y = at;
|
|
782
|
+
|
|
783
|
+
const { children } = item;
|
|
784
|
+
if (children !== undefined && children.length > 0) {
|
|
785
|
+
const inner = innerSize(size, axis);
|
|
786
|
+
if (item.grid !== undefined) {
|
|
787
|
+
const definite = assigned !== undefined || axis.specified !== undefined || size !== axis.preferred;
|
|
788
|
+
positionGrid(item, item.grid, children, dimension, inner, definite);
|
|
789
|
+
} else if (dimension === item.main) positionMain(item, children, dimension, inner);
|
|
790
|
+
else positionCross(item, children, dimension, inner);
|
|
791
|
+
}
|
|
792
|
+
positionAbsolutes(item, dimension, size);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/** Calculate parent-local rectangles for a resolved snapshot: flow, block and grid children, relative shifts and
|
|
796
|
+
* absolutely positioned children. Results follow the source tree; anonymous line boxes are folded away. */
|
|
797
|
+
export function solveLayout(this: void, root: FlexNode, assigned?: Partial<LayoutSize>): FlexLayout {
|
|
798
|
+
const item = prepare(root);
|
|
799
|
+
measureAxis(item, "width");
|
|
800
|
+
positionAxis(item, "width", assigned?.width, 0);
|
|
801
|
+
measureAxis(item, "height");
|
|
802
|
+
positionAxis(item, "height", assigned?.height, 0);
|
|
803
|
+
settleAnonymous(item);
|
|
804
|
+
return item.result;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
export const solveFlex = solveLayout;
|