Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iced widget helper to display the iced logo ☄️ #2498

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/slider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["svg"]
43 changes: 15 additions & 28 deletions examples/slider/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand All @@ -37,32 +29,27 @@ impl Slider {

fn view(&self) -> Element<Message> {
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()
}
}

Expand Down
1 change: 1 addition & 0 deletions widget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions widget/assets/iced-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pixels>,
) -> Element<'a, Message, Theme, Renderer>
where
Message: 'a,
Renderer: core::Renderer
+ core::text::Renderer<Font = core::Font>
+ 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<svg::Handle> = 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
Expand Down
2 changes: 1 addition & 1 deletion widget/src/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion widget/src/vertical_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading