@mun-ui/ui 0.2.0 → 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.
Files changed (33) hide show
  1. package/README.md +11 -1
  2. package/bin/desktop.mjs +143 -0
  3. package/bin/mun.mjs +27 -10
  4. package/native/mun-native/src/host_services.rs +202 -0
  5. package/native/mun-native/src/lib.rs +209 -42
  6. package/native/mun-native/src/text.rs +107 -16
  7. package/native/mun-runtime/src/collection.rs +18 -1
  8. package/native/mun-runtime/src/ir.rs +71 -1
  9. package/native/mun-runtime/src/layout.rs +85 -0
  10. package/native/mun-runtime/src/layout_cache.rs +6 -6
  11. package/native/mun-runtime/src/lib.rs +7 -4
  12. package/native/mun-runtime/src/runtime.rs +388 -17
  13. package/native/mun-runtime/src/scene.rs +37 -0
  14. package/native/mun-runtime/tests/host_services.rs +107 -0
  15. package/native/mun-runtime/tests/minimum_window_size.rs +80 -0
  16. package/native/mun-runtime/tests/priority_proposals.rs +69 -0
  17. package/native/mun-runtime/tests/text_proposals.rs +42 -0
  18. package/native/mun-runtime/tests/typography.rs +63 -0
  19. package/package.json +19 -16
  20. package/schemas/semantic-ui-ir-v1.schema.json +89 -0
  21. package/templates/desktop/gitignore +5 -0
  22. package/templates/desktop/package.json +10 -0
  23. package/templates/desktop/src-tauri/Cargo.toml +14 -0
  24. package/templates/desktop/src-tauri/build.rs +1 -0
  25. package/templates/desktop/src-tauri/capabilities/main.json +12 -0
  26. package/templates/desktop/src-tauri/icons/icon.icns +0 -0
  27. package/templates/desktop/src-tauri/icons/icon.ico +0 -0
  28. package/templates/desktop/src-tauri/icons/icon.png +0 -0
  29. package/templates/desktop/src-tauri/src/main.rs +2 -0
  30. package/templates/desktop/src-tauri/tauri.conf.json +42 -0
  31. package/templates/desktop/vite.config.ts +10 -0
  32. package/templates/project/package.json +3 -0
  33. package/templates/project/src/index.css +9 -0
@@ -1,5 +1,6 @@
1
1
  mod accessibility;
2
2
  pub mod dev;
3
+ mod host_services;
3
4
  pub mod offscreen;
4
5
  mod text;
5
6
 
@@ -19,9 +20,9 @@ use mun_runtime::{
19
20
  ButtonState as MunButtonState, Color, ImeRequest, InputEvent, InputPoint,
20
21
  KeyState as MunKeyState, LogicalKey, Modifiers, PhysicalKey as MunPhysicalKey,
21
22
  PlatformConventions, PointerButton as MunPointerButton, PointerId, Runtime, RuntimeLoadError,
22
- Scene, ScrollDelta, ScrollPhase,
23
+ Scene, SceneTextStyle, ScrollDelta, ScrollPhase,
23
24
  };
24
- use text::{LINE_HEIGHT_FACTOR, TextShaping, text_attrs};
25
+ use text::{LINE_HEIGHT_FACTOR, TextShaping};
25
26
  use winit::{
26
27
  application::ApplicationHandler,
27
28
  dpi::{LogicalPosition, LogicalSize, PhysicalPosition},
@@ -142,8 +143,11 @@ struct CachedTextBuffer {
142
143
  buffer: Buffer,
143
144
  text: String,
144
145
  font_size: f32,
145
- /// Shaped advance width of the line (logical points).
146
+ wrap_width: Option<f32>,
147
+ style: SceneTextStyle,
148
+ /// Shaped content bounds in logical points (may span multiple lines).
146
149
  width: f32,
150
+ height: f32,
147
151
  }
148
152
 
149
153
  fn shaped_width(buffer: &Buffer) -> f32 {
@@ -153,48 +157,108 @@ fn shaped_width(buffer: &Buffer) -> f32 {
153
157
  .fold(0.0, f32::max)
154
158
  }
155
159
 
160
+ fn shaped_height(buffer: &Buffer, font_size: f32) -> f32 {
161
+ buffer.layout_runs().count().max(1) as f32 * font_size * LINE_HEIGHT_FACTOR
162
+ }
163
+
164
+ /// Visible height of a buffer limited to `line_limit` lines; cosmic-text stops
165
+ /// laying out runs below the buffer height, which is how `.lineLimit` clips.
166
+ fn limited_height(style: SceneTextStyle, font_size: f32) -> Option<f32> {
167
+ style
168
+ .line_limit
169
+ .map(|limit| limit.max(1) as f32 * font_size * LINE_HEIGHT_FACTOR + 1.0)
170
+ }
171
+
172
+ fn line_align(style: SceneTextStyle) -> Option<glyphon::cosmic_text::Align> {
173
+ use glyphon::cosmic_text::Align;
174
+ match style.alignment {
175
+ mun_runtime::ir::UiTextAlignment::Leading => None,
176
+ mun_runtime::ir::UiTextAlignment::Center => Some(Align::Center),
177
+ mun_runtime::ir::UiTextAlignment::Trailing => Some(Align::Right),
178
+ }
179
+ }
180
+
156
181
  impl CachedTextBuffer {
157
- fn new(font_system: &mut FontSystem, text: &str, font_size: f32) -> Self {
182
+ fn configure(
183
+ buffer: &mut Buffer,
184
+ text: &str,
185
+ font_size: f32,
186
+ wrap_width: Option<f32>,
187
+ style: SceneTextStyle,
188
+ ) {
189
+ // Ordinary Text uses the same proposal used during intrinsic layout.
190
+ // Editing and control labels intentionally keep unbounded single lines.
191
+ buffer.set_wrap(if wrap_width.is_some() {
192
+ Wrap::WordOrGlyph
193
+ } else {
194
+ Wrap::None
195
+ });
196
+ buffer.set_size(wrap_width, limited_height(style, font_size));
197
+ buffer.set_text(
198
+ text,
199
+ &text::styled_attrs(style.text_style(font_size)),
200
+ Shaping::Advanced,
201
+ None,
202
+ );
203
+ let align = line_align(style);
204
+ for line in &mut buffer.lines {
205
+ line.set_align(align);
206
+ }
207
+ }
208
+
209
+ fn new(
210
+ font_system: &mut FontSystem,
211
+ text: &str,
212
+ font_size: f32,
213
+ wrap_width: Option<f32>,
214
+ style: SceneTextStyle,
215
+ ) -> Self {
158
216
  let mut buffer = Buffer::new(
159
217
  font_system,
160
218
  Metrics::new(font_size, font_size * LINE_HEIGHT_FACTOR),
161
219
  );
162
- // Scene text is single-line in the runtime layout model; wrapping here
163
- // would desynchronize drawn glyphs from layout and caret geometry. The
164
- // buffer is unbounded: clipping belongs to text-area bounds and
165
- // scissors, so window resizes never reshape text.
166
- buffer.set_wrap(Wrap::None);
167
- buffer.set_size(None, None);
168
- buffer.set_text(text, &text_attrs(), Shaping::Advanced, None);
220
+ Self::configure(&mut buffer, text, font_size, wrap_width, style);
169
221
  buffer.shape_until_scroll(font_system, false);
170
222
  Self {
171
223
  width: shaped_width(&buffer),
224
+ height: shaped_height(&buffer, font_size),
172
225
  buffer,
173
226
  text: text.to_owned(),
174
227
  font_size,
228
+ wrap_width,
229
+ style,
175
230
  }
176
231
  }
177
232
 
178
- fn update(&mut self, font_system: &mut FontSystem, text: &str, font_size: f32) -> bool {
179
- let mut dirty = false;
233
+ fn update(
234
+ &mut self,
235
+ font_system: &mut FontSystem,
236
+ text: &str,
237
+ font_size: f32,
238
+ wrap_width: Option<f32>,
239
+ style: SceneTextStyle,
240
+ ) -> bool {
241
+ if self.font_size.to_bits() == font_size.to_bits()
242
+ && self.text == text
243
+ && self.wrap_width == wrap_width
244
+ && self.style == style
245
+ {
246
+ return false;
247
+ }
180
248
  if self.font_size.to_bits() != font_size.to_bits() {
181
249
  self.buffer
182
250
  .set_metrics(Metrics::new(font_size, font_size * LINE_HEIGHT_FACTOR));
183
251
  self.font_size = font_size;
184
- dirty = true;
185
- }
186
- if self.text != text {
187
- self.buffer
188
- .set_text(text, &text_attrs(), Shaping::Advanced, None);
189
- self.text.clear();
190
- self.text.push_str(text);
191
- dirty = true;
192
252
  }
193
- if dirty {
194
- self.buffer.shape_until_scroll(font_system, false);
195
- self.width = shaped_width(&self.buffer);
196
- }
197
- dirty
253
+ Self::configure(&mut self.buffer, text, font_size, wrap_width, style);
254
+ self.text.clear();
255
+ self.text.push_str(text);
256
+ self.wrap_width = wrap_width;
257
+ self.style = style;
258
+ self.buffer.shape_until_scroll(font_system, false);
259
+ self.width = shaped_width(&self.buffer);
260
+ self.height = shaped_height(&self.buffer, font_size);
261
+ true
198
262
  }
199
263
  }
200
264
 
@@ -624,15 +688,23 @@ impl GpuRenderer {
624
688
  };
625
689
  let transform = presentation.transform_for(&text.id);
626
690
  let margin = text.font_size;
627
- let bottom = text.y + text.font_size * LINE_HEIGHT_FACTOR + margin;
628
- let width = self
629
- .text_buffers
630
- .get(&text.id)
631
- .filter(|cached| {
632
- cached.text == text.text
633
- && cached.font_size.to_bits() == text.font_size.to_bits()
634
- })
635
- .map(|cached| cached.width);
691
+ let cached = self.text_buffers.get(&text.id).filter(|cached| {
692
+ cached.text == text.text
693
+ && cached.font_size.to_bits() == text.font_size.to_bits()
694
+ && cached.wrap_width == text.wrap_width
695
+ && cached.style == text.style
696
+ });
697
+ let width = cached.map(|cached| cached.width);
698
+ // A not-yet-shaped wrapped text may reach below its first
699
+ // line; don't cull it using a one-line height estimate.
700
+ let height = cached.map(|cached| cached.height).unwrap_or_else(|| {
701
+ if text.wrap_width.is_some() {
702
+ self.config.height as f32
703
+ } else {
704
+ text.font_size * LINE_HEIGHT_FACTOR
705
+ }
706
+ });
707
+ let bottom = text.y + height + margin;
636
708
  let (left, top) = transform.transform_point(text.x - margin, text.y - margin);
637
709
  let (right, bottom) =
638
710
  transform.transform_point(text.x + width.unwrap_or(0.0) + margin, bottom);
@@ -657,7 +729,13 @@ impl GpuRenderer {
657
729
  {
658
730
  match self.text_buffers.get_mut(&text.id) {
659
731
  Some(cached) => {
660
- if cached.update(&mut font_system, &text.text, text.font_size) {
732
+ if cached.update(
733
+ &mut font_system,
734
+ &text.text,
735
+ text.font_size,
736
+ text.wrap_width,
737
+ text.style,
738
+ ) {
661
739
  self.stats.text_reshapes += 1;
662
740
  }
663
741
  }
@@ -665,7 +743,13 @@ impl GpuRenderer {
665
743
  self.stats.text_reshapes += 1;
666
744
  self.text_buffers.insert(
667
745
  text.id.clone(),
668
- CachedTextBuffer::new(&mut font_system, &text.text, text.font_size),
746
+ CachedTextBuffer::new(
747
+ &mut font_system,
748
+ &text.text,
749
+ text.font_size,
750
+ text.wrap_width,
751
+ text.style,
752
+ ),
669
753
  );
670
754
  }
671
755
  }
@@ -1453,12 +1537,42 @@ mod tests {
1453
1537
  fn retained_text_buffer_reshapes_only_when_text_or_size_change() {
1454
1538
  // The window size is deliberately not an input: resizing never reshapes.
1455
1539
  let mut font_system = FontSystem::new();
1456
- let mut cached = CachedTextBuffer::new(&mut font_system, "Hello", 16.0);
1540
+ let mut cached = CachedTextBuffer::new(
1541
+ &mut font_system,
1542
+ "Hello",
1543
+ 16.0,
1544
+ None,
1545
+ SceneTextStyle::default(),
1546
+ );
1457
1547
 
1458
- assert!(!cached.update(&mut font_system, "Hello", 16.0));
1459
- assert!(cached.update(&mut font_system, "World", 16.0));
1460
- assert!(!cached.update(&mut font_system, "World", 16.0));
1461
- assert!(cached.update(&mut font_system, "World", 18.0));
1548
+ assert!(!cached.update(
1549
+ &mut font_system,
1550
+ "Hello",
1551
+ 16.0,
1552
+ None,
1553
+ SceneTextStyle::default()
1554
+ ));
1555
+ assert!(cached.update(
1556
+ &mut font_system,
1557
+ "World",
1558
+ 16.0,
1559
+ None,
1560
+ SceneTextStyle::default()
1561
+ ));
1562
+ assert!(!cached.update(
1563
+ &mut font_system,
1564
+ "World",
1565
+ 16.0,
1566
+ None,
1567
+ SceneTextStyle::default()
1568
+ ));
1569
+ assert!(cached.update(
1570
+ &mut font_system,
1571
+ "World",
1572
+ 18.0,
1573
+ None,
1574
+ SceneTextStyle::default()
1575
+ ));
1462
1576
  }
1463
1577
 
1464
1578
  #[test]
@@ -1754,9 +1868,54 @@ struct WindowState {
1754
1868
  /// `MUN_NATIVE_TRACE=1`: JSON-lines trace of platform input and resulting
1755
1869
  /// semantic state on stderr, for verifying manual platform sessions.
1756
1870
  trace: bool,
1871
+ minimum_size: MinimumSizeSync,
1757
1872
  window: Arc<Window>,
1758
1873
  }
1759
1874
 
1875
+ /// Keeps the window's minimum inner size at the layout-derived minimum
1876
+ /// (`Runtime::minimum_window_size`). The minimum is recomputed only when the
1877
+ /// runtime's state revision changes, when motion starts or settles (animated
1878
+ /// sizes are read at their presentation values), or after a hot update; never
1879
+ /// per frame. Sizes are logical, so a scale-factor change needs nothing.
1880
+ #[derive(Default)]
1881
+ struct MinimumSizeSync {
1882
+ key: Option<(u64, bool)>,
1883
+ applied: Option<(f32, f32)>,
1884
+ }
1885
+
1886
+ impl MinimumSizeSync {
1887
+ /// Recompute on the next sync (the program itself was replaced).
1888
+ fn invalidate(&mut self) {
1889
+ self.key = None;
1890
+ }
1891
+
1892
+ fn sync(&mut self, window: &Window, runtime: &Runtime) {
1893
+ let key = (runtime.revision(), runtime.has_active_motion());
1894
+ if self.key == Some(key) {
1895
+ return;
1896
+ }
1897
+ self.key = Some(key);
1898
+ let (width, height) = runtime.minimum_window_size();
1899
+ if self.applied.is_some_and(|(applied_width, applied_height)| {
1900
+ (applied_width - width).abs() < 0.5 && (applied_height - height).abs() < 0.5
1901
+ }) {
1902
+ return;
1903
+ }
1904
+ self.applied = Some((width, height));
1905
+ let (width, height) = (width as f64, height as f64);
1906
+ window.set_min_inner_size(Some(LogicalSize::new(width, height)));
1907
+ // A minimum only limits later resizes: grow a window that is already
1908
+ // smaller (initial size below the minimum, or content that grew).
1909
+ let current = window.inner_size().to_logical::<f64>(window.scale_factor());
1910
+ if current.width < width || current.height < height {
1911
+ let _ = window.request_inner_size(LogicalSize::new(
1912
+ current.width.max(width),
1913
+ current.height.max(height),
1914
+ ));
1915
+ }
1916
+ }
1917
+ }
1918
+
1760
1919
  /// Drop the platform input method's marked text after the runtime deliberately
1761
1920
  /// committed or cancelled a composition (pointer relocation, focus change).
1762
1921
  /// winit's `set_ime_allowed(false)` only clears its own copy on macOS; the
@@ -1789,6 +1948,11 @@ impl WindowState {
1789
1948
  let shaping = Rc::new(TextShaping::new(FontSystem::new()));
1790
1949
  runtime.set_intrinsic_measurer(shaping.clone());
1791
1950
  runtime.set_platform_conventions(PlatformConventions::native());
1951
+ runtime.set_host_services(Box::new(host_services::DesktopHostServices));
1952
+ // Needs the real text measurer installed above; the window is still
1953
+ // hidden, so clamping the initial size up is not visible.
1954
+ let mut minimum_size = MinimumSizeSync::default();
1955
+ minimum_size.sync(&window, &runtime);
1792
1956
  let scale_factor = window.scale_factor() as f32;
1793
1957
  let size = window.inner_size();
1794
1958
  let logical_width = size.width.max(1) as f32 / scale_factor;
@@ -1812,6 +1976,7 @@ impl WindowState {
1812
1976
  ime_allowed: false,
1813
1977
  ime_cursor_area: None,
1814
1978
  trace: std::env::var_os("MUN_NATIVE_TRACE").is_some_and(|value| value == "1"),
1979
+ minimum_size,
1815
1980
  window,
1816
1981
  })
1817
1982
  }
@@ -1984,6 +2149,7 @@ impl WindowState {
1984
2149
  self.last_frame = now;
1985
2150
  self.runtime.step(dt);
1986
2151
  let step_micros = micros_since(now);
2152
+ self.minimum_size.sync(&self.window, &self.runtime);
1987
2153
 
1988
2154
  let stage = Instant::now();
1989
2155
  let (width, height) = self.logical_size();
@@ -2166,6 +2332,7 @@ impl Application {
2166
2332
  Ok((report, timings)) => {
2167
2333
  if let Some(state) = &mut self.state {
2168
2334
  state.window.set_title(state.runtime.title());
2335
+ state.minimum_size.invalidate();
2169
2336
  state.sync_text_input();
2170
2337
  state.window.request_redraw();
2171
2338
  }
@@ -9,8 +9,8 @@ use std::{
9
9
  collections::HashMap,
10
10
  };
11
11
 
12
- use glyphon::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping, Wrap};
13
- use mun_runtime::{IntrinsicMeasurer, IntrinsicSize, TextLineLayout};
12
+ use glyphon::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping, Style, Weight, Wrap};
13
+ use mun_runtime::{IntrinsicMeasurer, IntrinsicSize, TextLineLayout, TextStyle, ir::UiFontDesign};
14
14
  use unicode_segmentation::UnicodeSegmentation;
15
15
 
16
16
  /// Line height factor shared with the renderer's glyphon buffers.
@@ -20,8 +20,33 @@ const CONTROL_FONT_SIZE: f32 = 16.0;
20
20
  /// Hard cap on lines shaped within one frame generation (memory safety net).
21
21
  const LINE_CACHE_LIMIT: usize = 65_536;
22
22
 
23
- pub fn text_attrs() -> Attrs<'static> {
24
- Attrs::new().family(Family::SansSerif)
23
+ /// cosmic-text attributes for a Mün text style (family, weight, slant).
24
+ pub fn styled_attrs(style: TextStyle) -> Attrs<'static> {
25
+ let family = match style.design {
26
+ UiFontDesign::Default | UiFontDesign::Rounded => Family::SansSerif,
27
+ UiFontDesign::Monospaced => Family::Monospace,
28
+ UiFontDesign::Serif => Family::Serif,
29
+ };
30
+ Attrs::new()
31
+ .family(family)
32
+ .weight(Weight(style.weight.clamp(1, 1000)))
33
+ .style(if style.italic {
34
+ Style::Italic
35
+ } else {
36
+ Style::Normal
37
+ })
38
+ }
39
+
40
+ /// Cache key of a text style: size bits, weight, slant and family.
41
+ type StyleKey = (u32, u16, bool, u8);
42
+
43
+ fn style_key(style: TextStyle) -> StyleKey {
44
+ (
45
+ style.size.to_bits(),
46
+ style.weight,
47
+ style.italic,
48
+ style.design as u8,
49
+ )
25
50
  }
26
51
 
27
52
  /// Shaped lines are memoized in two frame generations: a line used during
@@ -40,22 +65,22 @@ pub struct TextShaping {
40
65
  /// `&str` without allocating.
41
66
  #[derive(Default)]
42
67
  struct LineCache {
43
- sizes: HashMap<u32, HashMap<String, TextLineLayout>>,
68
+ sizes: HashMap<StyleKey, HashMap<String, TextLineLayout>>,
44
69
  len: usize,
45
70
  }
46
71
 
47
72
  impl LineCache {
48
- fn get(&self, text: &str, size: u32) -> Option<&TextLineLayout> {
73
+ fn get(&self, text: &str, size: StyleKey) -> Option<&TextLineLayout> {
49
74
  self.sizes.get(&size)?.get(text)
50
75
  }
51
76
 
52
- fn remove(&mut self, text: &str, size: u32) -> Option<TextLineLayout> {
77
+ fn remove(&mut self, text: &str, size: StyleKey) -> Option<TextLineLayout> {
53
78
  let line = self.sizes.get_mut(&size)?.remove(text)?;
54
79
  self.len -= 1;
55
80
  Some(line)
56
81
  }
57
82
 
58
- fn insert(&mut self, text: &str, size: u32, line: TextLineLayout) {
83
+ fn insert(&mut self, text: &str, size: StyleKey, line: TextLineLayout) {
59
84
  if self
60
85
  .sizes
61
86
  .entry(size)
@@ -103,17 +128,17 @@ impl TextShaping {
103
128
  }
104
129
 
105
130
  fn shape_line(&self, text: &str, font_size: f32) -> TextLineLayout {
106
- self.with_line(text, font_size, TextLineLayout::clone)
131
+ self.with_line(text, TextStyle::body(font_size), TextLineLayout::clone)
107
132
  }
108
133
 
109
134
  /// Borrow the (cached) shaped line without copying its caret stops.
110
135
  fn with_line<R>(
111
136
  &self,
112
137
  text: &str,
113
- font_size: f32,
138
+ style: TextStyle,
114
139
  read: impl FnOnce(&TextLineLayout) -> R,
115
140
  ) -> R {
116
- let size = font_size.to_bits();
141
+ let size = style_key(style);
117
142
  if let Some(line) = self.lines.borrow().get(text, size) {
118
143
  return read(line);
119
144
  }
@@ -121,7 +146,7 @@ impl TextShaping {
121
146
  Some(line) => line,
122
147
  None => {
123
148
  self.shaped_lines.set(self.shaped_lines.get() + 1);
124
- shape_line(&mut self.font_system.borrow_mut(), text, font_size)
149
+ shape_styled_line(&mut self.font_system.borrow_mut(), text, style)
125
150
  }
126
151
  };
127
152
  let result = read(&line);
@@ -141,12 +166,22 @@ impl TextShaping {
141
166
  /// start maps to the cluster's leading edge (left for LTR, right for RTL);
142
167
  /// boundaries inside a multi-grapheme cluster (ligatures) are interpolated across
143
168
  /// the cluster's graphemes; the end of text maps to the last cluster's trailing edge.
169
+ #[cfg(test)]
144
170
  pub fn shape_line(font_system: &mut FontSystem, text: &str, font_size: f32) -> TextLineLayout {
171
+ shape_styled_line(font_system, text, TextStyle::body(font_size))
172
+ }
173
+
174
+ pub fn shape_styled_line(
175
+ font_system: &mut FontSystem,
176
+ text: &str,
177
+ style: TextStyle,
178
+ ) -> TextLineLayout {
179
+ let font_size = style.size;
145
180
  let line_height = font_size * LINE_HEIGHT_FACTOR;
146
181
  let mut buffer = Buffer::new(font_system, Metrics::new(font_size, line_height));
147
182
  buffer.set_wrap(Wrap::None);
148
183
  buffer.set_size(None, None);
149
- buffer.set_text(text, &text_attrs(), Shaping::Advanced, None);
184
+ buffer.set_text(text, &styled_attrs(style), Shaping::Advanced, None);
150
185
  buffer.shape_until_scroll(font_system, false);
151
186
 
152
187
  // Merge glyphs that share a cluster range (e.g. base + combining mark glyphs).
@@ -217,14 +252,56 @@ pub fn shape_line(font_system: &mut FontSystem, text: &str, font_size: f32) -> T
217
252
 
218
253
  impl IntrinsicMeasurer for TextShaping {
219
254
  fn measure_text(&self, text: &str) -> IntrinsicSize {
255
+ self.measure_styled_text(text, TextStyle::body(TEXT_FONT_SIZE), None, None)
256
+ }
257
+
258
+ fn measure_text_for_width(&self, text: &str, available_width: Option<f32>) -> IntrinsicSize {
259
+ self.measure_styled_text(text, TextStyle::body(TEXT_FONT_SIZE), available_width, None)
260
+ }
261
+
262
+ fn measure_styled_text(
263
+ &self,
264
+ text: &str,
265
+ style: TextStyle,
266
+ available_width: Option<f32>,
267
+ line_limit: Option<usize>,
268
+ ) -> IntrinsicSize {
220
269
  // 3pt top inset used by the retained scene plus the shaped line box.
221
- self.with_line(text, TEXT_FONT_SIZE, |line| {
270
+ let natural = self.with_line(text, style, |line| {
222
271
  IntrinsicSize::new(line.width.ceil(), (line.line_height + 2.0).ceil())
223
- })
272
+ });
273
+ let Some(width) = available_width.filter(|width| width.is_finite() && *width > 0.0) else {
274
+ return natural;
275
+ };
276
+ if width >= natural.width {
277
+ return natural;
278
+ }
279
+ // Use the same cosmic-text word/glyph breaks and line metrics as the
280
+ // drawing buffer. A one-line intrinsic height cannot size a narrow View.
281
+ let line_height = style.size * LINE_HEIGHT_FACTOR;
282
+ let mut fonts = self.font_system.borrow_mut();
283
+ let mut buffer = Buffer::new(&mut fonts, Metrics::new(style.size, line_height));
284
+ buffer.set_wrap(Wrap::WordOrGlyph);
285
+ buffer.set_size(Some(width), None);
286
+ buffer.set_text(text, &styled_attrs(style), Shaping::Advanced, None);
287
+ buffer.shape_until_scroll(&mut fonts, false);
288
+ let (line_width, line_count) = buffer
289
+ .layout_runs()
290
+ .fold((0.0_f32, 0usize), |(longest, count), line| {
291
+ (longest.max(line.line_w), count + 1)
292
+ });
293
+ let lines = match line_limit {
294
+ Some(limit) => line_count.clamp(1, limit.max(1)),
295
+ None => line_count.max(1),
296
+ };
297
+ IntrinsicSize::new(
298
+ line_width.min(width).ceil(),
299
+ (lines as f32 * line_height + 2.0).ceil(),
300
+ )
224
301
  }
225
302
 
226
303
  fn measure_action(&self, label: &str) -> IntrinsicSize {
227
- self.with_line(label, CONTROL_FONT_SIZE, |line| {
304
+ self.with_line(label, TextStyle::body(CONTROL_FONT_SIZE), |line| {
228
305
  IntrinsicSize::new((line.width + 34.0).ceil().max(92.0), 38.0)
229
306
  })
230
307
  }
@@ -273,6 +350,20 @@ mod tests {
273
350
  );
274
351
  }
275
352
 
353
+ #[test]
354
+ fn constrained_text_measures_multiple_shaped_lines() {
355
+ let shaping = TextShaping::new(FontSystem::new());
356
+ let text = "A line of text that must wrap when the proposed width is small.";
357
+ let natural = shaping.measure_text(text);
358
+ let narrow = shaping.measure_text_for_width(text, Some(110.0));
359
+ assert!(narrow.width <= 110.0);
360
+ assert!(
361
+ narrow.height > natural.height,
362
+ "narrow text needs multiple lines"
363
+ );
364
+ assert_eq!(shaping.measure_text_for_width(text, None), natural);
365
+ }
366
+
276
367
  #[test]
277
368
  fn shaped_caret_stops_follow_real_glyph_advances() {
278
369
  let mut fonts = font_system();
@@ -597,6 +597,22 @@ impl Expander<'_, '_> {
597
597
  .collect(),
598
598
  transaction: transaction.clone(),
599
599
  },
600
+ UiAction::Host {
601
+ service,
602
+ operation,
603
+ arguments,
604
+ state,
605
+ transaction,
606
+ } => UiAction::Host {
607
+ service: service.clone(),
608
+ operation: operation.clone(),
609
+ arguments: arguments
610
+ .iter()
611
+ .map(|argument| self.expression(argument, scopes))
612
+ .collect(),
613
+ state: state.as_ref().map(|state| self.rename_state(state, scopes)),
614
+ transaction: transaction.clone(),
615
+ },
600
616
  }
601
617
  }
602
618
 
@@ -808,9 +824,10 @@ impl Expander<'_, '_> {
808
824
  then_nodes: self.nodes(then_nodes, scopes)?,
809
825
  otherwise: self.nodes(otherwise, scopes)?,
810
826
  },
811
- UiNode::Text { base, value } => UiNode::Text {
827
+ UiNode::Text { base, value, font } => UiNode::Text {
812
828
  base: self.base(base, scopes),
813
829
  value: self.expression(value, scopes),
830
+ font: *font,
814
831
  },
815
832
  UiNode::Panel { base, shape } => UiNode::Panel {
816
833
  base: self.base(base, scopes),