cranpose-ui 0.20.0

Composable widgets, text, layouts and input for Cranpose apps
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use std::{
    cell::{Cell, Ref, RefCell},
    hash::{Hash, Hasher},
    rc::Rc,
    sync::Arc,
};

use cranpose_core::PackedU64;
use cranpose_foundation::{
    Constraints, DelegatableNode, DrawModifierNode, InvalidationKind, LayoutModifierNode,
    Measurable, ModifierNode, ModifierNodeContext, ModifierNodeElement, NodeCapabilities,
    NodeState, SemanticsConfiguration, SemanticsNode, Size,
};
use smallvec::SmallVec;

use crate::{
    density::Density,
    text::{
        AnnotatedString, TextLayoutOptions, TextStyle, layout_cache::CacheAdmission,
        recent_values::RecentValues,
    },
};

/// Node that stores text content and handles measurement, drawing, and semantics.
///
/// This node implements three capabilities:
/// - **Layout**: Measures text and returns appropriate size
/// - **Draw**: Supplies prepared text state consumed by scene building
/// - **Semantics**: Provides text content for accessibility
///
/// Matches Jetpack Compose: `TextStringSimpleNode` in
/// `compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/TextStringSimpleNode.kt`
#[derive(Debug)]
pub struct TextModifierNode {
    layout: Rc<TextPreparedLayoutOwner>,
    density: Density,
    state: NodeState,
}

const PREPARED_LAYOUT_CACHE_CAPACITY: usize = 4;

#[derive(Clone, Debug)]
struct TextPreparedLayoutCacheEntry {
    min_width: f32,
    // The max widths at which the text laid out from `min_width` gives
    // `layout` again.
    max_widths: cranpose_ui_layout::BoundRange,
    text_generation: PackedU64,
    font_scale_fingerprint: u32,
    layout: Rc<crate::text::PreparedTextLayout>,
}

impl TextPreparedLayoutCacheEntry {
    fn new(
        min_width: f32,
        (text_generation, font_scale_fingerprint): (u64, u32),
        layout: Rc<crate::text::PreparedTextLayout>,
    ) -> Self {
        Self {
            min_width,
            max_widths: layout.max_widths_kept(min_width),
            text_generation: PackedU64::new(text_generation),
            font_scale_fingerprint,
            layout,
        }
    }

    // Whether the layout stands for the text laid out between `min_width`
    // and `max_width` as it is: its lines and size the same.
    fn keeps(&self, (min_width, max_width): (f32, f32), stamp: (u64, u32)) -> bool {
        self.min_width.to_bits() == min_width.to_bits()
            && (self.text_generation.get(), self.font_scale_fingerprint) == stamp
            && self.max_widths.contains(max_width)
    }
}

/// A text node's text and its prepared layouts. The node and the slices
/// built from it share one owner for the node's life: an update changes its
/// source in place, so the slices read the new text without being rebuilt.
#[derive(Debug)]
struct TextPreparedLayoutOwner {
    source: RefCell<TextLayoutSource>,
    measured_widths: Cell<Option<(f32, f32)>>,
    cache: RefCell<SmallVec<[TextPreparedLayoutCacheEntry; 1]>>,
    // Whether the text changed in place since a hold was last asked of it:
    // a text that changes as a ticker's does measures again each frame,
    // which no hold spares, so it gives none.
    changed: Cell<bool>,
}

/// What a text is laid out from.
#[derive(Debug)]
struct TextLayoutSource {
    text: Rc<AnnotatedString>,
    style: Arc<TextStyle>,
    style_hash: PackedU64,
    options: TextLayoutOptions,
    // How the text service's shared cache keeps the layouts: those of a
    // text changed in place, as a ticker's is every frame, once it comes
    // back.
    admission: CacheAdmission,
}

impl TextLayoutSource {
    // The frame the text is laid out in between `widths`.
    fn frame(&self, (min_width, max_width): (f32, f32)) -> crate::text::measure::TextFrame {
        crate::text::measure::TextFrame {
            min_width,
            max_width,
            options: self.options,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct TextPreparedLayoutHandle {
    owner: Rc<TextPreparedLayoutOwner>,
}

impl TextPreparedLayoutOwner {
    fn new(text: Rc<AnnotatedString>, style: Arc<TextStyle>, options: TextLayoutOptions) -> Self {
        let style_hash = PackedU64::new(style.render_hash());
        Self {
            source: RefCell::new(TextLayoutSource {
                text,
                style,
                style_hash,
                options: options.normalized(),
                admission: CacheAdmission::First,
            }),
            measured_widths: Cell::new(None),
            changed: Cell::new(false),
            cache: RefCell::new(SmallVec::new()),
        }
    }

    /// Lays the text out from `text`, `style` and normalized `options` from
    /// now on, dropping the layouts prepared from the old ones. The source
    /// takes the element's style even when it equals the current one, so
    /// the element, the source and the layouts prepared next share one
    /// copy; the old one leaves with the layouts that hold it.
    fn update(
        &self,
        text: &Rc<AnnotatedString>,
        style: &Arc<TextStyle>,
        options: TextLayoutOptions,
    ) {
        let mut source = self.source.borrow_mut();
        let shared_style = Arc::ptr_eq(&source.style, style);
        let same_style = shared_style || *source.style == **style;
        if source.text == *text && same_style && source.options == options {
            return;
        }
        if !same_style {
            source.style_hash = PackedU64::new(style.render_hash());
        }
        if !shared_style {
            source.style = Arc::clone(style);
        }
        source.text = Rc::clone(text);
        source.options = options;
        source.admission = CacheAdmission::Changed;
        drop(source);
        self.cache.borrow_mut().clear();
        self.changed.set(true);
    }

    fn annotated_text(&self) -> Ref<'_, Rc<AnnotatedString>> {
        Ref::map(self.source.borrow(), |source| &source.text)
    }

    fn style(&self) -> Ref<'_, TextStyle> {
        Ref::map(self.source.borrow(), |source| &*source.style)
    }

    fn options(&self) -> TextLayoutOptions {
        self.source.borrow().options
    }

    // The layout at `widths`, which the cache holds first: the latest one
    // when it keeps the widths, else a cached one that takes them, else a
    // new one. The reader holds the cache borrowed, and no callback runs
    // while it is.
    fn prepared(&self, widths: (f32, f32)) -> Ref<'_, Rc<crate::text::PreparedTextLayout>> {
        let stamp = crate::render_state::text_layout_stamp();
        {
            let cache = self.cache.borrow();
            if cache
                .first()
                .is_some_and(|latest| latest.keeps(widths, stamp))
            {
                return Ref::map(cache, |cache| &cache[0].layout);
            }
        }
        if !self.bring_kept(widths, stamp) {
            self.prepare(widths, stamp);
        }
        Ref::map(self.cache.borrow(), |cache| &cache[0].layout)
    }

    // Puts first a cached layout that takes `widths`: the latest one, which
    // nothing else holds, realigned in place where it breaks into the same
    // lines there, as a width that animates asks for each frame, or one the
    // cache keeps. False when none does.
    fn bring_kept(&self, widths: (f32, f32), stamp: (u64, u32)) -> bool {
        let mut cache = self.cache.borrow_mut();
        let Some(latest) = cache.first_mut() else {
            return false;
        };
        // Its style, equal to the source's, gives the alignment.
        if (latest.text_generation.get(), latest.font_scale_fingerprint) == stamp
            && let Some(prepared) = Rc::get_mut(&mut latest.layout)
            && prepared.try_realign(widths, prepared.visual_style.paragraph_style.text_align)
        {
            latest.min_width = widths.0;
            latest.max_widths = prepared.max_widths_kept(widths.0);
            return true;
        }
        let Some(index) = cache.iter().position(|entry| entry.keeps(widths, stamp)) else {
            return false;
        };
        cache[..=index].rotate_right(1);
        true
    }

    // Prepares a layout at `widths` that no cached one keeps, which the
    // cache then holds first. Out of line: most reads take a cached layout.
    #[inline(never)]
    fn prepare(&self, widths: (f32, f32), stamp: (u64, u32)) {
        let source = self.source.borrow();
        let frame = source.frame(widths);
        let align = source.style.paragraph_style.text_align;
        // A layout at new widths takes the allocation of the one the full
        // cache lets go, as a width that animates asks for one every frame.
        let spare = {
            let mut cache = self.cache.borrow_mut();
            (cache.len() >= PREPARED_LAYOUT_CACHE_CAPACITY)
                .then(|| cache.pop())
                .flatten()
                .map(|entry| entry.layout)
        };
        let prepared = {
            // The cache holds layouts of this source only: the latest one, on
            // the fonts of now, takes the widths when it breaks into the same
            // lines there, as a width animates, or gives its glyphs to the new
            // one.
            let cache = self.cache.borrow();
            let latest = cache
                .first()
                .filter(|entry| {
                    (entry.text_generation.get(), entry.font_scale_fingerprint) == stamp
                })
                .map(|entry| &entry.layout);
            match latest.filter(|latest| latest.layout.holds(widths.0, widths.1)) {
                Some(latest) => latest.at_widths(widths, align, spare),
                None => {
                    let text = crate::text::measure::SharedText {
                        text: &source.text,
                        style: &source.style,
                        style_hash: source.style_hash.get(),
                    };
                    let shaped = latest.map(|latest| &latest.layout);
                    crate::text::measure::prepare_shared(&text, frame, source.admission, shaped)
                }
            }
        };
        if Arc::ptr_eq(&prepared.visual_style, &source.style) {
            let _ = prepared.visual_style_hash.set(source.style_hash);
        }
        drop(source);

        let mut cache = self.cache.borrow_mut();
        cache.insert(
            0,
            TextPreparedLayoutCacheEntry::new(widths.0, stamp, prepared),
        );
        cache.truncate(PREPARED_LAYOUT_CACHE_CAPACITY);
    }

    /// The size of the layout the cache holds for `widths` and the max widths
    /// that keep it; `None` when the cache holds none.
    fn cached_widths(&self, widths: (f32, f32)) -> Option<(Size, cranpose_ui_layout::BoundRange)> {
        let stamp = crate::render_state::text_layout_stamp();
        let cache = self.cache.borrow();
        let entry = cache.iter().find(|entry| entry.keeps(widths, stamp))?;
        Some((entry.layout.size(), entry.max_widths))
    }

    /// The widest word of the text: see [`crate::text::measure::min_intrinsic_text_width`].
    fn min_intrinsic_width(&self) -> f32 {
        let source = self.source.borrow();
        crate::text::measure::min_intrinsic_text_width(&source.text, &source.style)
    }

    fn measure_text_content(&self, widths: (f32, f32)) -> Size {
        self.prepared(widths).size()
    }

    fn measure_layout(&self, widths: (f32, f32)) -> (Size, cranpose_ui_layout::AlignmentLines) {
        self.measured_widths.set(Some(widths));
        let prepared = self.prepared(widths);
        (prepared.size(), prepared.alignment_lines())
    }

    fn measured_layout(&self) -> Option<Rc<crate::text::PreparedTextLayout>> {
        self.measured_widths
            .get()
            .map(|widths| Rc::clone(&self.prepared(widths)))
    }
}

impl TextPreparedLayoutHandle {
    fn new(owner: Rc<TextPreparedLayoutOwner>) -> Self {
        Self { owner }
    }

    pub(crate) fn annotated_text(&self) -> Ref<'_, Rc<AnnotatedString>> {
        self.owner.annotated_text()
    }

    pub(crate) fn style(&self) -> Ref<'_, TextStyle> {
        self.owner.style()
    }

    pub(crate) fn options(&self) -> TextLayoutOptions {
        self.owner.options()
    }

    pub(crate) fn measured_layout(&self) -> Option<Rc<crate::text::PreparedTextLayout>> {
        self.owner.measured_layout()
    }
}

impl TextModifierNode {
    /// A text node sized on `density`'s device pixel grid.
    pub fn new(
        text: Rc<AnnotatedString>,
        style: impl Into<Arc<TextStyle>>,
        options: TextLayoutOptions,
        density: Density,
    ) -> Self {
        Self {
            layout: Rc::new(TextPreparedLayoutOwner::new(text, style.into(), options)),
            density,
            state: NodeState::new(),
        }
    }

    /// The text's size rounded up to whole device pixels, as Compose sizes a
    /// text node (`TextLayoutResult.size` is the paragraph's size, `ceil`ed),
    /// so whatever follows it starts on the pixel grid.
    fn pixel_size(&self, size: Size) -> Size {
        Size {
            width: self.density.ceil(size.width),
            height: self.density.ceil(size.height),
        }
    }

    /// Returns the plain text of the node.
    pub fn text(&self) -> Ref<'_, str> {
        Ref::map(self.layout.annotated_text(), |text| text.text.as_str())
    }

    /// Returns the node's text with its span styles and annotations.
    pub fn annotated_text(&self) -> Rc<AnnotatedString> {
        Rc::clone(&self.layout.annotated_text())
    }

    /// Returns the style the text lays out with.
    pub fn style(&self) -> Ref<'_, TextStyle> {
        self.layout.style()
    }

    /// Returns the layout options: overflow, soft wrap, and minimum and maximum lines.
    pub fn options(&self) -> TextLayoutOptions {
        self.layout.options()
    }

    fn measure_text_content(&self, widths: (f32, f32)) -> Size {
        self.layout.measure_text_content(widths)
    }

    pub(crate) fn prepared_layout_handle(&self) -> TextPreparedLayoutHandle {
        TextPreparedLayoutHandle::new(Rc::clone(&self.layout))
    }
}

impl DelegatableNode for TextModifierNode {
    fn node_state(&self) -> &NodeState {
        &self.state
    }
}

impl ModifierNode for TextModifierNode {
    fn on_attach(&mut self, context: &mut dyn ModifierNodeContext) {
        context.invalidate(InvalidationKind::Layout);
        context.invalidate(InvalidationKind::Draw);
        context.invalidate(InvalidationKind::Semantics);
    }

    fn as_draw_node(&self) -> Option<&dyn DrawModifierNode> {
        Some(self)
    }

    fn as_draw_node_mut(&mut self) -> Option<&mut dyn DrawModifierNode> {
        Some(self)
    }

    fn as_semantics_node(&self) -> Option<&dyn SemanticsNode> {
        Some(self)
    }

    fn as_semantics_node_mut(&mut self) -> Option<&mut dyn SemanticsNode> {
        Some(self)
    }

    fn as_layout_node(&self) -> Option<&dyn LayoutModifierNode> {
        Some(self)
    }

    fn as_layout_node_mut(&mut self) -> Option<&mut dyn LayoutModifierNode> {
        Some(self)
    }
}

impl LayoutModifierNode for TextModifierNode {
    /// A text holds while its layout does and the bounds keep its size: it
    /// measures nothing it wraps.
    fn measure_hold(
        &self,
        _density: f32,
        constraints: Constraints,
        size: Size,
        _wrapped: cranpose_ui_layout::WrappedHold,
    ) -> Option<cranpose_ui_layout::ConstraintsHold> {
        // A text changed in place since its last hold, as a ticker's is,
        // measures again next frame whatever the hold.
        if self.layout.changed.take() {
            return None;
        }
        let (text_size, max_widths) = self.layout.cached_widths(text_widths(constraints))?;
        let text_size = self.pixel_size(text_size);
        if size != text_size {
            return None;
        }
        Some(cranpose_ui_layout::ConstraintsHold {
            width: cranpose_ui_layout::AxisHold {
                min: cranpose_ui_layout::BoundRange::exactly(constraints.min_width),
                max: cranpose_ui_layout::BoundRange::from(text_size.width).intersect(max_widths)?,
            },
            height: cranpose_ui_layout::AxisHold::sized(text_size.height),
        })
    }

    fn measure(
        &self,
        _context: &mut dyn ModifierNodeContext,
        _measurable: &dyn Measurable,
        constraints: Constraints,
    ) -> cranpose_ui_layout::LayoutModifierMeasureResult {
        let (text_size, alignment_lines) = self.layout.measure_layout(text_widths(constraints));
        let text_size = self.pixel_size(text_size);

        let width = text_size
            .width
            .clamp(constraints.min_width, constraints.max_width);
        let height = text_size
            .height
            .clamp(constraints.min_height, constraints.max_height);

        cranpose_ui_layout::LayoutModifierMeasureResult::with_size(Size { width, height })
            .with_alignment_lines(alignment_lines)
    }

    fn min_intrinsic_width(
        &self,
        _measurable: &dyn Measurable,
        _height: f32,
        _density: f32,
    ) -> f32 {
        self.density.ceil(self.layout.min_intrinsic_width())
    }

    fn max_intrinsic_width(
        &self,
        _measurable: &dyn Measurable,
        _height: f32,
        _density: f32,
    ) -> f32 {
        self.pixel_size(self.measure_text_content((0.0, f32::INFINITY)))
            .width
    }

    fn min_intrinsic_height(&self, _measurable: &dyn Measurable, width: f32, _density: f32) -> f32 {
        self.pixel_size(self.measure_text_content((0.0, intrinsic_width(width))))
            .height
    }

    fn max_intrinsic_height(&self, _measurable: &dyn Measurable, width: f32, _density: f32) -> f32 {
        self.pixel_size(self.measure_text_content((0.0, intrinsic_width(width))))
            .height
    }
}

// The widths a text lays out between under `constraints`: Compose's
// `finalConstraints` read the min and the max width.
fn text_widths(constraints: Constraints) -> (f32, f32) {
    (constraints.min_width.max(0.0), constraints.max_width)
}

// The max width an intrinsic height is asked at; a width that is not positive
// and finite is no limit.
fn intrinsic_width(width: f32) -> f32 {
    if width.is_finite() && width > 0.0 {
        width
    } else {
        f32::INFINITY
    }
}

impl DrawModifierNode for TextModifierNode {}

impl SemanticsNode for TextModifierNode {
    fn merge_semantics(&self, config: &mut SemanticsConfiguration) {
        config
            .content_description
            .get_or_insert_with(|| self.text().to_string());
    }

    fn reach(&self) -> cranpose_foundation::SemanticsReach {
        cranpose_foundation::SemanticsReach::default()
    }
}

/// Element that creates and updates TextModifierNode instances.
///
/// This follows the modifier element pattern where the element is responsible for:
/// - Creating new nodes (via `create`)
/// - Updating existing nodes when properties change (via `update`)
/// - Declaring capabilities (LAYOUT | DRAW | SEMANTICS)
///
/// Matches Jetpack Compose: `TextStringSimpleElement` in BasicText.kt
#[derive(Debug, Clone)]
pub struct TextModifierElement {
    text: Rc<AnnotatedString>,
    /// One copy for every text composed with an equal style: see
    /// [`shared_text_style`].
    style: Arc<TextStyle>,
    options: TextLayoutOptions,
    density: Density,
}

impl TextModifierElement {
    /// A text laid out on `density`'s device pixel grid, the composition's
    /// [`crate::density::density`] where a `Text` is composed.
    pub fn new(
        text: Rc<AnnotatedString>,
        style: TextStyle,
        options: TextLayoutOptions,
        density: Density,
    ) -> Self {
        Self::with_shared_style(text, shared_text_style(style), options, density)
    }

    /// [`Self::new`] for a style [`shared_text_style`] already shared.
    pub(crate) fn with_shared_style(
        text: Rc<AnnotatedString>,
        style: Arc<TextStyle>,
        options: TextLayoutOptions,
        density: Density,
    ) -> Self {
        Self {
            text,
            style,
            options: options.normalized(),
            density,
        }
    }
}

impl PartialEq for TextModifierElement {
    fn eq(&self, other: &Self) -> bool {
        self.text == other.text
            && (Arc::ptr_eq(&self.style, &other.style) || self.style == other.style)
            && self.options == other.options
            && self.density == other.density
    }
}

/// How many recent text styles [`shared_text_style`] keeps.
const SHARED_TEXT_STYLES: usize = 16;

// The text styles recently composed, which [`shared_text_style`] shares.
pub(crate) type RecentTextStyles = RecentValues<Arc<TextStyle>, SHARED_TEXT_STYLES>;

/// `style` shared with the texts recently composed with an equal one, so a
/// screen of texts in a few styles keeps a few copies, not one a text, and
/// a node updated to an element of the same style compares pointers. A
/// style none of the kept [`SHARED_TEXT_STYLES`] equals gets a copy of its
/// own, kept first; the last kept one goes.
pub(crate) fn shared_text_style(style: TextStyle) -> Arc<TextStyle> {
    crate::thread_state::with(|state| {
        let mut recent = state.recent_text_styles.borrow_mut();
        if let Some(kept) = recent.find(|kept| **kept == style) {
            return Arc::clone(kept);
        }
        Arc::clone(recent.keep(Arc::new(style)))
    })
}

impl Hash for TextModifierElement {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.text.render_hash().hash(state);
        self.options.hash(state);
        self.density.density().to_bits().hash(state);
    }
}

impl ModifierNodeElement for TextModifierElement {
    type Node = TextModifierNode;

    fn create(&self) -> Self::Node {
        TextModifierNode::new(
            Rc::clone(&self.text),
            Arc::clone(&self.style),
            self.options,
            self.density,
        )
    }

    fn update(&self, node: &mut Self::Node) {
        node.density = self.density;
        node.layout.update(&self.text, &self.style, self.options);
    }

    fn capabilities(&self) -> NodeCapabilities {
        NodeCapabilities::LAYOUT | NodeCapabilities::DRAW | NodeCapabilities::SEMANTICS
    }
}

#[cfg(test)]
#[path = "tests/text_modifier_node_tests.rs"]
mod tests;