-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
352 additions
and
6 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
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,171 @@ | ||
use std::fmt::Display; | ||
|
||
use floem::{ | ||
reactive::create_signal, | ||
view::View, | ||
views::{h_stack, label, v_stack, Decorators}, | ||
}; | ||
use oxytail_base::widgets::{ | ||
common_props::{OxySize, OxyVariant}, | ||
radio_button::{labeled_radio_button, radio_button, RadioProps}, | ||
}; | ||
|
||
#[derive(PartialEq, Eq, Clone)] | ||
enum RgbSpace { | ||
Red, | ||
Green, | ||
Blue, | ||
} | ||
|
||
impl Display for RgbSpace { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match *self { | ||
RgbSpace::Red => write!(f, "Red"), | ||
RgbSpace::Green => write!(f, "Green"), | ||
RgbSpace::Blue => write!(f, "Blue"), | ||
} | ||
} | ||
} | ||
|
||
fn labeled_radio_variant_with_state(variant: OxyVariant, size: OxySize) -> impl View { | ||
let (color, set_color) = create_signal(RgbSpace::Green); | ||
|
||
v_stack(( | ||
labeled_radio_button( | ||
RgbSpace::Red, | ||
color, | ||
|| RgbSpace::Red, | ||
Some(RadioProps { | ||
variant, | ||
size, | ||
..Default::default() | ||
}), | ||
) | ||
.on_click_stop(move |_| { | ||
set_color.set(RgbSpace::Red); | ||
}), | ||
labeled_radio_button( | ||
RgbSpace::Green, | ||
color, | ||
|| RgbSpace::Green, | ||
Some(RadioProps { | ||
variant, | ||
size, | ||
..Default::default() | ||
}), | ||
) | ||
.on_click_stop(move |_| { | ||
set_color.set(RgbSpace::Green); | ||
}), | ||
labeled_radio_button( | ||
RgbSpace::Blue, | ||
color, | ||
|| RgbSpace::Blue, | ||
Some(RadioProps { | ||
variant, | ||
size, | ||
..Default::default() | ||
}), | ||
) | ||
.on_click_stop(move |_| { | ||
set_color.set(RgbSpace::Blue); | ||
}), | ||
)) | ||
} | ||
|
||
fn radio_variant_with_state(variant: OxyVariant, size: OxySize) -> impl View { | ||
let (color, set_color) = create_signal(RgbSpace::Green); | ||
|
||
v_stack(( | ||
radio_button( | ||
RgbSpace::Red, | ||
color, | ||
Some(RadioProps { | ||
variant, | ||
size, | ||
..Default::default() | ||
}), | ||
) | ||
.on_click_stop(move |_| { | ||
set_color.set(RgbSpace::Red); | ||
}), | ||
radio_button( | ||
RgbSpace::Green, | ||
color, | ||
Some(RadioProps { | ||
variant, | ||
size, | ||
..Default::default() | ||
}), | ||
) | ||
.on_click_stop(move |_| { | ||
set_color.set(RgbSpace::Green); | ||
}), | ||
radio_button( | ||
RgbSpace::Blue, | ||
color, | ||
Some(RadioProps { | ||
variant, | ||
size, | ||
..Default::default() | ||
}), | ||
) | ||
.on_click_stop(move |_| { | ||
set_color.set(RgbSpace::Blue); | ||
}), | ||
)) | ||
} | ||
|
||
pub fn radio_variants() -> impl View { | ||
v_stack(( | ||
label(|| "Radio button variants"), | ||
h_stack(( | ||
radio_variant_with_state(OxyVariant::Default, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Neutral, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Primary, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Secondary, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Accent, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Ghost, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Link, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Info, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Success, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Warning, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Error, OxySize::Normal), | ||
)) | ||
.style(|s| s.gap(10, 10)), | ||
)) | ||
} | ||
pub fn radio_sizes() -> impl View { | ||
v_stack(( | ||
label(|| "Radio button sizes"), | ||
h_stack(( | ||
radio_variant_with_state(OxyVariant::Primary, OxySize::Large), | ||
radio_variant_with_state(OxyVariant::Primary, OxySize::Normal), | ||
radio_variant_with_state(OxyVariant::Primary, OxySize::Small), | ||
radio_variant_with_state(OxyVariant::Primary, OxySize::Tiny), | ||
)) | ||
.style(|s| s.gap(10, 10)), | ||
)) | ||
} | ||
pub fn labeled_radio_variants() -> impl View { | ||
v_stack(( | ||
label(|| "Labeled radio buttons"), | ||
h_stack(( | ||
labeled_radio_variant_with_state(OxyVariant::Primary, OxySize::Normal), | ||
labeled_radio_variant_with_state(OxyVariant::Secondary, OxySize::Normal), | ||
)) | ||
.style(|s| s.gap(10, 10)), | ||
)) | ||
} | ||
pub fn disabled_labeled_radio_variants() -> impl View { | ||
v_stack(( | ||
label(|| "Disabled state"), | ||
h_stack(( | ||
labeled_radio_variant_with_state(OxyVariant::Primary, OxySize::Normal) | ||
.disabled(|| true), | ||
labeled_radio_variant_with_state(OxyVariant::Secondary, OxySize::Normal) | ||
.disabled(|| true), | ||
)) | ||
.style(|s| s.gap(10, 10)), | ||
)) | ||
} |
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
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod checkbox; | |
pub mod text_input; | ||
pub mod common_props; | ||
pub mod toggle; | ||
pub mod radio_button; |
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,90 @@ | ||
use std::fmt::Display; | ||
|
||
use floem::{ | ||
reactive::ReadSignal, | ||
style::Style, | ||
view::View, | ||
views::{self, container, empty, h_stack, Decorators}, | ||
}; | ||
|
||
use crate::get_current_theme; | ||
|
||
use super::common_props::{OxySize, OxyVariant}; | ||
|
||
#[derive(Default, Clone, Copy)] | ||
pub struct RadioProps { | ||
pub variant: OxyVariant, | ||
pub outlined: bool, | ||
pub size: OxySize, | ||
} | ||
|
||
fn radio_button_svg<T>( | ||
represented_value: T, | ||
actual_value: ReadSignal<T>, | ||
inner_style_enhancer: Box<dyn Fn(Style) -> Style>, | ||
outer_style_enhancer: Box<dyn Fn(Style) -> Style>, | ||
) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
container(empty().style(move |s| { | ||
inner_style_enhancer(s).apply_if(actual_value.get() != represented_value, |s| { | ||
s.display(floem::style::Display::None) | ||
}) | ||
})) | ||
.style(move |s| outer_style_enhancer(s)) | ||
} | ||
|
||
fn upstream_radio_button<T>( | ||
represented_value: T, | ||
actual_value: ReadSignal<T>, | ||
inner_style_enhancer: Box<dyn Fn(Style) -> Style>, | ||
outer_style_enhancer: Box<dyn Fn(Style) -> Style>, | ||
) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
radio_button_svg( | ||
represented_value, | ||
actual_value, | ||
inner_style_enhancer, | ||
outer_style_enhancer, | ||
) | ||
.keyboard_navigatable() | ||
} | ||
|
||
pub fn radio_button<T>( | ||
represented_value: T, | ||
actual_value: ReadSignal<T>, | ||
props: Option<RadioProps>, | ||
) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
let theme = get_current_theme(); | ||
|
||
let props = props.unwrap_or(RadioProps::default()); | ||
|
||
let (inner_style, outer_style) = theme.get_radio_style(props); | ||
|
||
let base_widget = | ||
upstream_radio_button(represented_value, actual_value, inner_style, outer_style); | ||
|
||
base_widget | ||
} | ||
|
||
pub fn labeled_radio_button<T, S: Display + 'static>( | ||
represented_value: T, | ||
actual_value: ReadSignal<T>, | ||
label: impl Fn() -> S + 'static, | ||
props: Option<RadioProps>, | ||
) -> impl View | ||
where | ||
T: Eq + PartialEq + Clone + 'static, | ||
{ | ||
h_stack(( | ||
radio_button(represented_value, actual_value, props), | ||
views::label(label), | ||
)) | ||
.style(|s| s.gap(8, 0)) | ||
} |
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
Oops, something went wrong.