Implement Selectable for buttons (#3451)

This PR implements the `Selectable` trait for `ButtonLike`, `Button`,
and `IconButton`.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2023-11-29 18:46:41 -05:00 committed by GitHub
parent 481e42ade9
commit 9d53287341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 51 deletions

View file

@ -8,7 +8,6 @@ pub struct Button {
base: ButtonLike,
label: SharedString,
label_color: Option<Color>,
selected: bool,
}
impl Button {
@ -17,21 +16,29 @@ impl Button {
base: ButtonLike::new(id),
label: label.into(),
label_color: None,
selected: false,
}
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn color(mut self, label_color: impl Into<Option<Color>>) -> Self {
self.label_color = label_color.into();
self
}
}
impl Selectable for Button {
fn selected(mut self, selected: bool) -> Self {
self.base = self.base.selected(selected);
self
}
}
impl Disableable for Button {
fn disabled(mut self, disabled: bool) -> Self {
self.base = self.base.disabled(disabled);
self
}
}
impl Clickable for Button {
fn on_click(
mut self,
@ -42,13 +49,6 @@ impl Clickable for Button {
}
}
impl Disableable for Button {
fn disabled(mut self, disabled: bool) -> Self {
self.base = self.base.disabled(disabled);
self
}
}
impl ButtonCommon for Button {
fn id(&self) -> &ElementId {
self.base.id()
@ -76,7 +76,7 @@ impl RenderOnce for Button {
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
let label_color = if self.base.disabled {
Color::Disabled
} else if self.selected {
} else if self.base.selected {
Color::Selected
} else {
Color::Default

View file

@ -172,6 +172,7 @@ pub struct ButtonLike {
id: ElementId,
pub(super) style: ButtonStyle2,
pub(super) disabled: bool,
pub(super) selected: bool,
size: ButtonSize2,
tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView>>,
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
@ -184,6 +185,7 @@ impl ButtonLike {
id: id.into(),
style: ButtonStyle2::default(),
disabled: false,
selected: false,
size: ButtonSize2::Default,
tooltip: None,
children: SmallVec::new(),
@ -199,6 +201,13 @@ impl Disableable for ButtonLike {
}
}
impl Selectable for ButtonLike {
fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
}
impl Clickable for ButtonLike {
fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
self.on_click = Some(Box::new(handler));
@ -206,19 +215,6 @@ impl Clickable for ButtonLike {
}
}
// impl Selectable for ButtonLike {
// fn selected(&mut self, selected: bool) -> &mut Self {
// todo!()
// }
// fn selected_tooltip(
// &mut self,
// tooltip: Box<dyn Fn(&mut WindowContext) -> AnyView + 'static>,
// ) -> &mut Self {
// todo!()
// }
// }
impl ButtonCommon for ButtonLike {
fn id(&self) -> &ElementId {
&self.id

View file

@ -9,7 +9,6 @@ pub struct IconButton {
icon: Icon,
icon_size: IconSize,
icon_color: Color,
selected: bool,
}
impl IconButton {
@ -19,15 +18,9 @@ impl IconButton {
icon,
icon_size: IconSize::default(),
icon_color: Color::Default,
selected: false,
}
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn icon_size(mut self, icon_size: IconSize) -> Self {
self.icon_size = icon_size;
self
@ -43,6 +36,20 @@ impl IconButton {
}
}
impl Disableable for IconButton {
fn disabled(mut self, disabled: bool) -> Self {
self.base = self.base.disabled(disabled);
self
}
}
impl Selectable for IconButton {
fn selected(mut self, selected: bool) -> Self {
self.base = self.base.selected(selected);
self
}
}
impl Clickable for IconButton {
fn on_click(
mut self,
@ -53,13 +60,6 @@ impl Clickable for IconButton {
}
}
impl Disableable for IconButton {
fn disabled(mut self, disabled: bool) -> Self {
self.base = self.base.disabled(disabled);
self
}
}
impl ButtonCommon for IconButton {
fn id(&self) -> &ElementId {
self.base.id()
@ -87,7 +87,7 @@ impl RenderOnce for IconButton {
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
let icon_color = if self.base.disabled {
Color::Disabled
} else if self.selected {
} else if self.base.selected {
Color::Selected
} else {
self.icon_color

View file

@ -20,6 +20,12 @@ impl Render for IconButtonStory {
.w_8()
.child(IconButton::new("icon_a", Icon::Hash).selected(true)),
)
.child(Story::label("Disabled"))
.child(
div()
.w_8()
.child(IconButton::new("icon_a", Icon::Hash).disabled(true)),
)
.child(Story::label("With `on_click`"))
.child(
div()

View file

@ -1,15 +1,7 @@
use gpui::{AnyView, WindowContext};
/// A trait for elements that can be selected.
pub trait Selectable {
/// Sets whether the element is selected.
fn selected(self, selected: bool) -> Self;
/// Sets the tooltip that should be shown when the element is selected.
fn selected_tooltip(
self,
tooltip: Box<dyn Fn(&mut WindowContext) -> AnyView + 'static>,
) -> Self;
}
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]