-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widgets): 🎸 widget of radio button
- Loading branch information
Showing
7 changed files
with
129 additions
and
3 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
Binary file added
BIN
+692 Bytes
test_cases/ribir_widgets/radio/tests/radio_widget_with_default_by_wgpu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.14 KB
test_cases/ribir_widgets/radio/tests/radio_widget_with_material_by_wgpu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
use ribir_core::prelude::Classes; | ||
|
||
mod radio_cls; | ||
mod scrollbar_cls; | ||
|
||
pub fn initd_classes() -> Classes { | ||
let mut classes = Classes::default(); | ||
scrollbar_cls::init(&mut classes); | ||
radio_cls::init(&mut classes); | ||
classes | ||
} |
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,64 @@ | ||
use ribir_core::prelude::*; | ||
use ribir_widgets::prelude::*; | ||
|
||
use crate::InteractiveLayer; | ||
|
||
const TOTAL_SIZE: f32 = 40.; | ||
const CONTAINER_SIZE: f32 = 20.; | ||
const INDICATOR_SIZE: f32 = 10.; | ||
const BORDER_SIZE: f32 = 2.; | ||
|
||
pub(super) fn init(classes: &mut Classes) { | ||
classes.insert(RADIO_SELECTED, |_w| { | ||
fn_widget! { | ||
@InteractiveLayer { | ||
border_radii: Radius::all(TOTAL_SIZE / 2.), | ||
color: Palette::of(ctx!()).primary(), | ||
@Container { | ||
size: Size::new(TOTAL_SIZE, TOTAL_SIZE), | ||
cursor: CursorIcon::Pointer, | ||
@Container { | ||
h_align: HAlign::Center, | ||
v_align: VAlign::Center, | ||
size: Size::new(CONTAINER_SIZE, CONTAINER_SIZE), | ||
border_radius: Radius::all(CONTAINER_SIZE / 2.), | ||
border: Border::all( | ||
BorderSide::new(BORDER_SIZE, Palette::of(ctx!()).primary().into()) | ||
), | ||
@Container { | ||
v_align: VAlign::Center, | ||
h_align: HAlign::Center, | ||
size: Size::new(INDICATOR_SIZE, INDICATOR_SIZE), | ||
border_radius: Radius::all(INDICATOR_SIZE / 2.), | ||
background: Palette::of(ctx!()).primary(), | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.into_widget() | ||
}); | ||
|
||
classes.insert(RADIO_UNSELECTED, |_w| { | ||
fn_widget! { | ||
@InteractiveLayer { | ||
border_radii: Radius::all(TOTAL_SIZE / 2.), | ||
color: Palette::of(ctx!()).primary(), | ||
@Container { | ||
cursor: CursorIcon::Pointer, | ||
size: Size::new(TOTAL_SIZE, TOTAL_SIZE), | ||
@Container { | ||
h_align: HAlign::Center, | ||
v_align: VAlign::Center, | ||
size: Size::new(CONTAINER_SIZE, CONTAINER_SIZE), | ||
border_radius: Radius::all(CONTAINER_SIZE / 2.), | ||
border: Border::all( | ||
BorderSide::new(BORDER_SIZE, Palette::of(ctx!()).on_surface_variant().into()) | ||
), | ||
} | ||
} | ||
} | ||
} | ||
.into_widget() | ||
}); | ||
} |
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,59 @@ | ||
use ribir_core::prelude::*; | ||
|
||
class_names! { | ||
#[doc = "Class name for the radio button when selected"] | ||
RADIO_SELECTED, | ||
#[doc = "Class name for the radio button when unselected"] | ||
RADIO_UNSELECTED | ||
} | ||
|
||
#[derive(Declare)] | ||
pub struct Radio { | ||
pub checked: bool, | ||
} | ||
|
||
impl Compose for Radio { | ||
fn compose(this: impl StateWriter<Value = Self>) -> Widget<'static> { | ||
fn_widget! { | ||
@ pipe!($this.checked).map(move |checked| { | ||
fn_widget! { | ||
let class = match checked { | ||
true => RADIO_SELECTED, | ||
false => RADIO_UNSELECTED, | ||
}; | ||
@ Void { class: class } | ||
} | ||
}) | ||
} | ||
.into_widget() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ribir_core::test_helper::{Frame, MockMulti, WidgetTester}; | ||
use ribir_dev_helper::*; | ||
|
||
use super::*; | ||
|
||
fn radio_widget(_: &mut BuildCtx) -> Widget<'static> { | ||
fn_widget! { | ||
@MockMulti { | ||
@Radio { | ||
checked: false, | ||
} | ||
@Radio { | ||
checked: true, | ||
} | ||
} | ||
} | ||
.into_widget() | ||
} | ||
|
||
widget_image_tests!( | ||
radio_widget, | ||
WidgetTester::new(radio_widget) | ||
.with_wnd_size(Size::new(150., 80.)) | ||
.with_comparison(0.002) | ||
); | ||
} |