From 155f2ee3b9669ccb37258d54a566be7bd7332636 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 4 Feb 2025 17:34:26 +0800 Subject: [PATCH] chore: Refactor ClearButton to as method. (#598) --- crates/ui/src/dropdown.rs | 6 +++--- crates/ui/src/input/clear_button.rs | 17 +++++++---------- crates/ui/src/input/input.rs | 5 +++-- crates/ui/src/time/date_picker.rs | 6 ++---- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index d63be262..c10b9030 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -9,9 +9,9 @@ use rust_i18n::t; use crate::{ h_flex, - input::ClearButton, + input::clear_button, list::{self, List, ListDelegate, ListItem}, - v_flex, ActiveTheme, Disableable, Icon, IconName, Sizable, Size, StyleSized, StyledExt, + v_flex, ActiveTheme, Disableable as _, Icon, IconName, Sizable, Size, StyleSized, StyledExt, }; actions!(dropdown, [Up, Down, Enter, Escape]); @@ -674,7 +674,7 @@ where .child(self.display_title(window, cx)), ) .when(show_clean, |this| { - this.child(ClearButton::new(window, cx).map(|this| { + this.child(clear_button(cx).map(|this| { if self.disabled { this.disabled(true) } else { diff --git a/crates/ui/src/input/clear_button.rs b/crates/ui/src/input/clear_button.rs index 98137238..e772bdac 100644 --- a/crates/ui/src/input/clear_button.rs +++ b/crates/ui/src/input/clear_button.rs @@ -1,17 +1,14 @@ -use gpui::{App, Styled, Window}; +use gpui::{App, Styled}; use crate::{ button::{Button, ButtonVariants as _}, ActiveTheme as _, Icon, IconName, Sizable as _, }; -pub(crate) struct ClearButton {} - -impl ClearButton { - pub fn new(_: &mut Window, cx: &mut App) -> Button { - Button::new("clean") - .icon(Icon::new(IconName::CircleX).text_color(cx.theme().muted_foreground)) - .ghost() - .xsmall() - } +pub(crate) fn clear_button(cx: &App) -> Button { + Button::new("clean") + .icon(Icon::new(IconName::CircleX)) + .ghost() + .xsmall() + .text_color(cx.theme().muted_foreground) } diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index 18a56616..3f908d13 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -24,10 +24,11 @@ use gpui::{ use super::blink_cursor::BlinkCursor; use super::change::Change; use super::element::TextElement; -use super::{number_input, ClearButton}; +use super::number_input; use crate::history::History; use crate::indicator::Indicator; +use crate::input::clear_button; use crate::scroll::{Scrollbar, ScrollbarAxis, ScrollbarState}; use crate::ActiveTheme; use crate::Size; @@ -1640,7 +1641,7 @@ impl Render for TextInput { }) .when( self.cleanable && !self.loading && !self.text.is_empty() && self.is_single_line(), - |this| this.child(ClearButton::new(window, cx).on_click(cx.listener(Self::clean))), + |this| this.child(clear_button(cx).on_click(cx.listener(Self::clean))), ) .children(suffix) .when(self.is_multi_line(), |this| { diff --git a/crates/ui/src/time/date_picker.rs b/crates/ui/src/time/date_picker.rs index 2be1fa7f..a9c3276d 100644 --- a/crates/ui/src/time/date_picker.rs +++ b/crates/ui/src/time/date_picker.rs @@ -11,7 +11,7 @@ use crate::{ button::{Button, ButtonVariants as _}, dropdown::Escape, h_flex, - input::ClearButton, + input::clear_button, v_flex, ActiveTheme, Icon, IconName, Sizable, Size, StyleSized as _, StyledExt as _, }; @@ -326,9 +326,7 @@ impl Render for DatePicker { .gap_1() .child(div().w_full().overflow_hidden().child(display_title)) .when(show_clean, |this| { - this.child( - ClearButton::new(window, cx).on_click(cx.listener(Self::clean)), - ) + this.child(clear_button(cx).on_click(cx.listener(Self::clean))) }) .when(!show_clean, |this| { this.child(