Skip to content

Commit ef71c44

Browse files
authored
Merge pull request #6 from pop-os/more-widgets
More widgets
2 parents d58c81e + d05c9fb commit ef71c44

File tree

18 files changed

+1069
-142
lines changed

18 files changed

+1069
-142
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ TODOs before release:
88
- [x] Animation easing
99
- [x] add space widget
1010
- [x] add button widget
11-
- [ ] add row widget
12-
- [ ] add column widget
11+
- [x] add row widget
12+
- [x] add column widget
1313
- [ ] add toggle button widget
1414
- [x] Use iced 0.8
1515
- [x] use iced 0.8's framerate subscription

examples/counter/src/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use iced::widget::{button, column, text};
22
use iced::{
3-
executor, Alignment, Application, Command, Element, Event, Length, Settings, Subscription,
4-
Theme,
3+
executor,
4+
time::{Duration, Instant},
5+
Alignment, Application, Command, Element, Event, Length, Settings, Subscription, Theme,
56
};
6-
use std::time::Duration;
77

88
use cosmic_time::{self, keyframes, Timeline};
99

@@ -24,7 +24,7 @@ struct Counter {
2424
enum Message {
2525
IncrementPressed,
2626
DecrementPressed,
27-
Tick,
27+
Tick(Instant),
2828
}
2929

3030
impl Application for Counter {
@@ -83,7 +83,7 @@ impl Application for Counter {
8383
// cosmic-time assumes that the timeline is continuous. Try deleting it,
8484
// the height will animate smoothly from 300 to 150 right through keyframe `four`!
8585

86-
timeline.set_chain(animation.into()).start();
86+
timeline.set_chain(animation).start();
8787
// `Start` is very important! Your animation won't "start" without it.
8888
// Cosmic-time tries to be atomic, meaning that keyframes defined in the
8989
// same function call all start at the same time. Because there is process time
@@ -105,9 +105,7 @@ impl Application for Counter {
105105
// at what timeline you have built and decides for you how often your
106106
// application should redraw for you! When the animation is done idle
107107
// or finished, cosmic-time will keep your applicaiton idle!
108-
self.timeline
109-
.as_subscription::<Event>()
110-
.map(|_| Message::Tick)
108+
self.timeline.as_subscription::<Event>().map(Message::Tick)
111109
}
112110

113111
fn update(&mut self, message: Message) -> Command<Message> {
@@ -118,7 +116,7 @@ impl Application for Counter {
118116
Message::DecrementPressed => {
119117
self.value -= 1;
120118
}
121-
Message::Tick => {}
119+
Message::Tick(now) => self.timeline.now(now),
122120
}
123121
Command::none()
124122
}

examples/pokedex/src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use iced::futures;
22
use iced::widget::{self, column, container, image, row, text};
33
use iced::{
4+
time::{Duration, Instant},
45
Alignment, Application, Color, Command, Element, Event, Length, Settings, Subscription, Theme,
56
};
67

@@ -9,7 +10,6 @@ use cosmic_time::{
910
Quartic, Quintic, Sinusoidal, Timeline,
1011
};
1112
use once_cell::sync::Lazy;
12-
use std::time::Duration;
1313

1414
static SPACE: Lazy<keyframes::space::Id> = Lazy::new(keyframes::space::Id::unique);
1515

@@ -65,7 +65,7 @@ enum Pokedex {
6565
enum Message {
6666
PokemonFound(Result<Pokemon, Error>),
6767
Search,
68-
Tick,
68+
Tick(Instant),
6969
}
7070

7171
impl Application for Pokedex {
@@ -97,14 +97,19 @@ impl Application for Pokedex {
9797
Pokedex::Loaded { pokemon } => pokemon
9898
.timeline
9999
.as_subscription::<Event>()
100-
.map(|_| Message::Tick),
100+
.map(Message::Tick),
101101
_ => Subscription::none(),
102102
}
103103
}
104104

105105
fn update(&mut self, message: Message) -> Command<Message> {
106106
match message {
107-
Message::Tick => Command::none(),
107+
Message::Tick(now) => {
108+
if let Pokedex::Loaded { pokemon, .. } = self {
109+
pokemon.timeline.now(now);
110+
}
111+
Command::none()
112+
}
108113
Message::PokemonFound(Ok(pokemon)) => {
109114
*self = Pokedex::Loaded { pokemon };
110115

@@ -266,7 +271,7 @@ impl Pokemon {
266271
.loop_forever();
267272

268273
// println!("Animating wth ease: {ease:?}"); // Uncomment to see the type of easing
269-
timeline.set_chain(animation.into()).start();
274+
timeline.set_chain(animation).start();
270275

271276
Ok(Pokemon {
272277
timeline,

examples/stopwatch/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ impl Application for Stopwatch {
6666
self.state = State::Ticking {
6767
last_tick: Instant::now(),
6868
};
69-
self.timeline
70-
.set_chain(anim_to_destructive().into())
71-
.start();
69+
self.timeline.set_chain(anim_to_destructive()).start();
7270
}
7371
State::Ticking { .. } => {
7472
self.state = State::Idle;
75-
self.timeline.set_chain(anim_to_primary().into()).start();
73+
self.timeline.set_chain(anim_to_primary()).start();
7674
}
7775
},
7876
Message::Tick(now) => {
77+
self.timeline.now(now);
7978
if let State::Ticking { last_tick } = &mut self.state {
8079
self.duration += now - *last_tick;
8180
*last_tick = now;

src/keyframes.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod container;
33
pub mod space;
44
pub mod style_button;
55
pub mod style_container;
6+
pub mod toggler;
67

78
use iced_native::{widget, Length};
89

@@ -11,8 +12,7 @@ pub use container::Container;
1112
pub use space::Space;
1213
pub use style_button::StyleButton;
1314
pub use style_container::StyleContainer;
14-
15-
use std::time::Instant;
15+
pub use toggler::Toggler;
1616

1717
use crate::Timeline;
1818

@@ -27,15 +27,16 @@ pub trait IsChain {
2727
fn repeat(&self) -> Repeat;
2828
}
2929

30-
pub fn get_length(
31-
id: &widget::Id,
32-
timeline: &Timeline,
33-
now: &Instant,
34-
index: usize,
35-
default: Length,
36-
) -> Length {
30+
pub fn get_length(id: &widget::Id, timeline: &Timeline, index: usize, default: Length) -> Length {
3731
timeline
38-
.get(id, now, index)
32+
.get(id, index)
3933
.map(|m| Length::Fixed(m.value))
4034
.unwrap_or(default)
4135
}
36+
37+
fn as_f32(length: Option<Length>) -> Option<f32> {
38+
match length {
39+
Some(Length::Fixed(i)) => Some(i),
40+
_ => None,
41+
}
42+
}

src/keyframes/button.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use iced_native::time::Duration;
12
use iced_native::{widget, Element, Length, Padding};
23

3-
use std::time::{Duration, Instant};
4-
5-
use crate::keyframes::{get_length, Repeat};
4+
use crate::keyframes::{as_f32, get_length, Repeat};
65
use crate::timeline::DurFrame;
76
use crate::{Ease, Linear};
87

@@ -105,16 +104,15 @@ impl Button {
105104
Renderer::Theme: widget::button::StyleSheet,
106105
{
107106
let id: widget::Id = id.into();
108-
let now = Instant::now();
109107

110108
widget::Button::new(content)
111-
.width(get_length(&id, timeline, &now, 0, Length::Shrink))
112-
.height(get_length(&id, timeline, &now, 1, Length::Shrink))
109+
.width(get_length(&id, timeline, 0, Length::Shrink))
110+
.height(get_length(&id, timeline, 1, Length::Shrink))
113111
.padding([
114-
timeline.get(&id, &now, 2).map(|m| m.value).unwrap_or(5.0),
115-
timeline.get(&id, &now, 3).map(|m| m.value).unwrap_or(5.0),
116-
timeline.get(&id, &now, 4).map(|m| m.value).unwrap_or(5.0),
117-
timeline.get(&id, &now, 5).map(|m| m.value).unwrap_or(5.0),
112+
timeline.get(&id, 2).map(|m| m.value).unwrap_or(5.0),
113+
timeline.get(&id, 3).map(|m| m.value).unwrap_or(5.0),
114+
timeline.get(&id, 4).map(|m| m.value).unwrap_or(5.0),
115+
timeline.get(&id, 5).map(|m| m.value).unwrap_or(5.0),
118116
])
119117
}
120118

@@ -179,10 +177,3 @@ impl ExactSizeIterator for Button {
179177
6 - self.index
180178
}
181179
}
182-
183-
fn as_f32(length: Option<Length>) -> Option<f32> {
184-
match length {
185-
Some(Length::Fixed(i)) => Some(i),
186-
_ => None,
187-
}
188-
}

0 commit comments

Comments
 (0)