Skip to content

Commit 2b92023

Browse files
authored
Merge pull request #2644 from kgday/master
Modified clock example to make the clock more readable.
2 parents 24297c5 + d9a7329 commit 2b92023

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

examples/clock/src/main.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use iced::alignment;
21
use iced::mouse;
32
use iced::time;
43
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
54
use iced::widget::{canvas, container};
5+
use iced::{alignment, Radians};
66
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,
99
};
1010

1111
pub fn main() -> iced::Result {
@@ -117,9 +117,14 @@ impl<Message> canvas::Program<Message> for Clock {
117117
};
118118

119119
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;
120125

121126
frame.with_save(|frame| {
122-
frame.rotate(hand_rotation(self.now.hour(), 12));
127+
frame.rotate(hour_hand_angle);
123128
frame.stroke(&short_hand, wide_stroke());
124129
});
125130

@@ -155,6 +160,42 @@ impl<Message> canvas::Program<Message> for Clock {
155160
..canvas::Text::default()
156161
});
157162
});
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+
}
158199
});
159200

160201
vec![clock]

0 commit comments

Comments
 (0)