Skip to content

Commit

Permalink
Merge pull request #2109 from avsaase/feat/checkbox-disabled
Browse files Browse the repository at this point in the history
Add option to disable checkbox
  • Loading branch information
hecrj authored Feb 1, 2024
2 parents b95f3ab + b0d40d7 commit 759f0e9
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 89 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- `Themer` widget. [#2209](https://github.com/iced-rs/iced/pull/2209)
- 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)
- `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)
Expand Down Expand Up @@ -87,6 +88,7 @@ Many thanks to...
- @alec-deason
- @arslee07
- @AustinMReppert
- @avsaase
- @bungoboingo
- @Calastrophe
- @casperstorm
Expand Down
64 changes: 45 additions & 19 deletions examples/checkbox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use iced::executor;
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};

const ICON_FONT: Font = Font::with_name("icons");
Expand All @@ -11,14 +12,16 @@ pub fn main() -> iced::Result {

#[derive(Default)]
struct Example {
default_checkbox: bool,
custom_checkbox: bool,
default: bool,
styled: bool,
custom: bool,
}

#[derive(Debug, Clone, Copy)]
enum Message {
DefaultChecked(bool),
CustomChecked(bool),
DefaultToggled(bool),
CustomToggled(bool),
StyledToggled(bool),
FontLoaded(Result<(), font::Error>),
}

Expand All @@ -42,28 +45,51 @@ impl Application for Example {

fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::DefaultChecked(value) => self.default_checkbox = value,
Message::CustomChecked(value) => self.custom_checkbox = value,
Message::DefaultToggled(default) => {
self.default = default;
}
Message::StyledToggled(styled) => {
self.styled = styled;
}
Message::CustomToggled(custom) => {
self.custom = custom;
}
Message::FontLoaded(_) => (),
}

Command::none()
}

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,
line_height: text::LineHeight::Relative(1.0),
shaping: text::Shaping::Basic,
});
let default_checkbox = checkbox("Default", self.default)
.on_toggle(Message::DefaultToggled);

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)
.width(Length::Fill)
Expand Down
7 changes: 2 additions & 5 deletions examples/custom_shader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ impl Application for IcedCubes {
.step(0.01)
.width(100),
),
checkbox(
"Show Depth Buffer",
self.scene.show_depth_buffer,
Message::ShowDepthBuffer
),
checkbox("Show Depth Buffer", self.scene.show_depth_buffer)
.on_toggle(Message::ShowDepthBuffer),
]
.spacing(40);

Expand Down
7 changes: 2 additions & 5 deletions examples/events/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ impl Application for Events {
.map(Element::from),
);

let toggle = checkbox(
"Listen to runtime events",
self.enabled,
Message::Toggled,
);
let toggle = checkbox("Listen to runtime events", self.enabled)
.on_toggle(Message::Toggled);

let exit = button(
text("Exit")
Expand Down
3 changes: 2 additions & 1 deletion examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ fn view_controls<'a>(
row![
playback_controls,
speed_controls,
checkbox("Grid", is_grid_enabled, Message::ToggleGrid)
checkbox("Grid", is_grid_enabled)
.on_toggle(Message::ToggleGrid)
.size(16)
.spacing(5)
.text_size(16),
Expand Down
3 changes: 2 additions & 1 deletion examples/layout/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ impl Application for Layout {
let header = row![
text(self.example.title).size(20).font(Font::MONOSPACE),
horizontal_space(Length::Fill),
checkbox("Explain", self.explain, Message::ExplainToggled),
checkbox("Explain", self.explain)
.on_toggle(Message::ExplainToggled),
pick_list(
Theme::ALL,
Some(self.theme.clone()),
Expand Down
7 changes: 2 additions & 5 deletions examples/styling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,8 @@ impl Sandbox for Styling {
.width(Length::Fill)
.height(100);

let checkbox = checkbox(
"Check me!",
self.checkbox_value,
Message::CheckboxToggled,
);
let checkbox = checkbox("Check me!", self.checkbox_value)
.on_toggle(Message::CheckboxToggled);

let toggler = toggler(
String::from("Toggle me!"),
Expand Down
8 changes: 3 additions & 5 deletions examples/svg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ impl Sandbox for Tiger {
},
);

let apply_color_filter = checkbox(
"Apply a color filter",
self.apply_color_filter,
Message::ToggleColorFilter,
);
let apply_color_filter =
checkbox("Apply a color filter", self.apply_color_filter)
.on_toggle(Message::ToggleColorFilter);

container(
column![
Expand Down
11 changes: 4 additions & 7 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,10 @@ impl Task {
fn view(&self, i: usize) -> Element<TaskMessage> {
match &self.state {
TaskState::Idle => {
let checkbox = checkbox(
&self.description,
self.completed,
TaskMessage::Completed,
)
.width(Length::Fill)
.text_shaping(text::Shaping::Advanced);
let checkbox = checkbox(&self.description, self.completed)
.on_toggle(TaskMessage::Completed)
.width(Length::Fill)
.text_shaping(text::Shaping::Advanced);

row![
checkbox,
Expand Down
33 changes: 17 additions & 16 deletions examples/tour/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,13 @@ impl<'a> Step {
.width(Length::Fill)
.horizontal_alignment(alignment::Horizontal::Center),
)
.push(checkbox(
"Use nearest interpolation",
filter_method == image::FilterMethod::Nearest,
StepMessage::ImageUseNearestToggled,
))
.push(
checkbox(
"Use nearest interpolation",
filter_method == image::FilterMethod::Nearest,
)
.on_toggle(StepMessage::ImageUseNearestToggled),
)
.align_items(Alignment::Center)
}

Expand Down Expand Up @@ -616,16 +618,14 @@ impl<'a> Step {
} else {
text_input
})
.push(checkbox(
"Enable password mode",
is_secure,
StepMessage::ToggleSecureInput,
))
.push(checkbox(
"Show icon",
is_showing_icon,
StepMessage::ToggleTextInputIcon,
))
.push(
checkbox("Enable password mode", is_secure)
.on_toggle(StepMessage::ToggleSecureInput),
)
.push(
checkbox("Show icon", is_showing_icon)
.on_toggle(StepMessage::ToggleTextInputIcon),
)
.push(
"A text input produces a message every time it changes. It is \
very easy to keep track of its contents:",
Expand Down Expand Up @@ -658,7 +658,8 @@ impl<'a> Step {
.horizontal_alignment(alignment::Horizontal::Center),
)
} else {
checkbox("Explain layout", debug, StepMessage::DebugToggled)
checkbox("Explain layout", debug)
.on_toggle(StepMessage::DebugToggled)
.into()
})
.push("Feel free to go back and take a look.")
Expand Down
7 changes: 2 additions & 5 deletions examples/vectorial_text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ impl Sandbox for VectorialText {
column![
canvas(&self.state).width(Length::Fill).height(Length::Fill),
column![
checkbox(
"Use Japanese",
self.state.use_japanese,
Message::ToggleJapanese
),
checkbox("Use Japanese", self.state.use_japanese,)
.on_toggle(Message::ToggleJapanese),
row![
slider_with_label(
"Size",
Expand Down
3 changes: 3 additions & 0 deletions style/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ pub trait StyleSheet {

/// Produces the hovered [`Appearance`] of a checkbox.
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;
}
40 changes: 38 additions & 2 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl checkbox::StyleSheet for Theme {
Checkbox::Secondary => checkbox_appearance(
palette.background.base.text,
palette.background.base,
palette.background.base,
palette.background.strong,
is_checked,
),
Checkbox::Success => checkbox_appearance(
Expand Down Expand Up @@ -355,7 +355,7 @@ impl checkbox::StyleSheet for Theme {
Checkbox::Secondary => checkbox_appearance(
palette.background.base.text,
palette.background.weak,
palette.background.base,
palette.background.strong,
is_checked,
),
Checkbox::Success => checkbox_appearance(
Expand All @@ -373,6 +373,42 @@ impl checkbox::StyleSheet for Theme {
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(
Expand Down
Loading

0 comments on commit 759f0e9

Please sign in to comment.