Add selected_icon
to IconButton
This commit is contained in:
parent
f09d9ef723
commit
12b58f5b60
2 changed files with 21 additions and 1 deletions
|
@ -9,6 +9,7 @@ pub struct IconButton {
|
||||||
icon: Icon,
|
icon: Icon,
|
||||||
icon_size: IconSize,
|
icon_size: IconSize,
|
||||||
icon_color: Color,
|
icon_color: Color,
|
||||||
|
selected_icon: Option<Icon>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IconButton {
|
impl IconButton {
|
||||||
|
@ -18,6 +19,7 @@ impl IconButton {
|
||||||
icon,
|
icon,
|
||||||
icon_size: IconSize::default(),
|
icon_size: IconSize::default(),
|
||||||
icon_color: Color::Default,
|
icon_color: Color::Default,
|
||||||
|
selected_icon: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +33,11 @@ impl IconButton {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn selected_icon(mut self, icon: impl Into<Option<Icon>>) -> Self {
|
||||||
|
self.selected_icon = icon.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn action(self, action: Box<dyn Action>) -> Self {
|
pub fn action(self, action: Box<dyn Action>) -> Self {
|
||||||
self.on_click(move |_event, cx| cx.dispatch_action(action.boxed_clone()))
|
self.on_click(move |_event, cx| cx.dispatch_action(action.boxed_clone()))
|
||||||
}
|
}
|
||||||
|
@ -85,6 +92,11 @@ impl RenderOnce for IconButton {
|
||||||
type Rendered = ButtonLike;
|
type Rendered = ButtonLike;
|
||||||
|
|
||||||
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
||||||
|
let icon = self
|
||||||
|
.selected_icon
|
||||||
|
.filter(|_| self.base.selected)
|
||||||
|
.unwrap_or(self.icon);
|
||||||
|
|
||||||
let icon_color = if self.base.disabled {
|
let icon_color = if self.base.disabled {
|
||||||
Color::Disabled
|
Color::Disabled
|
||||||
} else if self.base.selected {
|
} else if self.base.selected {
|
||||||
|
@ -94,7 +106,7 @@ impl RenderOnce for IconButton {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.base.child(
|
self.base.child(
|
||||||
IconElement::new(self.icon)
|
IconElement::new(icon)
|
||||||
.size(self.icon_size)
|
.size(self.icon_size)
|
||||||
.color(icon_color),
|
.color(icon_color),
|
||||||
)
|
)
|
||||||
|
|
|
@ -20,6 +20,14 @@ impl Render for IconButtonStory {
|
||||||
.w_8()
|
.w_8()
|
||||||
.child(IconButton::new("icon_a", Icon::Hash).selected(true)),
|
.child(IconButton::new("icon_a", Icon::Hash).selected(true)),
|
||||||
)
|
)
|
||||||
|
.child(Story::label("Selected with `selected_icon`"))
|
||||||
|
.child(
|
||||||
|
div().w_8().child(
|
||||||
|
IconButton::new("icon_a", Icon::AudioOn)
|
||||||
|
.selected(true)
|
||||||
|
.selected_icon(Icon::AudioOff),
|
||||||
|
),
|
||||||
|
)
|
||||||
.child(Story::label("Disabled"))
|
.child(Story::label("Disabled"))
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue