Added the ability to change checkbox icon
This commit is contained in:
parent
0cb72f6971
commit
4fb0be1793
8 changed files with 123 additions and 9 deletions
9
examples/checkbox/Cargo.toml
Normal file
9
examples/checkbox/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "checkbox"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Casper Rogild Storm<casper@rogildstorm.com>"]
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
iced = { path = "../.." }
|
||||||
12
examples/checkbox/README.md
Normal file
12
examples/checkbox/README.md
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
## Checkbox
|
||||||
|
|
||||||
|
A box that can be checked.
|
||||||
|
|
||||||
|
The __[`main`]__ file contains all the code of the example.
|
||||||
|
|
||||||
|
You can run it with `cargo run`:
|
||||||
|
```
|
||||||
|
cargo run --package pick_list
|
||||||
|
```
|
||||||
|
|
||||||
|
[`main`]: src/main.rs
|
||||||
BIN
examples/checkbox/fonts/icons.ttf
Normal file
BIN
examples/checkbox/fonts/icons.ttf
Normal file
Binary file not shown.
63
examples/checkbox/src/main.rs
Normal file
63
examples/checkbox/src/main.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
use iced::widget::{checkbox, column, container};
|
||||||
|
use iced::{Element, Font, Length, Sandbox, Settings};
|
||||||
|
|
||||||
|
const ICON_FONT: Font = Font::External {
|
||||||
|
name: "Icons",
|
||||||
|
bytes: include_bytes!("../fonts/icons.ttf"),
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() -> iced::Result {
|
||||||
|
Example::run(Settings::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Example {
|
||||||
|
default_checkbox: bool,
|
||||||
|
custom_checkbox: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum Message {
|
||||||
|
DefaultChecked(bool),
|
||||||
|
CustomChecked(bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sandbox for Example {
|
||||||
|
type Message = Message;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Default::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn title(&self) -> String {
|
||||||
|
String::from("Checkbox - Iced")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, message: Message) {
|
||||||
|
match message {
|
||||||
|
Message::DefaultChecked(value) => self.default_checkbox = value,
|
||||||
|
Message::CustomChecked(value) => self.custom_checkbox = value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self) -> Element<Message> {
|
||||||
|
let default_checkbox =
|
||||||
|
checkbox("Default", self.default_checkbox, Message::DefaultChecked);
|
||||||
|
let custom_checkbox =
|
||||||
|
checkbox("Custom", self.custom_checkbox, Message::CustomChecked)
|
||||||
|
.icon(checkbox::Icon {
|
||||||
|
font: ICON_FONT,
|
||||||
|
code_point: '\u{e901}',
|
||||||
|
size: None,
|
||||||
|
});
|
||||||
|
|
||||||
|
let content = column![default_checkbox, custom_checkbox].spacing(22);
|
||||||
|
|
||||||
|
container(content)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.center_x()
|
||||||
|
.center_y()
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,17 @@ use crate::{
|
||||||
|
|
||||||
pub use iced_style::checkbox::{Appearance, StyleSheet};
|
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.
|
/// A box that can be checked.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
|
@ -45,6 +56,7 @@ where
|
||||||
spacing: u16,
|
spacing: u16,
|
||||||
text_size: Option<u16>,
|
text_size: Option<u16>,
|
||||||
font: Renderer::Font,
|
font: Renderer::Font,
|
||||||
|
icon: Icon<Renderer::Font>,
|
||||||
style: <Renderer::Theme as StyleSheet>::Style,
|
style: <Renderer::Theme as StyleSheet>::Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,6 +92,11 @@ where
|
||||||
spacing: Self::DEFAULT_SPACING,
|
spacing: Self::DEFAULT_SPACING,
|
||||||
text_size: None,
|
text_size: None,
|
||||||
font: Renderer::Font::default(),
|
font: Renderer::Font::default(),
|
||||||
|
icon: Icon {
|
||||||
|
font: Renderer::ICON_FONT,
|
||||||
|
code_point: Renderer::CHECKMARK_ICON,
|
||||||
|
size: None,
|
||||||
|
},
|
||||||
style: Default::default(),
|
style: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -116,6 +133,12 @@ where
|
||||||
self
|
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`].
|
/// Sets the style of the [`Checkbox`].
|
||||||
pub fn style(
|
pub fn style(
|
||||||
mut self,
|
mut self,
|
||||||
|
|
@ -243,17 +266,24 @@ where
|
||||||
custom_style.background,
|
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 {
|
if self.is_checked {
|
||||||
renderer.fill_text(text::Text {
|
renderer.fill_text(text::Text {
|
||||||
content: &Renderer::CHECKMARK_ICON.to_string(),
|
content: &code_point.to_string(),
|
||||||
font: Renderer::ICON_FONT,
|
font: font.clone(),
|
||||||
size: bounds.height * 0.7,
|
size,
|
||||||
bounds: Rectangle {
|
bounds: Rectangle {
|
||||||
x: bounds.center_x(),
|
x: bounds.center_x(),
|
||||||
y: bounds.center_y(),
|
y: bounds.center_y(),
|
||||||
..bounds
|
..bounds
|
||||||
},
|
},
|
||||||
color: custom_style.checkmark_color,
|
color: custom_style.icon_color,
|
||||||
horizontal_alignment: alignment::Horizontal::Center,
|
horizontal_alignment: alignment::Horizontal::Center,
|
||||||
vertical_alignment: alignment::Vertical::Center,
|
vertical_alignment: alignment::Vertical::Center,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ pub mod button {
|
||||||
|
|
||||||
pub mod checkbox {
|
pub mod checkbox {
|
||||||
//! Show toggle controls using checkboxes.
|
//! Show toggle controls using checkboxes.
|
||||||
pub use iced_native::widget::checkbox::{Appearance, StyleSheet};
|
pub use iced_native::widget::checkbox::{Appearance, Icon, StyleSheet};
|
||||||
|
|
||||||
/// A box that can be checked.
|
/// A box that can be checked.
|
||||||
pub type Checkbox<'a, Message, Renderer = crate::Renderer> =
|
pub type Checkbox<'a, Message, Renderer = crate::Renderer> =
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ use iced_core::{Background, Color};
|
||||||
pub struct Appearance {
|
pub struct Appearance {
|
||||||
/// The [`Background`] of the checkbox.
|
/// The [`Background`] of the checkbox.
|
||||||
pub background: Background,
|
pub background: Background,
|
||||||
/// The checkmark [`Color`] of the checkbox.
|
/// The icon [`Color`] of the checkbox.
|
||||||
pub checkmark_color: Color,
|
pub icon_color: Color,
|
||||||
/// The border radius of the checkbox.
|
/// The border radius of the checkbox.
|
||||||
pub border_radius: f32,
|
pub border_radius: f32,
|
||||||
/// The border width of the checkbox.
|
/// The border width of the checkbox.
|
||||||
|
|
|
||||||
|
|
@ -320,7 +320,7 @@ impl checkbox::StyleSheet for Theme {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn checkbox_appearance(
|
fn checkbox_appearance(
|
||||||
checkmark_color: Color,
|
icon_color: Color,
|
||||||
base: palette::Pair,
|
base: palette::Pair,
|
||||||
accent: palette::Pair,
|
accent: palette::Pair,
|
||||||
is_checked: bool,
|
is_checked: bool,
|
||||||
|
|
@ -331,7 +331,7 @@ fn checkbox_appearance(
|
||||||
} else {
|
} else {
|
||||||
base.color
|
base.color
|
||||||
}),
|
}),
|
||||||
checkmark_color,
|
icon_color,
|
||||||
border_radius: 2.0,
|
border_radius: 2.0,
|
||||||
border_width: 1.0,
|
border_width: 1.0,
|
||||||
border_color: accent.color,
|
border_color: accent.color,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue