2020-02-12 03:47:36 +01:00
|
|
|
use iced::{
|
|
|
|
|
canvas, executor, Application, Canvas, Color, Command, Element, Length,
|
|
|
|
|
Point, Settings,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
|
Clock::run(Settings::default())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Clock {
|
2020-02-12 04:00:13 +01:00
|
|
|
now: LocalTime,
|
2020-02-12 03:47:36 +01:00
|
|
|
clock: canvas::layer::Cached<LocalTime>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
enum Message {
|
|
|
|
|
Tick(chrono::DateTime<chrono::Local>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Application for Clock {
|
|
|
|
|
type Executor = executor::Default;
|
|
|
|
|
type Message = Message;
|
|
|
|
|
|
|
|
|
|
fn new() -> (Self, Command<Message>) {
|
|
|
|
|
let now: LocalTime = chrono::Local::now().into();
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
Clock {
|
2020-02-12 04:00:13 +01:00
|
|
|
now,
|
2020-02-12 03:47:36 +01:00
|
|
|
clock: canvas::layer::Cached::new(),
|
|
|
|
|
},
|
|
|
|
|
Command::none(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn title(&self) -> String {
|
|
|
|
|
String::from("Clock - Iced")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, message: Message) -> Command<Message> {
|
|
|
|
|
match message {
|
2020-02-12 04:00:13 +01:00
|
|
|
Message::Tick(local_time) => {
|
|
|
|
|
let now = local_time.into();
|
|
|
|
|
|
|
|
|
|
if now != self.now {
|
|
|
|
|
self.now = now;
|
|
|
|
|
|
|
|
|
|
self.clock.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 03:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn view(&mut self) -> Element<Message> {
|
|
|
|
|
Canvas::new()
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
2020-02-12 04:00:13 +01:00
|
|
|
.push(self.clock.with(&self.now))
|
2020-02-12 03:47:36 +01:00
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 04:00:13 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2020-02-12 03:47:36 +01:00
|
|
|
struct LocalTime {
|
|
|
|
|
hour: u32,
|
|
|
|
|
minute: u32,
|
|
|
|
|
second: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<chrono::DateTime<chrono::Local>> for LocalTime {
|
|
|
|
|
fn from(date_time: chrono::DateTime<chrono::Local>) -> LocalTime {
|
|
|
|
|
use chrono::Timelike;
|
|
|
|
|
|
|
|
|
|
LocalTime {
|
|
|
|
|
hour: date_time.hour(),
|
|
|
|
|
minute: date_time.minute(),
|
|
|
|
|
second: date_time.second(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl canvas::layer::Drawable for LocalTime {
|
|
|
|
|
fn draw(&self, frame: &mut canvas::Frame) {
|
|
|
|
|
let center = frame.center();
|
|
|
|
|
let radius = frame.width().min(frame.height()) as f32 / 2.0;
|
|
|
|
|
|
2020-02-12 06:41:24 +01:00
|
|
|
let path = canvas::Path::new(|path| {
|
|
|
|
|
path.arc(canvas::path::Arc {
|
|
|
|
|
center,
|
|
|
|
|
radius,
|
|
|
|
|
start_angle: 0.0,
|
|
|
|
|
end_angle: 360.0 * 2.0 * std::f32::consts::PI,
|
|
|
|
|
})
|
2020-02-12 03:47:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
frame.fill(
|
2020-02-12 06:41:24 +01:00
|
|
|
&path,
|
2020-02-12 03:47:36 +01:00
|
|
|
canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fn draw_handle(
|
|
|
|
|
n: u32,
|
|
|
|
|
total: u32,
|
|
|
|
|
length: f32,
|
2020-02-12 06:41:24 +01:00
|
|
|
path: &mut canvas::path::Builder,
|
2020-02-12 03:47:36 +01:00
|
|
|
) {
|
|
|
|
|
let turns = n as f32 / total as f32;
|
|
|
|
|
let t = 2.0 * std::f32::consts::PI * (turns - 0.25);
|
|
|
|
|
|
|
|
|
|
let x = length * t.cos();
|
|
|
|
|
let y = length * t.sin();
|
|
|
|
|
|
|
|
|
|
path.line_to(Point::new(x, y));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 06:41:24 +01:00
|
|
|
let path = canvas::Path::new(|path| {
|
|
|
|
|
path.move_to(center);
|
|
|
|
|
draw_handle(self.hour, 12, 0.6 * radius, path);
|
2020-02-12 03:47:36 +01:00
|
|
|
|
2020-02-12 06:41:24 +01:00
|
|
|
path.move_to(center);
|
|
|
|
|
draw_handle(self.minute, 60, 0.9 * radius, path)
|
|
|
|
|
});
|
2020-02-12 03:47:36 +01:00
|
|
|
|
|
|
|
|
frame.stroke(
|
2020-02-12 06:41:24 +01:00
|
|
|
&path,
|
2020-02-12 03:47:36 +01:00
|
|
|
canvas::Stroke {
|
|
|
|
|
width: 4.0,
|
|
|
|
|
color: Color::WHITE,
|
|
|
|
|
line_cap: canvas::LineCap::Round,
|
|
|
|
|
..canvas::Stroke::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2020-02-12 06:41:24 +01:00
|
|
|
let path = canvas::Path::new(|path| {
|
|
|
|
|
path.move_to(center);
|
|
|
|
|
draw_handle(self.second, 60, 0.9 * radius, path)
|
|
|
|
|
});
|
2020-02-12 03:47:36 +01:00
|
|
|
|
|
|
|
|
frame.stroke(
|
2020-02-12 06:41:24 +01:00
|
|
|
&path,
|
2020-02-12 03:47:36 +01:00
|
|
|
canvas::Stroke {
|
|
|
|
|
width: 2.0,
|
|
|
|
|
color: Color::WHITE,
|
|
|
|
|
line_cap: canvas::LineCap::Round,
|
|
|
|
|
..canvas::Stroke::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|