From 38d9c24af5f9850f707a301cdb2ab1cf0035f117 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 30 Apr 2025 17:18:44 +0800 Subject: [PATCH] chore: Update getter method to return reference to view components. (#827) --- Cargo.toml | 9 ++- crates/story/src/color_picker_story.rs | 78 ++++++++++++++++++++++++++ crates/story/src/lib.rs | 2 + crates/story/src/main.rs | 1 + crates/ui/src/color_picker.rs | 11 ++++ crates/ui/src/input/otp_input.rs | 4 +- crates/ui/src/slider.rs | 8 +-- crates/ui/src/switch.rs | 1 + crates/ui/src/table.rs | 24 ++++---- crates/ui/src/tag.rs | 2 +- 10 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 crates/story/src/color_picker_story.rs diff --git a/Cargo.toml b/Cargo.toml index dd380650..236b6431 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,10 @@ log = "0.4" serde = "1.0.203" serde_json = "1" smallvec = "1" -windows = { version = "0.58.0", features = [ - "Wdk", - "Wdk_System", - "Wdk_System_SystemServices", -] } + +[workspace.dependencies.windows] +version = "0.58.0" +features = ["Wdk", "Wdk_System", "Wdk_System_SystemServices"] [workspace.lints.clippy] almost_complete_range = "allow" diff --git a/crates/story/src/color_picker_story.rs b/crates/story/src/color_picker_story.rs new file mode 100644 index 00000000..94424d68 --- /dev/null +++ b/crates/story/src/color_picker_story.rs @@ -0,0 +1,78 @@ +use gpui::{ + prelude::FluentBuilder as _, App, AppContext, Context, Entity, Focusable, Hsla, IntoElement, + ParentElement as _, Render, Styled as _, Subscription, Window, +}; +use gpui_component::{ + blue_500, + color_picker::{ColorPicker, ColorPickerEvent}, + green_500, red_500, v_flex, yellow_500, Colorize, +}; + +use crate::section; + +pub struct ColorPickerStory { + color_picker: Entity, + selected_color: Option, + + _subscriptions: Vec, +} + +impl super::Story for ColorPickerStory { + fn title() -> &'static str { + "ColorPicker" + } + + fn description() -> &'static str { + "A color picker to select color." + } + + fn new_view(window: &mut Window, cx: &mut App) -> Entity { + Self::view(window, cx) + } +} + +impl ColorPickerStory { + pub fn view(window: &mut Window, cx: &mut App) -> Entity { + cx.new(|cx| Self::new(window, cx)) + } + + fn new(window: &mut Window, cx: &mut Context) -> Self { + let color_picker = cx.new(|cx| { + ColorPicker::new("1", window, cx) + .default_value(red_500()) + .featured_colors(vec![red_500(), blue_500(), green_500(), yellow_500()]) + }); + + let _subscriptions = vec![cx.subscribe(&color_picker, |this, _, ev, _| match ev { + ColorPickerEvent::Change(color) => { + this.selected_color = *color; + println!("Color changed to: {:?}", color); + } + })]; + + Self { + color_picker, + selected_color: Some(red_500()), + _subscriptions, + } + } +} + +impl Focusable for ColorPickerStory { + fn focus_handle(&self, cx: &gpui::App) -> gpui::FocusHandle { + self.color_picker.focus_handle(cx) + } +} + +impl Render for ColorPickerStory { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + v_flex().gap_3().child( + section("Normal") + .max_w_md() + .child(self.color_picker.clone()) + .when_some(self.selected_color, |this, color| { + this.child(color.to_hex()) + }), + ) + } +} diff --git a/crates/story/src/lib.rs b/crates/story/src/lib.rs index 5e02e1f0..027e1e8e 100644 --- a/crates/story/src/lib.rs +++ b/crates/story/src/lib.rs @@ -6,6 +6,7 @@ mod button_story; mod calendar_story; mod checkbox_story; mod clipboard_story; +mod color_picker_story; mod date_picker_story; mod drawer_story; mod dropdown_story; @@ -55,6 +56,7 @@ pub use button_story::ButtonStory; pub use calendar_story::CalendarStory; pub use checkbox_story::CheckboxStory; pub use clipboard_story::ClipboardStory; +pub use color_picker_story::ColorPickerStory; pub use date_picker_story::DatePickerStory; pub use drawer_story::DrawerStory; pub use dropdown_story::DropdownStory; diff --git a/crates/story/src/main.rs b/crates/story/src/main.rs index 8eb8fce7..2611e19c 100644 --- a/crates/story/src/main.rs +++ b/crates/story/src/main.rs @@ -47,6 +47,7 @@ impl Gallery { StoryContainer::panel::(window, cx), StoryContainer::panel::(window, cx), StoryContainer::panel::(window, cx), + StoryContainer::panel::(window, cx), StoryContainer::panel::(window, cx), StoryContainer::panel::(window, cx), StoryContainer::panel::(window, cx), diff --git a/crates/ui/src/color_picker.rs b/crates/ui/src/color_picker.rs index b8f1f084..dc9b43dd 100644 --- a/crates/ui/src/color_picker.rs +++ b/crates/ui/src/color_picker.rs @@ -134,11 +134,22 @@ impl ColorPicker { self } + /// Set default color value. + pub fn default_value(mut self, value: Hsla) -> Self { + self.value = Some(value); + self + } + /// Set current color value. pub fn set_value(&mut self, value: Hsla, window: &mut Window, cx: &mut Context) { self.update_value(Some(value), false, window, cx) } + /// Get current color value. + pub fn value(&self) -> Option { + self.value + } + /// Set the size of the color picker, default is `Size::Medium`. pub fn size(mut self, size: Size) -> Self { self.size = size; diff --git a/crates/ui/src/input/otp_input.rs b/crates/ui/src/input/otp_input.rs index ee8ab93c..04d7f558 100644 --- a/crates/ui/src/input/otp_input.rs +++ b/crates/ui/src/input/otp_input.rs @@ -91,8 +91,8 @@ impl OtpInput { } /// Return the value of the OTP Input. - pub fn value(&self) -> SharedString { - self.value.clone() + pub fn value(&self) -> &SharedString { + &self.value } /// Set masked to true use masked input. diff --git a/crates/ui/src/slider.rs b/crates/ui/src/slider.rs index e9cd1fac..810157b2 100644 --- a/crates/ui/src/slider.rs +++ b/crates/ui/src/slider.rs @@ -95,15 +95,15 @@ impl Slider { cx.notify(); } - fn update_thumb_pos(&mut self) { - self.percentage = self.value.clamp(self.min, self.max) / self.max; - } - /// Get the value of the slider. pub fn value(&self) -> f32 { self.value } + fn update_thumb_pos(&mut self) { + self.percentage = self.value.clamp(self.min, self.max) / self.max; + } + /// Update value by mouse position fn update_value_by_position( &mut self, diff --git a/crates/ui/src/switch.rs b/crates/ui/src/switch.rs index 6db980c5..48fadea8 100644 --- a/crates/ui/src/switch.rs +++ b/crates/ui/src/switch.rs @@ -8,6 +8,7 @@ use gpui::{ }; use std::{cell::RefCell, rc::Rc, time::Duration}; +/// A Switch element that can be toggled on or off. pub struct Switch { id: ElementId, base: Div, diff --git a/crates/ui/src/table.rs b/crates/ui/src/table.rs index ab4ce55f..05a6ede2 100644 --- a/crates/ui/src/table.rs +++ b/crates/ui/src/table.rs @@ -343,7 +343,11 @@ pub trait TableDelegate: Sized + 'static { fn load_more(&mut self, window: &mut Window, cx: &mut Context>) {} /// Render the last empty column, default to empty. - fn render_last_empty_col(&mut self, window: &mut Window, cx: &mut Context>) -> Div { + fn render_last_empty_col( + &mut self, + window: &mut Window, + cx: &mut Context>, + ) -> impl IntoElement { h_flex().w_3().h_full().flex_shrink_0() } @@ -650,11 +654,7 @@ where } /// Scroll table when mouse position is near the edge of the table bounds. - fn scroll_table_by_col_resizing( - &mut self, - mouse_position: Point, - col_group: ColGroup, - ) { + fn scroll_table_by_col_resizing(&mut self, mouse_position: Point, col_group: ColGroup) { // Do nothing if pos out of the table bounds right for avoid scroll to the right. if mouse_position.x > self.bounds.right() { return; @@ -663,9 +663,12 @@ where let mut offset = self.horizontal_scroll_handle.offset(); let col_bounds = col_group.bounds; - if mouse_position.x < self.bounds.left() && col_bounds.right() < self.bounds.left() + px(20.) { + if mouse_position.x < self.bounds.left() + && col_bounds.right() < self.bounds.left() + px(20.) + { offset.x += px(1.); - } else if mouse_position.x > self.bounds.right() && col_bounds.right() > self.bounds.right() - px(20.) + } else if mouse_position.x > self.bounds.right() + && col_bounds.right() > self.bounds.right() - px(20.) { offset.x -= px(1.); } @@ -973,10 +976,7 @@ where ); // scroll the table if the drag is near the edge - view.scroll_table_by_col_resizing( - e.event.position, - col_group, - ); + view.scroll_table_by_col_resizing(e.event.position, col_group); } }; }), diff --git a/crates/ui/src/tag.rs b/crates/ui/src/tag.rs index b543ffd0..7db115ee 100644 --- a/crates/ui/src/tag.rs +++ b/crates/ui/src/tag.rs @@ -72,7 +72,7 @@ impl TagVariant { } } -/// Tag is a small status indicator for UI elements. +/// Tag is a small status indicator. /// /// Only support: Medium, Small #[derive(IntoElement)]