cranpose-ui 0.29.0

Composable widgets, text, layouts and input for Cranpose apps
Documentation
// A node's chain keeps the padding, size, fill, offset, clip and background
// of its modifier, and a text's string and style, in their nodes alone. As the modifier
// reorders, repeats, drops and changes them, and the text changes, the
// nodes lay out and show what a fresh composition of the same state shows,
// and the node gives back a modifier equal to the one set.

use std::{cell::Cell, rc::Rc};

use cranpose_ui::{
    Box, BoxSpec, Color, LayoutBox, LayoutNode, LayoutTree, Modifier, Size, Text, TextStyle,
};

use crate::{directional_layout::tagged, text_measured_layout_integration::layout_composition};

const VIEWPORT: Size = Size {
    width: 200.0,
    height: 200.0,
};

const RED: Color = Color(1.0, 0.0, 0.0, 1.0);
const BLUE: Color = Color(0.0, 0.0, 1.0, 1.0);

fn modifier(variant: usize) -> Modifier {
    let base = Modifier::empty();
    match variant {
        0 => base.padding(4.0).size_points(40.0, 20.0).background(RED),
        1 => base.size_points(40.0, 20.0).padding(4.0).background(RED),
        2 => base.padding(8.0).size_points(40.0, 20.0).background(BLUE),
        3 => base.background(RED).padding(4.0),
        4 => base.padding(4.0).padding(4.0).size_points(10.0, 10.0),
        5 => base.padding(6.0).rounded_corners(3.0).background(BLUE),
        6 => base
            .fill_max_width_fraction(0.5)
            .offset(3.0, 4.0)
            .height(30.0),
        _ => base
            .offset(5.0, 1.0)
            .fill_max_width_fraction(0.25)
            .clip_to_bounds(),
    }
}

// The subject, the one child of a box tagged "wrapper": a tag on the subject
// would put an element in its modifier that equals only itself.
fn subject(variant: usize) {
    Box(
        Modifier::empty().content_description("wrapper"),
        BoxSpec::default(),
        move || {
            Box(modifier(variant), BoxSpec::default(), move || {
                Text(
                    ["one", "two two", "three"][variant % 3],
                    Modifier::empty().content_description("child"),
                    TextStyle::default(),
                );
            });
        },
    );
}

fn subject_box(layout: &LayoutTree) -> &LayoutBox {
    &tagged(layout.root(), "wrapper").children[0]
}

fn rect(node: &LayoutBox) -> (f32, f32, f32, f32) {
    (node.rect.x, node.rect.y, node.rect.width, node.rect.height)
}

#[test]
fn a_chain_holding_elements_in_its_nodes_follows_every_change_of_its_modifier() {
    let state = Rc::new(Cell::new(None));
    let slot = Rc::clone(&state);
    let mut composition = cranpose_ui::run_test_composition(move || {
        let variant = cranpose_core::rememberMutableStateOf(|| 0_usize);
        slot.set(Some(variant));
        subject(variant.get());
    });
    layout_composition(&mut composition, VIEWPORT);
    let variant = state.get().expect("variant state");
    for value in [1, 2, 3, 4, 5, 6, 7, 6, 0, 4, 2] {
        variant.set(value);
        while composition.process_invalid_scopes().expect("recompose") {}
        let layout = layout_composition(&mut composition, VIEWPORT);
        let mut fresh = cranpose_ui::run_test_composition(move || subject(value));
        let fresh = layout_composition(&mut fresh, VIEWPORT);
        let pairs = [
            (subject_box(&layout), subject_box(&fresh)),
            (
                tagged(layout.root(), "child"),
                tagged(fresh.root(), "child"),
            ),
        ];
        for (kept, made) in pairs {
            assert_eq!(rect(kept), rect(made), "after variant {value}");
        }
        let text = |layout: &LayoutTree| {
            let slices = tagged(layout.root(), "child").node_data.modifier_slices();
            slices.text_content().map(|text| text.to_string())
        };
        assert_eq!(text(&layout), text(&fresh), "the text of variant {value}");
        let id = subject_box(&layout).node_id;
        let held = composition
            .applier_mut()
            .with_node::<LayoutNode, _>(id, |node| node.modifier())
            .expect("the subject is a layout node");
        assert!(held == modifier(value), "the modifier of variant {value}");
    }
}

// A plain element compares its floats as Compose's data classes do: a padding
// of 0.0 that becomes -0.0 changes the node, and a NaN padding kept from one
// pass to the next equals itself and asks for no measure. The padding sits in
// a cell, since a state of a float takes -0.0 for 0.0 and composes nothing.
#[test]
fn a_plain_element_compares_its_floats_as_compose_data_classes() {
    let padding = Rc::new(Cell::new(0.0_f32));
    let read = Rc::clone(&padding);
    let pass_slot = Rc::new(Cell::new(None));
    let slot = Rc::clone(&pass_slot);
    let mut composition = cranpose_ui::run_test_composition(move || {
        let pass = cranpose_core::rememberMutableStateOf(|| 0_usize);
        slot.set(Some(pass));
        let read = Rc::clone(&read);
        Box(
            Modifier::empty().content_description("wrapper"),
            BoxSpec::default(),
            move || {
                pass.get();
                Box(
                    Modifier::empty().padding(read.get()),
                    BoxSpec::default(),
                    || {},
                );
            },
        );
    });
    let layout = layout_composition(&mut composition, VIEWPORT);
    let id = subject_box(&layout).node_id;
    let pass = pass_slot.get().expect("pass state");
    let mut compose_with = |value: f32| {
        padding.set(value);
        pass.set(pass.get() + 1);
        while composition.process_invalid_scopes().expect("recompose") {}
        let node = composition
            .applier_mut()
            .with_node::<LayoutNode, _>(id, |node| (node.modifier(), node.needs_measure()))
            .expect("the subject is a layout node");
        layout_composition(&mut composition, VIEWPORT);
        node
    };

    let (held, needs_measure) = compose_with(-0.0);
    assert!(needs_measure, "-0.0 after 0.0 is a change");
    assert!(held == Modifier::empty().padding(-0.0));
    assert!(held != Modifier::empty().padding(0.0));

    let (_, needs_measure) = compose_with(f32::NAN);
    assert!(needs_measure, "NaN after -0.0 is a change");
    let (held, needs_measure) = compose_with(f32::NAN);
    assert!(!needs_measure, "a NaN equals itself");
    assert!(held == Modifier::empty().padding(f32::NAN));
}