复用组件与样式
当同一种按钮或列表行重复出现时,把宽高、圆角和颜色写在每一处会很难维护。mirui 用两个机制做复用:#[compose] 组件函数与 StyleSheetPlugin 样式表。
1. 最小演示:prop!(T) 与动态 class
先看一个只有两枚胶囊按钮的独立例子:
StyleSheetPlugin注册了公共类"pill",以及随状态切换的"pill.on"和"pill.off";#[compose] fn status_badge(...)接收prop!(String)和prop!(bool),调用方既可以传静态字面量,也可以传${...}响应式表达式:
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}
正在加载预览…
2. 带入任务清单:封装筛选按钮与完成行褪色
回到任务清单:我们把 Open 和 Done 提炼成 filter_button 组件,并在 lib.rs 里注册 task、task.complete 和 filter 三个样式类。当某一行被勾选时,interface.rs 里的 task_row 自动挂上 "task.complete",文字随之变为柔和的次要墨色:
CHAPTER 04 · COMPONENT & STYLESHEET UMLStylePreset 静态预设、StyleSheetPlugin 动态类与 prop!(T) 关系图
`StylePreset`(`style!(...)`)用于在编译期复用静态宽高与圆角;`StyleSheetPlugin` 则在 `lib.rs` 中注册运行时动态切换的 `.class(...)` 规则。
.class("task", style!(text_color: ColorToken::OnSurface)).class("task.complete", ...)Precedence: Inline prop > Class rule > StylePreset⋯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 );
正在加载预览…
这一步做了什么
prop!(T)零中间损耗转发:filter_button的title: prop!(String)不会在组件入口额外创建一层订阅,而是把读取器直接交给最终的Button(text: ${ title })。- 内联属性优先于
class:直接写在控件上的属性优先级高于样式表类。因此想让某个颜色随class动态切换时,不要在控件上写死内联text_color,统一交给StyleSheetPlugin管理。
在本机运行
sh
cargo run -p mirui-first-app --features desktop --example desktop -- 4