Added the ability to change checkbox icon

This commit is contained in:
Casper Storm 2023-02-16 14:13:04 +01:00
parent 0cb72f6971
commit 4fb0be1793
No known key found for this signature in database
GPG key ID: BABF49AA70C405C2
8 changed files with 123 additions and 9 deletions

View file

@ -14,6 +14,17 @@ use crate::{
pub use iced_style::checkbox::{Appearance, StyleSheet};
/// The icon in a [`Checkbox`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Icon<Font> {
/// Font that will be used to display the `code_point`,
pub font: Font,
/// The unicode code point that will be used as the icon.
pub code_point: char,
/// Font size of the content.
pub size: Option<u16>,
}
/// A box that can be checked.
///
/// # Example
@ -45,6 +56,7 @@ where
spacing: u16,
text_size: Option<u16>,
font: Renderer::Font,
icon: Icon<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style,
}
@ -80,6 +92,11 @@ where
spacing: Self::DEFAULT_SPACING,
text_size: None,
font: Renderer::Font::default(),
icon: Icon {
font: Renderer::ICON_FONT,
code_point: Renderer::CHECKMARK_ICON,
size: None,
},
style: Default::default(),
}
}
@ -116,6 +133,12 @@ where
self
}
/// Sets the [`Icon`] of the [`Checkbox`].
pub fn icon(mut self, icon: Icon<Renderer::Font>) -> Self {
self.icon = icon;
self
}
/// Sets the style of the [`Checkbox`].
pub fn style(
mut self,
@ -243,17 +266,24 @@ where
custom_style.background,
);
let Icon {
font,
code_point,
size,
} = &self.icon;
let size = size.map(f32::from).unwrap_or(bounds.height * 0.7);
if self.is_checked {
renderer.fill_text(text::Text {
content: &Renderer::CHECKMARK_ICON.to_string(),
font: Renderer::ICON_FONT,
size: bounds.height * 0.7,
content: &code_point.to_string(),
font: font.clone(),
size,
bounds: Rectangle {
x: bounds.center_x(),
y: bounds.center_y(),
..bounds
},
color: custom_style.checkmark_color,
color: custom_style.icon_color,
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
});