|
| 1 | +use iced::alignment; |
| 2 | +use iced::executor; |
| 3 | +use iced::theme::{self, Theme}; |
| 4 | +use iced::time; |
| 5 | +use iced::widget::{button, column, container, row, text}; |
| 6 | +use iced::{Alignment, Application, Command, Element, Event, Length, Settings, Subscription}; |
| 7 | + |
| 8 | +use cosmic_time::{ |
| 9 | + self, |
| 10 | + style_button::{self, StyleButton}, |
| 11 | + Timeline, |
| 12 | +}; |
| 13 | +use once_cell::sync::Lazy; |
| 14 | + |
| 15 | +static BUTTON: Lazy<style_button::Id> = Lazy::new(style_button::Id::unique); |
| 16 | + |
| 17 | +use std::time::{Duration, Instant}; |
| 18 | + |
| 19 | +pub fn main() -> iced::Result { |
| 20 | + Stopwatch::run(Settings::default()) |
| 21 | +} |
| 22 | + |
| 23 | +struct Stopwatch { |
| 24 | + timeline: Timeline, |
| 25 | + duration: Duration, |
| 26 | + state: State, |
| 27 | +} |
| 28 | + |
| 29 | +enum State { |
| 30 | + Idle, |
| 31 | + Ticking { last_tick: Instant }, |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug, Clone)] |
| 35 | +enum Message { |
| 36 | + Toggle, |
| 37 | + Reset, |
| 38 | + Tick(Instant), |
| 39 | +} |
| 40 | + |
| 41 | +impl Application for Stopwatch { |
| 42 | + type Message = Message; |
| 43 | + type Theme = Theme; |
| 44 | + type Executor = executor::Default; |
| 45 | + type Flags = (); |
| 46 | + |
| 47 | + fn new(_flags: ()) -> (Stopwatch, Command<Message>) { |
| 48 | + ( |
| 49 | + Stopwatch { |
| 50 | + timeline: Timeline::new(), |
| 51 | + duration: Duration::default(), |
| 52 | + state: State::Idle, |
| 53 | + }, |
| 54 | + Command::none(), |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + fn title(&self) -> String { |
| 59 | + String::from("Stopwatch - Iced") |
| 60 | + } |
| 61 | + |
| 62 | + fn update(&mut self, message: Message) -> Command<Message> { |
| 63 | + match message { |
| 64 | + Message::Toggle => match self.state { |
| 65 | + State::Idle => { |
| 66 | + self.state = State::Ticking { |
| 67 | + last_tick: Instant::now(), |
| 68 | + }; |
| 69 | + self.timeline |
| 70 | + .set_chain(anim_to_destructive().into()) |
| 71 | + .start(); |
| 72 | + } |
| 73 | + State::Ticking { .. } => { |
| 74 | + self.state = State::Idle; |
| 75 | + self.timeline.set_chain(anim_to_primary().into()).start(); |
| 76 | + } |
| 77 | + }, |
| 78 | + Message::Tick(now) => { |
| 79 | + if let State::Ticking { last_tick } = &mut self.state { |
| 80 | + self.duration += now - *last_tick; |
| 81 | + *last_tick = now; |
| 82 | + } |
| 83 | + } |
| 84 | + Message::Reset => { |
| 85 | + self.duration = Duration::default(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Command::none() |
| 90 | + } |
| 91 | + |
| 92 | + fn subscription(&self) -> Subscription<Message> { |
| 93 | + Subscription::batch(vec![ |
| 94 | + match self.state { |
| 95 | + State::Idle => Subscription::none(), |
| 96 | + State::Ticking { .. } => time::every(Duration::from_millis(10)).map(Message::Tick), |
| 97 | + }, |
| 98 | + self.timeline.as_subscription::<Event>().map(Message::Tick), |
| 99 | + ]) |
| 100 | + } |
| 101 | + |
| 102 | + fn view(&self) -> Element<Message> { |
| 103 | + const MINUTE: u64 = 60; |
| 104 | + const HOUR: u64 = 60 * MINUTE; |
| 105 | + |
| 106 | + let seconds = self.duration.as_secs(); |
| 107 | + |
| 108 | + let duration = text(format!( |
| 109 | + "{:0>2}:{:0>2}:{:0>2}.{:0>2}", |
| 110 | + seconds / HOUR, |
| 111 | + (seconds % HOUR) / MINUTE, |
| 112 | + seconds % MINUTE, |
| 113 | + self.duration.subsec_millis() / 10, |
| 114 | + )) |
| 115 | + .size(40); |
| 116 | + |
| 117 | + let button = |label| { |
| 118 | + button(text(label).horizontal_alignment(alignment::Horizontal::Center)) |
| 119 | + .padding(10) |
| 120 | + .width(Length::Fixed(80.)) |
| 121 | + }; |
| 122 | + |
| 123 | + // must match the same order that the function used to parse into `u8`s |
| 124 | + let buttons = |i: u8| match i { |
| 125 | + 0 => theme::Button::Primary, |
| 126 | + 1 => theme::Button::Secondary, |
| 127 | + 2 => theme::Button::Positive, |
| 128 | + 3 => theme::Button::Destructive, |
| 129 | + 4 => theme::Button::Text, |
| 130 | + _ => panic!("custom is not supported"), |
| 131 | + }; |
| 132 | + |
| 133 | + let toggle_button = { |
| 134 | + let label = match self.state { |
| 135 | + State::Idle => "Start", |
| 136 | + State::Ticking { .. } => "Stop", |
| 137 | + }; |
| 138 | + |
| 139 | + StyleButton::as_widget( |
| 140 | + BUTTON.clone(), |
| 141 | + buttons, |
| 142 | + &self.timeline, |
| 143 | + text(label).horizontal_alignment(alignment::Horizontal::Center), |
| 144 | + ) |
| 145 | + .padding(10) |
| 146 | + .width(Length::Fixed(80.)) |
| 147 | + .on_press(Message::Toggle) |
| 148 | + }; |
| 149 | + |
| 150 | + let reset_button = button("Reset") |
| 151 | + .style(theme::Button::Secondary) |
| 152 | + .on_press(Message::Reset); |
| 153 | + |
| 154 | + let controls = row![toggle_button, reset_button].spacing(20); |
| 155 | + |
| 156 | + let content = column![duration, controls] |
| 157 | + .align_items(Alignment::Center) |
| 158 | + .spacing(20); |
| 159 | + |
| 160 | + container(content) |
| 161 | + .width(Length::Fill) |
| 162 | + .height(Length::Fill) |
| 163 | + .center_x() |
| 164 | + .center_y() |
| 165 | + .into() |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn anim_to_primary() -> style_button::Chain { |
| 170 | + style_button::Chain::new(BUTTON.clone()) |
| 171 | + .link(StyleButton::new(Duration::ZERO).style(as_u8(theme::Button::Destructive))) |
| 172 | + .link(StyleButton::new(Duration::from_millis(500)).style(as_u8(theme::Button::Primary))) |
| 173 | +} |
| 174 | + |
| 175 | +fn anim_to_destructive() -> style_button::Chain { |
| 176 | + style_button::Chain::new(BUTTON.clone()) |
| 177 | + .link(StyleButton::new(Duration::ZERO).style(as_u8(theme::Button::Primary))) |
| 178 | + .link(StyleButton::new(Duration::from_millis(500)).style(as_u8(theme::Button::Destructive))) |
| 179 | +} |
| 180 | + |
| 181 | +// Style implementations |
| 182 | + |
| 183 | +// the enum's default must be 0 |
| 184 | +fn as_u8(style: theme::Button) -> u8 { |
| 185 | + match style { |
| 186 | + theme::Button::Primary => 0, |
| 187 | + theme::Button::Secondary => 1, |
| 188 | + theme::Button::Positive => 2, |
| 189 | + theme::Button::Destructive => 3, |
| 190 | + theme::Button::Text => 4, |
| 191 | + _ => panic!("Custom is not supported"), |
| 192 | + } |
| 193 | +} |
0 commit comments