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:
parent
481e42ade9
commit
9d53287341
5 changed files with 45 additions and 51 deletions
|
@ -8,7 +8,6 @@ pub struct Button {
|
||||||
base: ButtonLike,
|
base: ButtonLike,
|
||||||
label: SharedString,
|
label: SharedString,
|
||||||
label_color: Option<Color>,
|
label_color: Option<Color>,
|
||||||
selected: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Button {
|
impl Button {
|
||||||
|
@ -17,21 +16,29 @@ impl Button {
|
||||||
base: ButtonLike::new(id),
|
base: ButtonLike::new(id),
|
||||||
label: label.into(),
|
label: label.into(),
|
||||||
label_color: None,
|
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 {
|
pub fn color(mut self, label_color: impl Into<Option<Color>>) -> Self {
|
||||||
self.label_color = label_color.into();
|
self.label_color = label_color.into();
|
||||||
self
|
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 {
|
impl Clickable for Button {
|
||||||
fn on_click(
|
fn on_click(
|
||||||
mut self,
|
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 {
|
impl ButtonCommon for Button {
|
||||||
fn id(&self) -> &ElementId {
|
fn id(&self) -> &ElementId {
|
||||||
self.base.id()
|
self.base.id()
|
||||||
|
@ -76,7 +76,7 @@ impl RenderOnce for Button {
|
||||||
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
||||||
let label_color = if self.base.disabled {
|
let label_color = if self.base.disabled {
|
||||||
Color::Disabled
|
Color::Disabled
|
||||||
} else if self.selected {
|
} else if self.base.selected {
|
||||||
Color::Selected
|
Color::Selected
|
||||||
} else {
|
} else {
|
||||||
Color::Default
|
Color::Default
|
||||||
|
|
|
@ -172,6 +172,7 @@ pub struct ButtonLike {
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
pub(super) style: ButtonStyle2,
|
pub(super) style: ButtonStyle2,
|
||||||
pub(super) disabled: bool,
|
pub(super) disabled: bool,
|
||||||
|
pub(super) selected: bool,
|
||||||
size: ButtonSize2,
|
size: ButtonSize2,
|
||||||
tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView>>,
|
tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView>>,
|
||||||
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
|
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
|
||||||
|
@ -184,6 +185,7 @@ impl ButtonLike {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
style: ButtonStyle2::default(),
|
style: ButtonStyle2::default(),
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
selected: false,
|
||||||
size: ButtonSize2::Default,
|
size: ButtonSize2::Default,
|
||||||
tooltip: None,
|
tooltip: None,
|
||||||
children: SmallVec::new(),
|
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 {
|
impl Clickable for ButtonLike {
|
||||||
fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
|
fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
|
||||||
self.on_click = Some(Box::new(handler));
|
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 {
|
impl ButtonCommon for ButtonLike {
|
||||||
fn id(&self) -> &ElementId {
|
fn id(&self) -> &ElementId {
|
||||||
&self.id
|
&self.id
|
||||||
|
|
|
@ -9,7 +9,6 @@ pub struct IconButton {
|
||||||
icon: Icon,
|
icon: Icon,
|
||||||
icon_size: IconSize,
|
icon_size: IconSize,
|
||||||
icon_color: Color,
|
icon_color: Color,
|
||||||
selected: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IconButton {
|
impl IconButton {
|
||||||
|
@ -19,15 +18,9 @@ impl IconButton {
|
||||||
icon,
|
icon,
|
||||||
icon_size: IconSize::default(),
|
icon_size: IconSize::default(),
|
||||||
icon_color: Color::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 {
|
pub fn icon_size(mut self, icon_size: IconSize) -> Self {
|
||||||
self.icon_size = icon_size;
|
self.icon_size = icon_size;
|
||||||
self
|
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 {
|
impl Clickable for IconButton {
|
||||||
fn on_click(
|
fn on_click(
|
||||||
mut self,
|
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 {
|
impl ButtonCommon for IconButton {
|
||||||
fn id(&self) -> &ElementId {
|
fn id(&self) -> &ElementId {
|
||||||
self.base.id()
|
self.base.id()
|
||||||
|
@ -87,7 +87,7 @@ impl RenderOnce for IconButton {
|
||||||
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
||||||
let icon_color = if self.base.disabled {
|
let icon_color = if self.base.disabled {
|
||||||
Color::Disabled
|
Color::Disabled
|
||||||
} else if self.selected {
|
} else if self.base.selected {
|
||||||
Color::Selected
|
Color::Selected
|
||||||
} else {
|
} else {
|
||||||
self.icon_color
|
self.icon_color
|
||||||
|
|
|
@ -20,6 +20,12 @@ 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("Disabled"))
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.w_8()
|
||||||
|
.child(IconButton::new("icon_a", Icon::Hash).disabled(true)),
|
||||||
|
)
|
||||||
.child(Story::label("With `on_click`"))
|
.child(Story::label("With `on_click`"))
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
|
|
|
@ -1,15 +1,7 @@
|
||||||
use gpui::{AnyView, WindowContext};
|
|
||||||
|
|
||||||
/// A trait for elements that can be selected.
|
/// A trait for elements that can be selected.
|
||||||
pub trait Selectable {
|
pub trait Selectable {
|
||||||
/// Sets whether the element is selected.
|
/// Sets whether the element is selected.
|
||||||
fn selected(self, selected: bool) -> Self;
|
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)]
|
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue