Skip to content

Commit 3850416

Browse files
committed
Add anim!() macro
Simplifies the animation code in the `view()`
1 parent e4e56e6 commit 3850416

File tree

9 files changed

+74
-19
lines changed

9 files changed

+74
-19
lines changed

examples/pokedex/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use iced::{
66
};
77

88
use cosmic_time::{
9-
self, chain, keyframes, Back, Bounce, Circular, Ease, Elastic, Exponential, Linear, Quadratic,
10-
Quartic, Quintic, Sinusoidal, Timeline,
9+
self, anim, chain, keyframes, Back, Bounce, Circular, Ease, Elastic, Exponential, Linear,
10+
Quadratic, Quartic, Quintic, Sinusoidal, Timeline,
1111
};
1212
use once_cell::sync::Lazy;
1313

@@ -165,7 +165,7 @@ impl Pokemon {
165165
fn view(&self) -> Element<Message> {
166166
row![
167167
column![
168-
keyframes::Space::as_widget(SPACE.clone(), &self.timeline),
168+
anim!(SPACE, &self.timeline),
169169
image::viewer(self.image.clone())
170170
],
171171
column![

examples/pong/src/main.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use iced::widget::{column, container, row, Space};
55
use iced::{executor, Application, Command, Event, Length, Settings, Subscription};
66
use iced_native::window;
77

8-
use cosmic_time::{self, chain, keyframes, Duration, Instant, Speed, Timeline};
8+
use cosmic_time::{self, anim, chain, keyframes, Duration, Instant, Speed, Timeline};
99

1010
use once_cell::sync::Lazy;
1111
use rand::prelude::*;
@@ -167,21 +167,15 @@ impl Application for Pong {
167167
let paddle_right = container(Space::new(width, height)).style(theme::Container::Paddle);
168168

169169
let content = row![
170-
column![
171-
keyframes::Space::as_widget(PADDLE_LEFT.clone(), &self.timeline),
172-
paddle_left
173-
],
170+
column![anim!(PADDLE_LEFT, &self.timeline), paddle_left],
174171
Space::new(Length::Fill, Length::Fill),
175-
column![
176-
keyframes::Space::as_widget(PADDLE_RIGHT.clone(), &self.timeline),
177-
paddle_right
178-
],
172+
column![anim!(PADDLE_RIGHT, &self.timeline), paddle_right],
179173
];
180174

181175
let ball = column![
182-
keyframes::Space::as_widget(BALL_Y.clone(), &self.timeline),
176+
anim!(BALL_Y, &self.timeline),
183177
row![
184-
keyframes::Space::as_widget(BALL_X.clone(), &self.timeline),
178+
anim!(BALL_X, &self.timeline),
185179
container(Space::new(width, width)).style(theme::Container::Ball)
186180
]
187181
];

examples/stopwatch/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use self::widget::Element;
88
use theme::Theme;
99

1010
use cosmic_time::{
11-
self, chain,
11+
self, anim, chain,
1212
style_button::{self, StyleButton},
1313
style_container::{self, StyleContainer},
1414
Sinusoidal, Timeline,
@@ -145,8 +145,8 @@ impl Application for Stopwatch {
145145
State::Ticking { .. } => "Stop",
146146
};
147147

148-
StyleButton::as_widget(
149-
BUTTON.clone(),
148+
anim!(
149+
BUTTON,
150150
buttons,
151151
&self.timeline,
152152
text(label).horizontal_alignment(alignment::Horizontal::Center),
@@ -166,8 +166,8 @@ impl Application for Stopwatch {
166166
.align_items(Alignment::Center)
167167
.spacing(20);
168168

169-
StyleContainer::as_widget(
170-
CONTAINER.clone(),
169+
anim!(
170+
CONTAINER,
171171
// Cool! Because we implemented the function on our custom, theme's type, adding
172172
// the map argument is easy!
173173
theme::Container::map(),

src/keyframes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ macro_rules! chain{
2626
};
2727
}
2828

29+
#[macro_export]
30+
macro_rules! anim{
31+
($id:expr, $($x:expr),+ $(,)?) => {
32+
$id.clone().as_widget($($x),+)
33+
};
34+
}
35+
2936
#[derive(Debug, Copy, Clone, PartialEq, Default)]
3037
pub enum Repeat {
3138
#[default]

src/keyframes/button.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ impl Id {
2828
pub fn to_chain_with_children(self, children: Vec<Button>) -> Chain {
2929
Chain::with_children(self, children)
3030
}
31+
32+
pub fn as_widget<'a, Message, Renderer>(
33+
self,
34+
timeline: &crate::Timeline,
35+
content: impl Into<Element<'a, Message, Renderer>>,
36+
) -> widget::Button<'a, Message, Renderer>
37+
where
38+
Renderer: iced_native::Renderer,
39+
Renderer::Theme: widget::button::StyleSheet,
40+
{
41+
Button::as_widget(self, timeline, content)
42+
}
3143
}
3244

3345
impl From<Id> for widget::Id {

src/keyframes/container.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ impl Id {
2828
pub fn to_chain_with_children(self, children: Vec<Container>) -> Chain {
2929
Chain::with_children(self, children)
3030
}
31+
32+
pub fn as_widget<'a, Message, Renderer>(
33+
self,
34+
timeline: &crate::Timeline,
35+
content: impl Into<Element<'a, Message, Renderer>>,
36+
) -> widget::Container<'a, Message, Renderer>
37+
where
38+
Renderer: iced_native::Renderer,
39+
Renderer::Theme: widget::container::StyleSheet,
40+
{
41+
Container::as_widget(self, timeline, content)
42+
}
3143
}
3244

3345
impl From<Id> for widget::Id {

src/keyframes/space.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ impl Id {
2828
pub fn to_chain_with_children(self, children: Vec<Space>) -> Chain {
2929
Chain::with_children(self, children)
3030
}
31+
32+
pub fn as_widget(self, timeline: &crate::Timeline) -> widget::Space {
33+
Space::as_widget(self, timeline)
34+
}
3135
}
3236

3337
impl From<Id> for widget::Id {

src/keyframes/style_button.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ impl Id {
2929
pub fn to_chain_with_children(self, children: Vec<StyleButton>) -> Chain {
3030
Chain::with_children(self, children)
3131
}
32+
33+
pub fn as_widget<'a, Message, Renderer>(
34+
self,
35+
style: fn(u8) -> <Renderer::Theme as StyleSheet>::Style,
36+
timeline: &crate::Timeline,
37+
content: impl Into<Element<'a, Message, Renderer>>,
38+
) -> crate::widget::Button<'a, Message, Renderer>
39+
where
40+
Renderer: iced_native::Renderer,
41+
Renderer::Theme: widget::button::StyleSheet,
42+
{
43+
StyleButton::as_widget(self, style, timeline, content)
44+
}
3245
}
3346

3447
impl From<Id> for widget::Id {

src/keyframes/style_container.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ impl Id {
2929
pub fn to_chain_with_children(self, children: Vec<StyleContainer>) -> Chain {
3030
Chain::with_children(self, children)
3131
}
32+
33+
pub fn as_widget<'a, Message, Renderer>(
34+
self,
35+
style: fn(u8) -> <Renderer::Theme as StyleSheet>::Style,
36+
timeline: &crate::Timeline,
37+
content: impl Into<Element<'a, Message, Renderer>>,
38+
) -> crate::widget::Container<'a, Message, Renderer>
39+
where
40+
Renderer: iced_native::Renderer,
41+
Renderer::Theme: widget::container::StyleSheet,
42+
{
43+
StyleContainer::as_widget(self, style, timeline, content)
44+
}
3245
}
3346

3447
impl From<Id> for widget::Id {

0 commit comments

Comments
 (0)