150 lines
3.8 KiB
Rust
150 lines
3.8 KiB
Rust
use iced::alignment;
|
|
use iced::button;
|
|
use iced::executor;
|
|
use iced::theme::{self, Theme};
|
|
use iced::time;
|
|
use iced::{
|
|
Alignment, Application, Button, Column, Command, Container, Element,
|
|
Length, Row, Settings, Subscription, Text,
|
|
};
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
pub fn main() -> iced::Result {
|
|
Stopwatch::run(Settings::default())
|
|
}
|
|
|
|
struct Stopwatch {
|
|
duration: Duration,
|
|
state: State,
|
|
toggle: button::State,
|
|
reset: button::State,
|
|
}
|
|
|
|
enum State {
|
|
Idle,
|
|
Ticking { last_tick: Instant },
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum Message {
|
|
Toggle,
|
|
Reset,
|
|
Tick(Instant),
|
|
}
|
|
|
|
impl Application for Stopwatch {
|
|
type Message = Message;
|
|
type Theme = Theme;
|
|
type Executor = executor::Default;
|
|
type Flags = ();
|
|
|
|
fn new(_flags: ()) -> (Stopwatch, Command<Message>) {
|
|
(
|
|
Stopwatch {
|
|
duration: Duration::default(),
|
|
state: State::Idle,
|
|
toggle: button::State::new(),
|
|
reset: button::State::new(),
|
|
},
|
|
Command::none(),
|
|
)
|
|
}
|
|
|
|
fn title(&self) -> String {
|
|
String::from("Stopwatch - Iced")
|
|
}
|
|
|
|
fn update(&mut self, message: Message) -> Command<Message> {
|
|
match message {
|
|
Message::Toggle => match self.state {
|
|
State::Idle => {
|
|
self.state = State::Ticking {
|
|
last_tick: Instant::now(),
|
|
};
|
|
}
|
|
State::Ticking { .. } => {
|
|
self.state = State::Idle;
|
|
}
|
|
},
|
|
Message::Tick(now) => match &mut self.state {
|
|
State::Ticking { last_tick } => {
|
|
self.duration += now - *last_tick;
|
|
*last_tick = now;
|
|
}
|
|
_ => {}
|
|
},
|
|
Message::Reset => {
|
|
self.duration = Duration::default();
|
|
}
|
|
}
|
|
|
|
Command::none()
|
|
}
|
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
|
match self.state {
|
|
State::Idle => Subscription::none(),
|
|
State::Ticking { .. } => {
|
|
time::every(Duration::from_millis(10)).map(Message::Tick)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&mut self) -> Element<Message> {
|
|
const MINUTE: u64 = 60;
|
|
const HOUR: u64 = 60 * MINUTE;
|
|
|
|
let seconds = self.duration.as_secs();
|
|
|
|
let duration = Text::new(format!(
|
|
"{:0>2}:{:0>2}:{:0>2}.{:0>2}",
|
|
seconds / HOUR,
|
|
(seconds % HOUR) / MINUTE,
|
|
seconds % MINUTE,
|
|
self.duration.subsec_millis() / 10,
|
|
))
|
|
.size(40);
|
|
|
|
let button = |state, label| {
|
|
Button::new(
|
|
state,
|
|
Text::new(label)
|
|
.horizontal_alignment(alignment::Horizontal::Center),
|
|
)
|
|
.padding(10)
|
|
.width(Length::Units(80))
|
|
};
|
|
|
|
let toggle_button = {
|
|
let label = match self.state {
|
|
State::Idle => "Start",
|
|
State::Ticking { .. } => "Stop",
|
|
};
|
|
|
|
button(&mut self.toggle, label).on_press(Message::Toggle)
|
|
};
|
|
|
|
let reset_button = button(&mut self.reset, "Reset")
|
|
.style(theme::Button::Destructive)
|
|
.on_press(Message::Reset);
|
|
|
|
let controls = Row::new()
|
|
.spacing(20)
|
|
.push(toggle_button)
|
|
.push(reset_button);
|
|
|
|
let content = Column::new()
|
|
.align_items(Alignment::Center)
|
|
.spacing(20)
|
|
.push(duration)
|
|
.push(controls);
|
|
|
|
Container::new(content)
|
|
.width(Length::Fill)
|
|
.height(Length::Fill)
|
|
.center_x()
|
|
.center_y()
|
|
.into()
|
|
}
|
|
}
|