Merge pull request #2109 from avsaase/feat/checkbox-disabled

Add option to disable checkbox
This commit is contained in:
Héctor Ramón 2024-02-01 13:51:11 +01:00 committed by GitHub
commit 759f0e9225
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 164 additions and 89 deletions

View file

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Gradients in Oklab color space. [#2055](https://github.com/iced-rs/iced/pull/2055) - Gradients in Oklab color space. [#2055](https://github.com/iced-rs/iced/pull/2055)
- `Themer` widget. [#2209](https://github.com/iced-rs/iced/pull/2209) - `Themer` widget. [#2209](https://github.com/iced-rs/iced/pull/2209)
- Cut functionality for `TextEditor`. [#2215](https://github.com/iced-rs/iced/pull/2215) - Cut functionality for `TextEditor`. [#2215](https://github.com/iced-rs/iced/pull/2215)
- Disabled support for `Checkbox`. [#2109](https://github.com/iced-rs/iced/pull/2109)
- `skip_taskbar` window setting for Windows. [#2211](https://github.com/iced-rs/iced/pull/2211) - `skip_taskbar` window setting for Windows. [#2211](https://github.com/iced-rs/iced/pull/2211)
- `fetch_maximized` and `fetch_minimized` window commands. [#2189](https://github.com/iced-rs/iced/pull/2189) - `fetch_maximized` and `fetch_minimized` window commands. [#2189](https://github.com/iced-rs/iced/pull/2189)
- `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172) - `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172)
@ -87,6 +88,7 @@ Many thanks to...
- @alec-deason - @alec-deason
- @arslee07 - @arslee07
- @AustinMReppert - @AustinMReppert
- @avsaase
- @bungoboingo - @bungoboingo
- @Calastrophe - @Calastrophe
- @casperstorm - @casperstorm

View file

@ -1,6 +1,7 @@
use iced::executor; use iced::executor;
use iced::font::{self, Font}; use iced::font::{self, Font};
use iced::widget::{checkbox, column, container, text}; use iced::theme;
use iced::widget::{checkbox, column, container, row, text};
use iced::{Application, Command, Element, Length, Settings, Theme}; use iced::{Application, Command, Element, Length, Settings, Theme};
const ICON_FONT: Font = Font::with_name("icons"); const ICON_FONT: Font = Font::with_name("icons");
@ -11,14 +12,16 @@ pub fn main() -> iced::Result {
#[derive(Default)] #[derive(Default)]
struct Example { struct Example {
default_checkbox: bool, default: bool,
custom_checkbox: bool, styled: bool,
custom: bool,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Message { enum Message {
DefaultChecked(bool), DefaultToggled(bool),
CustomChecked(bool), CustomToggled(bool),
StyledToggled(bool),
FontLoaded(Result<(), font::Error>), FontLoaded(Result<(), font::Error>),
} }
@ -42,8 +45,15 @@ impl Application for Example {
fn update(&mut self, message: Message) -> Command<Message> { fn update(&mut self, message: Message) -> Command<Message> {
match message { match message {
Message::DefaultChecked(value) => self.default_checkbox = value, Message::DefaultToggled(default) => {
Message::CustomChecked(value) => self.custom_checkbox = value, self.default = default;
}
Message::StyledToggled(styled) => {
self.styled = styled;
}
Message::CustomToggled(custom) => {
self.custom = custom;
}
Message::FontLoaded(_) => (), Message::FontLoaded(_) => (),
} }
@ -51,19 +61,35 @@ impl Application for Example {
} }
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {
let default_checkbox = let default_checkbox = checkbox("Default", self.default)
checkbox("Default", self.default_checkbox, Message::DefaultChecked); .on_toggle(Message::DefaultToggled);
let custom_checkbox =
checkbox("Custom", self.custom_checkbox, Message::CustomChecked)
.icon(checkbox::Icon {
font: ICON_FONT,
code_point: '\u{e901}',
size: None,
line_height: text::LineHeight::Relative(1.0),
shaping: text::Shaping::Basic,
});
let content = column![default_checkbox, custom_checkbox].spacing(22); let styled_checkbox = |label, style| {
checkbox(label, self.styled)
.on_toggle_maybe(self.default.then_some(Message::StyledToggled))
.style(style)
};
let checkboxes = row![
styled_checkbox("Primary", theme::Checkbox::Primary),
styled_checkbox("Secondary", theme::Checkbox::Secondary),
styled_checkbox("Success", theme::Checkbox::Success),
styled_checkbox("Danger", theme::Checkbox::Danger),
]
.spacing(20);
let custom_checkbox = checkbox("Custom", self.custom)
.on_toggle(Message::CustomToggled)
.icon(checkbox::Icon {
font: ICON_FONT,
code_point: '\u{e901}',
size: None,
line_height: text::LineHeight::Relative(1.0),
shaping: text::Shaping::Basic,
});
let content =
column![default_checkbox, checkboxes, custom_checkbox].spacing(20);
container(content) container(content)
.width(Length::Fill) .width(Length::Fill)

View file

@ -89,11 +89,8 @@ impl Application for IcedCubes {
.step(0.01) .step(0.01)
.width(100), .width(100),
), ),
checkbox( checkbox("Show Depth Buffer", self.scene.show_depth_buffer)
"Show Depth Buffer", .on_toggle(Message::ShowDepthBuffer),
self.scene.show_depth_buffer,
Message::ShowDepthBuffer
),
] ]
.spacing(40); .spacing(40);

View file

@ -85,11 +85,8 @@ impl Application for Events {
.map(Element::from), .map(Element::from),
); );
let toggle = checkbox( let toggle = checkbox("Listen to runtime events", self.enabled)
"Listen to runtime events", .on_toggle(Message::Toggled);
self.enabled,
Message::Toggled,
);
let exit = button( let exit = button(
text("Exit") text("Exit")

View file

@ -185,7 +185,8 @@ fn view_controls<'a>(
row![ row![
playback_controls, playback_controls,
speed_controls, speed_controls,
checkbox("Grid", is_grid_enabled, Message::ToggleGrid) checkbox("Grid", is_grid_enabled)
.on_toggle(Message::ToggleGrid)
.size(16) .size(16)
.spacing(5) .spacing(5)
.text_size(16), .text_size(16),

View file

@ -86,7 +86,8 @@ impl Application for Layout {
let header = row![ let header = row![
text(self.example.title).size(20).font(Font::MONOSPACE), text(self.example.title).size(20).font(Font::MONOSPACE),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
checkbox("Explain", self.explain, Message::ExplainToggled), checkbox("Explain", self.explain)
.on_toggle(Message::ExplainToggled),
pick_list( pick_list(
Theme::ALL, Theme::ALL,
Some(self.theme.clone()), Some(self.theme.clone()),

View file

@ -115,11 +115,8 @@ impl Sandbox for Styling {
.width(Length::Fill) .width(Length::Fill)
.height(100); .height(100);
let checkbox = checkbox( let checkbox = checkbox("Check me!", self.checkbox_value)
"Check me!", .on_toggle(Message::CheckboxToggled);
self.checkbox_value,
Message::CheckboxToggled,
);
let toggler = toggler( let toggler = toggler(
String::from("Toggle me!"), String::from("Toggle me!"),

View file

@ -51,11 +51,9 @@ impl Sandbox for Tiger {
}, },
); );
let apply_color_filter = checkbox( let apply_color_filter =
"Apply a color filter", checkbox("Apply a color filter", self.apply_color_filter)
self.apply_color_filter, .on_toggle(Message::ToggleColorFilter);
Message::ToggleColorFilter,
);
container( container(
column![ column![

View file

@ -352,13 +352,10 @@ impl Task {
fn view(&self, i: usize) -> Element<TaskMessage> { fn view(&self, i: usize) -> Element<TaskMessage> {
match &self.state { match &self.state {
TaskState::Idle => { TaskState::Idle => {
let checkbox = checkbox( let checkbox = checkbox(&self.description, self.completed)
&self.description, .on_toggle(TaskMessage::Completed)
self.completed, .width(Length::Fill)
TaskMessage::Completed, .text_shaping(text::Shaping::Advanced);
)
.width(Length::Fill)
.text_shaping(text::Shaping::Advanced);
row![ row![
checkbox, checkbox,

View file

@ -554,11 +554,13 @@ impl<'a> Step {
.width(Length::Fill) .width(Length::Fill)
.horizontal_alignment(alignment::Horizontal::Center), .horizontal_alignment(alignment::Horizontal::Center),
) )
.push(checkbox( .push(
"Use nearest interpolation", checkbox(
filter_method == image::FilterMethod::Nearest, "Use nearest interpolation",
StepMessage::ImageUseNearestToggled, filter_method == image::FilterMethod::Nearest,
)) )
.on_toggle(StepMessage::ImageUseNearestToggled),
)
.align_items(Alignment::Center) .align_items(Alignment::Center)
} }
@ -616,16 +618,14 @@ impl<'a> Step {
} else { } else {
text_input text_input
}) })
.push(checkbox( .push(
"Enable password mode", checkbox("Enable password mode", is_secure)
is_secure, .on_toggle(StepMessage::ToggleSecureInput),
StepMessage::ToggleSecureInput, )
)) .push(
.push(checkbox( checkbox("Show icon", is_showing_icon)
"Show icon", .on_toggle(StepMessage::ToggleTextInputIcon),
is_showing_icon, )
StepMessage::ToggleTextInputIcon,
))
.push( .push(
"A text input produces a message every time it changes. It is \ "A text input produces a message every time it changes. It is \
very easy to keep track of its contents:", very easy to keep track of its contents:",
@ -658,7 +658,8 @@ impl<'a> Step {
.horizontal_alignment(alignment::Horizontal::Center), .horizontal_alignment(alignment::Horizontal::Center),
) )
} else { } else {
checkbox("Explain layout", debug, StepMessage::DebugToggled) checkbox("Explain layout", debug)
.on_toggle(StepMessage::DebugToggled)
.into() .into()
}) })
.push("Feel free to go back and take a look.") .push("Feel free to go back and take a look.")

View file

@ -75,11 +75,8 @@ impl Sandbox for VectorialText {
column![ column![
canvas(&self.state).width(Length::Fill).height(Length::Fill), canvas(&self.state).width(Length::Fill).height(Length::Fill),
column![ column![
checkbox( checkbox("Use Japanese", self.state.use_japanese,)
"Use Japanese", .on_toggle(Message::ToggleJapanese),
self.state.use_japanese,
Message::ToggleJapanese
),
row![ row![
slider_with_label( slider_with_label(
"Size", "Size",

View file

@ -24,4 +24,7 @@ pub trait StyleSheet {
/// Produces the hovered [`Appearance`] of a checkbox. /// Produces the hovered [`Appearance`] of a checkbox.
fn hovered(&self, style: &Self::Style, is_checked: bool) -> Appearance; fn hovered(&self, style: &Self::Style, is_checked: bool) -> Appearance;
/// Produces the disabled [`Appearance`] of a checkbox.
fn disabled(&self, style: &Self::Style, is_checked: bool) -> Appearance;
} }

View file

@ -319,7 +319,7 @@ impl checkbox::StyleSheet for Theme {
Checkbox::Secondary => checkbox_appearance( Checkbox::Secondary => checkbox_appearance(
palette.background.base.text, palette.background.base.text,
palette.background.base, palette.background.base,
palette.background.base, palette.background.strong,
is_checked, is_checked,
), ),
Checkbox::Success => checkbox_appearance( Checkbox::Success => checkbox_appearance(
@ -355,7 +355,7 @@ impl checkbox::StyleSheet for Theme {
Checkbox::Secondary => checkbox_appearance( Checkbox::Secondary => checkbox_appearance(
palette.background.base.text, palette.background.base.text,
palette.background.weak, palette.background.weak,
palette.background.base, palette.background.strong,
is_checked, is_checked,
), ),
Checkbox::Success => checkbox_appearance( Checkbox::Success => checkbox_appearance(
@ -373,6 +373,42 @@ impl checkbox::StyleSheet for Theme {
Checkbox::Custom(custom) => custom.hovered(self, is_checked), Checkbox::Custom(custom) => custom.hovered(self, is_checked),
} }
} }
fn disabled(
&self,
style: &Self::Style,
is_checked: bool,
) -> checkbox::Appearance {
let palette = self.extended_palette();
match style {
Checkbox::Primary => checkbox_appearance(
palette.primary.strong.text,
palette.background.weak,
palette.background.strong,
is_checked,
),
Checkbox::Secondary => checkbox_appearance(
palette.background.strong.color,
palette.background.weak,
palette.background.weak,
is_checked,
),
Checkbox::Success => checkbox_appearance(
palette.success.base.text,
palette.background.weak,
palette.success.weak,
is_checked,
),
Checkbox::Danger => checkbox_appearance(
palette.danger.base.text,
palette.background.weak,
palette.danger.weak,
is_checked,
),
Checkbox::Custom(custom) => custom.active(self, is_checked),
}
}
} }
fn checkbox_appearance( fn checkbox_appearance(

View file

@ -28,7 +28,7 @@ pub use crate::style::checkbox::{Appearance, StyleSheet};
/// ///
/// let is_checked = true; /// let is_checked = true;
/// ///
/// Checkbox::new("Toggle me!", is_checked, Message::CheckboxToggled); /// Checkbox::new("Toggle me!", is_checked).on_toggle(Message::CheckboxToggled);
/// ``` /// ```
/// ///
/// ![Checkbox drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/checkbox.png?raw=true) /// ![Checkbox drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/checkbox.png?raw=true)
@ -43,7 +43,7 @@ pub struct Checkbox<
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
is_checked: bool, is_checked: bool,
on_toggle: Box<dyn Fn(bool) -> Message + 'a>, on_toggle: Option<Box<dyn Fn(bool) -> Message + 'a>>,
label: String, label: String,
width: Length, width: Length,
size: f32, size: f32,
@ -70,18 +70,12 @@ where
/// Creates a new [`Checkbox`]. /// Creates a new [`Checkbox`].
/// ///
/// It expects: /// It expects:
/// * a boolean describing whether the [`Checkbox`] is checked or not
/// * the label of the [`Checkbox`] /// * the label of the [`Checkbox`]
/// * a function that will be called when the [`Checkbox`] is toggled. It /// * a boolean describing whether the [`Checkbox`] is checked or not
/// will receive the new state of the [`Checkbox`] and must produce a pub fn new(label: impl Into<String>, is_checked: bool) -> Self {
/// `Message`.
pub fn new<F>(label: impl Into<String>, is_checked: bool, f: F) -> Self
where
F: 'a + Fn(bool) -> Message,
{
Checkbox { Checkbox {
is_checked, is_checked,
on_toggle: Box::new(f), on_toggle: None,
label: label.into(), label: label.into(),
width: Length::Shrink, width: Length::Shrink,
size: Self::DEFAULT_SIZE, size: Self::DEFAULT_SIZE,
@ -101,6 +95,31 @@ where
} }
} }
/// Sets the function that will be called when the [`Checkbox`] is toggled.
/// It will receive the new state of the [`Checkbox`] and must produce a
/// `Message`.
///
/// Unless `on_toggle` is called, the [`Checkbox`] will be disabled.
pub fn on_toggle<F>(mut self, f: F) -> Self
where
F: 'a + Fn(bool) -> Message,
{
self.on_toggle = Some(Box::new(f));
self
}
/// Sets the function that will be called when the [`Checkbox`] is toggled,
/// if `Some`.
///
/// If `None`, the checkbox will be disabled.
pub fn on_toggle_maybe<F>(mut self, f: Option<F>) -> Self
where
F: Fn(bool) -> Message + 'a,
{
self.on_toggle = f.map(|f| Box::new(f) as _);
self
}
/// Sets the size of the [`Checkbox`]. /// Sets the size of the [`Checkbox`].
pub fn size(mut self, size: impl Into<Pixels>) -> Self { pub fn size(mut self, size: impl Into<Pixels>) -> Self {
self.size = size.into().0; self.size = size.into().0;
@ -235,9 +254,10 @@ where
let mouse_over = cursor.is_over(layout.bounds()); let mouse_over = cursor.is_over(layout.bounds());
if mouse_over { if mouse_over {
shell.publish((self.on_toggle)(!self.is_checked)); if let Some(on_toggle) = &self.on_toggle {
shell.publish((on_toggle)(!self.is_checked));
return event::Status::Captured; return event::Status::Captured;
}
} }
} }
_ => {} _ => {}
@ -254,7 +274,7 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
if cursor.is_over(layout.bounds()) { if cursor.is_over(layout.bounds()) && self.on_toggle.is_some() {
mouse::Interaction::Pointer mouse::Interaction::Pointer
} else { } else {
mouse::Interaction::default() mouse::Interaction::default()
@ -272,10 +292,13 @@ where
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
let is_mouse_over = cursor.is_over(layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
let is_disabled = self.on_toggle.is_none();
let mut children = layout.children(); let mut children = layout.children();
let custom_style = if is_mouse_over { let custom_style = if is_disabled {
theme.disabled(&self.style, self.is_checked)
} else if is_mouse_over {
theme.hovered(&self.style, self.is_checked) theme.hovered(&self.style, self.is_checked)
} else { } else {
theme.active(&self.style, self.is_checked) theme.active(&self.style, self.is_checked)

View file

@ -158,13 +158,12 @@ where
pub fn checkbox<'a, Message, Theme, Renderer>( pub fn checkbox<'a, Message, Theme, Renderer>(
label: impl Into<String>, label: impl Into<String>,
is_checked: bool, is_checked: bool,
f: impl Fn(bool) -> Message + 'a,
) -> Checkbox<'a, Message, Theme, Renderer> ) -> Checkbox<'a, Message, Theme, Renderer>
where where
Theme: checkbox::StyleSheet + text::StyleSheet, Theme: checkbox::StyleSheet + text::StyleSheet,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Checkbox::new(label, is_checked, f) Checkbox::new(label, is_checked)
} }
/// Creates a new [`Radio`]. /// Creates a new [`Radio`].