加上过渡动画
界面里的过渡动画经常会在中途被打断:进度条滑到一半时用户又点了一下,或者在宽度不同的筛选标签之间快速切换。这一章给顶部进度条加上可中途折返的补间(Tween),并用弹簧(Spring)驱动筛选栏的选中外框。
1. 最小演示:FollowBounds 弹簧跟随框
先单独看筛选栏的跟随框是怎么工作的。下面这个例子里,三枚按钮宽度各不相同,最后一个 View 挂上了 FollowBounds::new(id("spring_first")).with_transition(...)。点击任意按钮时,pick_tab 事件只需把 follower.target 指向被点击的实体,临界阻尼弹簧就会自动算好位置与宽度的过渡:
780#[model]
781#[derive(Default)]
782pub struct SpringTabs {
783 #[observe]
784 pub active: u8,
785}
786
787#[event]
788fn pick_tab(
789 index: u8,
790 tabs: model!(SpringTabs),
791 target: entity!(),
792 followers: query!(mut FollowBounds),
793) {
794 tabs.set_active(index);
795 for follower in followers {
796 follower.target = target;
797 }
798}
799
800#[model]
801impl SpringTabs {
802 pub fn set_active(&mut self, index: u8) {
803 self.active = index;
804 }
805}
806
807#[compose]
808fn spring_scene(tabs: model!(SpringTabs), reduced: bool) {
809 ui! {
810 Column (
811 grow: 1.0,
812 padding: Padding::all(20),
813 justify: JustifyContent::SpaceBetween,
814 text_color: ColorToken::OnSurface
815 ) {
816 Text (
817 "Tap any pill — FollowBounds retargets mid-flight",
818 height: 22,
819 font_size: 13,
820 text_color: ColorToken::OnSurfaceVariant,
821 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
822 )
823 Row (
824 height: 52,
825 padding: Padding::all(5),
826 column_gap: 6,
827 align: AlignItems::Center,
828 bg_color: ColorToken::SurfaceVariant,
829 border_radius: 12
830 ) {
831 Button (
832 "MCU",
833 id: "spring_first",
834 grow: 1.0,
835 height: 38,
836 border_radius: 8,
837 normal_color: ColorToken::Surface,
838 pressed_color: ColorToken::SurfaceVariant,
839 text_color: ColorToken::OnSurface
840 ) on Tap(pick_tab::callback(0, tabs))
841 Button (
842 "Desktop",
843 grow: 1.4,
844 height: 38,
845 border_radius: 8,
846 normal_color: ColorToken::Surface,
847 pressed_color: ColorToken::SurfaceVariant,
848 text_color: ColorToken::OnSurface
849 ) on Tap(pick_tab::callback(1, tabs))
850 Button (
851 "Web",
852 grow: 1.0,
853 height: 38,
854 border_radius: 8,
855 normal_color: ColorToken::Surface,
856 pressed_color: ColorToken::SurfaceVariant,
857 text_color: ColorToken::OnSurface
858 ) on Tap(pick_tab::callback(2, tabs))
+859 View (
+860 id: "spring_ring",
+861 border_width: 2,
+862 border_color: ColorToken::Primary,
+863 border_radius: 9
+864 ) [
+865 FollowBounds::new(id("spring_first"))
+866 .with_transition(indicator_transition(reduced)),
+867 ]
868 }
869 Text (
870 text: ${
871 match tabs.active() {
872 0 => "Target 01 · SPI framebuffer with dirty-rect clips",
873 1 => "Target 02 · Batched GPU draws with retained textures",
874 _ => "Target 03 · Browser canvas driven by requestAnimationFrame",
875 }
876 },
877 height: 20,
878 font_size: 12,
879 text_color: ColorToken::OnSurfaceVariant,
880 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
881 )
882 }
883 };
884}
正在加载预览…
2. 带入任务清单:可折返进度条 + 筛选跟随框
现在把这套弹簧外框装进任务清单的筛选栏(见 interface.rs),再在 motion.rs 里用 #[system] + #[animate] 给顶部标题栏加一条平滑补间的完成进度条:
CHAPTER 05 · ECS MOTION & VIEW UML#[system] 目标追踪、#[animate] 逐帧插值、#[view] 绘制与 FollowBounds 关系图
`aim_progress` 系统每帧计算目标比例 `goal = completed / total`。一旦目标变化,立即以**当前屏幕已显示的 `progress.0` 为起点**生成新轨迹,绝不生硬跳回 0。
commands.insert(entity, AnimateProgress(settings.0.motion(progress.0, goal)));Mid-flight retargeting without jump discontinuities⋯motion.rs:6
6#[component]
7pub(crate) struct Progress(pub Fixed);
8
9#[component]
10pub(crate) struct ProgressTarget(pub Fixed);
11
12pub(crate) struct MotionSettings(pub Transition);
⋯motion.rs:14
+14#[system(order = Animation)]
+15pub(crate) fn aim_progress(
+16 tasks: model!(Tasks),
+17 settings: res!(MotionSettings),
+18 meters: query!(Entity, mut ProgressTarget, Progress),
+19 commands: commands!(),
+20) {
+21 let goal = progress_ratio(tasks.completed(), tasks.total());
+22 for (entity, target, progress) in meters {
+23 if target.0 != goal {
+24 target.0 = goal;
+25 commands.insert(entity, AnimateProgress(settings.0.motion(progress.0, goal)));
+26 }
+27 }
+28}
+29
+30#[animate(component = AnimateProgress)]
+31pub(crate) fn apply_progress(value: motion!(), progress: component!(mut Progress)) {
+32 progress.0 = value;
+33}
34
35#[view(component = Progress, invalidation = Visual)]
36pub(crate) fn paint_progress(progress: component!(), theme: res!(Theme), paint: paint!()) {
37 let mut area = paint.rect();
38 area.w = area.w * progress.0.clamp(Fixed::ZERO, Fixed::ONE);
39 if area.w > Fixed::ZERO {
40 paint.draw(&DrawCommand::Fill {
41 area,
42 transform: paint.transform(),
43 quad: None,
44 color: ThemedColor::Token(ColorToken::Primary).resolve_in(&theme, paint.state()),
45 radius: Fixed::from_int(3),
46 opa: 255,
47 });
48 }
49}
⋯interface.rs:169
169#[compose]
170fn filters(tasks: model!(Tasks), chapter: Chapter, reduced: bool) -> Entity {
171 let (open, done) = (Filter::Open, Filter::Done);
172 ui! {
173 Row (
174 height: 38,
175 shrink: 0.0,
176 padding: Padding::all(3),
177 bg_color: ColorToken::SurfaceVariant,
178 border_radius: 10
179 ) {
180 all_tasks (tasks, chapter.has_motion().then(|| id("filter_marker")), chapter)
181 filter_button (
182 tasks,
183 "Open",
184 open,
185 chapter.has_motion().then(|| id("filter_marker")),
186 chapter
187 )
188 filter_button (
189 tasks,
190 "Done",
191 done,
192 chapter.has_motion().then(|| id("filter_marker")),
193 chapter
194 )
+195 View (
+196 id: "filter_marker",
+197 visible: ${ chapter.has_motion() },
+198 border_color: ColorToken::Primary,
+199 border_width: 2,
+200 border_radius: 6
+201 ) [
+202 FollowBounds::new(id("filter_all"))
+203 .with_transition(indicator_transition(reduced || !chapter.has_motion())),
+204 ]
205 }
206 }
207}
⋯interface.rs:237
237#[compose]
238pub(crate) fn task_list(
239 tasks: model!(Tasks),
240 draft: model!(TextDocument),
241 chapter: Chapter,
242 reduced: bool,
243) {
244 let start = progress_ratio(tasks.completed(), tasks.total());
245 ui! {
246 Column (
247 grow: 1.0,
248 min_height: 0,
249 padding: Padding::all(18),
250 row_gap: 9,
251 text_color: ColorToken::OnSurface,
252 font_size: 14
253 ) {
254 Column (
255 height: if chapter.has_motion() { 102 } else { 92 },
256 padding: Padding::all(14),
257 row_gap: 6,
258 shrink: 0.0,
259 bg_color: ColorToken::Secondary,
260 border_radius: 12
261 ) {
262 Text ("Task list", font_size: 30, height: 39, text_color: ColorToken::OnSecondary)
263 Text (
264 text: ${ format_args!("{} of {} complete", tasks.completed(), tasks.total()) },
265 capacity: 32,
266 font_size: 13,
267 height: 19,
268 text_color: ColorToken::OnSecondary
269 )
+270 if chapter.has_motion() {
+271 View (height: 4, bg_color: ColorToken::OnSurfaceVariant, border_radius: 2) [
+272 Progress(start),
+273 ProgressTarget(start),
+274 ]
+275 }
276 }
⋯interface.rs:317
317 }
318 };
319}
⋯lib.rs:87
+87 app.add_resource(motion::MotionSettings(motion::progress_transition(reduced)));
+88 app.with_widget(motion::paint_progress::view().with_systems([
+89 motion::aim_progress::system(tasks.clone()),
+90 motion::AnimateProgress::system(),
+91 ]));
正在加载预览…
这一步做了什么
- 从当前显示值起跳(Retargeting):在
motion.rs的aim_progress系统里,每当goal改变,新的补间都是从当前已经画在屏幕上的progress.0出发(settings.0.motion(progress.0, goal)),而不是从 0 重新开始。 - 动画结束即卸载:
#[animate(component = AnimateProgress)]在每一帧更新Progress,一旦抵达终点就会自动从实体上移除AnimateProgress组件,静止帧零额外开销。
在本机运行
sh
cargo run -p mirui-first-app --features desktop --example desktop -- 5