跳到正文
开发版

响应式状态与样式 ​

mirui 通过 #[model] 实例、Signal<T>、Computed<T> 与 StyleSheetPlugin 声明式地驱动控件属性与界面树结构。只要修改模型字段或信号,下一帧便只更新读取了该数据的控件——无需手写树比对或手动接线观察者。

关于 #[compose] 函数、res!(T) 资源借用、prop!(T) 转发、ECS 系统与自定义 #[view] 绘制描述符,请参阅组合、角色与自定义视图。关于自动落盘与启动恢复,请参阅状态快照与自动落盘。

Model
612#[model]
613pub struct Gauge {
614 #[observe]
615 pub level: i32,
616}
617
618impl Default for Gauge {
619 fn default() -> Self {
620 Self { level: 42 }
621 }
622}
623
624#[model]
625impl Gauge {
626 pub fn step(&mut self, delta: i32) {
627 self.level = (self.level + delta).clamp(0, 100);
628 }
629}
630
631#[compose]
632fn reactive_scene(gauge: model!(Gauge)) {
633 ui! {
634 Column (
635 grow: 1.0,
636 padding: Padding::all(20),
637 justify: JustifyContent::SpaceBetween,
638 text_color: ColorToken::OnSurface
639 ) {
640 Row (height: 28, justify: JustifyContent::SpaceBetween, align: AlignItems::Center) {
641 Text (
642 "Signal binding",
643 grow: 1.0,
644 height: 20,
645 font_size: 14,
646 text_color: ColorToken::OnSurfaceVariant,
647 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
648 )
649 Text (
650 text: ${ format_args!("{} %", gauge.level()) },
651 capacity: 12,
652 grow: 1.0,
653 font_size: 22,
654 height: 28,
655 paragraph: ParagraphStyle::label().with_align(TextAlign::End)
656 )
657 }
658 View (height: 14, bg_color: ColorToken::SurfaceVariant, border_radius: 7, clip_children: true) {
659 View (
660 width: ${ Dimension::percent(gauge.level()) },
661 height: Dimension::percent(100),
662 bg_color: ${ if gauge.level() >= 80 { ColorToken::Primary } else { ColorToken::Secondary } },
663 border_radius: 7
664 )
665 }
666 Row (height: 40, column_gap: 8) {
667 Button (
668 "- 10",
669 grow: 1.0,
670 height: 40,
671 border_radius: 9,
672 normal_color: ColorToken::SurfaceVariant,
673 pressed_color: ColorToken::Outline,
674 text_color: ColorToken::OnSurface
675 ) on Tap { gauge.step(-10); }
676 Button (
677 "42",
678 width: 64,
679 height: 40,
680 border_radius: 9,
681 normal_color: ColorToken::SurfaceVariant,
682 pressed_color: ColorToken::Outline,
683 text_color: ColorToken::OnSurfaceVariant
684 ) on Tap { gauge.step(42 - gauge.level()); }
685 Button (
686 "+ 10",
687 grow: 1.0,
688 height: 40,
689 border_radius: 9,
690 normal_color: ColorToken::Primary,
691 pressed_color: ColorToken::Secondary,
692 text_color: ColorToken::OnPrimary
693 ) on Tap { gauge.step(10); }
694 }
695 }
696 };
697}
正在加载预览…

点击 + 10 或 - 10 修改 Gauge::level——数字读数、百分比填充条宽度以及满载告警色会在同一帧内同步更新。

REACTIVE FLUSH PIPELINE响应式单帧合并刷新与局部脏区管线

回调将 `gauge.level` 从 40 改为 50。在最外层可变方法返回时,生成的句柄比对 `#[observe]` 前后快照(`40 != 50`)。

gauge.step(10); // 40 -> 50 (snapshot changed)Allocations: 0 B · Widget tree touched: No

已注册的模型实例 ​

在结构体与对应的固有 impl 块上标注 #[model],即可生成其实例句柄与转发方法。每次调用 app.add_model(value).finish() 都会注册一个独立的模型实例;克隆句柄只会共享同一个实例,不会复制底层业务数据。

rust
use mirui::prelude::*;

#[model]
struct Counter {
    #[observe]
    count: u32,
}

#[model]
impl Counter {
    #[observe]
    fn is_even(&self) -> bool { self.count % 2 == 0 }

    fn increment(&mut self) { self.count += 1; }
    fn decrement(&mut self) { self.count = self.count.saturating_sub(1); }
}

let mut app = App::headless(32, 32);
let counter = app.add_model(Counter { count: 0 }).finish();
counter.increment();
  • 字段读取器 (#[observe]):#[observe] 会为实现了 Clone + Eq 的字段(包含 String 与 Vec<T>)自动生成同名读取方法。在 ${...} 内部调用 counter.count() 即可让当前控件属性订阅该字段。若字段最终值未变(或在同一事务内改完又改回原值),不会通知订阅者。字段读取器返回值的克隆副本,不直接借用模型内部内存。
  • 派生读取器 (#[observe] fn):在接收 &self、无额外参数且返回值满足 Clone + Eq 的方法上标注 #[observe],只有当计算结果发生变化时才会向外发布更新。
  • 可失败事务 (Result<Change, E>):可变方法可返回模型的变更类型(默认为 ())或 Result<Change, E>。一旦返回 Err,框架会跳过本次观察发布与修订号递增,并丢弃待发送的副作用。请务必在修改模型字段之前完成校验——返回 Err 不会自动回滚已经写入的字段。
  • 具名变更掩码 (watch):对于不适合按值克隆比对的大对象变更,可声明 #[model(change = ChangeSet, watch(visual = ChangeSet::VISUAL))]。当可变方法返回的 ChangeSet 包含 VISUAL 位时,model.visual_revision() 递增并唤醒所有观察该通道的读取者。
  • 业务副作用提取器 (#[effects]):在 #[model] impl 块中声明返回 Option<E> 或 [Option<E>; N] 的提取方法:
rust
#[effects]
fn take_note(&mut self) -> Option<Note> {
    self.pending_note.take()
}

player.on_effect(move |note: &Note| play_note(note.pitch))?;

model.on_effect(handler) 为每个模型实例的每种事件类型安装一个消费者。副作用在状态发布完毕且模型数据借用释放之后派发;消费者可以读取已提交的最新状态,但不能在回调内开启新的模型写入事务。

内联子模型 ​

在 #[model] 结构体内部声明 child: model!(owned Child),即可将子模型内联存储在父模型中。构造时通过 .into() 转换原始结构体,且只需在 App 上注册最外层的父模型。集合类型同样使用 model!(owned ModelList<Item>)、model!(owned ModelMap<Key, Item>) 或 model!(owned ModelSet<Key>)(详见有界列表与键值复用)。

rust
use mirui::prelude::*;

#[model]
struct Scoreboard {
    left: model!(owned Counter),
    right: model!(owned Counter),
}

#[model]
impl Scoreboard {
    fn new() -> Self {
        Self {
            left: Counter { count: 0 }.into(),
            right: Counter { count: 0 }.into(),
        }
    }

    #[observe]
    fn total(&self) -> u32 {
        self.left.count() + self.right.count()
    }

    fn increment_both(&mut self) {
        self.left.increment();
        self.right.increment();
    }
}

无论通过父句柄还是子句柄调用可变方法,最外层调用结束时都会一次性提交整棵已注册的模型树。所有被观察字段与派生读取器只在最终值确实改变时发布一次通知。

模型角色参数 (model!(T)) ​

model!(T) 角色宏让函数签名与组件字段无需暴露底层的生成句柄名。在 #[component] 中声明 model!(T) 字段可持有一个已注册实例,在 #[compose] 函数参数中声明 model!(T) 则可接收该实例:

rust
use mirui::prelude::*;
use mirui::ui::widgets::{Button, Text};

#[compose]
fn counter_value(counter: model!(Counter), label: &'static str) -> Entity {
    ui! {
        Text(text: ${ format!("{label}: {}", counter.count()) })
    }
}

#[compose]
fn counter_controls(counter: model!(Counter)) {
    ui! {
        Row {
            Button(text: "−") on Tap { counter.decrement(); }
            Button(text: "+") on Tap { counter.increment(); }
            counter_value(counter, "count")
        }
    };
}

ui! 生成的事件回调与直接调用的子组合函数会自动共享 counter 句柄,无需在每个使用处手写 .clone()。


响应式原语 (Signal、Computed、Effect) ​

mirui::core::reactive 提供了三个独立的响应式原语:

rust
use mirui::core::reactive::{Signal, Computed, Effect};

let count = Signal::new(0i32);          // 持有值并追踪读取者
count.set(1);                           // 替换新值
count.update(|n| *n += 1);              // 原地修改
let n = count.get();                    // 读取并订阅当前上下文
count.with(|n| println!("{n}"));        // 借用读取(不克隆)

let doubled = {
    let count = count.clone();
    Computed::new(move || count.get() * 2)   // 惰性派生,仅在依赖变化且被读取时重算
};

let _effect = Effect::new(move || {
    // 每当内部读取的信号发生变化时重新执行
});
  • Signal<T> 持有独立的响应式值。get() / with() 将当前读取者注册为订阅方;set() / update() 将所有订阅方标记为脏。
  • Computed<T> 惰性派生值:仅在输入源发生变化且自身被读取时重新计算。
  • Effect 在创建时立即执行一次闭包,并在其读取的任意信号变脏后于帧刷新阶段重新运行。在组合函数内,可使用 effect_with_widget(entity, closure) 将副作用的生命周期绑定到具体控件实体。

关于 #[model]、Signal<T>、#[component]、#[compose] 与 #[view] 的职责对照表,请参阅选择状态与渲染角色。


可复用样式 ​

style! 宏在编译期构造强类型的样式预设(StylePreset)。通过 style: 属性将其应用到控件上,同时可以直接在控件上覆盖个别字段。

rust
const STEP: StylePreset = style!(grow: 1.0, height: 44, border_radius: 22);

ui! {
    Row (column_gap: 4) {
        Button (style: STEP, text: "-")
        Button (style: STEP, height: 48, text: "+")
    }
};

样式表类规则 ​

安装 StyleSheetPlugin 时可注册全局共享的样式类规则。控件可以声明单个类名、有序类名列表,或通过 ${...} 动态切换类名。当多个类设置了同一字段时,排在后面的类生效;控件自身的 style: 预设与直接内联书写的字段拥有更高优先级。

StyleSheetPlugin
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}
正在加载预览…

点击任意一枚胶囊开关——两枚按钮共用 "pill" 基础类,并根据自身状态在 "pill.on" 与 "pill.off" 之间动态切换。

在 StyleSheetPlugin 或 StyleSheet 上调用 .bind_class(name, getter),可以让某条样式类规则根据响应式状态动态计算;无论多少个控件引用该类,每次更新只计算一次:

rust
#[compose]
fn register_compact_steps() {
    res!(mut StyleSheet).with(|mut styles| {
        styles.set("step", style!(grow: 1.0, height: 36, border_radius: 18));
    });
}

响应式属性 ​

在 ui! 中,为属性值加上 $ 前缀即可建立响应式绑定。attr: $signal 是 attr: ${ signal.get() } 的简写:

rust
let label = Signal::new(0i32);

#[compose]
fn counter_label(label: Signal<i32>) {
    ui! {
        Text (text: ${ alloc::format!("Count: {}", label.get()) }, height: 40)
    };
}

当 label 变化时,框架只更新该控件的 text 属性,不会销毁重建控件。支持响应式绑定的属性涵盖:

  • 布局与几何:width、height、left、top、direction、visible、scroll_offset
  • 视觉与样式:style、class、bg_color、text_color、border_color、border_width、render_key、Button.normal_color
  • 文本与路径:text、font_size、Text.paragraph、path
  • 交互控件值:ProgressBar.value、Slider.value、Switch.on、Checkbox.checked

通过响应式绑定程序化更新 Slider.value、Switch.on 或 Checkbox.checked 时,控件状态与过渡动画会同步更新,但不会派发用户交互通知事件(ValueChanged / Toggled)。


几何响应式属性 ​

$ 响应应用业务状态,而 @ 则响应布局阶段算出的几何尺寸,并在头部显式列出所依赖的几何源:

@width & @id(stage)
2167#[compose]
2168fn state_at_scene() {
2169 ui! {
2170 Column (
2171 container: true,
2172 grow: 1.0,
2173 padding: Padding::all(18),
2174 justify: JustifyContent::SpaceBetween,
2175 text_color: ColorToken::OnSurface
2176 ) {
2177 Text (
2178 "Drag the bottom-right grip to cross 320 px",
2179 height: 28,
2180 font_size: @width {
2181 if width < Fixed::from_int(320) { 13_u16 } else { 18_u16 }
2182 },
2183 text_color: ColorToken::Primary
2184 )
2185 View (
2186 id: "stage",
2187 width: Dimension::percent(100),
2188 height: 44,
2189 padding: Padding::all(12),
2190 bg_color: ColorToken::Secondary,
2191 border_radius: 8
2192 ) {
2193 Text ("#stage (100% width)", height: 20, font_size: 13, text_color: ColorToken::OnSecondary)
2194 }
2195 View (
2196 width: @id(stage).width { stage.width / 2 },
2197 height: 36,
2198 padding: Padding::all(8),
2199 bg_color: ColorToken::SurfaceVariant,
2200 border_radius: 8
2201 ) {
2202 Text ("@id(stage).width / 2", height: 20, font_size: 12)
2203 }
2204 }
2205 };
2206}
正在加载预览…

拖动预览框右下角的圆形手柄改变视口宽度,当容器宽度跨过 320 px 时,顶部标题通过 @width 自动切换字号,底部卡片则通过 @id(stage).width / 2 始终保持 #stage 一半的宽度。

rust
ui! {
    Column (container: true) {
        Text(
            "Status",
            font_size: @width {
                if width < Fixed::from_int(520) { 18_u16 } else { 30_u16 }
            },
            padding: @(width, height) {
                Padding::all(width.min(height) / 24)
            }
        )

        View(id: "stage", width: Dimension::percent(100), height: 180)

        Text(
            "Stage label",
            width: @id(stage).width { stage.width },
            top: @id("stage").height as stage_height { stage_height / 8 }
        )
    }
}

不带限定名的 width 与 height 会向上查找最近一个标记了 container: true 的祖先容器;具名依赖(@id(stage).width)则通过控件 ID 注册表解析。单个控件最多可声明 4 个不同的几何依赖,全程零运行时堆分配。


响应式控制流 ​

在控制流头部加上 $ 即可让分支切换具备响应性:

rust
#[compose]
fn status_panel(show: Signal<bool>, other: Signal<bool>, state: Signal<Load>) {
    ui! {
        Column (grow: 1.0) {
            if ${ show.get() } {
                Text ("visible")
            } elif ${ other.get() } {
                Text ("alt")
            } else {
                Text ("hidden")
            }
            match ${ state.get() } {
                Load::Loading => { Text ("loading") }
                Load::Ready(s) => { Text (s) }
            }
        }
    };
}
  • if $cond / elif / else 会在首次构建时创建所有分支,随后仅通过切换可见性来呈现对应分支,不会丢失各分支内部的控件状态。
  • match $expr 在所有分支模式都不绑定变量(例如 Load::Loading)时会缓存全部分支;只要任意分支绑定了载荷(如 Load::Ready(s)),当源值改变时就会重建当前激活的分支,以便分支体拿到最新的载荷值。
  • 不带 $ 的 if / match 是纯静态控制流——仅在首次构建界面树时求值一次,后续不再重算。

列表迭代 ​

walk 用于遍历集合生成多行子控件。配合 $ 使用时,集合内容一变就会自动协调行节点:

  • 按位置索引(walk ${ items.get() } with item):行节点按位置下标对齐。列表变长时在末尾追加新行,变短时销毁末尾多余行。适合只追加或截断尾部的日志列表。
  • 按稳定键值(walk ${ items.entries() } with row by row.0):行节点按稳定身份(ModelItemId 或业务键)对齐。当在列表中间插入、删除或重排元素时,存活下来的行会完整保留原有的实体、焦点与动画状态。

关于有界 ModelList、ModelMap 与 ModelSet 的详细接口,请参阅有界列表与键值复用。


单帧合并刷新机制 ​

在回调中调用 signal.set(...) 或修改 #[model] 并不会当场去改写控件树,而是将受影响的订阅者标脏并推入待处理队列。在每一帧的调度流水线中——系统(Systems)与插件更新执行完毕后、布局计算开始前——flush_signal_dirty 会一次性排空队列:重跑脏 Effect 并应用脏控件属性。同一帧内对同一状态源的多次连续修改会自动合并为单次界面更新。


边界条件与契约 ​

  • 可观察值类型要求:#[observe] 字段与派生方法返回值必须实现 Clone + Eq。每个已注册模型实例会为其被观察字段保留一份快照以供事务提交时比对。
  • 自定义视图的订阅声明:在 #[view] 参数里仅写 model!(component.field) 不会建立自动重绘订阅;必须显式加上 watch(...) 指明要监听的字段、派生方法或具名修订号。绘制回调本身是只读的。
  • Computed 与 Effect 归属权:在某个 App 内创建的 Computed 与 Effect 归该 App 所有,不能跨两个不同的 App 实例混读模型。
  • 缓存分支约束 (if $ / match $):未激活的缓存分支会暂停其显示属性绑定,直到再次被选中;不同分支内部的控件具名 id 必须保持全局唯一。若分支内部还嵌套了动态 if $、match $ 或 walk,请将它们包裹在一层容器控件内,使动态子节点始终挂在分支根节点之下。