Common patterns
Self-contained examples covering reactive thresholds, flex sizing, spring-driven selection rings, custom vector views, curve text, and scrollable viewports.
Reactive state & threshold styling
Drive a numeric readout, a percentage bar width, and a threshold color from one observed integer field without manual callbacks.
//~focus-start:reactive
#[model]
pub struct Gauge {
#[observe]
pub level: i32,
}
impl Default for Gauge {
fn default() -> Self {
Self { level: 42 }
}
}
#[model]
impl Gauge {
pub fn step(&mut self, delta: i32) {
self.level = (self.level + delta).clamp(0, 100);
}
}
#[compose]
fn reactive_scene(gauge: model!(Gauge)) {
ui! {
Column (
grow: 1.0,
padding: Padding::all(20),
justify: JustifyContent::SpaceBetween,
text_color: ColorToken::OnSurface
) {
Row (height: 28, justify: JustifyContent::SpaceBetween, align: AlignItems::Center) {
Text (
"Signal binding",
grow: 1.0,
height: 20,
font_size: 14,
text_color: ColorToken::OnSurfaceVariant,
paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
)
Text (
text: ${ format_args!("{} %", gauge.level()) },
capacity: 12,
grow: 1.0,
font_size: 22,
height: 28,
paragraph: ParagraphStyle::label().with_align(TextAlign::End)
)
}
View (height: 14, bg_color: ColorToken::SurfaceVariant, border_radius: 7, clip_children: true) {
View (
width: ${ Dimension::percent(gauge.level()) },
height: Dimension::percent(100),
bg_color: ${ if gauge.level() >= 80 { ColorToken::Primary } else { ColorToken::Secondary } },
border_radius: 7
)
}
Row (height: 40, column_gap: 8) {
Button (
"- 10",
grow: 1.0,
height: 40,
border_radius: 9,
normal_color: ColorToken::SurfaceVariant,
pressed_color: ColorToken::Outline,
text_color: ColorToken::OnSurface
) on Tap { gauge.step(-10); }
Button (
"42",
width: 64,
height: 40,
border_radius: 9,
normal_color: ColorToken::SurfaceVariant,
pressed_color: ColorToken::Outline,
text_color: ColorToken::OnSurfaceVariant
) on Tap { gauge.step(42 - gauge.level()); }
Button (
"+ 10",
grow: 1.0,
height: 40,
border_radius: 9,
normal_color: ColorToken::Primary,
pressed_color: ColorToken::Secondary,
text_color: ColorToken::OnPrimary
) on Tap { gauge.step(10); }
}
}
};
}
//~focus-end:reactiveMarking level with #[observe] generates the gauge.level() getter. Both width: ${ Dimension::percent(gauge.level()) } and bg_color: ${ ... } subscribe to that field and recompute only when gauge.step(...) mutates it.
Flex sizing & space distribution
Toggle a primary card between compact and expanded widths while its sibling claims the remaining row space with grow: 1.0.
//~focus-start:layout
#[model]
#[derive(Default)]
pub struct FlexDeck {
#[observe]
pub compact: bool,
}
#[model]
impl FlexDeck {
pub fn toggle(&mut self) {
self.compact = !self.compact;
}
}
#[compose]
fn layout_scene(deck: model!(FlexDeck)) {
ui! {
Column (
grow: 1.0,
padding: Padding::all(18),
row_gap: 12,
text_color: ColorToken::OnSurface
) {
Row (height: 34, column_gap: 8, align: AlignItems::Center) {
Text (
text: ${ if deck.compact() { "Compact · 96 px card" } else { "Expanded · 156 px card" } },
capacity: 28,
grow: 1.0,
min_width: 0,
height: 22,
font_size: 13,
text_color: ColorToken::OnSurfaceVariant,
paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
)
Button (
"Toggle width",
width: 112,
height: 32,
font_size: 12,
border_radius: 8,
normal_color: ColorToken::Primary,
pressed_color: ColorToken::Secondary,
text_color: ColorToken::OnPrimary
) on Tap { deck.toggle(); }
}
Row (
grow: 1.0,
padding: Padding::all(14),
column_gap: 10,
align: AlignItems::Center,
justify: JustifyContent::SpaceBetween,
bg_color: ColorToken::SurfaceVariant,
border_radius: 12
) {
Column (
width: ${ Dimension::px(if deck.compact() { 96 } else { 156 }) },
height: 76,
padding: Padding::all(10),
justify: JustifyContent::SpaceBetween,
bg_color: ColorToken::Surface,
border_radius: 9
) {
View (width: 24, height: 6, bg_color: ColorToken::Primary, border_radius: 3)
Text ("Primary", font_size: 12, height: 18)
}
Column (
grow: 1.0,
height: 76,
padding: Padding::all(10),
justify: JustifyContent::SpaceBetween,
bg_color: ColorToken::Surface,
border_radius: 9
) {
View (width: 36, height: 6, bg_color: ColorToken::Secondary, border_radius: 3)
Text ("Flex grow: 1.0", font_size: 12, height: 18, text_color: ColorToken::OnSurfaceVariant)
}
}
}
};
}
//~focus-end:layoutExplicit dimensions (Dimension::px) coexist with flex distribution (grow: 1.0). When the reactive width changes, layout invalidates only the affected row.
Spring selection rings (FollowBounds)
Float a selection frame above a row of variable-width buttons so rapid taps retarget mid-flight while preserving velocity.
//~focus-start:spring,mini-motion
#[model]
#[derive(Default)]
pub struct SpringTabs {
#[observe]
pub active: u8,
}
#[event]
fn pick_tab(
index: u8,
tabs: model!(SpringTabs),
target: entity!(),
followers: query!(mut FollowBounds),
) {
tabs.set_active(index);
for follower in followers {
follower.target = target;
}
}
#[model]
impl SpringTabs {
pub fn set_active(&mut self, index: u8) {
self.active = index;
}
}
#[compose]
fn spring_scene(tabs: model!(SpringTabs), reduced: bool) {
ui! {
Column (
grow: 1.0,
padding: Padding::all(20),
justify: JustifyContent::SpaceBetween,
text_color: ColorToken::OnSurface
) {
Text (
"Tap any pill — FollowBounds retargets mid-flight",
height: 22,
font_size: 13,
text_color: ColorToken::OnSurfaceVariant,
paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
)
Row (
height: 52,
padding: Padding::all(5),
column_gap: 6,
align: AlignItems::Center,
bg_color: ColorToken::SurfaceVariant,
border_radius: 12
) {
Button (
"MCU",
id: "spring_first",
grow: 1.0,
height: 38,
border_radius: 8,
normal_color: ColorToken::Surface,
pressed_color: ColorToken::SurfaceVariant,
text_color: ColorToken::OnSurface
) on Tap(pick_tab::callback(0, tabs))
Button (
"Desktop",
grow: 1.4,
height: 38,
border_radius: 8,
normal_color: ColorToken::Surface,
pressed_color: ColorToken::SurfaceVariant,
text_color: ColorToken::OnSurface
) on Tap(pick_tab::callback(1, tabs))
Button (
"Web",
grow: 1.0,
height: 38,
border_radius: 8,
normal_color: ColorToken::Surface,
pressed_color: ColorToken::SurfaceVariant,
text_color: ColorToken::OnSurface
) on Tap(pick_tab::callback(2, tabs))
//~add-start:mini-motion
View (
id: "spring_ring",
border_width: 2,
border_color: ColorToken::Primary,
border_radius: 9
) [
FollowBounds::new(id("spring_first"))
.with_transition(indicator_transition(reduced)),
]
//~add-end:mini-motion
}
Text (
text: ${
match tabs.active() {
0 => "Target 01 · SPI framebuffer with dirty-rect clips",
1 => "Target 02 · Batched GPU draws with retained textures",
_ => "Target 03 · Browser canvas driven by requestAnimationFrame",
}
},
height: 20,
font_size: 12,
text_color: ColorToken::OnSurfaceVariant,
paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
)
}
};
}
//~focus-end:spring,mini-motionFollowBounds maps the target entity's layout rectangle into the follower's parent space. Updating follower.target = target inside an #[event] handler hands the new destination to the INTERACTIVE spring.
Custom vector widgets (#[view])
Author a custom widget tag for ui! that strokes static vector paths scaled to the widget's layout box.
//~focus-start:vector
static APERTURE_BLADE: Path = path!(M 50 6 L 94 50 L 50 94 L 6 50 Z);
static INNER_RING: Path = path!(M 50 24 L 76 50 L 50 76 L 24 50 Z);
const ACCENTS: [Color; 3] = [
Color::rgb(52, 82, 255),
Color::rgb(207, 67, 43),
Color::rgb(42, 157, 143),
];
#[component]
#[derive(Clone, Copy)]
pub struct VectorGlyph {
pub accent: i32,
pub stroke: i32,
}
impl Default for VectorGlyph {
fn default() -> Self {
Self {
accent: 0,
stroke: 2,
}
}
}
impl VectorGlyph {
fn cycle(self) -> Self {
Self {
accent: (self.accent + 1) % 3,
stroke: if self.stroke >= 4 { 2 } else { self.stroke + 1 },
}
}
}
#[view(component = VectorGlyph, name = "VectorGlyph", priority = 60)]
fn paint_glyph(glyph: component!(), paint: paint!()) {
let rect = paint.rect();
let transform = paint
.transform()
.compose(&Transform::translate(rect.x, rect.y))
.compose(&Transform::scale(
rect.w / Fixed::from_int(100),
rect.h / Fixed::from_int(100),
));
let color = ACCENTS[(glyph.accent.rem_euclid(3)) as usize];
let stroke_paint = Paint::Color(color.into());
let dash: [Fixed; 0] = [];
paint.draw(&DrawCommand::StrokePath {
path: &APERTURE_BLADE,
transform,
paint: &stroke_paint,
width: Fixed::from_int(glyph.stroke),
opa: 255,
line_cap: LineCap::Round,
line_join: LineJoin::Round,
miter_limit: Fixed::from_int(4),
dash: &dash,
});
paint.draw(&DrawCommand::StrokePath {
path: &INNER_RING,
transform,
paint: &stroke_paint,
width: Fixed::from_int(1),
opa: 180,
line_cap: LineCap::Round,
line_join: LineJoin::Round,
miter_limit: Fixed::from_int(4),
dash: &dash,
});
}
#[compose]
fn vector_scene() {
ui! {
Column (
grow: 1.0,
padding: Padding::all(18),
justify: JustifyContent::SpaceBetween,
text_color: ColorToken::OnSurface
) {
Text (
"Custom #[view] — tap any tile to cycle stroke & pigment",
height: 20,
font_size: 13,
text_color: ColorToken::OnSurfaceVariant,
paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
)
Row (
grow: 1.0,
justify: JustifyContent::SpaceEvenly,
align: AlignItems::Center,
column_gap: 12
) {
VectorGlyph (
accent: 0,
stroke: 2,
width: 72,
height: 72,
bg_color: ColorToken::SurfaceVariant,
border_radius: 16
) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
cmd.insert(ev.target, next);
}
})
VectorGlyph (
accent: 1,
stroke: 3,
width: 72,
height: 72,
bg_color: ColorToken::SurfaceVariant,
border_radius: 16
) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
cmd.insert(ev.target, next);
}
})
VectorGlyph (
accent: 2,
stroke: 4,
width: 72,
height: 72,
bg_color: ColorToken::SurfaceVariant,
border_radius: 16
) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
cmd.insert(ev.target, next);
}
})
}
}
};
}
//~focus-end:vectorpath!(...) builds a zero-allocation static Path. #[view(component = VectorGlyph)] exposes VectorGlyph directly inside ui! and scales its 100 × 100 local coordinates to paint.rect() during rendering.
Text along Bézier curves (TextPath)
Place shaped text along a static or reactive vector curve and slide its leading offset without copying path commands.
//~focus-start:curve
static WAVE_CREST: Path = path!(M 8 64 Q 60 18 112 64 Q 164 110 216 64);
static WAVE_VALLEY: Path = path!(M 8 50 Q 60 96 112 50 Q 164 4 216 50);
#[model]
pub struct CurveRoute {
crest: PathId,
valley: PathId,
#[observe]
pub inverted: bool,
#[observe]
pub offset: i32,
}
impl CurveRoute {
pub fn new(crest: PathId, valley: PathId) -> Self {
Self {
crest,
valley,
inverted: false,
offset: 4,
}
}
}
#[model]
impl CurveRoute {
#[observe]
pub fn placement(&self) -> TextPath {
let path = if self.inverted {
self.valley
} else {
self.crest
};
TextPath::new(path).with_offset(Fixed::from_int(self.offset))
}
pub fn flip(&mut self) {
self.inverted = !self.inverted;
}
pub fn advance(&mut self) {
self.offset = (self.offset + 14) % 42;
}
}
#[compose]
fn curve_scene(route: model!(CurveRoute)) {
ui! {
Column (
grow: 1.0,
padding: Padding::all(16),
row_gap: 10,
text_color: ColorToken::OnSurface
) {
Row (height: 30, column_gap: 6, align: AlignItems::Center) {
Text (
text: ${ format_args!("+{} px", route.offset()) },
capacity: 16,
grow: 1.0,
min_width: 0,
height: 22,
font_size: 13,
text_color: ColorToken::OnSurfaceVariant,
paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
)
Button (
"Slide +14",
width: 72,
height: 28,
font_size: 12,
border_radius: 7,
normal_color: ColorToken::SurfaceVariant,
pressed_color: ColorToken::Outline,
text_color: ColorToken::OnSurface
) on Tap { route.advance(); }
Button (
"Flip wave",
width: 74,
height: 28,
font_size: 12,
border_radius: 7,
normal_color: ColorToken::Primary,
pressed_color: ColorToken::Secondary,
text_color: ColorToken::OnPrimary
) on Tap { route.flip(); }
}
View (
grow: 1.0,
bg_color: ColorToken::SurfaceVariant,
border_radius: 12,
clip_children: true
) {
Text (
"mirui rides the wave",
path: ${ route.placement() },
position: Position::Absolute,
left: 6,
top: 8,
width: Dimension::percent(96),
height: Dimension::percent(88),
font_size: 14,
text_color: ColorToken::Primary
)
}
}
};
}
//~focus-end:curveapp.paths().insert_static(...) registers a compile-time Path and returns a lightweight PathId. Binding path: ${ route.placement() } updates glyph placement whenever the model's TextPath offset or curve selection changes.
Animated scroll containers (ScrollOffset)
Drive a clipped scroll container with commands.scroll_to and ScrollConfig::with_transition while keeping direct pointer and wheel scrolling active.
//~focus-start:scroll
#[compose]
fn scroll_item(title: &'static str) -> Entity {
ui! {
Row (
height: 38,
shrink: 0.0,
padding: Padding::all(10),
align: AlignItems::Center,
bg_color: ColorToken::Surface,
border_radius: 8
) {
Text (title, height: 18, font_size: 13)
}
}
}
#[compose]
fn scroll_button(label: &'static str, list: Entity, y: i32, primary: bool) -> Entity {
ui! {
Button (
label,
grow: 1.0,
height: 30,
font_size: 12,
border_radius: 7,
normal_color: if primary { ColorToken::Primary } else { ColorToken::SurfaceVariant },
pressed_color: if primary { ColorToken::Secondary } else { ColorToken::Outline },
text_color: if primary { ColorToken::OnPrimary } else { ColorToken::OnSurface }
) on Tap {
commands!().scroll_to(
list,
ScrollOffset {
x: Fixed::ZERO,
y: Fixed::from_int(y),
},
);
}
}
}
#[compose]
fn scroll_scene(reduced: bool) {
ui! {
Column (
grow: 1.0,
padding: Padding::all(16),
row_gap: 10,
text_color: ColorToken::OnSurface
) {
Row (height: 32, column_gap: 6, align: AlignItems::Center) {
scroll_button ("Top (#1)", id("scroll_list"), 0, false)
scroll_button ("Middle (#4)", id("scroll_list"), 132, false)
scroll_button ("Bottom (#7)", id("scroll_list"), 264, true)
}
Column (
id: "scroll_list",
grow: 1.0,
min_height: 0,
padding: Padding::all(8),
row_gap: 6,
bg_color: ColorToken::SurfaceVariant,
border_radius: 10,
clip_children: true
) [
ScrollOffset::default(),
ScrollConfig::default().with_transition(indicator_transition(reduced)),
] {
scroll_item ("01 · FramebufSurface (MCU SPI)")
scroll_item ("02 · SdlSurface (Desktop CPU)")
scroll_item ("03 · SdlGpuSurface (OpenGL / Metal)")
scroll_item ("04 · WgpuSurface (Vulkan / DX12)")
scroll_item ("05 · WebCanvasSurface (Browser)")
scroll_item ("06 · LinuxDrmSurface (KMS / GBM)")
scroll_item ("07 · NuttxSurface (Embedded RTOS)")
}
}
};
}
//~focus-end:scrollAttaching ScrollOffset::default() and ScrollConfig::default().with_transition(...) turns a Column(clip_children: true) into a scrollable viewport; commands.scroll_to(list, offset) animates the container along that transition and redirects mid-flight if tapped again.