@mun-ui/ui 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +114 -2
- package/bin/compile.mjs +38 -0
- package/bin/desktop.mjs +143 -0
- package/bin/dev-protocol.mjs +94 -0
- package/bin/formatter.mjs +109 -0
- package/bin/hot-reload.mjs +38 -0
- package/bin/mun.mjs +538 -0
- package/bin/native.mjs +133 -0
- package/bin/pnpm-workspace.mjs +58 -0
- package/bin/program-patch.mjs +104 -0
- package/bin/project.mjs +322 -0
- package/bin/watch.mjs +316 -0
- package/bin/workflow.mjs +261 -0
- package/dist/astro.d.ts +3 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +3 -0
- package/dist/astro.js.map +1 -0
- package/dist/closures.d.ts +2 -0
- package/dist/closures.d.ts.map +1 -0
- package/dist/closures.js +2 -0
- package/dist/closures.js.map +1 -0
- package/dist/compiler/index.d.ts +2 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/compiler/index.js +2 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/core.d.ts +3 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +3 -0
- package/dist/core.js.map +1 -0
- package/dist/experimental.d.ts +2 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +2 -0
- package/dist/experimental.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +2 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/legacy.d.ts +3 -0
- package/dist/legacy.d.ts.map +1 -0
- package/dist/legacy.js +3 -0
- package/dist/legacy.js.map +1 -0
- package/dist/mun.d.ts +5 -0
- package/dist/mun.d.ts.map +1 -0
- package/dist/mun.js +4 -0
- package/dist/mun.js.map +1 -0
- package/dist/progressive-blur/index.d.ts +8 -0
- package/dist/progressive-blur/index.d.ts.map +1 -0
- package/dist/progressive-blur/index.js +8 -0
- package/dist/progressive-blur/index.js.map +1 -0
- package/dist/progressive-blur/plan.d.ts +3 -0
- package/dist/progressive-blur/plan.d.ts.map +1 -0
- package/dist/progressive-blur/plan.js +3 -0
- package/dist/progressive-blur/plan.js.map +1 -0
- package/dist/react-types.d.ts +2 -0
- package/dist/react-types.d.ts.map +1 -0
- package/dist/react-types.js +2 -0
- package/dist/react-types.js.map +1 -0
- package/dist/rendering-quality.d.ts +4 -0
- package/dist/rendering-quality.d.ts.map +1 -0
- package/dist/rendering-quality.js +4 -0
- package/dist/rendering-quality.js.map +1 -0
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +2 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vite.d.ts +2 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +2 -0
- package/dist/vite.js.map +1 -0
- package/editors/install.mjs +102 -0
- package/editors/lsp/mun-lsp.mjs +123 -0
- package/editors/lsp/service.mjs +306 -0
- package/editors/lsp/source.mjs +58 -0
- package/editors/vscode/client.mjs +40 -0
- package/editors/vscode/dev-client.mjs +83 -0
- package/editors/vscode/discovery.mjs +46 -0
- package/editors/vscode/export.mjs +42 -0
- package/editors/vscode/extension.cjs +226 -0
- package/editors/vscode/language-configuration.json +15 -0
- package/editors/vscode/package.json +119 -0
- package/editors/vscode/protocol.mjs +29 -0
- package/editors/vscode/syntaxes/mun.tmLanguage.json +130 -0
- package/native/Cargo.lock +3581 -0
- package/native/Cargo.toml +21 -0
- package/native/README.md +21 -0
- package/native/mun-native/Cargo.toml +23 -0
- package/native/mun-native/src/accessibility.rs +738 -0
- package/native/mun-native/src/dev.rs +604 -0
- package/native/mun-native/src/host_services.rs +202 -0
- package/native/mun-native/src/lib.rs +2843 -0
- package/native/mun-native/src/main.rs +204 -0
- package/native/mun-native/src/offscreen.rs +393 -0
- package/native/mun-native/src/rect.wgsl +63 -0
- package/native/mun-native/src/text.rs +401 -0
- package/native/mun-native/tests/offscreen_rendering.rs +161 -0
- package/native/mun-native/tests/stress.rs +408 -0
- package/native/mun-native/tests/window_lifecycle.rs +183 -0
- package/native/mun-runtime/Cargo.toml +13 -0
- package/native/mun-runtime/src/accessibility.rs +193 -0
- package/native/mun-runtime/src/collection.rs +1090 -0
- package/native/mun-runtime/src/gesture.rs +1070 -0
- package/native/mun-runtime/src/input.rs +325 -0
- package/native/mun-runtime/src/ir.rs +852 -0
- package/native/mun-runtime/src/kinetics.rs +500 -0
- package/native/mun-runtime/src/layout.rs +258 -0
- package/native/mun-runtime/src/layout_cache.rs +209 -0
- package/native/mun-runtime/src/lib.rs +52 -0
- package/native/mun-runtime/src/motion.rs +1162 -0
- package/native/mun-runtime/src/retained.rs +574 -0
- package/native/mun-runtime/src/runtime.rs +7449 -0
- package/native/mun-runtime/src/scene.rs +567 -0
- package/native/mun-runtime/src/scroll.rs +521 -0
- package/native/mun-runtime/src/scroll_view.rs +220 -0
- package/native/mun-runtime/src/text_edit.rs +670 -0
- package/native/mun-runtime/src/timeline.rs +1534 -0
- package/native/mun-runtime/src/validate.rs +677 -0
- package/native/mun-runtime/tests/accessibility_semantics.rs +168 -0
- package/native/mun-runtime/tests/compiler_contract.rs +110 -0
- package/native/mun-runtime/tests/flexible_frames.rs +47 -0
- package/native/mun-runtime/tests/host_services.rs +107 -0
- package/native/mun-runtime/tests/hot_update.rs +367 -0
- package/native/mun-runtime/tests/incremental_runtime.rs +323 -0
- package/native/mun-runtime/tests/ir_validation.rs +167 -0
- package/native/mun-runtime/tests/keyed_collections.rs +251 -0
- package/native/mun-runtime/tests/minimum_window_size.rs +80 -0
- package/native/mun-runtime/tests/native_controls_style.rs +491 -0
- package/native/mun-runtime/tests/native_demo.rs +285 -0
- package/native/mun-runtime/tests/native_layout_style.rs +175 -0
- package/native/mun-runtime/tests/native_settings_pane.rs +152 -0
- package/native/mun-runtime/tests/priority_proposals.rs +69 -0
- package/native/mun-runtime/tests/scroll_view.rs +403 -0
- package/native/mun-runtime/tests/text_editing.rs +491 -0
- package/native/mun-runtime/tests/text_proposals.rs +42 -0
- package/native/mun-runtime/tests/typography.rs +63 -0
- package/package.json +213 -3
- package/schemas/README.md +19 -0
- package/schemas/semantic-ui-ir-v1.schema.json +2061 -0
- package/templates/desktop/gitignore +5 -0
- package/templates/desktop/package.json +10 -0
- package/templates/desktop/src-tauri/Cargo.toml +14 -0
- package/templates/desktop/src-tauri/build.rs +1 -0
- package/templates/desktop/src-tauri/capabilities/main.json +12 -0
- package/templates/desktop/src-tauri/icons/icon.icns +0 -0
- package/templates/desktop/src-tauri/icons/icon.ico +0 -0
- package/templates/desktop/src-tauri/icons/icon.png +0 -0
- package/templates/desktop/src-tauri/src/main.rs +2 -0
- package/templates/desktop/src-tauri/tauri.conf.json +42 -0
- package/templates/desktop/vite.config.ts +10 -0
- package/templates/native/App.mun +15 -0
- package/templates/native/gitignore +3 -0
- package/templates/native/mun.toml +8 -0
- package/templates/project/gitignore +5 -0
- package/templates/project/index.html +13 -0
- package/templates/project/package.json +24 -0
- package/templates/project/src/App.mun +13 -0
- package/templates/project/src/index.css +30 -0
- package/templates/project/src/main.ts +5 -0
- package/templates/project/tsconfig.json +20 -0
- package/templates/project/vite.config.ts +6 -0
- package/templates/vite/src/App.css +134 -0
- package/templates/vite/src/App.tsx +91 -0
- package/templates/vite/src/index.css +23 -0
- package/templates/vite/vite.config.ts +10 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
//! Renderer-neutral intrinsic measurement boundary for native layout.
|
|
2
|
+
//!
|
|
3
|
+
//! Taffy remains an adapter below Mün semantics. Exact glyph/control metrics may
|
|
4
|
+
//! come from a native backend, but those backends expose only semantic intrinsic
|
|
5
|
+
//! sizes here rather than leaking shaping or renderer objects into the runtime.
|
|
6
|
+
|
|
7
|
+
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
8
|
+
pub struct IntrinsicSize {
|
|
9
|
+
pub width: f32,
|
|
10
|
+
pub height: f32,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
impl IntrinsicSize {
|
|
14
|
+
pub const fn new(width: f32, height: f32) -> Self {
|
|
15
|
+
Self { width, height }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// Retained layout input for a leaf. Text keeps its content as well as its
|
|
20
|
+
/// unconstrained size, so the Taffy measurement callback can answer a width
|
|
21
|
+
/// proposal instead of reusing a fixed single-line measurement.
|
|
22
|
+
#[derive(Clone, Debug, PartialEq)]
|
|
23
|
+
pub(crate) enum LeafMeasure {
|
|
24
|
+
Intrinsic(IntrinsicSize),
|
|
25
|
+
Text {
|
|
26
|
+
content: String,
|
|
27
|
+
natural: IntrinsicSize,
|
|
28
|
+
style: TextStyle,
|
|
29
|
+
line_limit: Option<usize>,
|
|
30
|
+
},
|
|
31
|
+
/// A leaf in a flexible frame. Like a SwiftUI view with `maxWidth:
|
|
32
|
+
/// .infinity`, it takes the space it is offered, so on its flexible axes
|
|
33
|
+
/// (`[width, height]`) its intrinsic size is its ideal size only: under a
|
|
34
|
+
/// min-content proposal it reports 0 there and never holds a stack (or the
|
|
35
|
+
/// window) open. An authored `minWidth` / `minHeight` still applies.
|
|
36
|
+
Flexible {
|
|
37
|
+
measure: Box<LeafMeasure>,
|
|
38
|
+
axes: [bool; 2],
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
impl LeafMeasure {
|
|
43
|
+
pub(crate) fn is_text(&self) -> bool {
|
|
44
|
+
match self {
|
|
45
|
+
Self::Text { .. } => true,
|
|
46
|
+
Self::Intrinsic(_) => false,
|
|
47
|
+
Self::Flexible { measure, .. } => measure.is_text(),
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Shaped geometry of one single-line text run, exposed by a text backend.
|
|
53
|
+
///
|
|
54
|
+
/// `carets` holds one `(scalar offset, x)` stop for every extended grapheme
|
|
55
|
+
/// boundary of the shaped text, sorted by offset, with `x` in unscaled logical
|
|
56
|
+
/// points from the run's origin. The runtime derives caret, selection, preedit
|
|
57
|
+
/// and pointer-to-offset mapping from these stops; backends never own editing state.
|
|
58
|
+
#[derive(Clone, Debug, Default, PartialEq)]
|
|
59
|
+
pub struct TextLineLayout {
|
|
60
|
+
pub carets: Vec<(usize, f32)>,
|
|
61
|
+
pub width: f32,
|
|
62
|
+
pub line_height: f32,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
impl TextLineLayout {
|
|
66
|
+
/// Caret x for a scalar offset, using the nearest stop at or before it.
|
|
67
|
+
pub fn x_for_offset(&self, offset: usize) -> f32 {
|
|
68
|
+
self.carets
|
|
69
|
+
.iter()
|
|
70
|
+
.rev()
|
|
71
|
+
.find(|(stop, _)| *stop <= offset)
|
|
72
|
+
.or(self.carets.first())
|
|
73
|
+
.map_or(0.0, |(_, x)| *x)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// The grapheme boundary whose caret position is visually nearest to `x`.
|
|
77
|
+
pub fn offset_for_x(&self, x: f32) -> usize {
|
|
78
|
+
self.carets
|
|
79
|
+
.iter()
|
|
80
|
+
.min_by(|(_, a), (_, b)| (a - x).abs().total_cmp(&(b - x).abs()))
|
|
81
|
+
.map_or(0, |(offset, _)| *offset)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// Visual horizontal extent covering every boundary inside `range`.
|
|
85
|
+
pub fn span(&self, range: std::ops::Range<usize>) -> Option<(f32, f32)> {
|
|
86
|
+
if range.is_empty() {
|
|
87
|
+
return None;
|
|
88
|
+
}
|
|
89
|
+
let xs = self
|
|
90
|
+
.carets
|
|
91
|
+
.iter()
|
|
92
|
+
.filter(|(offset, _)| range.contains(offset) || *offset == range.end)
|
|
93
|
+
.map(|(_, x)| *x);
|
|
94
|
+
let (min, max) = xs.fold((f32::INFINITY, f32::NEG_INFINITY), |(lo, hi), x| {
|
|
95
|
+
(lo.min(x), hi.max(x))
|
|
96
|
+
});
|
|
97
|
+
(min.is_finite() && max > min).then_some((min, max))
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// Supplies backend-derived intrinsic sizes for semantic leaf content.
|
|
102
|
+
///
|
|
103
|
+
/// Measurements are intentionally unconstrained content sizes. Explicit Mün
|
|
104
|
+
/// width/height constraints and container alignment remain authoritative in
|
|
105
|
+
/// the Taffy adapter.
|
|
106
|
+
/// Font request for shaping one Text: renderer-neutral metrics only.
|
|
107
|
+
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
108
|
+
pub struct TextStyle {
|
|
109
|
+
pub size: f32,
|
|
110
|
+
/// CSS/OpenType weight 100..=900.
|
|
111
|
+
pub weight: u16,
|
|
112
|
+
pub italic: bool,
|
|
113
|
+
pub design: crate::ir::UiFontDesign,
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
impl TextStyle {
|
|
117
|
+
pub const fn body(size: f32) -> Self {
|
|
118
|
+
Self {
|
|
119
|
+
size,
|
|
120
|
+
weight: 400,
|
|
121
|
+
italic: false,
|
|
122
|
+
design: crate::ir::UiFontDesign::Default,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pub fn line_height(&self) -> f32 {
|
|
127
|
+
self.size * 1.25
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pub trait IntrinsicMeasurer {
|
|
132
|
+
fn measure_text(&self, text: &str) -> IntrinsicSize;
|
|
133
|
+
|
|
134
|
+
/// Measure a styled Text at an optional width proposal and line limit.
|
|
135
|
+
/// The default scales the unstyled single-size model so headless tooling
|
|
136
|
+
/// keeps working; native renderers shape with the real font.
|
|
137
|
+
fn measure_styled_text(
|
|
138
|
+
&self,
|
|
139
|
+
text: &str,
|
|
140
|
+
style: TextStyle,
|
|
141
|
+
available_width: Option<f32>,
|
|
142
|
+
line_limit: Option<usize>,
|
|
143
|
+
) -> IntrinsicSize {
|
|
144
|
+
let base =
|
|
145
|
+
self.measure_text_for_width(text, available_width.map(|w| w * 24.0 / style.size));
|
|
146
|
+
let scale = style.size / 24.0;
|
|
147
|
+
let mut height = base.height * scale;
|
|
148
|
+
if let Some(limit) = line_limit {
|
|
149
|
+
height = height.min((limit.max(1) as f32 * style.line_height() + 2.0).ceil());
|
|
150
|
+
}
|
|
151
|
+
IntrinsicSize::new((base.width * scale).ceil(), height.ceil())
|
|
152
|
+
}
|
|
153
|
+
/// Return the text's proposed size when laid out at the available width.
|
|
154
|
+
/// `None` means an unspecified width (its ideal, unwrapped measurement).
|
|
155
|
+
/// Default measurers preserve existing single-line headless behavior.
|
|
156
|
+
fn measure_text_for_width(&self, text: &str, available_width: Option<f32>) -> IntrinsicSize {
|
|
157
|
+
let _ = available_width;
|
|
158
|
+
self.measure_text(text)
|
|
159
|
+
}
|
|
160
|
+
fn measure_action(&self, label: &str) -> IntrinsicSize;
|
|
161
|
+
fn measure_panel(&self) -> IntrinsicSize;
|
|
162
|
+
|
|
163
|
+
fn measure_text_field(&self, value: &str, placeholder: Option<&str>) -> IntrinsicSize {
|
|
164
|
+
let text = if value.is_empty() {
|
|
165
|
+
placeholder.unwrap_or("")
|
|
166
|
+
} else {
|
|
167
|
+
value
|
|
168
|
+
};
|
|
169
|
+
let measured = self.measure_action(text);
|
|
170
|
+
IntrinsicSize::new(measured.width.max(180.0), measured.height)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// Single-line shaped geometry for editing presentation and pointer mapping.
|
|
174
|
+
///
|
|
175
|
+
/// The default is a deterministic headless model (uniform advance per
|
|
176
|
+
/// grapheme cluster) for tests and IR tooling without a font backend. Native
|
|
177
|
+
/// renderers must override it with real shaping data.
|
|
178
|
+
fn text_line(&self, text: &str, font_size: f32) -> TextLineLayout {
|
|
179
|
+
let advance = font_size * 0.6;
|
|
180
|
+
let carets = crate::text_edit::grapheme_boundaries(text)
|
|
181
|
+
.into_iter()
|
|
182
|
+
.enumerate()
|
|
183
|
+
.map(|(index, offset)| (offset, index as f32 * advance))
|
|
184
|
+
.collect::<Vec<_>>();
|
|
185
|
+
TextLineLayout {
|
|
186
|
+
width: carets.last().map_or(0.0, |(_, x)| *x),
|
|
187
|
+
carets,
|
|
188
|
+
line_height: font_size * 1.25,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// Checkbox-style toggle: a 14pt box, a 6pt gap and the label line.
|
|
193
|
+
fn measure_toggle(&self, label: &str) -> IntrinsicSize {
|
|
194
|
+
let line = self.text_line(label, 16.0);
|
|
195
|
+
IntrinsicSize::new(
|
|
196
|
+
(line.width + 20.0).ceil(),
|
|
197
|
+
line.line_height.max(22.0).ceil(),
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// Linear progress: an optional label line above a 6pt track.
|
|
202
|
+
fn measure_progress(&self, label: Option<&str>) -> IntrinsicSize {
|
|
203
|
+
let label = label.map_or(0.0, |label| self.text_line(label, 16.0).line_height + 4.0);
|
|
204
|
+
IntrinsicSize::new(160.0, (label + 6.0).ceil())
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn measure_radio_group(&self, labels: &[String]) -> IntrinsicSize {
|
|
208
|
+
let width = labels
|
|
209
|
+
.iter()
|
|
210
|
+
.map(|label| self.measure_action(label).width)
|
|
211
|
+
.fold(120.0, f32::max);
|
|
212
|
+
IntrinsicSize::new(width, (labels.len().max(1) as f32) * 30.0)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/// Compatibility fallback used when no native backend measurer is supplied.
|
|
217
|
+
///
|
|
218
|
+
/// These preserve the pre-boundary runtime metrics exactly. Native backends may
|
|
219
|
+
/// replace them with glyph/control measurements through the runtime measurement API.
|
|
220
|
+
#[derive(Clone, Copy, Debug, Default)]
|
|
221
|
+
pub struct FallbackIntrinsicMeasurer;
|
|
222
|
+
|
|
223
|
+
impl IntrinsicMeasurer for FallbackIntrinsicMeasurer {
|
|
224
|
+
fn measure_text(&self, _text: &str) -> IntrinsicSize {
|
|
225
|
+
IntrinsicSize::new(240.0, 32.0)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
fn measure_action(&self, label: &str) -> IntrinsicSize {
|
|
229
|
+
IntrinsicSize::new((label.chars().count() as f32 * 9.0 + 34.0).max(92.0), 38.0)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
fn measure_panel(&self) -> IntrinsicSize {
|
|
233
|
+
IntrinsicSize::new(160.0, 96.0)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
#[cfg(test)]
|
|
238
|
+
mod tests {
|
|
239
|
+
use super::*;
|
|
240
|
+
|
|
241
|
+
#[test]
|
|
242
|
+
fn fallback_metrics_preserve_existing_runtime_baselines() {
|
|
243
|
+
let measurer = FallbackIntrinsicMeasurer;
|
|
244
|
+
assert_eq!(
|
|
245
|
+
measurer.measure_text("arbitrary"),
|
|
246
|
+
IntrinsicSize::new(240.0, 32.0)
|
|
247
|
+
);
|
|
248
|
+
assert_eq!(
|
|
249
|
+
measurer.measure_action("Go"),
|
|
250
|
+
IntrinsicSize::new(92.0, 38.0)
|
|
251
|
+
);
|
|
252
|
+
assert_eq!(
|
|
253
|
+
measurer.measure_action("Long action label"),
|
|
254
|
+
IntrinsicSize::new(187.0, 38.0)
|
|
255
|
+
);
|
|
256
|
+
assert_eq!(measurer.measure_panel(), IntrinsicSize::new(160.0, 96.0));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
//! Persistent Taffy tree reused across frames and compatible hot updates.
|
|
2
|
+
//!
|
|
3
|
+
//! Every frame the runtime still derives the complete layout input (style,
|
|
4
|
+
//! intrinsic measurement and children) of every live node from Mün semantics,
|
|
5
|
+
//! so nothing here can go stale: this module only diffs that input against the
|
|
6
|
+
//! retained Taffy nodes, keyed by semantic node id, and touches the nodes whose
|
|
7
|
+
//! input actually changed. Taffy's per-node layout cache then recomputes only
|
|
8
|
+
//! dirty nodes and their ancestors; an unchanged subtree is answered from cache.
|
|
9
|
+
use rustc_hash::FxHashMap;
|
|
10
|
+
use taffy::{TaffyError, prelude::*};
|
|
11
|
+
|
|
12
|
+
use crate::layout::LeafMeasure;
|
|
13
|
+
|
|
14
|
+
pub(crate) type LayoutTree = TaffyTree<LeafMeasure>;
|
|
15
|
+
/// Semantic node id -> retained layout node. Keys are long node-id strings
|
|
16
|
+
/// hashed for every node every frame, so a fast non-DoS hasher is used.
|
|
17
|
+
pub(crate) type LayoutNodes = FxHashMap<String, NodeId>;
|
|
18
|
+
|
|
19
|
+
/// Layout input of one node of the frame being built.
|
|
20
|
+
pub(crate) struct LayoutSpec<'a> {
|
|
21
|
+
pub id: &'a str,
|
|
22
|
+
pub style: Style,
|
|
23
|
+
/// Intrinsic size of a leaf; always `None` for a node with children.
|
|
24
|
+
pub context: Option<LeafMeasure>,
|
|
25
|
+
/// Indices into the frame's spec list.
|
|
26
|
+
pub children: Vec<usize>,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Work done by one [`LayoutCache::sync`] (structural counters, deterministic
|
|
30
|
+
/// for a given sequence of frames).
|
|
31
|
+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
32
|
+
pub struct LayoutSyncStats {
|
|
33
|
+
pub live: usize,
|
|
34
|
+
pub created: usize,
|
|
35
|
+
pub removed: usize,
|
|
36
|
+
pub restyled: usize,
|
|
37
|
+
pub remeasured: usize,
|
|
38
|
+
pub rechildren: usize,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
impl LayoutSyncStats {
|
|
42
|
+
/// Nodes whose own layout input changed this frame.
|
|
43
|
+
pub fn invalidated(&self) -> usize {
|
|
44
|
+
self.created + self.removed + self.restyled + self.remeasured + self.rechildren
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
pub(crate) struct LayoutCache {
|
|
49
|
+
pub taffy: LayoutTree,
|
|
50
|
+
pub nodes: LayoutNodes,
|
|
51
|
+
/// Window node proposing the window size to the root views.
|
|
52
|
+
wrapper: Option<NodeId>,
|
|
53
|
+
/// Nodes created for a semantic id already used in the same frame. Ids are
|
|
54
|
+
/// validated unique, so this stays empty; they are dropped next frame.
|
|
55
|
+
duplicates: Vec<NodeId>,
|
|
56
|
+
pub last_sync: LayoutSyncStats,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
impl Default for LayoutCache {
|
|
60
|
+
fn default() -> Self {
|
|
61
|
+
Self {
|
|
62
|
+
taffy: TaffyTree::new(),
|
|
63
|
+
nodes: LayoutNodes::default(),
|
|
64
|
+
wrapper: None,
|
|
65
|
+
duplicates: Vec::new(),
|
|
66
|
+
last_sync: LayoutSyncStats::default(),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
impl LayoutCache {
|
|
72
|
+
pub fn wrapper(&self) -> Option<NodeId> {
|
|
73
|
+
self.wrapper
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// Bring the retained tree in line with `specs` (children listed before
|
|
77
|
+
/// their parents) and the window `roots`.
|
|
78
|
+
pub fn sync(
|
|
79
|
+
&mut self,
|
|
80
|
+
specs: &[LayoutSpec<'_>],
|
|
81
|
+
roots: &[usize],
|
|
82
|
+
wrapper_style: Style,
|
|
83
|
+
) -> Result<(), TaffyError> {
|
|
84
|
+
let mut stats = LayoutSyncStats {
|
|
85
|
+
live: specs.len(),
|
|
86
|
+
..Default::default()
|
|
87
|
+
};
|
|
88
|
+
let mut previous = std::mem::take(&mut self.nodes);
|
|
89
|
+
self.nodes.reserve(specs.len());
|
|
90
|
+
let mut stale = std::mem::take(&mut self.duplicates);
|
|
91
|
+
let mut ids = Vec::with_capacity(specs.len());
|
|
92
|
+
for spec in specs {
|
|
93
|
+
let reused = if self.nodes.contains_key(spec.id) {
|
|
94
|
+
None
|
|
95
|
+
} else {
|
|
96
|
+
previous.remove_entry(spec.id)
|
|
97
|
+
};
|
|
98
|
+
let node = match reused {
|
|
99
|
+
Some((key, node)) => {
|
|
100
|
+
if self.taffy.style(node)? != &spec.style {
|
|
101
|
+
self.taffy.set_style(node, spec.style.clone())?;
|
|
102
|
+
self.invalidate(node)?;
|
|
103
|
+
stats.restyled += 1;
|
|
104
|
+
}
|
|
105
|
+
if self.taffy.get_node_context(node) != spec.context.as_ref() {
|
|
106
|
+
self.taffy.set_node_context(node, spec.context.clone())?;
|
|
107
|
+
self.invalidate(node)?;
|
|
108
|
+
stats.remeasured += 1;
|
|
109
|
+
}
|
|
110
|
+
self.nodes.insert(key, node);
|
|
111
|
+
node
|
|
112
|
+
}
|
|
113
|
+
None => {
|
|
114
|
+
let node = match &spec.context {
|
|
115
|
+
Some(context) => self
|
|
116
|
+
.taffy
|
|
117
|
+
.new_leaf_with_context(spec.style.clone(), context.clone())?,
|
|
118
|
+
None => self.taffy.new_leaf(spec.style.clone())?,
|
|
119
|
+
};
|
|
120
|
+
stats.created += 1;
|
|
121
|
+
if self.nodes.contains_key(spec.id) {
|
|
122
|
+
self.duplicates.push(node);
|
|
123
|
+
} else {
|
|
124
|
+
self.nodes.insert(spec.id.to_owned(), node);
|
|
125
|
+
}
|
|
126
|
+
node
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
ids.push(node);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Detach every stale node from its children first: a child may have
|
|
133
|
+
// moved to a live parent, and removing its old parent must not clear
|
|
134
|
+
// the new link.
|
|
135
|
+
stale.extend(previous.into_values());
|
|
136
|
+
stats.removed = stale.len();
|
|
137
|
+
for &node in &stale {
|
|
138
|
+
self.taffy.set_children(node, &[])?;
|
|
139
|
+
}
|
|
140
|
+
for node in stale {
|
|
141
|
+
if let Some(parent) = self.taffy.parent(node) {
|
|
142
|
+
self.invalidate(parent)?;
|
|
143
|
+
}
|
|
144
|
+
self.taffy.remove(node)?;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let mut children = Vec::new();
|
|
148
|
+
for (spec, &node) in specs.iter().zip(&ids) {
|
|
149
|
+
children.clear();
|
|
150
|
+
children.extend(spec.children.iter().map(|&child| ids[child]));
|
|
151
|
+
if self.set_children_if_changed(node, &children)? {
|
|
152
|
+
stats.rechildren += 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let wrapper = match self.wrapper {
|
|
157
|
+
Some(wrapper) => {
|
|
158
|
+
if self.taffy.style(wrapper)? != &wrapper_style {
|
|
159
|
+
self.taffy.set_style(wrapper, wrapper_style)?;
|
|
160
|
+
self.invalidate(wrapper)?;
|
|
161
|
+
}
|
|
162
|
+
wrapper
|
|
163
|
+
}
|
|
164
|
+
None => {
|
|
165
|
+
let wrapper = self.taffy.new_leaf(wrapper_style)?;
|
|
166
|
+
self.wrapper = Some(wrapper);
|
|
167
|
+
wrapper
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
children.clear();
|
|
171
|
+
children.extend(roots.iter().map(|&root| ids[root]));
|
|
172
|
+
self.set_children_if_changed(wrapper, &children)?;
|
|
173
|
+
self.last_sync = stats;
|
|
174
|
+
Ok(())
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fn set_children_if_changed(
|
|
178
|
+
&mut self,
|
|
179
|
+
node: NodeId,
|
|
180
|
+
children: &[NodeId],
|
|
181
|
+
) -> Result<bool, TaffyError> {
|
|
182
|
+
if self.taffy.child_ids(node).eq(children.iter().copied()) {
|
|
183
|
+
return Ok(false);
|
|
184
|
+
}
|
|
185
|
+
// A child moving here leaves its previous parent; that parent's layout
|
|
186
|
+
// depends on the child too.
|
|
187
|
+
for &child in children {
|
|
188
|
+
if let Some(parent) = self.taffy.parent(child).filter(|parent| *parent != node) {
|
|
189
|
+
self.invalidate(parent)?;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
self.taffy.set_children(node, children)?;
|
|
193
|
+
self.invalidate(node)?;
|
|
194
|
+
Ok(true)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// Clear the cached layout of `node` and of every ancestor. Taffy's own
|
|
198
|
+
/// propagation stops at a node whose cache is already empty; walking the
|
|
199
|
+
/// whole ancestor chain keeps an ancestor from answering from stale cache
|
|
200
|
+
/// even if some intermediate node was never laid out.
|
|
201
|
+
fn invalidate(&mut self, node: NodeId) -> Result<(), TaffyError> {
|
|
202
|
+
let mut current = Some(node);
|
|
203
|
+
while let Some(node) = current {
|
|
204
|
+
self.taffy.mark_dirty(node)?;
|
|
205
|
+
current = self.taffy.parent(node);
|
|
206
|
+
}
|
|
207
|
+
Ok(())
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//! Mün's native semantic runtime.
|
|
2
|
+
//!
|
|
3
|
+
//! This crate consumes backend-neutral UI IR. It owns state, transactions,
|
|
4
|
+
//! motion channels, layout adaptation, and retained scene construction. GPU,
|
|
5
|
+
//! window-system, and accessibility adapters live outside these semantics.
|
|
6
|
+
|
|
7
|
+
pub mod accessibility;
|
|
8
|
+
pub mod collection;
|
|
9
|
+
pub mod gesture;
|
|
10
|
+
pub mod input;
|
|
11
|
+
pub mod ir;
|
|
12
|
+
pub mod layout;
|
|
13
|
+
pub mod layout_cache;
|
|
14
|
+
pub mod motion;
|
|
15
|
+
pub mod retained;
|
|
16
|
+
pub mod runtime;
|
|
17
|
+
pub mod scene;
|
|
18
|
+
pub mod scroll;
|
|
19
|
+
pub mod scroll_view;
|
|
20
|
+
pub mod text_edit;
|
|
21
|
+
pub mod timeline;
|
|
22
|
+
pub mod validate;
|
|
23
|
+
|
|
24
|
+
pub use accessibility::{AccessibilityBounds, AccessibilityNode, AccessibilityTree};
|
|
25
|
+
pub use gesture::{
|
|
26
|
+
DragAxis, DragAxisConstraint, DragCancelPlan, DragRecognizer, DragRecognizerConfig,
|
|
27
|
+
DragReleaseOptions, DragReleasePlan, DragSettleSpec, DragState, GestureAxisRelease,
|
|
28
|
+
GesturePhase, VelocityTracker, VelocityTrackerConfig, constrain_with_rubber_band,
|
|
29
|
+
constrained_drag_axis_value, nearest_snap, plan_drag_axis_cancel, plan_drag_axis_release,
|
|
30
|
+
rubber_band_distance,
|
|
31
|
+
};
|
|
32
|
+
pub use input::{
|
|
33
|
+
ButtonState, InputEvent, InputOutcome, InputPoint, KeyState, LogicalKey, Modifiers,
|
|
34
|
+
PhysicalKey, PointerButton, PointerId, ScrollDelta, ScrollPhase,
|
|
35
|
+
};
|
|
36
|
+
pub use layout::{
|
|
37
|
+
FallbackIntrinsicMeasurer, IntrinsicMeasurer, IntrinsicSize, TextLineLayout, TextStyle,
|
|
38
|
+
};
|
|
39
|
+
pub use retained::{
|
|
40
|
+
RetainedIdentityKey, RetainedNode, RetainedNodeKind, RetainedNumberKey, RetainedReconciliation,
|
|
41
|
+
RetainedTree, RetainedTreeError,
|
|
42
|
+
};
|
|
43
|
+
pub use runtime::{
|
|
44
|
+
HostServices, HotUpdateReport, HotUpdateTimings, ImeRequest, PlatformConventions, Runtime,
|
|
45
|
+
RuntimeDiagnostic, RuntimeFrame, RuntimeLoadError, SEMANTIC_UI_IR_VERSION, StateMutation,
|
|
46
|
+
Transaction,
|
|
47
|
+
};
|
|
48
|
+
pub use scene::{Color, Rect, Scene, SceneTextStyle};
|
|
49
|
+
pub use scroll::{
|
|
50
|
+
NestedScrollRouteResult, ScrollAxis, ScrollContainerState, ScrollMetrics, ScrollRange,
|
|
51
|
+
ScrollRouteResult, ScrollState, ScrollTracker, route_nested_content_delta,
|
|
52
|
+
};
|