Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 'Toggler' animation #2779

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions core/src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ where
self.raw.transition(new_state, Instant::now());
}

/// Instantaneously transitions the [`Animation`] from its current state to the given new state.
pub fn force(mut self, new_state: T) -> Self {
self.force_mut(new_state);
self
}

/// Instantaneously transitions the [`Animation`] from its current state to the given new state, by reference.
pub fn force_mut(&mut self, new_state: T) {
self.raw.transition_instantaneous(new_state, Instant::now());
}

/// Returns true if the [`Animation`] is currently in progress.
///
/// An [`Animation`] is in progress when it is transitioning to a different state.
Expand Down
6 changes: 4 additions & 2 deletions core/src/theme/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,17 @@ fn lighten(color: Color, amount: f32) -> Color {
from_hsl(hsl)
}

fn deviate(color: Color, amount: f32) -> Color {
/// Lighten dark colors and darken light ones by the specefied amount.
pub fn deviate(color: Color, amount: f32) -> Color {
if is_dark(color) {
lighten(color, amount)
} else {
darken(color, amount)
}
}

fn mix(a: Color, b: Color, factor: f32) -> Color {
/// Mix with another color with the given ratio (from 0 to 1)
pub fn mix(a: Color, b: Color, factor: f32) -> Color {
let a_lin = Rgb::from(a).into_linear();
let b_lin = Rgb::from(b).into_linear();

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
}
}
1 change: 1 addition & 0 deletions widget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ wgpu = ["iced_renderer/wgpu"]
markdown = ["dep:pulldown-cmark", "dep:url"]
highlighter = ["dep:iced_highlighter"]
advanced = []
animations = []

[dependencies]
iced_renderer.workspace = true
Expand Down
Loading