Skip to content

Commit bee65bf

Browse files
committed
Add animations example
1 parent 220901e commit bee65bf

File tree

6 files changed

+96
-2
lines changed

6 files changed

+96
-2
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ auto-detect-theme = ["iced_core/auto-detect-theme"]
6767
strict-assertions = ["iced_renderer/strict-assertions"]
6868
# Redraws on every runtime event, and not only when a widget requests it
6969
unconditional-rendering = ["iced_winit/unconditional-rendering"]
70+
# Enables widget animations
71+
animations = ["iced_widget/animations"]
7072

7173
[dependencies]
7274
iced_core.workspace = true

examples/animations/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "animations"
3+
version = "0.1.0"
4+
authors = ["LazyTanuki"]
5+
edition = "2021"
6+
publish = false
7+
8+
[dependencies]
9+
iced.workspace = true
10+
iced.features = ["animations"]

examples/animations/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Animations
2+
3+
An application to showcase Iced widgets that have default animations.
4+
5+
The __[`main`]__ file contains all the code of the example.
6+
7+
You can run it with `cargo run`:
8+
```
9+
cargo run --package animations
10+
```
11+
12+
[`main`]: src/main.rs

examples/animations/src/main.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use iced::{
2+
widget::{column, Toggler},
3+
Element, Task, Theme,
4+
};
5+
6+
pub fn main() -> iced::Result {
7+
iced::application("Animated widgets", Animations::update, Animations::view)
8+
.theme(Animations::theme)
9+
.run()
10+
}
11+
12+
#[derive(Default)]
13+
struct Animations {
14+
toggled: bool,
15+
}
16+
17+
#[derive(Debug, Clone)]
18+
enum Message {
19+
Toggle(bool),
20+
}
21+
22+
impl Animations {
23+
fn update(&mut self, message: Message) -> Task<Message> {
24+
match message {
25+
Message::Toggle(t) => {
26+
self.toggled = t;
27+
Task::none()
28+
}
29+
}
30+
}
31+
32+
fn view(&self) -> Element<Message> {
33+
let main_text = iced::widget::text(
34+
"You can find all widgets with default animations here.",
35+
);
36+
let toggle = Toggler::new(self.toggled)
37+
.label("Toggle me!")
38+
.on_toggle(Message::Toggle);
39+
column![main_text, toggle]
40+
.spacing(10)
41+
.padding(50)
42+
.max_width(800)
43+
.into()
44+
}
45+
46+
fn theme(&self) -> Theme {
47+
Theme::Light
48+
}
49+
}

widget/src/toggler.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ where
447447

448448
let mut children = layout.children();
449449
let toggler_layout = children.next().unwrap();
450-
let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>();
451450

452451
if self.label.is_some() {
453452
let label_layout = children.next().unwrap();
@@ -493,7 +492,7 @@ where
493492
style.background,
494493
);
495494

496-
let x_ratio = state.transition.interpolate(0.0, 1.0, state.now);
495+
let x_ratio = style.foreground_bounds_horizontal_progress;
497496
let toggler_foreground_bounds = Rectangle {
498497
x: bounds.x
499498
+ (2.0 * space + (x_ratio * (bounds.width - bounds.height))),
@@ -567,6 +566,8 @@ pub struct Style {
567566
pub foreground_border_width: f32,
568567
/// The [`Color`] of the foreground border of the toggler.
569568
pub foreground_border_color: Color,
569+
/// The horizontal progress ratio of the foreground bounds of the toggler.
570+
pub foreground_bounds_horizontal_progress: f32,
570571
}
571572

572573
/// The theme catalog of a [`Toggler`].
@@ -655,12 +656,25 @@ pub fn default(theme: &Theme, status: Status) -> Style {
655656
Status::Disabled => palette.background.base.color,
656657
};
657658

659+
let foreground_bounds_horizontal_progress = match status {
660+
Status::Active {
661+
is_toggled: _,
662+
animation_progress,
663+
} => animation_progress,
664+
Status::Hovered {
665+
is_toggled: _,
666+
animation_progress,
667+
} => animation_progress,
668+
Status::Disabled => 1.0,
669+
};
670+
658671
Style {
659672
background,
660673
foreground,
661674
foreground_border_width: 0.0,
662675
foreground_border_color: Color::TRANSPARENT,
663676
background_border_width: 0.0,
664677
background_border_color: Color::TRANSPARENT,
678+
foreground_bounds_horizontal_progress,
665679
}
666680
}

0 commit comments

Comments
 (0)