Skip to content

Commit

Permalink
update checkbox (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton authored Nov 1, 2024
1 parent 9309eb8 commit be77201
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/widget-gallery/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn checkbox_view() -> impl IntoView {
.disabled(|| true)
}),
form_item("Labelled Checkbox:".to_string(), width, move || {
Checkbox::new_labeled_rw(is_checked, || "Check me!")
Checkbox::labeled_rw(is_checked, || "Check me!")
}),
form_item(
"Disabled Labelled Checkbox:".to_string(),
Expand Down
33 changes: 22 additions & 11 deletions src/views/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![deny(missing_docs)]
//! A checkbox view for boolean selection.

use crate::{
style_class,
view::IntoView,
Expand All @@ -9,9 +12,15 @@ use crate::{
use floem_reactive::{SignalGet, SignalUpdate};
use std::fmt::Display;

style_class!(pub CheckboxClass);
style_class!(
/// The style class that is applied to the checkbox.
pub CheckboxClass
);

style_class!(pub LabeledCheckboxClass);
style_class!(
/// The style class that is applied to the labeled checkbox stack.
pub LabeledCheckboxClass
);

fn checkbox_svg(checked: impl SignalGet<bool> + 'static) -> impl IntoView {
const CHECKBOX_SVG: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="-2 -2 16 16"><polygon points="5.19,11.83 0.18,7.44 1.82,5.56 4.81,8.17 10,1.25 12,2.75" /></svg>"#;
Expand All @@ -22,11 +31,13 @@ fn checkbox_svg(checked: impl SignalGet<bool> + 'static) -> impl IntoView {
.keyboard_navigable()
}

/// The `Checkbox` struct provides various methods to create and manage checkboxes.
/// # A customizable checkbox view for boolean selection.
///
/// The `Checkbox` struct provides several constructors, each offering different levels of
/// customization and ease of use. The simplest is the [Checkbox::new_rw] constructor, which gets direct access to a signal and will update it when the checkbox is clicked.
///
/// # Related Functions
/// - [`checkbox`]
/// - [`labeled_checkbox`]
/// Choose the constructor that best fits your needs based on whether you require labeling
/// and how you prefer to manage the checkbox's state (via closure or direct signal manipulation).
pub struct Checkbox;

impl Checkbox {
Expand Down Expand Up @@ -65,7 +76,7 @@ impl Checkbox {
///
/// This method is useful when you want a labeled checkbox whose state is determined by a closure.
/// The label is also provided by a closure, allowing for dynamic updates.
pub fn new_labeled<S: Display + 'static>(
pub fn labeled<S: Display + 'static>(
checked: impl Fn() -> bool + 'static,
label: impl Fn() -> S + 'static,
) -> ValueContainer<bool> {
Expand All @@ -90,7 +101,7 @@ impl Checkbox {
///
/// This method is ideal when you need a labeled checkbox that not only reflects a signal's state but also updates it.
/// Clicking the checkbox will toggle its state and update the signal accordingly.
pub fn new_labeled_rw<S: Display + 'static>(
pub fn labeled_rw<S: Display + 'static>(
checked: impl SignalGet<bool> + SignalUpdate<bool> + Copy + 'static,
label: impl Fn() -> S + 'static,
) -> impl IntoView {
Expand All @@ -103,15 +114,15 @@ impl Checkbox {
}
}

/// Renders a checkbox the provided checked signal.
/// Renders a checkbox the provided checked signal. See also [`Checkbox::new`] and [`Checkbox::new_rw`].
pub fn checkbox(checked: impl Fn() -> bool + 'static) -> ValueContainer<bool> {
Checkbox::new(checked)
}

/// Renders a checkbox using the provided checked signal.
/// Renders a checkbox using the provided checked signal. See also [`Checkbox::labeled`] and [`Checkbox::labeled_rw`].
pub fn labeled_checkbox<S: Display + 'static>(
checked: impl Fn() -> bool + 'static,
label: impl Fn() -> S + 'static,
) -> ValueContainer<bool> {
Checkbox::new_labeled(checked, label)
Checkbox::labeled(checked, label)
}

0 comments on commit be77201

Please sign in to comment.