Skip to content

Commit 32745f4

Browse files
authored
Merge pull request #1707 from casperstorm/feat/checkbox-icon
Added the ability to change `Checkbox` icon
2 parents e3fbaed + 4fb0be1 commit 32745f4

8 files changed

Lines changed: 123 additions & 9 deletions

File tree

examples/checkbox/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "checkbox"
3+
version = "0.1.0"
4+
authors = ["Casper Rogild Storm<casper@rogildstorm.com>"]
5+
edition = "2021"
6+
publish = false
7+
8+
[dependencies]
9+
iced = { path = "../.." }

examples/checkbox/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Checkbox
2+
3+
A box that can be checked.
4+
5+
The __[`main`]__ file contains all the code of the example.
6+
7+
You can run it with `cargo run`:
8+
```
9+
cargo run --package pick_list
10+
```
11+
12+
[`main`]: src/main.rs

examples/checkbox/fonts/icons.ttf

1.24 KB
Binary file not shown.

examples/checkbox/src/main.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use iced::widget::{checkbox, column, container};
2+
use iced::{Element, Font, Length, Sandbox, Settings};
3+
4+
const ICON_FONT: Font = Font::External {
5+
name: "Icons",
6+
bytes: include_bytes!("../fonts/icons.ttf"),
7+
};
8+
9+
pub fn main() -> iced::Result {
10+
Example::run(Settings::default())
11+
}
12+
13+
#[derive(Default)]
14+
struct Example {
15+
default_checkbox: bool,
16+
custom_checkbox: bool,
17+
}
18+
19+
#[derive(Debug, Clone, Copy)]
20+
enum Message {
21+
DefaultChecked(bool),
22+
CustomChecked(bool),
23+
}
24+
25+
impl Sandbox for Example {
26+
type Message = Message;
27+
28+
fn new() -> Self {
29+
Default::default()
30+
}
31+
32+
fn title(&self) -> String {
33+
String::from("Checkbox - Iced")
34+
}
35+
36+
fn update(&mut self, message: Message) {
37+
match message {
38+
Message::DefaultChecked(value) => self.default_checkbox = value,
39+
Message::CustomChecked(value) => self.custom_checkbox = value,
40+
}
41+
}
42+
43+
fn view(&self) -> Element<Message> {
44+
let default_checkbox =
45+
checkbox("Default", self.default_checkbox, Message::DefaultChecked);
46+
let custom_checkbox =
47+
checkbox("Custom", self.custom_checkbox, Message::CustomChecked)
48+
.icon(checkbox::Icon {
49+
font: ICON_FONT,
50+
code_point: '\u{e901}',
51+
size: None,
52+
});
53+
54+
let content = column![default_checkbox, custom_checkbox].spacing(22);
55+
56+
container(content)
57+
.width(Length::Fill)
58+
.height(Length::Fill)
59+
.center_x()
60+
.center_y()
61+
.into()
62+
}
63+
}

native/src/widget/checkbox.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ use crate::{
1414

1515
pub use iced_style::checkbox::{Appearance, StyleSheet};
1616

17+
/// The icon in a [`Checkbox`].
18+
#[derive(Debug, Clone, PartialEq, Eq)]
19+
pub struct Icon<Font> {
20+
/// Font that will be used to display the `code_point`,
21+
pub font: Font,
22+
/// The unicode code point that will be used as the icon.
23+
pub code_point: char,
24+
/// Font size of the content.
25+
pub size: Option<u16>,
26+
}
27+
1728
/// A box that can be checked.
1829
///
1930
/// # Example
@@ -45,6 +56,7 @@ where
4556
spacing: u16,
4657
text_size: Option<u16>,
4758
font: Renderer::Font,
59+
icon: Icon<Renderer::Font>,
4860
style: <Renderer::Theme as StyleSheet>::Style,
4961
}
5062

@@ -80,6 +92,11 @@ where
8092
spacing: Self::DEFAULT_SPACING,
8193
text_size: None,
8294
font: Renderer::Font::default(),
95+
icon: Icon {
96+
font: Renderer::ICON_FONT,
97+
code_point: Renderer::CHECKMARK_ICON,
98+
size: None,
99+
},
83100
style: Default::default(),
84101
}
85102
}
@@ -116,6 +133,12 @@ where
116133
self
117134
}
118135

136+
/// Sets the [`Icon`] of the [`Checkbox`].
137+
pub fn icon(mut self, icon: Icon<Renderer::Font>) -> Self {
138+
self.icon = icon;
139+
self
140+
}
141+
119142
/// Sets the style of the [`Checkbox`].
120143
pub fn style(
121144
mut self,
@@ -243,17 +266,24 @@ where
243266
custom_style.background,
244267
);
245268

269+
let Icon {
270+
font,
271+
code_point,
272+
size,
273+
} = &self.icon;
274+
let size = size.map(f32::from).unwrap_or(bounds.height * 0.7);
275+
246276
if self.is_checked {
247277
renderer.fill_text(text::Text {
248-
content: &Renderer::CHECKMARK_ICON.to_string(),
249-
font: Renderer::ICON_FONT,
250-
size: bounds.height * 0.7,
278+
content: &code_point.to_string(),
279+
font: font.clone(),
280+
size,
251281
bounds: Rectangle {
252282
x: bounds.center_x(),
253283
y: bounds.center_y(),
254284
..bounds
255285
},
256-
color: custom_style.checkmark_color,
286+
color: custom_style.icon_color,
257287
horizontal_alignment: alignment::Horizontal::Center,
258288
vertical_alignment: alignment::Vertical::Center,
259289
});

src/widget.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub mod button {
3131

3232
pub mod checkbox {
3333
//! Show toggle controls using checkboxes.
34-
pub use iced_native::widget::checkbox::{Appearance, StyleSheet};
34+
pub use iced_native::widget::checkbox::{Appearance, Icon, StyleSheet};
3535

3636
/// A box that can be checked.
3737
pub type Checkbox<'a, Message, Renderer = crate::Renderer> =

style/src/checkbox.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use iced_core::{Background, Color};
66
pub struct Appearance {
77
/// The [`Background`] of the checkbox.
88
pub background: Background,
9-
/// The checkmark [`Color`] of the checkbox.
10-
pub checkmark_color: Color,
9+
/// The icon [`Color`] of the checkbox.
10+
pub icon_color: Color,
1111
/// The border radius of the checkbox.
1212
pub border_radius: f32,
1313
/// The border width of the checkbox.

style/src/theme.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl checkbox::StyleSheet for Theme {
320320
}
321321

322322
fn checkbox_appearance(
323-
checkmark_color: Color,
323+
icon_color: Color,
324324
base: palette::Pair,
325325
accent: palette::Pair,
326326
is_checked: bool,
@@ -331,7 +331,7 @@ fn checkbox_appearance(
331331
} else {
332332
base.color
333333
}),
334-
checkmark_color,
334+
icon_color,
335335
border_radius: 2.0,
336336
border_width: 1.0,
337337
border_color: accent.color,

0 commit comments

Comments
 (0)