Add support for optional icon to Button
(#3479)
This PR extends `Button` with support for an optional icon to be displayed next to the label. As part of this, the functionality for displaying an icon within a button has been factored out into an internal `ButtonIcon` component. `ButtonIcon` is now used by both `IconButton` and `Button` to encapsulate the concerns of an icon that is rendered within a button. Release Notes: - N/A
This commit is contained in:
parent
4b23c5c658
commit
c3e7732eab
5 changed files with 157 additions and 25 deletions
|
@ -1,7 +1,11 @@
|
|||
use gpui::AnyView;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::{ButtonCommon, ButtonLike, ButtonSize, ButtonStyle, Label, LineHeightStyle};
|
||||
use crate::{
|
||||
ButtonCommon, ButtonLike, ButtonSize, ButtonStyle, Icon, IconSize, Label, LineHeightStyle,
|
||||
};
|
||||
|
||||
use super::button_icon::ButtonIcon;
|
||||
|
||||
#[derive(IntoElement)]
|
||||
pub struct Button {
|
||||
|
@ -9,6 +13,10 @@ pub struct Button {
|
|||
label: SharedString,
|
||||
label_color: Option<Color>,
|
||||
selected_label: Option<SharedString>,
|
||||
icon: Option<Icon>,
|
||||
icon_size: Option<IconSize>,
|
||||
icon_color: Option<Color>,
|
||||
selected_icon: Option<Icon>,
|
||||
}
|
||||
|
||||
impl Button {
|
||||
|
@ -18,6 +26,10 @@ impl Button {
|
|||
label: label.into(),
|
||||
label_color: None,
|
||||
selected_label: None,
|
||||
icon: None,
|
||||
icon_size: None,
|
||||
icon_color: None,
|
||||
selected_icon: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,6 +42,26 @@ impl Button {
|
|||
self.selected_label = label.into().map(Into::into);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn icon(mut self, icon: impl Into<Option<Icon>>) -> Self {
|
||||
self.icon = icon.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn icon_size(mut self, icon_size: impl Into<Option<IconSize>>) -> Self {
|
||||
self.icon_size = icon_size.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn icon_color(mut self, icon_color: impl Into<Option<Color>>) -> Self {
|
||||
self.icon_color = icon_color.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn selected_icon(mut self, icon: impl Into<Option<Icon>>) -> Self {
|
||||
self.selected_icon = icon.into();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Selectable for Button {
|
||||
|
@ -81,23 +113,35 @@ impl RenderOnce for Button {
|
|||
type Rendered = ButtonLike;
|
||||
|
||||
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
||||
let is_disabled = self.base.disabled;
|
||||
let is_selected = self.base.selected;
|
||||
|
||||
let label = self
|
||||
.selected_label
|
||||
.filter(|_| self.base.selected)
|
||||
.filter(|_| is_selected)
|
||||
.unwrap_or(self.label);
|
||||
|
||||
let label_color = if self.base.disabled {
|
||||
let label_color = if is_disabled {
|
||||
Color::Disabled
|
||||
} else if self.base.selected {
|
||||
} else if is_selected {
|
||||
Color::Selected
|
||||
} else {
|
||||
self.label_color.unwrap_or_default()
|
||||
};
|
||||
|
||||
self.base.child(
|
||||
Label::new(label)
|
||||
.color(label_color)
|
||||
.line_height_style(LineHeightStyle::UILabel),
|
||||
)
|
||||
self.base
|
||||
.children(self.icon.map(|icon| {
|
||||
ButtonIcon::new(icon)
|
||||
.disabled(is_disabled)
|
||||
.selected(is_selected)
|
||||
.selected_icon(self.selected_icon)
|
||||
.size(self.icon_size)
|
||||
.color(self.icon_color)
|
||||
}))
|
||||
.child(
|
||||
Label::new(label)
|
||||
.color(label_color)
|
||||
.line_height_style(LineHeightStyle::UILabel),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue