Fix to limit change when Input is disabled.

Add clean button to List query input.
Add ButtonRouneded with px support.
This commit is contained in:
Jason Lee 2024-07-17 10:47:25 +08:00
parent 702863eb83
commit 36411750ee
3 changed files with 24 additions and 5 deletions

View file

@ -6,8 +6,8 @@ use crate::{
}; };
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, px, ClickEvent, DefiniteLength, Div, ElementId, FocusHandle, div, prelude::FluentBuilder as _, px, ClickEvent, DefiniteLength, Div, ElementId, FocusHandle,
Hsla, InteractiveElement, IntoElement, MouseButton, ParentElement, RenderOnce, SharedString, Hsla, InteractiveElement, IntoElement, MouseButton, ParentElement, Pixels, RenderOnce,
StatefulInteractiveElement as _, Styled, WindowContext, SharedString, StatefulInteractiveElement as _, Styled, WindowContext,
}; };
pub enum ButtonRounded { pub enum ButtonRounded {
@ -15,6 +15,13 @@ pub enum ButtonRounded {
Small, Small,
Medium, Medium,
Large, Large,
Size(Pixels),
}
impl From<Pixels> for ButtonRounded {
fn from(px: Pixels) -> Self {
ButtonRounded::Size(px)
}
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -100,8 +107,8 @@ impl Button {
self self
} }
pub fn rounded(mut self, rounded: ButtonRounded) -> Self { pub fn rounded(mut self, rounded: impl Into<ButtonRounded>) -> Self {
self.rounded = rounded; self.rounded = rounded.into();
self self
} }
@ -180,6 +187,7 @@ impl RenderOnce for Button {
.when_some(self.height, |this, height| this.h(height)) .when_some(self.height, |this, height| this.h(height))
.map(|this| { .map(|this| {
if self.label.is_none() { if self.label.is_none() {
// Icon Button
match self.size { match self.size {
Size::Size(px) => this.size(px), Size::Size(px) => this.size(px),
Size::XSmall => this.size_5(), Size::XSmall => this.size_5(),
@ -187,6 +195,7 @@ impl RenderOnce for Button {
Size::Large | Size::Medium => this.size_8(), Size::Large | Size::Medium => this.size_8(),
} }
} else { } else {
// Normal Button
match self.size { match self.size {
Size::XSmall => this.px_1().py_1().h_5(), Size::XSmall => this.px_1().py_1().h_5(),
Size::Small => this.px_3().py_2().h_6(), Size::Small => this.px_3().py_2().h_6(),
@ -198,6 +207,7 @@ impl RenderOnce for Button {
ButtonRounded::Small => this.rounded(px(cx.theme().radius * 0.5)), ButtonRounded::Small => this.rounded(px(cx.theme().radius * 0.5)),
ButtonRounded::Medium => this.rounded(px(cx.theme().radius)), ButtonRounded::Medium => this.rounded(px(cx.theme().radius)),
ButtonRounded::Large => this.rounded(px(cx.theme().radius * 2.0)), ButtonRounded::Large => this.rounded(px(cx.theme().radius * 2.0)),
ButtonRounded::Size(px) => this.rounded(px),
ButtonRounded::None => this.rounded_none(), ButtonRounded::None => this.rounded_none(),
}) })
.when(self.selected, |this| { .when(self.selected, |this| {

View file

@ -508,6 +508,10 @@ impl ViewInputHandler for TextInput {
new_text: &str, new_text: &str,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
if self.disabled {
return;
}
let range = range_utf16 let range = range_utf16
.as_ref() .as_ref()
.map(|range_utf16| self.range_from_utf16(range_utf16)) .map(|range_utf16| self.range_from_utf16(range_utf16))
@ -531,6 +535,10 @@ impl ViewInputHandler for TextInput {
new_selected_range_utf16: Option<Range<usize>>, new_selected_range_utf16: Option<Range<usize>>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
if self.disabled {
return;
}
let range = range_utf16 let range = range_utf16
.as_ref() .as_ref()
.map(|range_utf16| self.range_from_utf16(range_utf16)) .map(|range_utf16| self.range_from_utf16(range_utf16))
@ -864,7 +872,7 @@ impl Render for TextInput {
Button::new("clean-text", cx) Button::new("clean-text", cx)
.icon(IconName::Close) .icon(IconName::Close)
.style(ButtonStyle::Ghost) .style(ButtonStyle::Ghost)
.size(px(14.)) .size(px(15.))
.cursor_pointer() .cursor_pointer()
.on_click(cx.listener(Self::clean)), .on_click(cx.listener(Self::clean)),
) )

View file

@ -81,6 +81,7 @@ where
.appearance(false) .appearance(false)
.prefix(Icon::new(IconName::Search).view(cx)) .prefix(Icon::new(IconName::Search).view(cx))
.placeholder("Search...") .placeholder("Search...")
.cleanable(true)
}); });
cx.subscribe(&query_input, Self::on_query_input_event) cx.subscribe(&query_input, Self::on_query_input_event)