embedded-react 0.18.0 → 0.20.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/NOTICE +1 -1
- package/README.md +8 -4
- package/aot/compile.mjs +178 -58
- package/aot/compiler/animations.mjs +290 -281
- package/aot/compiler/boot.mjs +70 -33
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +9 -9
- package/aot/compiler/collect.mjs +324 -205
- package/aot/compiler/control-flow.mjs +209 -197
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image.mjs +51 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +23 -23
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +372 -333
- package/aot/compiler/handlers.mjs +473 -387
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +4 -4
- package/aot/compiler/ir/reads.mjs +19 -19
- package/aot/compiler/ir/types.mjs +14 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/lower.mjs +77 -80
- package/aot/compiler/nodes.mjs +169 -152
- package/aot/compiler/options.mjs +35 -30
- package/aot/compiler/out.mjs +18 -22
- package/aot/compiler/package/animations.mjs +669 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +177 -0
- package/aot/compiler/package/bytecode.mjs +286 -0
- package/aot/compiler/package/checks.mjs +1022 -0
- package/aot/compiler/package/codegen.mjs +1329 -0
- package/aot/compiler/package/enums.mjs +138 -0
- package/aot/compiler/package/format.mjs +760 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +1606 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/tables.mjs +103 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +664 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +21 -21
- package/aot/compiler/static-eval.mjs +25 -23
- package/aot/compiler/style-text.mjs +161 -172
- package/aot/compiler/svg.mjs +477 -251
- package/aot/screenshot-smoke.mjs +55 -41
- package/aot/style-map.mjs +102 -90
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +14 -10
- package/assets/emit-c.mjs +83 -62
- package/assets/emit-container.mjs +124 -38
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +16 -14
- package/cli.mjs +359 -172
- package/compile-bin.mjs +5 -5
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +46 -37
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +51 -38
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +31 -23
- package/package.json +11 -12
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +101 -82
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +4 -4
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +6 -6
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +5 -5
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
package/aot/screenshot-smoke.mjs
CHANGED
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
/*
|
|
18
18
|
* `npm run aot:smoke` — automated compile-and-screenshot smoke test for the Flow B (AOT) demos.
|
|
19
19
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
20
|
+
* It builds the linux-aot SDL host (which loads a package), then for each demo: (1) compiles App.jsx →
|
|
21
|
+
* dist/app.erpkg (`node aot/compile.mts`), (2) runs the host headless via ER_AOT_SHOT to render ONE frame to
|
|
22
|
+
* a BMP, and (3) checks the image actually has content (distinct colors above a floor) — i.e., it compiled,
|
|
23
|
+
* loaded, and rendered something rather than crashing or drawing a blank screen. Exits non-zero if any demo
|
|
24
|
+
* fails, so it can gate CI.
|
|
25
25
|
*
|
|
26
26
|
* Prereq: the linux-aot CMake build must be configurable (SDL2 found). It reuses examples/linux-aot/build;
|
|
27
27
|
* if that isn't configured yet, set CMAKE_TOOLCHAIN_FILE (e.g. a vcpkg toolchain) and it will configure it.
|
|
@@ -32,22 +32,29 @@ import {existsSync, mkdirSync, readFileSync, rmSync} from 'node:fs';
|
|
|
32
32
|
import {resolve, dirname} from 'node:path';
|
|
33
33
|
import {fileURLToPath} from 'node:url';
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
35
|
+
// Where a BMP's header keeps what the check reads, and the 'BM' it starts with.
|
|
36
|
+
const BMP_MAGIC = [0x42, 0x4d];
|
|
37
|
+
const BMP_PIXEL_OFFSET_AT = 10;
|
|
38
|
+
const BMP_WIDTH_AT = 18;
|
|
39
|
+
const BMP_HEIGHT_AT = 22;
|
|
40
|
+
const BMP_BITS_PER_PIXEL_AT = 28;
|
|
41
|
+
|
|
42
|
+
const here = dirname(fileURLToPath(import.meta.url)); // packages/embedded-react/aot
|
|
43
|
+
const packageDirectory = resolve(here, '..'); // packages/embedded-react
|
|
44
|
+
const repositoryRoot = resolve(here, '../../..');
|
|
45
|
+
const exampleDirectory = resolve(repositoryRoot, 'examples/linux-aot');
|
|
46
|
+
const buildDirectory = resolve(exampleDirectory, 'build');
|
|
47
|
+
const executable = resolve(
|
|
48
|
+
buildDirectory,
|
|
42
49
|
process.platform === 'win32'
|
|
43
50
|
? 'embedded-react-desktop-aot.exe'
|
|
44
51
|
: 'embedded-react-desktop-aot',
|
|
45
52
|
);
|
|
46
|
-
const
|
|
53
|
+
const temporaryDirectory = resolve(packageDirectory, 'dist', '.smoke');
|
|
47
54
|
|
|
48
55
|
/**
|
|
49
|
-
* The demos to smoke-test. `screen` sets ER_AOT_SCREEN_W/H
|
|
50
|
-
*
|
|
56
|
+
* The demos to smoke-test. `screen` sets ER_AOT_SCREEN_W/H, the panel the package is for and the window's size;
|
|
57
|
+
* `minColors` is the floor of distinct sampled colors that counts as "rendered".
|
|
51
58
|
*/
|
|
52
59
|
const DEMOS = [
|
|
53
60
|
{name: 'watch-face', screen: {w: 240, h: 280}, minColors: 40},
|
|
@@ -64,14 +71,14 @@ const DEMOS = [
|
|
|
64
71
|
* @returns The number of distinct colors sampled.
|
|
65
72
|
*/
|
|
66
73
|
function bmpDistinctColors(path, step = 4) {
|
|
67
|
-
const
|
|
68
|
-
if (
|
|
74
|
+
const bitmap = readFileSync(path);
|
|
75
|
+
if (bitmap[0] !== BMP_MAGIC[0] || bitmap[1] !== BMP_MAGIC[1]) {
|
|
69
76
|
throw new Error(`not a BMP: ${path}`);
|
|
70
77
|
}
|
|
71
|
-
const pixelOffset =
|
|
72
|
-
const width =
|
|
73
|
-
const height = Math.abs(
|
|
74
|
-
const bitsPerPixel =
|
|
78
|
+
const pixelOffset = bitmap.readUInt32LE(BMP_PIXEL_OFFSET_AT);
|
|
79
|
+
const width = bitmap.readInt32LE(BMP_WIDTH_AT);
|
|
80
|
+
const height = Math.abs(bitmap.readInt32LE(BMP_HEIGHT_AT));
|
|
81
|
+
const bitsPerPixel = bitmap.readUInt16LE(BMP_BITS_PER_PIXEL_AT);
|
|
75
82
|
if (bitsPerPixel !== 24 && bitsPerPixel !== 32) {
|
|
76
83
|
throw new Error(`unexpected BMP bpp ${bitsPerPixel}`);
|
|
77
84
|
}
|
|
@@ -81,54 +88,60 @@ function bmpDistinctColors(path, step = 4) {
|
|
|
81
88
|
for (let y = 0; y < height; y += step) {
|
|
82
89
|
for (let x = 0; x < width; x += step) {
|
|
83
90
|
const index = pixelOffset + y * rowSize + x * bytesPerPixel;
|
|
84
|
-
colors.add(
|
|
91
|
+
colors.add(
|
|
92
|
+
(bitmap[index] << 16) | (bitmap[index + 1] << 8) | bitmap[index + 2],
|
|
93
|
+
); // BGR triplet
|
|
85
94
|
}
|
|
86
95
|
}
|
|
87
96
|
return colors.size;
|
|
88
97
|
}
|
|
89
98
|
|
|
90
|
-
function run(command,
|
|
91
|
-
execFileSync(command,
|
|
99
|
+
function run(command, commandArguments, options = {}) {
|
|
100
|
+
execFileSync(command, commandArguments, {stdio: 'pipe', ...options});
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
/** Configures the linux-aot CMake build on the first run. */
|
|
95
104
|
function ensureConfigured() {
|
|
96
|
-
if (existsSync(resolve(
|
|
105
|
+
if (existsSync(resolve(buildDirectory, 'CMakeCache.txt'))) return;
|
|
97
106
|
console.log('• configuring linux-aot build (first run)…');
|
|
98
|
-
const
|
|
107
|
+
const cmakeArguments = ['-S', exampleDirectory, '-B', buildDirectory];
|
|
99
108
|
if (process.env.CMAKE_TOOLCHAIN_FILE) {
|
|
100
|
-
|
|
109
|
+
cmakeArguments.push(
|
|
110
|
+
`-DCMAKE_TOOLCHAIN_FILE=${process.env.CMAKE_TOOLCHAIN_FILE}`,
|
|
111
|
+
);
|
|
101
112
|
}
|
|
102
113
|
if (process.platform === 'win32') {
|
|
103
|
-
|
|
114
|
+
cmakeArguments.push('-G', 'MinGW Makefiles');
|
|
104
115
|
}
|
|
105
|
-
run('cmake',
|
|
116
|
+
run('cmake', cmakeArguments);
|
|
106
117
|
}
|
|
107
118
|
|
|
108
119
|
/**
|
|
109
|
-
* Compiles one demo,
|
|
110
|
-
*
|
|
120
|
+
* Compiles one demo, renders one frame and checks the frame has content. A step that cannot run (the compiler or
|
|
121
|
+
* the host) throws.
|
|
111
122
|
*
|
|
112
123
|
* @param demo The demo, from DEMOS.
|
|
113
124
|
*
|
|
114
125
|
* @returns Why the demo failed, or null when it rendered.
|
|
115
126
|
*/
|
|
116
127
|
function smokeTest(demo) {
|
|
117
|
-
const shot = resolve(
|
|
128
|
+
const shot = resolve(temporaryDirectory, `${demo.name}.bmp`);
|
|
118
129
|
rmSync(shot, {force: true});
|
|
119
130
|
|
|
120
|
-
// Compile
|
|
121
|
-
const
|
|
131
|
+
// Compile at the demo's screen size, then render one frame to the BMP in a window of that size.
|
|
132
|
+
const screenEnvironment = {...process.env};
|
|
122
133
|
if (demo.screen) {
|
|
123
|
-
|
|
124
|
-
|
|
134
|
+
screenEnvironment.ER_AOT_SCREEN_W = String(demo.screen.w);
|
|
135
|
+
screenEnvironment.ER_AOT_SCREEN_H = String(demo.screen.h);
|
|
125
136
|
}
|
|
126
137
|
run('node', [resolve(here, 'compile.mts'), demo.name], {
|
|
127
|
-
cwd:
|
|
128
|
-
env:
|
|
138
|
+
cwd: packageDirectory,
|
|
139
|
+
env: screenEnvironment,
|
|
140
|
+
});
|
|
141
|
+
run(executable, [resolve(packageDirectory, 'dist/app.erpkg')], {
|
|
142
|
+
cwd: buildDirectory,
|
|
143
|
+
env: {...screenEnvironment, ER_NO_HIDPI: '1', ER_AOT_SHOT: shot},
|
|
129
144
|
});
|
|
130
|
-
run('cmake', ['--build', buildDir]);
|
|
131
|
-
run(exe, [], {cwd: buildDir, env: {...process.env, ER_AOT_SHOT: shot}});
|
|
132
145
|
|
|
133
146
|
// The frame must exist and show more than a blank screen.
|
|
134
147
|
if (!existsSync(shot)) {
|
|
@@ -143,8 +156,9 @@ function smokeTest(demo) {
|
|
|
143
156
|
}
|
|
144
157
|
|
|
145
158
|
let failures = 0;
|
|
146
|
-
mkdirSync(
|
|
159
|
+
mkdirSync(temporaryDirectory, {recursive: true});
|
|
147
160
|
ensureConfigured();
|
|
161
|
+
run('cmake', ['--build', buildDirectory]);
|
|
148
162
|
|
|
149
163
|
for (const demo of DEMOS) {
|
|
150
164
|
let failure;
|
package/aot/style-map.mjs
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { NAMED_COLORS } from '../src/embedded-react/named-colors.js';
|
|
20
|
+
import { numberProblem } from './compiler/check.mjs';
|
|
20
21
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
21
22
|
- Constants
|
|
22
23
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -37,14 +38,14 @@ const JUSTIFY = {
|
|
|
37
38
|
'space-evenly': 'ER_JUSTIFY_SPACE_EVENLY',
|
|
38
39
|
};
|
|
39
40
|
const FLEX_DIRECTION = {
|
|
40
|
-
column: '
|
|
41
|
+
column: 'ER_FLEX_COLUMN',
|
|
41
42
|
row: 'ER_FLEX_ROW',
|
|
42
43
|
'row-reverse': 'ER_FLEX_ROW_REVERSE',
|
|
43
|
-
'column-reverse': '
|
|
44
|
+
'column-reverse': 'ER_FLEX_COLUMN_REVERSE',
|
|
44
45
|
};
|
|
45
46
|
const POSITION = {
|
|
46
|
-
relative: '
|
|
47
|
-
absolute: '
|
|
47
|
+
relative: 'ER_POSITION_RELATIVE',
|
|
48
|
+
absolute: 'ER_POSITION_ABSOLUTE',
|
|
48
49
|
};
|
|
49
50
|
const DISPLAY = { flex: 'ER_DISPLAY_FLEX', none: 'ER_DISPLAY_NONE' };
|
|
50
51
|
/** Maps a JSX host tag to its ERNodeType enum, for the components the generic node emitter handles. */
|
|
@@ -57,7 +58,7 @@ export const NODE_TYPES = {
|
|
|
57
58
|
ScrollView: 'ER_NODE_SCROLL_VIEW',
|
|
58
59
|
};
|
|
59
60
|
/** Numeric style keys that can be state-driven and the ERProps field each one writes. */
|
|
60
|
-
const
|
|
61
|
+
const NUMBER_FIELDS = {
|
|
61
62
|
width: 'width',
|
|
62
63
|
height: 'height',
|
|
63
64
|
minWidth: 'min_width',
|
|
@@ -96,7 +97,7 @@ const NUM_FIELDS = {
|
|
|
96
97
|
};
|
|
97
98
|
/**
|
|
98
99
|
* Enum style keys that can be state-driven: the value (a string literal or a ternary of them) lowers to the
|
|
99
|
-
* matching ER_* constant via its table. Changing one in
|
|
100
|
+
* matching ER_* constant via its table. Changing one in an update re-runs layout (props_hash covers it).
|
|
100
101
|
*/
|
|
101
102
|
const ENUM_FIELDS = {
|
|
102
103
|
flexDirection: { field: 'flex_direction', table: FLEX_DIRECTION },
|
|
@@ -107,12 +108,12 @@ const ENUM_FIELDS = {
|
|
|
107
108
|
display: { field: 'display', table: DISPLAY },
|
|
108
109
|
};
|
|
109
110
|
/**
|
|
110
|
-
* Every style key whose value can be state-driven, with its ERProps field and how
|
|
111
|
+
* Every style key whose value can be state-driven, with its ERProps field and how an update lowers it (see
|
|
111
112
|
* DynamicStyleField). Keys absent here (such as the `flex` shorthand, `flexBasis`, the four insets and
|
|
112
113
|
* `fontWeight`) can only be static.
|
|
113
114
|
*/
|
|
114
115
|
export const DYN_FIELDS = {
|
|
115
|
-
...Object.fromEntries(Object.entries(
|
|
116
|
+
...Object.fromEntries(Object.entries(NUMBER_FIELDS).map(([styleKey, field]) => [
|
|
116
117
|
styleKey,
|
|
117
118
|
{ field, kind: 'num' },
|
|
118
119
|
])),
|
|
@@ -125,6 +126,12 @@ export const DYN_FIELDS = {
|
|
|
125
126
|
borderColor: { field: 'border_color', kind: 'color' },
|
|
126
127
|
opacity: { field: 'opacity', kind: 'opacity' },
|
|
127
128
|
};
|
|
129
|
+
/** The engine's opacity for fully opaque, which a 0–1 opacity scales to. */
|
|
130
|
+
export const OPACITY_OPAQUE = 255;
|
|
131
|
+
/** The alpha of a color written without one. */
|
|
132
|
+
const ALPHA_OPAQUE = 0xff;
|
|
133
|
+
/** The lightest numeric fontWeight drawn bold. */
|
|
134
|
+
const BOLD_WEIGHT = 600;
|
|
128
135
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
129
136
|
- Implementation
|
|
130
137
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -159,7 +166,7 @@ export function parseColorValue(color) {
|
|
|
159
166
|
// Split out the channels; a 6-digit color is opaque.
|
|
160
167
|
let red, green, blue, alpha;
|
|
161
168
|
if (hex.length === 6) {
|
|
162
|
-
alpha =
|
|
169
|
+
alpha = ALPHA_OPAQUE;
|
|
163
170
|
red = parseInt(hex.slice(0, 2), 16);
|
|
164
171
|
green = parseInt(hex.slice(2, 4), 16);
|
|
165
172
|
blue = parseInt(hex.slice(4, 6), 16);
|
|
@@ -174,14 +181,19 @@ export function parseColorValue(color) {
|
|
|
174
181
|
return ((alpha << 24) | (red << 16) | (green << 8) | blue) >>> 0;
|
|
175
182
|
}
|
|
176
183
|
/**
|
|
177
|
-
*
|
|
184
|
+
* A number for a float field, checked.
|
|
178
185
|
*
|
|
179
|
-
* @param
|
|
186
|
+
* @param rawValue The value; anything `Number()` converts to a finite float.
|
|
180
187
|
*
|
|
181
|
-
* @returns
|
|
188
|
+
* @returns The number.
|
|
182
189
|
*/
|
|
183
|
-
export function
|
|
184
|
-
|
|
190
|
+
export function floatConstant(rawValue) {
|
|
191
|
+
// The check pass refuses what a float cannot hold; a value that reaches here unchecked fails the same way.
|
|
192
|
+
const problem = numberProblem(rawValue);
|
|
193
|
+
if (problem) {
|
|
194
|
+
throw problem;
|
|
195
|
+
}
|
|
196
|
+
return Number(rawValue);
|
|
185
197
|
}
|
|
186
198
|
/**
|
|
187
199
|
* Looks an enum style value up in its table.
|
|
@@ -201,7 +213,7 @@ const enumKey = (table, name) => (value) => {
|
|
|
201
213
|
return constant;
|
|
202
214
|
};
|
|
203
215
|
/**
|
|
204
|
-
* A pixel dimension as
|
|
216
|
+
* A pixel dimension as a constant.
|
|
205
217
|
*
|
|
206
218
|
* @param value The style value; must be a finite number.
|
|
207
219
|
*
|
|
@@ -211,19 +223,20 @@ const dimension = (value) => {
|
|
|
211
223
|
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
212
224
|
throw new Error(`expected a numeric dimension, got ${JSON.stringify(value)}`);
|
|
213
225
|
}
|
|
214
|
-
|
|
226
|
+
// Adding 0 turns -0 into 0, whose bits a float field keeps.
|
|
227
|
+
return Math.round(value) + 0;
|
|
215
228
|
};
|
|
216
229
|
/**
|
|
217
|
-
* A dimension that may be a percentage. `'50%'` → the engine's float `*
|
|
230
|
+
* A dimension that may be a percentage. `'50%'` → the engine's float `*_percent` field (% of the parent);
|
|
218
231
|
* a number → the pixel field. Used for width / height / flexBasis and the four insets, all of which the
|
|
219
232
|
* engine resolves at layout.
|
|
220
233
|
*
|
|
221
|
-
* @param
|
|
222
|
-
* @param
|
|
234
|
+
* @param pixelField ERProps pixel field (e.g. 'width').
|
|
235
|
+
* @param percentField ERProps percentage field (e.g. 'width_percent').
|
|
223
236
|
*
|
|
224
237
|
* @returns The lowering for that key.
|
|
225
238
|
*/
|
|
226
|
-
const
|
|
239
|
+
const percentOrPixels = (pixelField, percentField) => value => {
|
|
227
240
|
if (typeof value === 'string' && value.trim().endsWith('%')) {
|
|
228
241
|
const percent = parseFloat(value);
|
|
229
242
|
if (!Number.isFinite(percent)) {
|
|
@@ -231,148 +244,148 @@ const pctOrPx = (pxField, pctField) => value => {
|
|
|
231
244
|
}
|
|
232
245
|
// The engine reads a 0.0 percentage as "not set", so lower 0% to the pixel field, where 0 means the same.
|
|
233
246
|
if (percent === 0)
|
|
234
|
-
return [{ field:
|
|
235
|
-
|
|
236
|
-
const floatLiteral = Number.isInteger(percent)
|
|
237
|
-
? `${percent}.0f`
|
|
238
|
-
: `${percent}f`;
|
|
239
|
-
return [{ field: pctField, expr: floatLiteral }];
|
|
247
|
+
return [{ field: pixelField, value: 0 }];
|
|
248
|
+
return [{ field: percentField, value: percent }];
|
|
240
249
|
}
|
|
241
|
-
return [{ field:
|
|
250
|
+
return [{ field: pixelField, value: dimension(value) }];
|
|
242
251
|
};
|
|
243
252
|
/**
|
|
244
|
-
* Per-style-key lowering. Each entry maps a style value to one or more { field,
|
|
245
|
-
* (field = ERProps
|
|
253
|
+
* Per-style-key lowering. Each entry maps a style value to one or more { field, value } ERProps writes
|
|
254
|
+
* (field = ERProps member, value = the constant). `flex` expands to several fields.
|
|
246
255
|
*/
|
|
247
256
|
const KEYS = {
|
|
248
257
|
// Layout
|
|
249
|
-
width:
|
|
250
|
-
height:
|
|
251
|
-
flexBasis:
|
|
252
|
-
minWidth: width => [{ field: 'min_width',
|
|
253
|
-
maxWidth: width => [{ field: 'max_width',
|
|
254
|
-
minHeight: height => [{ field: 'min_height',
|
|
255
|
-
maxHeight: height => [{ field: 'max_height',
|
|
256
|
-
padding: padding => [{ field: 'padding',
|
|
258
|
+
width: percentOrPixels('width', 'width_percent'),
|
|
259
|
+
height: percentOrPixels('height', 'height_percent'),
|
|
260
|
+
flexBasis: percentOrPixels('flex_basis', 'flex_basis_percent'),
|
|
261
|
+
minWidth: width => [{ field: 'min_width', value: dimension(width) }],
|
|
262
|
+
maxWidth: width => [{ field: 'max_width', value: dimension(width) }],
|
|
263
|
+
minHeight: height => [{ field: 'min_height', value: dimension(height) }],
|
|
264
|
+
maxHeight: height => [{ field: 'max_height', value: dimension(height) }],
|
|
265
|
+
padding: padding => [{ field: 'padding', value: dimension(padding) }],
|
|
257
266
|
paddingHorizontal: padding => [
|
|
258
|
-
{ field: 'padding_horizontal',
|
|
267
|
+
{ field: 'padding_horizontal', value: dimension(padding) },
|
|
259
268
|
],
|
|
260
269
|
paddingVertical: padding => [
|
|
261
|
-
{ field: 'padding_vertical',
|
|
270
|
+
{ field: 'padding_vertical', value: dimension(padding) },
|
|
271
|
+
],
|
|
272
|
+
paddingLeft: padding => [{ field: 'padding_left', value: dimension(padding) }],
|
|
273
|
+
paddingTop: padding => [{ field: 'padding_top', value: dimension(padding) }],
|
|
274
|
+
paddingRight: padding => [
|
|
275
|
+
{ field: 'padding_right', value: dimension(padding) },
|
|
262
276
|
],
|
|
263
|
-
paddingLeft: padding => [{ field: 'padding_left', expr: dimension(padding) }],
|
|
264
|
-
paddingTop: padding => [{ field: 'padding_top', expr: dimension(padding) }],
|
|
265
|
-
paddingRight: padding => [{ field: 'padding_right', expr: dimension(padding) }],
|
|
266
277
|
paddingBottom: padding => [
|
|
267
|
-
{ field: 'padding_bottom',
|
|
278
|
+
{ field: 'padding_bottom', value: dimension(padding) },
|
|
268
279
|
],
|
|
269
|
-
margin: margin => [{ field: 'margin',
|
|
280
|
+
margin: margin => [{ field: 'margin', value: dimension(margin) }],
|
|
270
281
|
marginHorizontal: margin => [
|
|
271
|
-
{ field: 'margin_horizontal',
|
|
282
|
+
{ field: 'margin_horizontal', value: dimension(margin) },
|
|
272
283
|
],
|
|
273
284
|
marginVertical: margin => [
|
|
274
|
-
{ field: 'margin_vertical',
|
|
285
|
+
{ field: 'margin_vertical', value: dimension(margin) },
|
|
275
286
|
],
|
|
276
|
-
marginLeft: margin => [{ field: 'margin_left',
|
|
277
|
-
marginTop: margin => [{ field: 'margin_top',
|
|
278
|
-
marginRight: margin => [{ field: 'margin_right',
|
|
279
|
-
marginBottom: margin => [{ field: 'margin_bottom',
|
|
280
|
-
gap: gap => [{ field: 'gap',
|
|
281
|
-
rowGap: gap => [{ field: 'row_gap',
|
|
282
|
-
columnGap: gap => [{ field: 'column_gap',
|
|
283
|
-
flexGrow: grow => [{ field: 'flex_grow',
|
|
284
|
-
flexShrink: shrink => [{ field: 'flex_shrink',
|
|
287
|
+
marginLeft: margin => [{ field: 'margin_left', value: dimension(margin) }],
|
|
288
|
+
marginTop: margin => [{ field: 'margin_top', value: dimension(margin) }],
|
|
289
|
+
marginRight: margin => [{ field: 'margin_right', value: dimension(margin) }],
|
|
290
|
+
marginBottom: margin => [{ field: 'margin_bottom', value: dimension(margin) }],
|
|
291
|
+
gap: gap => [{ field: 'gap', value: dimension(gap) }],
|
|
292
|
+
rowGap: gap => [{ field: 'row_gap', value: dimension(gap) }],
|
|
293
|
+
columnGap: gap => [{ field: 'column_gap', value: dimension(gap) }],
|
|
294
|
+
flexGrow: grow => [{ field: 'flex_grow', value: dimension(grow) }],
|
|
295
|
+
flexShrink: shrink => [{ field: 'flex_shrink', value: dimension(shrink) }],
|
|
285
296
|
flexDirection: direction => [
|
|
286
297
|
{
|
|
287
298
|
field: 'flex_direction',
|
|
288
|
-
|
|
299
|
+
value: enumKey(FLEX_DIRECTION, 'flexDirection')(direction),
|
|
289
300
|
},
|
|
290
301
|
],
|
|
291
302
|
alignItems: alignment => [
|
|
292
|
-
{ field: 'align_items',
|
|
303
|
+
{ field: 'align_items', value: enumKey(ALIGN, 'alignItems')(alignment) },
|
|
293
304
|
],
|
|
294
305
|
alignSelf: alignment => [
|
|
295
|
-
{ field: 'align_self',
|
|
306
|
+
{ field: 'align_self', value: enumKey(ALIGN, 'alignSelf')(alignment) },
|
|
296
307
|
],
|
|
297
308
|
justifyContent: justification => [
|
|
298
309
|
{
|
|
299
310
|
field: 'justify_content',
|
|
300
|
-
|
|
311
|
+
value: enumKey(JUSTIFY, 'justifyContent')(justification),
|
|
301
312
|
},
|
|
302
313
|
],
|
|
303
314
|
// Positioning: `position: 'absolute'` takes a node out of flow; left/top/right/bottom are its anchors.
|
|
304
315
|
position: position => [
|
|
305
|
-
{ field: 'position',
|
|
316
|
+
{ field: 'position', value: enumKey(POSITION, 'position')(position) },
|
|
306
317
|
],
|
|
307
318
|
display: display => [
|
|
308
|
-
{ field: 'display',
|
|
319
|
+
{ field: 'display', value: enumKey(DISPLAY, 'display')(display) },
|
|
309
320
|
],
|
|
310
|
-
left:
|
|
311
|
-
top:
|
|
312
|
-
right:
|
|
313
|
-
bottom:
|
|
321
|
+
left: percentOrPixels('left', 'left_percent'),
|
|
322
|
+
top: percentOrPixels('top', 'top_percent'),
|
|
323
|
+
right: percentOrPixels('right', 'right_percent'),
|
|
324
|
+
bottom: percentOrPixels('bottom', 'bottom_percent'),
|
|
314
325
|
// `flex: n` → grow=n, shrink=1, basis=0 (RN semantics; matches native_ui_bridge apply_flex).
|
|
315
326
|
flex: flexValue => {
|
|
316
327
|
const factor = Number(flexValue);
|
|
317
328
|
if (factor > 0) {
|
|
318
329
|
return [
|
|
319
|
-
{ field: 'flex_grow',
|
|
320
|
-
{ field: 'flex_shrink',
|
|
321
|
-
{ field: 'flex_basis',
|
|
330
|
+
{ field: 'flex_grow', value: dimension(factor) },
|
|
331
|
+
{ field: 'flex_shrink', value: 1 },
|
|
332
|
+
{ field: 'flex_basis', value: 0 },
|
|
322
333
|
];
|
|
323
334
|
}
|
|
324
335
|
if (factor === 0) {
|
|
325
336
|
return [
|
|
326
|
-
{ field: 'flex_grow',
|
|
327
|
-
{ field: 'flex_shrink',
|
|
337
|
+
{ field: 'flex_grow', value: 0 },
|
|
338
|
+
{ field: 'flex_shrink', value: 0 },
|
|
328
339
|
];
|
|
329
340
|
}
|
|
330
341
|
return [
|
|
331
|
-
{ field: 'flex_grow',
|
|
332
|
-
{ field: 'flex_shrink',
|
|
342
|
+
{ field: 'flex_grow', value: 0 },
|
|
343
|
+
{ field: 'flex_shrink', value: 1 },
|
|
333
344
|
];
|
|
334
345
|
},
|
|
335
346
|
// View visual
|
|
336
347
|
backgroundColor: color => [
|
|
337
|
-
{ field: 'background_color',
|
|
348
|
+
{ field: 'background_color', value: parseColorValue(color) },
|
|
338
349
|
],
|
|
339
|
-
borderRadius: radius => [{ field: 'border_radius',
|
|
350
|
+
borderRadius: radius => [{ field: 'border_radius', value: dimension(radius) }],
|
|
340
351
|
// Per-corner radii; the engine reads 0 in one of these as "use borderRadius" (same as Flow A).
|
|
341
352
|
borderTopLeftRadius: radius => [
|
|
342
|
-
{ field: 'border_top_left_radius',
|
|
353
|
+
{ field: 'border_top_left_radius', value: dimension(radius) },
|
|
343
354
|
],
|
|
344
355
|
borderTopRightRadius: radius => [
|
|
345
|
-
{ field: 'border_top_right_radius',
|
|
356
|
+
{ field: 'border_top_right_radius', value: dimension(radius) },
|
|
346
357
|
],
|
|
347
358
|
borderBottomRightRadius: radius => [
|
|
348
|
-
{ field: 'border_bottom_right_radius',
|
|
359
|
+
{ field: 'border_bottom_right_radius', value: dimension(radius) },
|
|
349
360
|
],
|
|
350
361
|
borderBottomLeftRadius: radius => [
|
|
351
|
-
{ field: 'border_bottom_left_radius',
|
|
362
|
+
{ field: 'border_bottom_left_radius', value: dimension(radius) },
|
|
363
|
+
],
|
|
364
|
+
borderWidth: width => [{ field: 'border_width', value: dimension(width) }],
|
|
365
|
+
borderColor: color => [
|
|
366
|
+
{ field: 'border_color', value: parseColorValue(color) },
|
|
352
367
|
],
|
|
353
|
-
borderWidth: width => [{ field: 'border_width', expr: dimension(width) }],
|
|
354
|
-
borderColor: color => [{ field: 'border_color', expr: colorLiteral(color) }],
|
|
355
368
|
opacity: opacity => [
|
|
356
369
|
{
|
|
357
370
|
field: 'opacity',
|
|
358
|
-
|
|
371
|
+
value: Math.round(Math.max(0, Math.min(1, Number(opacity))) * OPACITY_OPAQUE),
|
|
359
372
|
},
|
|
360
373
|
],
|
|
361
|
-
zIndex: zIndex => [{ field: 'z_index',
|
|
374
|
+
zIndex: zIndex => [{ field: 'z_index', value: dimension(zIndex) }],
|
|
362
375
|
// Text
|
|
363
|
-
color: color => [{ field: 'color',
|
|
364
|
-
fontSize: size => [{ field: 'font_size',
|
|
376
|
+
color: color => [{ field: 'color', value: parseColorValue(color) }],
|
|
377
|
+
fontSize: size => [{ field: 'font_size', value: dimension(size) }],
|
|
365
378
|
fontWeight: weight => [
|
|
366
379
|
{
|
|
367
380
|
field: 'font_weight',
|
|
368
|
-
|
|
381
|
+
value: weight === 'bold' || Number(weight) >= BOLD_WEIGHT ? 1 : 0,
|
|
369
382
|
},
|
|
370
383
|
],
|
|
371
384
|
lineHeight: lineHeight => [
|
|
372
|
-
{ field: 'line_height',
|
|
385
|
+
{ field: 'line_height', value: dimension(lineHeight) },
|
|
373
386
|
],
|
|
374
387
|
letterSpacing: spacing => [
|
|
375
|
-
{ field: 'letter_spacing',
|
|
388
|
+
{ field: 'letter_spacing', value: dimension(spacing) },
|
|
376
389
|
],
|
|
377
390
|
};
|
|
378
391
|
/**
|
|
@@ -387,9 +400,8 @@ export const isStyleKey = (key) => Object.hasOwn(KEYS, key);
|
|
|
387
400
|
export const STYLE_KEYS = Object.keys(KEYS);
|
|
388
401
|
/**
|
|
389
402
|
* Lowers one flattened style object to a list of ERProps field assignments. This is the build-time mirror
|
|
390
|
-
* of what native_ui_bridge.c does at runtime in Flow A: the
|
|
391
|
-
*
|
|
392
|
-
* (er_props_default), so both render the same pixels. An unsupported key throws, so the compiler fails
|
|
403
|
+
* of what native_ui_bridge.c does at runtime in Flow A: the package builds the same prop bags as Flow A,
|
|
404
|
+
* from the same defaults (er_props_default), so both render the same pixels. An unsupported key throws, so the compiler fails
|
|
393
405
|
* loudly instead of silently dropping a style.
|
|
394
406
|
*
|
|
395
407
|
* @param style Flattened style (plain key→value; values already statically resolved).
|