-
Notifications
You must be signed in to change notification settings - Fork 202
chore: add documentation to radio_button.rs and fix warning in image.rs #861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| //! Module for creating and managing radio buttons with optional labels, reactivity, and styling. | ||
| //! | ||
| //! This module includes [`RadioButton`] and helper functions for building both standalone and labeled radio buttons. | ||
| //! | ||
| //! It supports multiple levels of reactivity using closures or reactive signals (`RwSignal`, etc.). | ||
| //! | ||
| //! # Usage | ||
| //! | ||
| //! ```rust | ||
| //! # use floem::views::radio_button; | ||
| //! use floem_reactive::{RwSignal, SignalGet}; | ||
| //! let selected = RwSignal::new("A".to_string()); | ||
| //! radio_button("A".to_string(), move || selected.get()); | ||
| //! ``` | ||
| //! | ||
| //! For labels: | ||
| //! ```rust | ||
| //! # use floem::views::labeled_radio_button; | ||
| //! use floem_reactive::{RwSignal, SignalGet}; | ||
| //! let selected = RwSignal::new("A".to_string()); | ||
| //! labeled_radio_button("A".to_string(), move || selected.get(), || "Option A"); | ||
| //! ``` | ||
| #[deny(missing_docs)] | ||
| use crate::{ | ||
| style_class, | ||
| view::View, | ||
|
|
@@ -13,6 +36,9 @@ style_class!(pub RadioButtonDotClass); | |
| style_class!(pub RadioButtonDotSelectedClass); | ||
| style_class!(pub LabeledRadioButtonClass); | ||
|
|
||
| /// Internal helper to create the visual representation of a radio button. | ||
| /// | ||
| /// Conditionally shows the selection dot based on whether `actual_value == represented_value`. | ||
| fn radio_button_svg<T>(represented_value: T, actual_value: impl SignalGet<T> + 'static) -> impl View | ||
| where | ||
| T: Eq + PartialEq + Clone + 'static, | ||
|
|
@@ -25,19 +51,32 @@ where | |
| .class(RadioButtonClass) | ||
| } | ||
|
|
||
| /// The `RadioButton` struct provides various methods to create and manage radio buttons. | ||
| /// A struct for building radio buttons using different data update strategies. | ||
| /// | ||
| /// The radio button is visually selectable and supports keyboard navigation. | ||
| /// | ||
| /// # Reactivity Options | ||
| /// | ||
| /// # Related Functions | ||
| /// - [`radio_button`] | ||
| /// - [`labeled_radio_button`] | ||
| /// - [`RadioButton::new`] – for closures returning a value. | ||
| /// - [`RadioButton::new_get`] – for read-only reactive signals. | ||
| /// - [`RadioButton::new_rw`] – for read-write reactive signals. | ||
| /// | ||
| /// # Related | ||
| /// See [`radio_button`] and [`labeled_radio_button`] for simplified constructors. | ||
| pub struct RadioButton; | ||
|
|
||
| impl RadioButton { | ||
| /// Creates a new radio button with a closure that determines its selected state. | ||
| /// Creates a new radio button using a closure for its current value. | ||
| /// | ||
| /// The returned value is wrapped in a [`ValueContainer`] so it can be managed declaratively. | ||
| /// | ||
| /// This method is useful when you want a radio button whose state is determined by a closure. | ||
| /// The state can be dynamically updated by the closure, and the radio button will reflect these changes. | ||
| #[allow(clippy::new_ret_no_self)] | ||
| /// # Example | ||
| /// ```rust | ||
| /// use floem::views::RadioButton; | ||
| /// use floem_reactive::{RwSignal, SignalGet}; | ||
| /// let selected = RwSignal::new("A".to_string()); | ||
| /// RadioButton::new("A".to_string(), move || selected.get()); | ||
| /// ``` | ||
| pub fn new<T>(represented_value: T, actual_value: impl Fn() -> T + 'static) -> ValueContainer<T> | ||
| where | ||
| T: Eq + PartialEq + Clone + 'static, | ||
|
|
@@ -54,10 +93,9 @@ impl RadioButton { | |
| ) | ||
| } | ||
|
|
||
| /// Creates a new radio button with a signal that provides its selected state. | ||
| /// Creates a read-only reactive radio button. | ||
| /// | ||
| /// Use this method when you have a signal that provides the current state of the radio button. | ||
| /// The radio button will automatically update its state based on the signal. | ||
| /// Useful for when the button state is externally managed and shouldn't be changed by the user. | ||
|
Comment on lines
-59
to
+98
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is an improvement over the previous docs. |
||
| pub fn new_get<T>( | ||
| represented_value: T, | ||
| actual_value: impl SignalGet<T> + 'static, | ||
|
|
@@ -68,10 +106,17 @@ impl RadioButton { | |
| radio_button_svg(represented_value, actual_value).keyboard_navigable() | ||
| } | ||
|
|
||
| /// Creates a new radio button with a signal that provides and updates its selected state. | ||
| /// Creates a reactive radio button with two-way binding. | ||
| /// | ||
| /// This method is ideal when you need a radio button that not only reflects a signal's state but also updates it. | ||
| /// Clicking the radio button will set the signal to the represented value. | ||
| /// When selected, the radio button will set the underlying signal to its represented value. | ||
|
Comment on lines
-73
to
+111
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? |
||
| /// | ||
| /// # Example | ||
| /// ```rust | ||
| /// use floem::views::RadioButton; | ||
| /// use floem_reactive::RwSignal; | ||
| /// let selected = RwSignal::new("Option1".to_string()); | ||
| /// RadioButton::new_rw("Option2".to_string(), selected); | ||
| /// ``` | ||
| pub fn new_rw<T>( | ||
| represented_value: T, | ||
| actual_value: impl SignalGet<T> + SignalUpdate<T> + Copy + 'static, | ||
|
|
@@ -88,10 +133,7 @@ impl RadioButton { | |
| }) | ||
| } | ||
|
|
||
| /// Creates a new labeled radio button with a closure that determines its selected state. | ||
| /// | ||
| /// This method is useful when you want a labeled radio button whose state is determined by a closure. | ||
| /// The label is also provided by a closure, allowing for dynamic updates. | ||
| /// Creates a new **labeled** radio button from a closure and label generator. | ||
|
Comment on lines
-91
to
+136
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing here. I think this is removing clarity. |
||
| pub fn new_labeled<S: std::fmt::Display + 'static, T>( | ||
| represented_value: T, | ||
| actual_value: impl Fn() -> T + 'static, | ||
|
|
@@ -117,10 +159,7 @@ impl RadioButton { | |
| ) | ||
| } | ||
|
|
||
| /// Creates a new labeled radio button with a signal that provides its selected state. | ||
| /// | ||
| /// Use this method when you have a signal that provides the current state of the radio button and you also want a label. | ||
| /// The radio button and label will automatically update based on the signal. | ||
| /// Creates a read-only **labeled** radio button from a signal and label. | ||
|
Comment on lines
-120
to
+162
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing |
||
| pub fn new_labeled_get<S: std::fmt::Display + 'static, T>( | ||
| represented_value: T, | ||
| actual_value: impl SignalGet<T> + 'static, | ||
|
|
@@ -138,10 +177,7 @@ impl RadioButton { | |
| .keyboard_navigable() | ||
| } | ||
|
|
||
| /// Creates a new labeled radio button with a signal that provides and updates its selected state. | ||
| /// | ||
| /// This method is ideal when you need a labeled radio button that not only reflects a signal's state but also updates it. | ||
| /// Clicking the radio button will set the signal to the represented value. | ||
| /// Creates a reactive **labeled** radio button with two-way binding and dynamic label. | ||
|
Comment on lines
-141
to
+180
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| pub fn new_labeled_rw<S: std::fmt::Display + 'static, T>( | ||
| represented_value: T, | ||
| actual_value: impl SignalGet<T> + SignalUpdate<T> + Copy + 'static, | ||
|
|
@@ -165,8 +201,7 @@ impl RadioButton { | |
| } | ||
| } | ||
|
|
||
| /// Renders a radio button that appears as selected if the signal equals the given enum value. | ||
| /// Can be combined with a label and a stack with a click event (as in `examples/widget-gallery`). | ||
| /// Shorthand for [`RadioButton::new`] to create a reactive radio button. | ||
| pub fn radio_button<T>( | ||
| represented_value: T, | ||
| actual_value: impl Fn() -> T + 'static, | ||
|
|
@@ -177,7 +212,7 @@ where | |
| RadioButton::new(represented_value, actual_value) | ||
| } | ||
|
|
||
| /// Renders a radio button that appears as selected if the signal equals the given enum value. | ||
| /// Shorthand for [`RadioButton::new_labeled`] to create a reactive labeled radio button. | ||
| pub fn labeled_radio_button<S: std::fmt::Display + 'static, T>( | ||
| represented_value: T, | ||
| actual_value: impl Fn() -> T + 'static, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this is an improvement over the previous docs. Why did you change it?