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') 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') 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') 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 From 28a27f08edccd53e06ad693e63b0a62dae921da5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 19:14:13 +0100 Subject: Remove `sandbox` by making `application` more generic :tada: --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 5110c78e..318ca74c 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -8,7 +8,7 @@ use iced::{ }; pub fn main() -> iced::Result { - iced::sandbox("Clock - Iced", Clock::update, Clock::view) + iced::application("Clock - Iced", Clock::update, Clock::view) .subscription(Clock::subscription) .theme(Clock::theme) .antialiased() -- cgit From 36b73781f1fed0b4918d34932c2dcd17fa55cbe5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 19:49:54 +0100 Subject: Scale theme name in `clock` example --- examples/clock/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 318ca74c..e3f3fa34 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -145,10 +145,10 @@ impl canvas::Program for Clock { frame.rotate(Degrees(-90.0 * rotate_factor)); frame.fill_text(canvas::Text { content: theme.to_string(), - size: 15.into(), + size: (radius / 15.0).into(), position: Point::new( - (0.8 * radius - 8.0) * rotate_factor, - -8.0, + (0.78 * radius) * rotate_factor, + -width * 2.0, ), color: palette.primary.weak.text, horizontal_alignment: if rotate_factor > 0.0 { -- cgit From 9152904af1630a6d373b2fd7c284835bf0a3ca95 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 20:13:44 +0100 Subject: Improve styling of `clock` example --- examples/clock/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index e3f3fa34..953ecf6f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -61,7 +61,7 @@ impl Clock { } fn theme(&self) -> Theme { - Theme::ALL[(self.now.unix_timestamp() as usize / 60) % Theme::ALL.len()] + Theme::ALL[(self.now.unix_timestamp() as usize / 10) % Theme::ALL.len()] .clone() } } @@ -94,7 +94,7 @@ impl canvas::Program for Clock { let radius = frame.width().min(frame.height()) / 2.0; let background = Path::circle(center, radius); - frame.fill(&background, palette.primary.weak.color); + frame.fill(&background, palette.secondary.strong.color); let short_hand = Path::line(Point::ORIGIN, Point::new(0.0, -0.5 * radius)); @@ -107,7 +107,7 @@ impl canvas::Program for Clock { let thin_stroke = || -> Stroke { Stroke { width, - style: stroke::Style::Solid(palette.primary.weak.text), + style: stroke::Style::Solid(palette.secondary.strong.text), line_cap: LineCap::Round, ..Stroke::default() } @@ -116,7 +116,7 @@ impl canvas::Program for Clock { let wide_stroke = || -> Stroke { Stroke { width: width * 3.0, - style: stroke::Style::Solid(palette.primary.weak.text), + style: stroke::Style::Solid(palette.secondary.strong.text), line_cap: LineCap::Round, ..Stroke::default() } @@ -150,7 +150,7 @@ impl canvas::Program for Clock { (0.78 * radius) * rotate_factor, -width * 2.0, ), - color: palette.primary.weak.text, + color: palette.secondary.strong.text, horizontal_alignment: if rotate_factor > 0.0 { alignment::Horizontal::Right } else { -- cgit From 846d76cd3f3f2faae5efbb3fda2a2bcb3b064481 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:46:52 +0100 Subject: Remove `Sandbox` trait :tada: --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 953ecf6f..ebb22cef 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -11,7 +11,7 @@ pub fn main() -> iced::Result { iced::application("Clock - Iced", Clock::update, Clock::view) .subscription(Clock::subscription) .theme(Clock::theme) - .antialiased() + .antialiasing(true) .run() } -- cgit From 54f44754eb216d4b2c08cd2a7c3582f1dc295205 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:16:38 +0100 Subject: Move `Program` to `application` module --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index ebb22cef..897f8f1b 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -8,7 +8,7 @@ use iced::{ }; pub fn main() -> iced::Result { - iced::application("Clock - Iced", Clock::update, Clock::view) + iced::program("Clock - Iced", Clock::update, Clock::view) .subscription(Clock::subscription) .theme(Clock::theme) .antialiasing(true) -- cgit