From 5e6c9eeb7e6f9ee985257853a140c19ec7ca1de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 11 Jul 2024 04:33:19 +0200 Subject: [PATCH] Add `iced` widget helper to display the iced logo :comet: --- examples/slider/Cargo.toml | 1 + examples/slider/src/main.rs | 43 ++++++++++++----------------------- widget/Cargo.toml | 1 + widget/assets/iced-logo.svg | 2 ++ widget/src/helpers.rs | 35 ++++++++++++++++++++++++++++ widget/src/slider.rs | 2 +- widget/src/vertical_slider.rs | 2 +- 7 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 widget/assets/iced-logo.svg diff --git a/examples/slider/Cargo.toml b/examples/slider/Cargo.toml index fad8916e46..05e74d2c83 100644 --- a/examples/slider/Cargo.toml +++ b/examples/slider/Cargo.toml @@ -7,3 +7,4 @@ publish = false [dependencies] iced.workspace = true +iced.features = ["svg"] diff --git a/examples/slider/src/main.rs b/examples/slider/src/main.rs index 0b4c29aa1a..fd312763d8 100644 --- a/examples/slider/src/main.rs +++ b/examples/slider/src/main.rs @@ -1,5 +1,5 @@ -use iced::widget::{center, column, container, slider, text, vertical_slider}; -use iced::{Element, Length}; +use iced::widget::{column, container, iced, slider, text, vertical_slider}; +use iced::{Alignment, Element, Length}; pub fn main() -> iced::Result { iced::run("Slider - Iced", Slider::update, Slider::view) @@ -12,19 +12,11 @@ pub enum Message { pub struct Slider { value: u8, - default: u8, - step: u8, - shift_step: u8, } impl Slider { fn new() -> Self { - Slider { - value: 50, - default: 50, - step: 5, - shift_step: 1, - } + Slider { value: 50 } } fn update(&mut self, message: Message) { @@ -37,32 +29,27 @@ impl Slider { fn view(&self) -> Element { let h_slider = container( - slider(0..=100, self.value, Message::SliderChanged) - .default(self.default) - .step(self.step) - .shift_step(self.shift_step), + slider(1..=100, self.value, Message::SliderChanged) + .default(50) + .shift_step(5), ) .width(250); let v_slider = container( - vertical_slider(0..=100, self.value, Message::SliderChanged) - .default(self.default) - .step(self.step) - .shift_step(self.shift_step), + vertical_slider(1..=100, self.value, Message::SliderChanged) + .default(50) + .shift_step(5), ) .height(200); let text = text(self.value); - center( - column![ - container(v_slider).center_x(Length::Fill), - container(h_slider).center_x(Length::Fill), - container(text).center_x(Length::Fill) - ] - .spacing(25), - ) - .into() + column![v_slider, h_slider, text, iced(self.value as f32),] + .width(Length::Fill) + .align_items(Alignment::Center) + .spacing(20) + .padding(20) + .into() } } diff --git a/widget/Cargo.toml b/widget/Cargo.toml index 3c9f6a54e3..498a768b1f 100644 --- a/widget/Cargo.toml +++ b/widget/Cargo.toml @@ -31,6 +31,7 @@ iced_renderer.workspace = true iced_runtime.workspace = true num-traits.workspace = true +once_cell.workspace = true rustc-hash.workspace = true thiserror.workspace = true unicode-segmentation.workspace = true diff --git a/widget/assets/iced-logo.svg b/widget/assets/iced-logo.svg new file mode 100644 index 0000000000..459b7fbb5a --- /dev/null +++ b/widget/assets/iced-logo.svg @@ -0,0 +1,2 @@ + + diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 9056d19119..d76319595a 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -890,6 +890,41 @@ where crate::Svg::new(handle) } +/// Creates an [`Element`] that displays the iced logo with the given `text_size`. +/// +/// Useful for showing some love to your favorite GUI library in your "About" screen, +/// for instance. +#[cfg(feature = "svg")] +pub fn iced<'a, Message, Theme, Renderer>( + text_size: impl Into, +) -> Element<'a, Message, Theme, Renderer> +where + Message: 'a, + Renderer: core::Renderer + + core::text::Renderer + + core::svg::Renderer + + 'a, + Theme: text::Catalog + crate::svg::Catalog + 'a, +{ + use crate::core::{Alignment, Font}; + use crate::svg; + use once_cell::sync::Lazy; + + static LOGO: Lazy = Lazy::new(|| { + svg::Handle::from_memory(include_bytes!("../assets/iced-logo.svg")) + }); + + let text_size = text_size.into(); + + row![ + svg(LOGO.clone()).width(text_size * 1.3), + text("iced").size(text_size).font(Font::MONOSPACE) + ] + .spacing(text_size.0 / 3.0) + .align_items(Alignment::Center) + .into() +} + /// Creates a new [`Canvas`]. /// /// [`Canvas`]: crate::Canvas diff --git a/widget/src/slider.rs b/widget/src/slider.rs index a8f1d19250..74e6f8d3a4 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -237,7 +237,7 @@ where let steps = (percent * (end - start) / step).round(); let value = steps * step + start; - T::from_f64(value) + T::from_f64(value.min(end)) }; new_value diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index defb442ff5..33c591f50a 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -239,7 +239,7 @@ where let steps = (percent * (end - start) / step).round(); let value = steps * step + start; - T::from_f64(value) + T::from_f64(value.min(end)) }; new_value