button: Impl Selectable for the dropdown button (#913)
## Break Change - Add `is_selected` to the `Selectable` trait; Returns true if the element is selected.
This commit is contained in:
parent
901312bc5a
commit
dc1d516a30
10 changed files with 72 additions and 4 deletions
|
|
@ -695,6 +695,7 @@ impl Render for ButtonStory {
|
||||||
DropdownButton::new("dropdown-button1")
|
DropdownButton::new("dropdown-button1")
|
||||||
.small()
|
.small()
|
||||||
.button(Button::new("btn").label("Click Me"))
|
.button(Button::new("btn").label("Click Me"))
|
||||||
|
.selected(selected)
|
||||||
.popup_menu(move |this, _, _| {
|
.popup_menu(move |this, _, _| {
|
||||||
this.menu("Disabled", Box::new(Disabled))
|
this.menu("Disabled", Box::new(Disabled))
|
||||||
.menu("Loading", Box::new(Loading))
|
.menu("Loading", Box::new(Loading))
|
||||||
|
|
@ -705,6 +706,7 @@ impl Render for ButtonStory {
|
||||||
.child(
|
.child(
|
||||||
DropdownButton::new("dropdown-button2")
|
DropdownButton::new("dropdown-button2")
|
||||||
.button(Button::new("btn").label("Click Me"))
|
.button(Button::new("btn").label("Click Me"))
|
||||||
|
.selected(selected)
|
||||||
.popup_menu(move |this, _, _| {
|
.popup_menu(move |this, _, _| {
|
||||||
this.menu("Disabled", Box::new(Disabled))
|
this.menu("Disabled", Box::new(Disabled))
|
||||||
.menu("Loading", Box::new(Loading))
|
.menu("Loading", Box::new(Loading))
|
||||||
|
|
@ -716,6 +718,19 @@ impl Render for ButtonStory {
|
||||||
DropdownButton::new("dropdown-button3")
|
DropdownButton::new("dropdown-button3")
|
||||||
.outline()
|
.outline()
|
||||||
.button(Button::new("btn").label("Outline Dropdown"))
|
.button(Button::new("btn").label("Outline Dropdown"))
|
||||||
|
.selected(selected)
|
||||||
|
.popup_menu(move |this, _, _| {
|
||||||
|
this.menu("Disabled", Box::new(Disabled))
|
||||||
|
.menu("Loading", Box::new(Loading))
|
||||||
|
.menu("Selected", Box::new(Selected))
|
||||||
|
.menu("Compact", Box::new(Compact))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
DropdownButton::new("dropdown-button4")
|
||||||
|
.ghost()
|
||||||
|
.button(Button::new("btn").label("Ghost Dropdown"))
|
||||||
|
.selected(selected)
|
||||||
.popup_menu(move |this, _, _| {
|
.popup_menu(move |this, _, _| {
|
||||||
this.menu("Disabled", Box::new(Disabled))
|
this.menu("Disabled", Box::new(Disabled))
|
||||||
.menu("Loading", Box::new(Loading))
|
.menu("Loading", Box::new(Loading))
|
||||||
|
|
|
||||||
|
|
@ -331,6 +331,10 @@ impl Selectable for Button {
|
||||||
self.selected = selected;
|
self.selected = selected;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.selected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sizable for Button {
|
impl Sizable for Button {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use gpui::{
|
||||||
use crate::{
|
use crate::{
|
||||||
h_flex,
|
h_flex,
|
||||||
popup_menu::{PopupMenu, PopupMenuExt},
|
popup_menu::{PopupMenu, PopupMenuExt},
|
||||||
IconName, Sizable, Size,
|
IconName, Selectable, Sizable, Size,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{Button, ButtonRounded, ButtonVariant, ButtonVariants};
|
use super::{Button, ButtonRounded, ButtonVariant, ButtonVariants};
|
||||||
|
|
@ -18,6 +18,7 @@ pub struct DropdownButton {
|
||||||
button: Option<Button>,
|
button: Option<Button>,
|
||||||
popup_menu:
|
popup_menu:
|
||||||
Option<Box<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static>>,
|
Option<Box<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static>>,
|
||||||
|
selected: bool,
|
||||||
|
|
||||||
// The button props
|
// The button props
|
||||||
compact: Option<bool>,
|
compact: Option<bool>,
|
||||||
|
|
@ -34,7 +35,7 @@ impl DropdownButton {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
button: None,
|
button: None,
|
||||||
popup_menu: None,
|
popup_menu: None,
|
||||||
|
selected: false,
|
||||||
compact: None,
|
compact: None,
|
||||||
outline: None,
|
outline: None,
|
||||||
variant: None,
|
variant: None,
|
||||||
|
|
@ -70,6 +71,11 @@ impl DropdownButton {
|
||||||
self.outline = Some(true);
|
self.outline = Some(true);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn selected(mut self, selected: bool) -> Self {
|
||||||
|
self.selected = selected;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Styled for DropdownButton {
|
impl Styled for DropdownButton {
|
||||||
|
|
@ -92,6 +98,21 @@ impl ButtonVariants for DropdownButton {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Selectable for DropdownButton {
|
||||||
|
fn element_id(&self) -> &ElementId {
|
||||||
|
&self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn selected(mut self, selected: bool) -> Self {
|
||||||
|
self.selected = selected;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.selected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl RenderOnce for DropdownButton {
|
impl RenderOnce for DropdownButton {
|
||||||
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
|
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
|
||||||
self.base
|
self.base
|
||||||
|
|
@ -112,6 +133,7 @@ impl RenderOnce for DropdownButton {
|
||||||
right: true,
|
right: true,
|
||||||
bottom: true,
|
bottom: true,
|
||||||
})
|
})
|
||||||
|
.selected(self.selected)
|
||||||
.when_some(self.compact, |this, _| this.compact())
|
.when_some(self.compact, |this, _| this.compact())
|
||||||
.when_some(self.outline, |this, _| this.outline())
|
.when_some(self.outline, |this, _| this.outline())
|
||||||
.when_some(self.size, |this, size| this.with_size(size))
|
.when_some(self.size, |this, size| this.with_size(size))
|
||||||
|
|
@ -119,7 +141,7 @@ impl RenderOnce for DropdownButton {
|
||||||
)
|
)
|
||||||
.when_some(self.popup_menu, |this, popup_menu| {
|
.when_some(self.popup_menu, |this, popup_menu| {
|
||||||
this.child(
|
this.child(
|
||||||
Button::new("btn")
|
Button::new("popup")
|
||||||
.icon(IconName::ChevronDown)
|
.icon(IconName::ChevronDown)
|
||||||
.rounded(self.rounded)
|
.rounded(self.rounded)
|
||||||
.border_edges(Edges {
|
.border_edges(Edges {
|
||||||
|
|
@ -134,6 +156,7 @@ impl RenderOnce for DropdownButton {
|
||||||
bottom_left: false,
|
bottom_left: false,
|
||||||
bottom_right: true,
|
bottom_right: true,
|
||||||
})
|
})
|
||||||
|
.selected(self.selected)
|
||||||
.when_some(self.compact, |this, _| this.compact())
|
.when_some(self.compact, |this, _| this.compact())
|
||||||
.when_some(self.outline, |this, _| this.outline())
|
.when_some(self.outline, |this, _| this.outline())
|
||||||
.when_some(self.size, |this, size| this.with_size(size))
|
.when_some(self.size, |this, size| this.with_size(size))
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,10 @@ impl Selectable for Checkbox {
|
||||||
fn selected(self, selected: bool) -> Self {
|
fn selected(self, selected: bool) -> Self {
|
||||||
self.checked(selected)
|
self.checked(selected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.checked
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParentElement for Checkbox {
|
impl ParentElement for Checkbox {
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,10 @@ impl Selectable for ListItem {
|
||||||
self.selected = selected;
|
self.selected = selected;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.selected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Styled for ListItem {
|
impl Styled for ListItem {
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,8 @@ where
|
||||||
T: Selectable + IntoElement + 'static,
|
T: Selectable + IntoElement + 'static,
|
||||||
{
|
{
|
||||||
self.trigger = Some(Box::new(|is_open, _, _| {
|
self.trigger = Some(Box::new(|is_open, _, _| {
|
||||||
trigger.selected(is_open).into_any_element()
|
let selected = trigger.is_selected();
|
||||||
|
trigger.selected(selected || is_open).into_any_element()
|
||||||
}));
|
}));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,10 @@ impl Selectable for SidebarFooter {
|
||||||
fn element_id(&self) -> &gpui::ElementId {
|
fn element_id(&self) -> &gpui::ElementId {
|
||||||
&self.id
|
&self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.selected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl Collapsible for SidebarFooter {
|
impl Collapsible for SidebarFooter {
|
||||||
fn is_collapsed(&self) -> bool {
|
fn is_collapsed(&self) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,10 @@ impl Selectable for SidebarHeader {
|
||||||
fn element_id(&self) -> &gpui::ElementId {
|
fn element_id(&self) -> &gpui::ElementId {
|
||||||
&self.id
|
&self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.selected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Collapsible for SidebarHeader {
|
impl Collapsible for SidebarHeader {
|
||||||
|
|
|
||||||
|
|
@ -289,9 +289,14 @@ impl From<Pixels> for Size {
|
||||||
|
|
||||||
/// A trait for defining element that can be selected.
|
/// A trait for defining element that can be selected.
|
||||||
pub trait Selectable: Sized {
|
pub trait Selectable: Sized {
|
||||||
|
/// Returns the element id of the element.
|
||||||
fn element_id(&self) -> &ElementId;
|
fn element_id(&self) -> &ElementId;
|
||||||
|
|
||||||
/// Set the selected state of the element.
|
/// Set the selected state of the element.
|
||||||
fn selected(self, selected: bool) -> Self;
|
fn selected(self, selected: bool) -> Self;
|
||||||
|
|
||||||
|
/// Returns true if the element is selected.
|
||||||
|
fn is_selected(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A trait for defining element that can be disabled.
|
/// A trait for defining element that can be disabled.
|
||||||
|
|
|
||||||
|
|
@ -536,6 +536,10 @@ impl Selectable for Tab {
|
||||||
self.selected = selected;
|
self.selected = selected;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_selected(&self) -> bool {
|
||||||
|
self.selected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InteractiveElement for Tab {
|
impl InteractiveElement for Tab {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue