Skip to content

Commit

Permalink
Add animations example
Browse files Browse the repository at this point in the history
  • Loading branch information
lazytanuki committed Feb 8, 2025
1 parent 863963b commit 3e22619
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ auto-detect-theme = ["iced_core/auto-detect-theme"]
strict-assertions = ["iced_renderer/strict-assertions"]
# Redraws on every runtime event, and not only when a widget requests it
unconditional-rendering = ["iced_winit/unconditional-rendering"]
# Enables widget animations
animations = ["iced_widget/animations"]

[dependencies]
iced_core.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions examples/animations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "animations"
version = "0.1.0"
authors = ["LazyTanuki"]
edition = "2021"
publish = false

[dependencies]
iced.workspace = true
iced.features = ["animations"]
12 changes: 12 additions & 0 deletions examples/animations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Animations

An application to showcase Iced widgets that have default animations.

The __[`main`]__ file contains all the code of the example.

You can run it with `cargo run`:
```
cargo run --package animations
```

[`main`]: src/main.rs
49 changes: 49 additions & 0 deletions examples/animations/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use iced::{
widget::{column, Toggler},
Element, Task, Theme,
};

pub fn main() -> iced::Result {
iced::application("Animated widgets", Animations::update, Animations::view)
.theme(Animations::theme)
.run()
}

#[derive(Default)]
struct Animations {
toggled: bool,
}

#[derive(Debug, Clone)]
enum Message {
Toggle(bool),
}

impl Animations {
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::Toggle(t) => {
self.toggled = t;
Task::none()
}
}
}

fn view(&self) -> Element<Message> {
let main_text = iced::widget::text(
"You can find all widgets with default animations here.",
);
let toggle = Toggler::new(self.toggled)
.label("Toggle me!")
.on_toggle(Message::Toggle);
column![main_text, toggle]
.spacing(10)
.padding(50)
.max_width(800)
.into()
}

fn theme(&self) -> Theme {
Theme::Light
}
}

0 comments on commit 3e22619

Please sign in to comment.