chore: Update getter method to return reference to view components. (#827)

This commit is contained in:
Jason Lee 2025-04-30 17:18:44 +08:00 committed by GitHub
parent 3a915dba89
commit 38d9c24af5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 116 additions and 24 deletions

View file

@ -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"

View file

@ -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<ColorPicker>,
selected_color: Option<Hsla>,
_subscriptions: Vec<Subscription>,
}
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<impl Render + Focusable> {
Self::view(window, cx)
}
}
impl ColorPickerStory {
pub fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
cx.new(|cx| Self::new(window, cx))
}
fn new(window: &mut Window, cx: &mut Context<Self>) -> 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<Self>) -> 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())
}),
)
}
}

View file

@ -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;

View file

@ -47,6 +47,7 @@ impl Gallery {
StoryContainer::panel::<CalendarStory>(window, cx),
StoryContainer::panel::<CheckboxStory>(window, cx),
StoryContainer::panel::<ClipboardStory>(window, cx),
StoryContainer::panel::<ColorPickerStory>(window, cx),
StoryContainer::panel::<DatePickerStory>(window, cx),
StoryContainer::panel::<DropdownStory>(window, cx),
StoryContainer::panel::<DrawerStory>(window, cx),

View file

@ -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>) {
self.update_value(Some(value), false, window, cx)
}
/// Get current color value.
pub fn value(&self) -> Option<Hsla> {
self.value
}
/// Set the size of the color picker, default is `Size::Medium`.
pub fn size(mut self, size: Size) -> Self {
self.size = size;

View file

@ -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.

View file

@ -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,

View file

@ -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,

View file

@ -343,7 +343,11 @@ pub trait TableDelegate: Sized + 'static {
fn load_more(&mut self, window: &mut Window, cx: &mut Context<Table<Self>>) {}
/// Render the last empty column, default to empty.
fn render_last_empty_col(&mut self, window: &mut Window, cx: &mut Context<Table<Self>>) -> Div {
fn render_last_empty_col(
&mut self,
window: &mut Window,
cx: &mut Context<Table<Self>>,
) -> 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<Pixels>,
col_group: ColGroup,
) {
fn scroll_table_by_col_resizing(&mut self, mouse_position: Point<Pixels>, 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);
}
};
}),

View file

@ -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)]