From c22269bff3085012d326a0df77bf27ad5bcb41b7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 05:33:47 +0100 Subject: Introduce `Program` API --- examples/clock/src/main.rs | 53 ++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) (limited to 'examples/clock/src') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 87da0c7e..c307fab9 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,17 +1,17 @@ -use iced::executor; use iced::mouse; use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; use iced::widget::{canvas, container}; use iced::{ - Application, Command, Element, Length, Point, Rectangle, Renderer, - Settings, Subscription, Theme, Vector, + Element, Length, Point, Rectangle, Renderer, Subscription, Theme, Vector, }; pub fn main() -> iced::Result { - Clock::run(Settings { - antialiasing: true, - ..Settings::default() - }) + iced::sandbox(Clock::update, Clock::view) + .title("Clock - Iced") + .subscription(Clock::subscription) + .theme(Clock::theme) + .antialiased() + .run() } struct Clock { @@ -24,28 +24,8 @@ enum Message { Tick(time::OffsetDateTime), } -impl Application for Clock { - type Executor = executor::Default; - type Message = Message; - type Theme = Theme; - type Flags = (); - - fn new(_flags: ()) -> (Self, Command) { - ( - Clock { - now: time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - clock: Cache::default(), - }, - Command::none(), - ) - } - - fn title(&self) -> String { - String::from("Clock - Iced") - } - - fn update(&mut self, message: Message) -> Command { +impl Clock { + fn update(&mut self, message: Message) { match message { Message::Tick(local_time) => { let now = local_time; @@ -56,8 +36,6 @@ impl Application for Clock { } } } - - Command::none() } fn view(&self) -> Element { @@ -82,7 +60,18 @@ impl Application for Clock { } fn theme(&self) -> Theme { - Theme::TokyoNight + Theme::ALL[(self.now.unix_timestamp() as usize / 60) % Theme::ALL.len()] + .clone() + } +} + +impl Default for Clock { + fn default() -> Self { + Self { + now: time::OffsetDateTime::now_local() + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), + clock: Cache::default(), + } } } -- cgit From bb71e8481ed59f991b9bd9dc55ea7e011ba0aac6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 16:12:07 +0100 Subject: Make `sandbox` helper take a `title` as well --- examples/clock/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples/clock/src') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index c307fab9..fcad5d34 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -6,8 +6,7 @@ use iced::{ }; pub fn main() -> iced::Result { - iced::sandbox(Clock::update, Clock::view) - .title("Clock - Iced") + iced::sandbox("Clock - Iced", Clock::update, Clock::view) .subscription(Clock::subscription) .theme(Clock::theme) .antialiased() -- cgit From bad3b1ac4777b4d9b57c6ba9473fb97753a77124 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 16:52:21 +0100 Subject: Show name of current `Theme` in `clock` example --- examples/clock/src/main.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'examples/clock/src') 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 canvas::Program 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 canvas::Program 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) } -- cgit