|
1 | | -use iced::alignment; |
2 | 1 | use iced::mouse; |
3 | 2 | use iced::time; |
4 | 3 | use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; |
5 | 4 | use iced::widget::{canvas, container}; |
| 5 | +use iced::{alignment, Radians}; |
6 | 6 | use iced::{ |
7 | | - Degrees, Element, Fill, Font, Point, Rectangle, Renderer, Subscription, |
8 | | - Theme, Vector, |
| 7 | + Degrees, Element, Fill, Font, Point, Rectangle, Renderer, Size, |
| 8 | + Subscription, Theme, Vector, |
9 | 9 | }; |
10 | 10 |
|
11 | 11 | pub fn main() -> iced::Result { |
@@ -117,9 +117,14 @@ impl<Message> canvas::Program<Message> for Clock { |
117 | 117 | }; |
118 | 118 |
|
119 | 119 | frame.translate(Vector::new(center.x, center.y)); |
| 120 | + let minutes_portion = |
| 121 | + Radians::from(hand_rotation(self.now.minute(), 60)) / 12.0; |
| 122 | + let hour_hand_angle = |
| 123 | + Radians::from(hand_rotation(self.now.hour(), 12)) |
| 124 | + + minutes_portion; |
120 | 125 |
|
121 | 126 | frame.with_save(|frame| { |
122 | | - frame.rotate(hand_rotation(self.now.hour(), 12)); |
| 127 | + frame.rotate(hour_hand_angle); |
123 | 128 | frame.stroke(&short_hand, wide_stroke()); |
124 | 129 | }); |
125 | 130 |
|
@@ -155,6 +160,42 @@ impl<Message> canvas::Program<Message> for Clock { |
155 | 160 | ..canvas::Text::default() |
156 | 161 | }); |
157 | 162 | }); |
| 163 | + |
| 164 | + // Draw clock numbers |
| 165 | + for hour in 1..=12 { |
| 166 | + let angle = Radians::from(hand_rotation(hour, 12)) |
| 167 | + - Radians::from(Degrees(90.0)); |
| 168 | + let x = radius * angle.0.cos(); |
| 169 | + let y = radius * angle.0.sin(); |
| 170 | + |
| 171 | + frame.fill_text(canvas::Text { |
| 172 | + content: format!("{}", hour), |
| 173 | + size: (radius / 5.0).into(), |
| 174 | + position: Point::new(x * 0.82, y * 0.82), |
| 175 | + color: palette.secondary.strong.text, |
| 176 | + horizontal_alignment: alignment::Horizontal::Center, |
| 177 | + vertical_alignment: alignment::Vertical::Center, |
| 178 | + font: Font::MONOSPACE, |
| 179 | + ..canvas::Text::default() |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + // Draw ticks |
| 184 | + for tick in 0..60 { |
| 185 | + let angle = hand_rotation(tick, 60); |
| 186 | + let width = if tick % 5 == 0 { 3.0 } else { 1.0 }; |
| 187 | + |
| 188 | + frame.with_save(|frame| { |
| 189 | + frame.rotate(angle); |
| 190 | + frame.fill( |
| 191 | + &Path::rectangle( |
| 192 | + Point::new(0.0, radius - 15.0), |
| 193 | + Size::new(width, 7.0), |
| 194 | + ), |
| 195 | + palette.secondary.strong.text, |
| 196 | + ); |
| 197 | + }); |
| 198 | + } |
158 | 199 | }); |
159 | 200 |
|
160 | 201 | vec![clock] |
|
0 commit comments