Add selected_label
to Button
(#3478)
This PR adds a new `selected_label` method to `Button`. This can be used to set a different label that should be rendered when the `Button` is selected. Release Notes: - N/A
This commit is contained in:
parent
4f507756d6
commit
4b23c5c658
2 changed files with 19 additions and 1 deletions
|
@ -8,6 +8,7 @@ pub struct Button {
|
||||||
base: ButtonLike,
|
base: ButtonLike,
|
||||||
label: SharedString,
|
label: SharedString,
|
||||||
label_color: Option<Color>,
|
label_color: Option<Color>,
|
||||||
|
selected_label: Option<SharedString>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Button {
|
impl Button {
|
||||||
|
@ -16,6 +17,7 @@ impl Button {
|
||||||
base: ButtonLike::new(id),
|
base: ButtonLike::new(id),
|
||||||
label: label.into(),
|
label: label.into(),
|
||||||
label_color: None,
|
label_color: None,
|
||||||
|
selected_label: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +25,11 @@ impl Button {
|
||||||
self.label_color = label_color.into();
|
self.label_color = label_color.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn selected_label<L: Into<SharedString>>(mut self, label: impl Into<Option<L>>) -> Self {
|
||||||
|
self.selected_label = label.into().map(Into::into);
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Selectable for Button {
|
impl Selectable for Button {
|
||||||
|
@ -74,6 +81,11 @@ impl RenderOnce for Button {
|
||||||
type Rendered = ButtonLike;
|
type Rendered = ButtonLike;
|
||||||
|
|
||||||
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
||||||
|
let label = self
|
||||||
|
.selected_label
|
||||||
|
.filter(|_| self.base.selected)
|
||||||
|
.unwrap_or(self.label);
|
||||||
|
|
||||||
let label_color = if self.base.disabled {
|
let label_color = if self.base.disabled {
|
||||||
Color::Disabled
|
Color::Disabled
|
||||||
} else if self.base.selected {
|
} else if self.base.selected {
|
||||||
|
@ -83,7 +95,7 @@ impl RenderOnce for Button {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.base.child(
|
self.base.child(
|
||||||
Label::new(self.label)
|
Label::new(label)
|
||||||
.color(label_color)
|
.color(label_color)
|
||||||
.line_height_style(LineHeightStyle::UILabel),
|
.line_height_style(LineHeightStyle::UILabel),
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,6 +16,12 @@ impl Render for ButtonStory {
|
||||||
.child(Button::new("default_filled", "Click me"))
|
.child(Button::new("default_filled", "Click me"))
|
||||||
.child(Story::label("Selected"))
|
.child(Story::label("Selected"))
|
||||||
.child(Button::new("selected_filled", "Click me").selected(true))
|
.child(Button::new("selected_filled", "Click me").selected(true))
|
||||||
|
.child(Story::label("Selected with `selected_label`"))
|
||||||
|
.child(
|
||||||
|
Button::new("selected_label_filled", "Click me")
|
||||||
|
.selected(true)
|
||||||
|
.selected_label("I have been selected"),
|
||||||
|
)
|
||||||
.child(Story::label("With `label_color`"))
|
.child(Story::label("With `label_color`"))
|
||||||
.child(Button::new("filled_with_label_color", "Click me").color(Color::Created))
|
.child(Button::new("filled_with_label_color", "Click me").color(Color::Created))
|
||||||
.child(Story::label("Default (Subtle)"))
|
.child(Story::label("Default (Subtle)"))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue