-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use std::fmt::Display; | ||
|
||
use floem::{ | ||
reactive::create_signal, | ||
view::View, | ||
views::{v_stack, Decorators}, | ||
widgets::{labeled_radio_button, radio_button}, | ||
}; | ||
|
||
use crate::form::{form, form_item}; | ||
|
||
#[derive(PartialEq, Eq, Clone)] | ||
enum OperatingSystem { | ||
Windows, | ||
MacOS, | ||
Linux, | ||
} | ||
|
||
impl Display for OperatingSystem { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match *self { | ||
OperatingSystem::Windows => write!(f, "Windows"), | ||
OperatingSystem::MacOS => write!(f, "MacOS"), | ||
OperatingSystem::Linux => write!(f, "Linux"), | ||
} | ||
} | ||
} | ||
|
||
pub fn radio_buttons_view() -> impl View { | ||
let width = 160.0; | ||
let (operating_system, set_operating_system) = create_signal(OperatingSystem::Windows); | ||
form({ | ||
( | ||
form_item("Radio Buttons:".to_string(), width, move || { | ||
v_stack(( | ||
radio_button(OperatingSystem::Windows, operating_system).on_click_stop( | ||
move |_| { | ||
set_operating_system.set(OperatingSystem::Windows); | ||
}, | ||
), | ||
radio_button(OperatingSystem::MacOS, operating_system).on_click_stop( | ||
move |_| { | ||
set_operating_system.set(OperatingSystem::MacOS); | ||
}, | ||
), | ||
radio_button(OperatingSystem::Linux, operating_system).on_click_stop( | ||
move |_| { | ||
set_operating_system.set(OperatingSystem::Linux); | ||
}, | ||
), | ||
)) | ||
.style(|s| s.gap(0.0, 10.0).margin_left(5.0)) | ||
}), | ||
form_item("Disabled Radio Buttons:".to_string(), width, move || { | ||
v_stack(( | ||
radio_button(OperatingSystem::Windows, operating_system) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::Windows); | ||
}) | ||
.disabled(|| true), | ||
radio_button(OperatingSystem::MacOS, operating_system) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::MacOS); | ||
}) | ||
.disabled(|| true), | ||
radio_button(OperatingSystem::Linux, operating_system) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::Linux); | ||
}) | ||
.disabled(|| true), | ||
)) | ||
.style(|s| s.gap(0.0, 10.0).margin_left(5.0)) | ||
}), | ||
form_item("Labelled Radio Buttons:".to_string(), width, move || { | ||
v_stack(( | ||
labeled_radio_button(OperatingSystem::Windows, operating_system, || { | ||
OperatingSystem::Windows | ||
}) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::Windows); | ||
}), | ||
labeled_radio_button(OperatingSystem::MacOS, operating_system, || { | ||
OperatingSystem::MacOS | ||
}) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::MacOS); | ||
}), | ||
labeled_radio_button(OperatingSystem::Linux, operating_system, || { | ||
OperatingSystem::Linux | ||
}) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::Linux); | ||
}), | ||
)) | ||
}), | ||
form_item( | ||
"Disabled Labelled Radio Buttons:".to_string(), | ||
width, | ||
move || { | ||
v_stack(( | ||
labeled_radio_button(OperatingSystem::Windows, operating_system, || { | ||
OperatingSystem::Windows | ||
}) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::Windows); | ||
}) | ||
.disabled(|| true), | ||
labeled_radio_button(OperatingSystem::MacOS, operating_system, || { | ||
OperatingSystem::MacOS | ||
}) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::MacOS); | ||
}) | ||
.disabled(|| true), | ||
labeled_radio_button(OperatingSystem::Linux, operating_system, || { | ||
OperatingSystem::Linux | ||
}) | ||
.on_click_stop(move |_| { | ||
set_operating_system.set(OperatingSystem::Linux); | ||
}) | ||
.disabled(|| true), | ||
)) | ||
}, | ||
), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::{ | ||
style_class, | ||
view::View, | ||
views::{self, container, empty, h_stack, Decorators}, | ||
}; | ||
use floem_reactive::ReadSignal; | ||
|
||
style_class!(pub RadioButtonClass); | ||
style_class!(pub RadioButtonDotClass); | ||
style_class!(pub RadioButtonDotSelectedClass); | ||
style_class!(pub LabeledRadioButtonClass); | ||
|
||
fn radio_button_svg<T>(represented_value: T, actual_value: ReadSignal<T>) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
container(empty().class(RadioButtonDotClass).style(move |s| { | ||
s.apply_if(actual_value.get() != represented_value, |s| { | ||
s.display(taffy::style::Display::None) | ||
}) | ||
})) | ||
.class(RadioButtonClass) | ||
} | ||
|
||
/// 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`). | ||
pub fn radio_button<T>(represented_value: T, actual_value: ReadSignal<T>) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
radio_button_svg(represented_value, actual_value).keyboard_navigatable() | ||
} | ||
|
||
/// Renders a radio button that appears as selected if the signal equals the given enum value. | ||
pub fn labeled_radio_button<S: std::fmt::Display + 'static, T>( | ||
represented_value: T, | ||
actual_value: ReadSignal<T>, | ||
label: impl Fn() -> S + 'static, | ||
) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
h_stack(( | ||
radio_button_svg(represented_value, actual_value), | ||
views::label(label), | ||
)) | ||
.class(LabeledRadioButtonClass) | ||
.style(|s| s.items_center()) | ||
.keyboard_navigatable() | ||
} |