Skip to content
Development

Reuse components and styles ​

As an interface grows, repeating dimensions, radii, and colors on every widget quickly clutters the tree. mirui keeps trees concise with #[compose] functions and StyleSheetPlugin classes.

1. Minimal demo: prop!(T) and reactive class ​

Here is a two-pill example in isolation:

  • StyleSheetPlugin registers a base "pill" rule alongside state-specific "pill.on" and "pill.off" rules;
  • #[compose] fn status_badge(...) accepts prop!(String) and prop!(bool), allowing callers to pass either static literals or reactive ${...} expressions:
CONCEPT
113 app.add_plugin(
114 StyleSheetPlugin::new()
115 .class("pill", style!(height: 36, border_radius: 8))
116 .class("pill.on", style!(text_color: ColorToken::Primary))
117 .class("pill.off", style!(text_color: ColorToken::OnSurfaceVariant)),
118 );
⋯recipes.rs:483
483#[model]
484#[derive(Default)]
485pub struct MiniPill {
486 #[observe]
487 pub active: bool,
488}
489
490#[model]
491impl MiniPill {
492 pub fn toggle(&mut self) {
493 self.active = !self.active;
494 }
495}
496
497#[compose]
498fn status_badge(title: prop!(String), active: prop!(bool), pill: model!(MiniPill)) -> Entity {
499 ui! {
500 Button (
501 text: ${ title },
502 grow: 1.0,
503 normal_color: ColorToken::SurfaceVariant,
504 pressed_color: ColorToken::Outline,
505 class: ${ ("pill", if active.get() { "pill.on" } else { "pill.off" }) }
506 ) on Tap { pill.toggle(); }
507 }
508}
509
510#[compose]
511fn mini_style_scene(pill: model!(MiniPill)) {
512 ui! {
513 Column (
514 grow: 1.0,
515 padding: Padding::all(20),
516 justify: JustifyContent::SpaceBetween,
517 text_color: ColorToken::OnSurface
518 ) {
519 Text (
520 "Both buttons share #[compose] & the 'pill' stylesheet class",
521 height: 20,
522 font_size: 13,
523 text_color: ColorToken::OnSurfaceVariant
524 )
525 Row (height: 44, column_gap: 10, align: AlignItems::Center) {
526 status_badge (
527 ${ if pill.active() { "● Active class (.pill.on)" } else { "○ Muted class (.pill.off)" } },
528 ${ pill.active() },
529 pill
530 )
531 status_badge ("Mirror badge", ${ !pill.active() }, pill)
532 }
533 }
534 };
535}
Loading preview…

Tap either pill—both badges flip between .pill.on and .pill.off through their reactive class bindings.


2. In the task list: reusable filter tabs and completed row styles ​

Back in the task list, we extract filter_button in interface.rs and register the task, task.complete, and filter classes in lib.rs. When a task is checked off, task_row attaches "task.complete" and dims its label automatically:

CHAPTER 04 · COMPONENT & STYLESHEET UMLStylePreset, StyleSheetPlugin classes & prop!(T) forwarding

`StylePreset` (`style!(...)`) shares static compile-time layout/shape properties, while `StyleSheetPlugin` registers runtime `.class(...)` rules in `lib.rs`.

.class("task", style!(text_color: ColorToken::OnSurface)).class("task.complete", ...)Precedence: Inline prop > Class rule > StylePreset
04 / 07
interface.rs · lib.rs Added in this step97 lines
⋯interface.rs:51
51#[compose]
52fn task_row(
53 tasks: model!(Tasks),
54 task: model!(Task),
55 draft: model!(TextDocument),
56 key: ModelItemId,
57 chapter: Chapter,
58) -> Entity {
59 let title = task.read(|task| task.title.clone());
60 ui! {
61 Column (visible: ${ tasks.filter().includes(task.done()) }, height: 49, shrink: 0.0) {
62 Row (height: 48, align: AlignItems::Center) {
63 Button (
64 grow: 1.0,
65 min_width: 0,
66 height: 44,
67 clip_children: true,
68 normal_color: ColorToken::Surface,
69 pressed_color: ColorToken::SurfaceVariant,
70 class: ${
71 (
72 chapter.has_presets().then_some("task"),
73 (chapter.has_presets() && task.done()).then_some("task.complete"),
74 )
75 }
76 ) {
77 Row (grow: 1.0, min_width: 0, align: AlignItems::Center, column_gap: 12) {
78 Column (
79 width: 20,
80 height: 20,
81 shrink: 0.0,
82 border_width: 1,
83 border_radius: 6,
84 align: AlignItems::Center,
85 justify: JustifyContent::Center,
86 bg_color: ${ if task.done() { ColorToken::Primary } else { ColorToken::Surface } },
87 border_color: ${ if task.done() { ColorToken::Primary } else { ColorToken::OnSurfaceVariant } }
88 ) {
89 Icon (
90 visible: ${ task.done() },
91 path: ICON_CHECK,
92 width: 14,
93 height: 14,
94 color: ColorToken::OnPrimary
95 )
96 }
97 Text (
98 text: title,
99 grow: 1.0,
100 min_width: 0,
101 height: 22,
102 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
103 )
104 }
105 } on Tap { task.toggle(); }
106 if chapter.has_list() {
107 Button (
108 "Remove",
109 width: 54,
110 height: 44,
111 font_size: 12,
112 normal_color: ColorToken::Surface,
113 pressed_color: ColorToken::SurfaceVariant,
114 text_color: ColorToken::OnSurfaceVariant
115 ) on Tap(edit_tasks::callback(Edit::Remove(key), tasks, draft))
116 }
117 }
118 View (height: 1, bg_color: ColorToken::Outline)
119 }
120 }
121}
⋯interface.rs:138
138#[compose]
139fn filter_button(
140 tasks: model!(Tasks),
141 title: prop!(String),
142 filter: Filter,
143 marker: Option<Entity>,
144 chapter: Chapter,
145) -> Entity {
146 ui! {
147 Button (
148 text: ${ title },
149 style: filter_style(),
150 normal_color: ${ tab_surface(tasks.filter() == filter) },
151 class: chapter.has_presets().then_some("filter")
152 ) on Tap(select_filter::callback(filter, tasks, marker))
153 }
154}
⋯lib.rs:75
75 app.add_plugin(
76 StyleSheetPlugin::new()
77 .class("task", style!(text_color: ColorToken::OnSurface))
78 .class(
79 "task.complete",
80 style!(text_color: ColorToken::OnSurfaceVariant),
81 )
82 .class("filter", style!(font_size: 13)),
83 );
Loading preview…

Toggle a few rows and compare the muted ink of completed items against open ones. Click the lib.rs tab in the source header to see the StyleSheetPlugin registration.

What changed in this step ​

  1. Zero-overhead prop!(T) forwarding: Declaring title: prop!(String) on filter_button forwards the getter directly to the inner Button(text: ${ title }) without creating an intermediate subscription.
  2. Inline attributes override classes: An attribute written directly on a widget takes precedence over a stylesheet class. Keep state-driven colors in StyleSheetPlugin so class switches can control them cleanly.

Run on your machine ​

sh
cargo run -p mirui-first-app --features desktop --example desktop -- 4