From ef49753d32590942b9b4eb9b4ef567effe69e14a Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Fri, 30 Aug 2024 15:26:56 +0800 Subject: [PATCH] Improve `dropdown` disabled state (#192) --- crates/story/src/dropdown_story.rs | 2 +- crates/ui/src/dropdown.rs | 28 ++++++++++++++++++++++------ crates/ui/src/input/clear_button.rs | 3 +-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/crates/story/src/dropdown_story.rs b/crates/story/src/dropdown_story.rs index b4ef10a4..aa2b014c 100644 --- a/crates/story/src/dropdown_story.rs +++ b/crates/story/src/dropdown_story.rs @@ -138,7 +138,7 @@ impl DropdownStory { disabled_dropdown: cx.new_view(|cx| { Dropdown::new("disabled-dropdown", Vec::::new(), None, cx) .small() - .disabled() + .disabled(true) }), } }) diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index 0684be3f..3a71de53 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -12,7 +12,7 @@ use crate::{ input::ClearButton, list::{self, List, ListDelegate, ListItem}, theme::{ActiveTheme, Colorize}, - v_flex, Icon, IconName, Sizable, Size, StyleSized, StyledExt, + v_flex, Disableable, Icon, IconName, Sizable, Size, StyleSized, StyledExt, }; actions!(dropdown, [Up, Down, Enter, Escape]); @@ -386,8 +386,8 @@ where } /// Set the disable state for the dropdown. - pub fn disabled(mut self) -> Self { - self.disabled = true; + pub fn disabled(mut self, disabled: bool) -> Self { + self.disabled = disabled; self } @@ -590,7 +590,13 @@ where .border_color(cx.theme().input) .rounded(px(cx.theme().radius)) .shadow_sm() - .cursor_pointer() + .map(|this| { + if self.disabled { + this.cursor_not_allowed() + } else { + this.cursor_pointer() + } + }) .overflow_hidden() .input_text_size(self.size) .map(|this| match self.width { @@ -615,7 +621,13 @@ where .child(self.display_title(cx)), ) .when(show_clean, |this| { - this.child(ClearButton::new(cx).on_click(cx.listener(Self::clean))) + this.child(ClearButton::new(cx).map(|this| { + if self.disabled { + this.disabled(true) + } else { + this.on_click(cx.listener(Self::clean)) + } + })) }) .when(!show_clean, |this| { let icon = match self.icon.clone() { @@ -629,7 +641,11 @@ where } }; - this.child(Icon::new(icon).text_color(cx.theme().muted_foreground)) + this.child( + Icon::new(icon) + .text_color(cx.theme().muted_foreground) + .when(self.disabled, |this| this.cursor_not_allowed()), + ) }), ) .child( diff --git a/crates/ui/src/input/clear_button.rs b/crates/ui/src/input/clear_button.rs index 0908e137..64204fd6 100644 --- a/crates/ui/src/input/clear_button.rs +++ b/crates/ui/src/input/clear_button.rs @@ -1,4 +1,4 @@ -use gpui::{px, Styled as _, WindowContext}; +use gpui::{px, WindowContext}; use crate::{button::Button, IconName, Sizable as _}; @@ -10,6 +10,5 @@ impl ClearButton { .icon(IconName::CircleX) .ghost() .with_size(px(14.)) - .cursor_pointer() } }