1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
use iced::{
canvas, executor, Application, Canvas, Color, Command, Element, Length,
Point, Settings,
};
pub fn main() {
Clock::run(Settings::default())
}
struct Clock {
now: LocalTime,
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 {
now,
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 {
Message::Tick(local_time) => {
let now = local_time.into();
if now != self.now {
self.now = now;
self.clock.clear();
}
}
}
Command::none()
}
fn view(&mut self) -> Element<Message> {
Canvas::new()
.width(Length::Fill)
.height(Length::Fill)
.push(self.clock.with(&self.now))
.into()
}
}
#[derive(Debug, PartialEq, Eq)]
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;
let mut path = canvas::Path::new();
path.arc(canvas::path::Arc {
center,
radius,
start_angle: 0.0,
end_angle: 360.0 * 2.0 * std::f32::consts::PI,
});
frame.fill(
path,
canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)),
);
fn draw_handle(
n: u32,
total: u32,
length: f32,
path: &mut canvas::Path,
) {
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));
}
let mut path = canvas::Path::new();
path.move_to(center);
draw_handle(self.hour, 12, 0.6 * radius, &mut path);
path.move_to(center);
draw_handle(self.minute, 60, 0.9 * radius, &mut path);
frame.stroke(
path,
canvas::Stroke {
width: 4.0,
color: Color::WHITE,
line_cap: canvas::LineCap::Round,
..canvas::Stroke::default()
},
);
let mut path = canvas::Path::new();
path.move_to(center);
draw_handle(self.second, 60, 0.9 * radius, &mut path);
frame.stroke(
path,
canvas::Stroke {
width: 2.0,
color: Color::WHITE,
line_cap: canvas::LineCap::Round,
..canvas::Stroke::default()
},
);
}
}
|