diff options
author | 2024-03-16 16:52:21 +0100 | |
---|---|---|
committer | 2024-03-16 16:52:21 +0100 | |
commit | bad3b1ac4777b4d9b57c6ba9473fb97753a77124 (patch) | |
tree | b5675155923b3576df03a6ed67d0943807a38493 /examples/clock | |
parent | 348e00e11cd976a16493f4ce693db614886c1ecd (diff) | |
download | iced-bad3b1ac4777b4d9b57c6ba9473fb97753a77124.tar.gz iced-bad3b1ac4777b4d9b57c6ba9473fb97753a77124.tar.bz2 iced-bad3b1ac4777b4d9b57c6ba9473fb97753a77124.zip |
Show name of current `Theme` in `clock` example
Diffstat (limited to 'examples/clock')
-rw-r--r-- | examples/clock/src/main.rs | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index fcad5d34..5110c78e 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,8 +1,10 @@ +use iced::alignment; use iced::mouse; use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; use iced::widget::{canvas, container}; use iced::{ - Element, Length, Point, Rectangle, Renderer, Subscription, Theme, Vector, + Degrees, Element, Font, Length, Point, Rectangle, Renderer, Subscription, + Theme, Vector, }; pub fn main() -> iced::Result { @@ -133,8 +135,31 @@ impl<Message> canvas::Program<Message> for Clock { }); frame.with_save(|frame| { - frame.rotate(hand_rotation(self.now.second(), 60)); + let rotation = hand_rotation(self.now.second(), 60); + + frame.rotate(rotation); frame.stroke(&long_hand, thin_stroke()); + + let rotate_factor = if rotation < 180.0 { 1.0 } else { -1.0 }; + + frame.rotate(Degrees(-90.0 * rotate_factor)); + frame.fill_text(canvas::Text { + content: theme.to_string(), + size: 15.into(), + position: Point::new( + (0.8 * radius - 8.0) * rotate_factor, + -8.0, + ), + color: palette.primary.weak.text, + horizontal_alignment: if rotate_factor > 0.0 { + alignment::Horizontal::Right + } else { + alignment::Horizontal::Left + }, + vertical_alignment: alignment::Vertical::Bottom, + font: Font::MONOSPACE, + ..canvas::Text::default() + }); }); }); @@ -142,8 +167,8 @@ impl<Message> canvas::Program<Message> for Clock { } } -fn hand_rotation(n: u8, total: u8) -> f32 { +fn hand_rotation(n: u8, total: u8) -> Degrees { let turns = n as f32 / total as f32; - 2.0 * std::f32::consts::PI * turns + Degrees(360.0 * turns) } |