Skip to content
Development

Give the interface motion ​

Responsive motion must handle interruption gracefully: if a user double-taps a task while the progress bar is half-way across, the bar should reverse smoothly from its current position rather than snapping back to zero.

1. Minimal demo: FollowBounds spring selection ring ​

First, let's isolate the filter bar's selection ring. Below, three buttons have different widths, and a trailing View attaches FollowBounds::new(id("spring_first")).with_transition(...). When a button is tapped, pick_tab sets follower.target = target and the critically damped spring interpolates both position and width:

CONCEPT
recipes.rs Added in this step105 lines
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}
Loading preview…

Tap rapidly across MCU, Desktop, and Web—watch the blue outline reverse mid-flight while preserving velocity and resizing to the new target.


2. In the task list: retargetable progress tween + filter spring ​

Now we mount that FollowBounds ring inside filters (interface.rs) and add a custom Progress view driven by aim_progress and #[animate] (motion.rs) in the header:

CHAPTER 05 · ECS MOTION & VIEW UMLHow #[system], #[animate], #[view] & FollowBounds drive retargetable motion

On each frame, `aim_progress` computes `goal = completed / total`. If `target.0 != goal`, it starts a new motion from the **currently displayed `progress.0`** (`settings.0.motion(progress.0, goal)`).

commands.insert(entity, AnimateProgress(settings.0.motion(progress.0, goal)));Mid-flight retargeting without jump discontinuities
05 / 07
motion.rs · interface.rs · lib.rs Added in this step130 lines
⋯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 ]));
Loading preview…

Double-tap a row while the progress bar is still moving—watch it reverse smoothly from mid-flight. Then switch between Open and Done.

What changed in this step ​

  1. Retargeting from the displayed value: In motion.rs, whenever goal changes, aim_progress starts a new tween from the currently displayed progress.0 (settings.0.motion(progress.0, goal)).
  2. Zero cost at rest: #[animate(component = AnimateProgress)] updates Progress each frame and removes AnimateProgress from the entity as soon as the tween settles.

Run on your machine ​

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