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:
Marshall Bowers 2023-12-01 13:29:11 -05:00 committed by GitHub
parent 4f507756d6
commit 4b23c5c658
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View file

@ -8,6 +8,7 @@ pub struct Button {
base: ButtonLike,
label: SharedString,
label_color: Option<Color>,
selected_label: Option<SharedString>,
}
impl Button {
@ -16,6 +17,7 @@ impl Button {
base: ButtonLike::new(id),
label: label.into(),
label_color: None,
selected_label: None,
}
}
@ -23,6 +25,11 @@ impl Button {
self.label_color = label_color.into();
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 {
@ -74,6 +81,11 @@ impl RenderOnce for Button {
type Rendered = ButtonLike;
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 {
Color::Disabled
} else if self.base.selected {
@ -83,7 +95,7 @@ impl RenderOnce for Button {
};
self.base.child(
Label::new(self.label)
Label::new(label)
.color(label_color)
.line_height_style(LineHeightStyle::UILabel),
)

View file

@ -16,6 +16,12 @@ impl Render for ButtonStory {
.child(Button::new("default_filled", "Click me"))
.child(Story::label("Selected"))
.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(Button::new("filled_with_label_color", "Click me").color(Color::Created))
.child(Story::label("Default (Subtle)"))