Skip to content
  1. Docs
  2. enzUI framework
  3. Developers

Motion

What animates by default, visibility transitions, springs, animated lists and images, and how to animate a custom widget.

Framework widgets animate their own state changes, so most apps write no motion code. When you want more, wrap an element in a motion helper, or drive a spring yourself. Everything follows the theme's motion scale and the user's reduced-motion preference.

The gallery's Animation and motion page

What animates by default

WidgetMotionDuration
Buttons, icon buttons, segmented controls, tabs, menu rows, links, pagination and every other control built on FocusButtonFill, text, border and shadow fade between rest, hover, release and selected; a press lands at oncemotion-fast
Text, search, password, chip, time and autocomplete fieldsBorder and fill fade between rest, hover and focusmotion-fast
Checkbox, radioFill, border and dot fade; the tick or dot pops in and shrinks awaymotion-fast, 160 ms pop
Switch, toggleThe thumb slidesmotion-normal
Slider, range sliderThumbs glide to a value set by a key or the app (a drag stays direct); the knob swells under the pointermotion-fast, 140 ms
Progress barThe fill glides; with .busy(true) a highlight sweeps along a part-done fillmotion-normal
GaugeThe ring sweeps to a new valuemotion-normal
Disclosure, expander, tree rowsThe chevron turns; the section expands160 ms, motion-normal
Menus, popovers, pickers, combo box, context menusOpen with a fade and a small scale from the anchored edge140 ms
ToastsRise into the stack and fade intheme spring
Navigation split view drawerSlides in over a fading scrimtheme spring
Carousel, wizardThe new page or step fades inmotion-normal

Changing the defaults

  • The whole app: the theme's motion scale. motion.reduced_motion = true (in code on EnzTheme.motion, or in the user's enzui/theme.toml) lands every change at once; the desktop's reduced-motion setting does the same unless the theme says otherwise. fast_ms, normal_ms, slow_ms and easing retime every widget at once.
  • One widget: .reduced_motion(true) on a builder (FocusButton, FieldChrome, EnzToggle, EnzGauge, lists, trees, menus, spinners and others) lands that widget's changes at once; false animates it even under a reduced-motion theme. A progress bar takes .animated(false).
  • For a session: ENZUI_MOTION_SCALE=4 multiplies every duration, to watch motion slowly while developing.

Showing and hiding

enzui_widgets::motion::motion(child, visible, effect) animates an element's visibility with a MotionEffect: Slide, Size, Scale or Fade. Effects compose by nesting. The wrapper keeps the child while it exits and removes its tree once hidden, requests frames only while needed, and applies reduced motion without an app timer.

rust
use enzui_widgets::motion::{MotionEffect, SlideDirection, motion};

let panel = motion(
    container(enz_text("A panel on the same spring")).padding(16),
    self.panel,
    MotionEffect::Slide(SlideDirection::Left),
)
.spring(Some(spring));
  • Size changes reserved layout space. By default it holds the wider endpoint and lays out once, at the end; .reflow(true) lays the reservation out on every frame instead, so a list beside an inspector narrows with it.
  • Slide, scale and fade do not invalidate layout on every frame.
  • .enter_on_mount() gives an element an entrance the first time it appears.
  • cross_fade keeps both endpoints and reserves their larger size, so budget for two trees.

Three more helpers:

  • motion::animated(child) springs a child to the offset and opacity each view gives it, and .enters_from(offset, opacity) gives it an entrance when first mounted.
  • motion::fade_on_change(key, child) fades a child in whenever key changes (another item, another step) and gives it fresh widget state.
  • keyed_column(rows).animate_moves() slides rows to their new places when rows arrive, leave or reorder, with exact layout on every frame.

Springs

enzui_theme::Spring describes a spring by stiffness and damping ratio; duration() and bounce() give the equivalent perceptual parameters. Presets include Spring::SMOOTH, Spring::SNAPPY and Spring::BOUNCY, and ExpressiveMotion provides Material 3 expressive and standard springs (ExpressiveMotion::DefaultSpatial.spring(), .standard_spring()).

motion::SpringAnimation<T> drives a value on a spring and handles reversals mid-flight without stopping. With enzui_core::velocity::VelocityTracker it can carry the speed of a throw:

rust
// Pointer released: hand the measured speed to the spring.
self.tracker.add(now, [at.x, at.y]);
let [vx, vy] = self.tracker.velocity(now);
self.card.release(from, Point::new(vx, vy), target, now);

// While it moves, ask for frames.
fn subscription(&self) -> Subscription<Message> {
    if self.card.is_animating(Instant::now()) {
        iced::window::frames().map(Message::Frame)
    } else {
        Subscription::none()
    }
}

snap(value) moves it at once (to follow a held pointer), set_spring(..) changes the spring, and value(now) reads the current position.

Animated images and SVG

enz_animated_image and enz_animated_svg play GIF, APNG, animated WebP and SVG (SMIL and CSS) animation. They draw only when the picture changes, pause out of view, and show their first frame under reduced motion. See Media and images.

enz_frame_clock wraps any element so it animates itself from frame timestamps, with no app-side tick and no per-frame view() rebuild.

Animating a custom widget

A widget that animates itself from update on Event::Window(RedrawRequested(now)) asks the shell for its next frame. Which request it makes decides how that frame is produced:

  • Its paint or layout changes: shell.request_redraw(). A caret, a counter, a meter, or any widget that moves, resizes or reflows content itself. The frame is drawn.
  • It only moves, fades, scales, rotates or clips a recorded node: shell.request_track_frame() with shell.publish_track(..). The widget takes a node once (motion::next_scene_node()), records its paint under it in draw, and the runtime moves the last recorded frame along the tracks on the render lane, off the UI thread. Until the node has been recorded once, ask for request_redraw() instead.
  • Its siblings must move too: shell.invalidate_layout(), once rather than every frame where possible.
  • Later: shell.request_redraw_at(instant).

The rule: if a frame would look wrong when the last recorded picture is moved along the published tracks, use request_redraw(). enzui_frame_check::FrameCheck tests this by comparing replayed frames with full redraws; see Testing.