Skip to content

Commit ee4bb5c

Browse files
authored
Allow the checkbox to be themed (#462)
1 parent b89ae47 commit ee4bb5c

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

wgui/doc/widgets.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ _Translated by key_
356356

357357
`box_size`: **float** (default: 24)
358358

359+
`color_checked`: #FFAABB | #FFAABBCC
360+
359361
`value`: **string**
360362

361363
_optional value that will be sent with internal events_

wgui/src/components/button.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -428,29 +428,19 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
428428
style.overflow.y = taffy::Overflow::Hidden;
429429

430430
// update colors to default ones if they are not specified
431-
let color = if let Some(color) = params.color {
432-
color
433-
} else {
434-
theme.button_color
435-
};
431+
let color = params.color.unwrap_or(theme.button_color);
436432

437-
let border_color = if let Some(border_color) = params.border_color {
438-
border_color
439-
} else {
440-
Color::new(color.r, color.g, color.b, color.a + 0.25)
441-
};
433+
let border_color = params
434+
.border_color
435+
.unwrap_or_else(|| Color::new(color.r, color.g, color.b, color.a + 0.25));
442436

443-
let hover_color = if let Some(hover_color) = params.hover_color {
444-
hover_color
445-
} else {
446-
Color::new(color.r + 0.25, color.g + 0.25, color.g + 0.25, color.a + 0.15)
447-
};
437+
let hover_color = params
438+
.hover_color
439+
.unwrap_or_else(|| Color::new(color.r + 0.25, color.g + 0.25, color.g + 0.25, color.a + 0.15));
448440

449-
let hover_border_color = if let Some(hover_border_color) = params.hover_border_color {
450-
hover_border_color
451-
} else {
452-
Color::new(color.r + 0.5, color.g + 0.5, color.g + 0.5, color.a + 0.5)
453-
};
441+
let hover_border_color = params
442+
.hover_border_color
443+
.unwrap_or_else(|| Color::new(color.r + 0.5, color.g + 0.5, color.g + 0.5, color.a + 0.5));
454444

455445
let gradient_intensity = theme.gradient_intensity;
456446

wgui/src/components/checkbox.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::{
3131
pub struct Params {
3232
pub text: Translation,
3333
pub style: taffy::Style,
34+
pub color_checked: Option<Color>,
3435
pub box_size: f32,
3536
pub checked: bool,
3637
pub radio_group: Option<Rc<ComponentRadioGroup>>,
@@ -43,6 +44,7 @@ impl Default for Params {
4344
Self {
4445
text: Translation::from_raw_text(""),
4546
style: Default::default(),
47+
color_checked: None,
4648
box_size: 24.0,
4749
checked: false,
4850
radio_group: None,
@@ -84,6 +86,8 @@ struct Data {
8486
id_label: WidgetID, // Label, parent of container
8587
value: Option<Rc<str>>, // arbitrary value assigned to the element
8688
radio_group: Option<Weak<ComponentRadioGroup>>,
89+
90+
color_checked: Color,
8791
}
8892

8993
pub struct ComponentCheckbox {
@@ -92,6 +96,8 @@ pub struct ComponentCheckbox {
9296
state: Rc<RefCell<State>>,
9397
}
9498

99+
const COLOR_UNCHECKED: Color = Color::new(0.0, 0.0, 0.0, 0.0);
100+
95101
impl ComponentTrait for ComponentCheckbox {
96102
fn base(&self) -> &ComponentBase {
97103
&self.base
@@ -106,12 +112,9 @@ impl ComponentTrait for ComponentCheckbox {
106112
}
107113
}
108114

109-
const COLOR_CHECKED: Color = Color::new(0.1, 0.5, 1.0, 1.0);
110-
const COLOR_UNCHECKED: Color = Color::new(0.1, 0.5, 1.0, 0.0);
111-
112115
fn set_box_checked(widgets: &layout::WidgetMap, data: &Data, checked: bool) {
113116
widgets.call(data.id_inner_box, |rect: &mut WidgetRectangle| {
114-
rect.params.color = if checked { COLOR_CHECKED } else { COLOR_UNCHECKED }
117+
rect.params.color = if checked { data.color_checked } else { COLOR_UNCHECKED }
115118
});
116119
}
117120

@@ -315,6 +318,7 @@ fn register_event_mouse_release(
315318

316319
pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Result<(WidgetPair, Rc<ComponentCheckbox>)> {
317320
let mut style = params.style;
321+
let theme = &ess.layout.state.theme;
318322

319323
// force-override style
320324
style.flex_wrap = taffy::FlexWrap::NoWrap;
@@ -343,6 +347,8 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
343347
(WLength::Units(5.0), WLength::Units(8.0))
344348
};
345349

350+
let color_checked = params.color_checked.unwrap_or(theme.accent_color);
351+
346352
let (root, _) = ess.layout.add_child(
347353
ess.parent,
348354
WidgetRectangle::create(WidgetRectangleParams {
@@ -383,7 +389,7 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
383389
outer_box.id,
384390
WidgetRectangle::create(WidgetRectangleParams {
385391
round: round_5,
386-
color: if params.checked { COLOR_CHECKED } else { COLOR_UNCHECKED },
392+
color: if params.checked { color_checked } else { COLOR_UNCHECKED },
387393
..Default::default()
388394
}),
389395
taffy::Style {
@@ -413,6 +419,7 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
413419
id_label: label.id,
414420
value: params.value,
415421
radio_group: params.radio_group.as_ref().map(Rc::downgrade),
422+
color_checked,
416423
});
417424

418425
let state = Rc::new(RefCell::new(State {

wgui/src/parser/component_checkbox.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub fn parse_component_checkbox(
9191
radio_group,
9292
value: component_value,
9393
tooltip: tooltip.get_info(),
94+
color_checked: None,
9495
},
9596
)?;
9697

0 commit comments

Comments
 (0)