From 814ea0e824773c42037c1a1b510485e214d03e63 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 25 Mar 2025 15:04:28 +0800 Subject: [PATCH] alert: Add to support styled. (#736) --- README.md | 58 +++++++++++++-------------- crates/ui/src/alert.rs | 90 +++++++++++++++++++++++------------------- 2 files changed, 79 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 769f830f..1eaf7b39 100644 --- a/README.md +++ b/README.md @@ -7,38 +7,38 @@ UI components for building fantastic desktop application by using [GPUI](https:/ ## Features +- Accordion +- Alert +- Badge +- Breadcrumb +- Button, Link +- Checkbox, Radio, Switch +- ColorPicker +- DatePicker, DateRangePicker, Calendar +- Dock, Tiles +- Drawer +- Dropdown +- Icon +- Label +- List +- Menu +- Modal +- Notification +- Popover +- Progress & Indicator +- Resizable +- Sidebar +- Skeleton +- Slider +- Table +- Tabs +- TextInput, TextArea, OtpInput +- TextView (Markdown, Simple HTML) to native rendering, syntax highlighting. - Theming - TitleBar -- Dock, Tiles -- TextInput, TextArea, OtpInput -- Button, Link -- Label -- Icon -- Checkbox, Radio, Switch -- Dropdown -- Tabs -- Notification -- Tooltip -- Popover -- Resizable -- Progress & Indicator -- Slider -- Skeleton -- DatePicker, DateRangePicker, Calendar -- ColorPicker -- List -- Table -- Menu -- Drawer -- Modal -- WebView -- Accordion -- Sidebar -- Breadcrumb -- Badge -- TextView (Markdown, Simple HTML) to native rendering, syntax highlighting. - Toggle, ToggleGroup -- Alert +- Tooltip +- WebView ## Showcase diff --git a/crates/ui/src/alert.rs b/crates/ui/src/alert.rs index c6121e81..4007e348 100644 --- a/crates/ui/src/alert.rs +++ b/crates/ui/src/alert.rs @@ -1,6 +1,6 @@ use gpui::{ - div, prelude::FluentBuilder as _, px, relative, App, Hsla, IntoElement, ParentElement as _, - RenderOnce, SharedString, Styled, Window, + div, prelude::FluentBuilder as _, px, relative, App, Div, Hsla, IntoElement, + ParentElement as _, RenderOnce, SharedString, Styled, Window, }; use crate::{h_flex, text::Text, ActiveTheme as _, Icon, IconName, Sizable, Size, StyledExt}; @@ -37,6 +37,7 @@ impl AlertVariant { /// Alert used to display a message to the user. #[derive(IntoElement)] pub struct Alert { + base: Div, variant: AlertVariant, icon: Option, title: Option, @@ -48,6 +49,7 @@ impl Alert { /// Create a new alert with the given message. fn new(message: impl Into) -> Self { Self { + base: div(), variant: AlertVariant::default(), icon: None, title: None, @@ -110,6 +112,12 @@ impl Sizable for Alert { } } +impl Styled for Alert { + fn style(&mut self) -> &mut gpui::StyleRefinement { + self.base.style() + } +} + impl RenderOnce for Alert { fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { let (radius, padding_x, padding_y, gap, line_height, icon_mt) = match self.size { @@ -135,44 +143,46 @@ impl RenderOnce for Alert { let color = self.variant.color(cx); - h_flex() - .rounded(radius) - .border_1() - .border_color(color) - .bg(color.opacity(0.05)) - .text_color(self.variant.fg(cx)) - .px(padding_x) - .py(padding_y) - .gap(gap) - .overflow_hidden() - .items_start() - .map(|this| match self.size { - Size::Large => this.text_base(), - _ => this.text_sm(), - }) - .line_height(relative(line_height)) - .child( - div().mt(icon_mt).child( - self.icon - .unwrap_or(IconName::Info.into()) - .with_size(self.size) - .flex_shrink_0(), + self.base.child( + h_flex() + .rounded(radius) + .border_1() + .border_color(color) + .bg(color.opacity(0.05)) + .text_color(self.variant.fg(cx)) + .px(padding_x) + .py(padding_y) + .gap(gap) + .overflow_hidden() + .items_start() + .map(|this| match self.size { + Size::Large => this.text_base(), + _ => this.text_sm(), + }) + .line_height(relative(line_height)) + .child( + div().mt(icon_mt).child( + self.icon + .unwrap_or(IconName::Info.into()) + .with_size(self.size) + .flex_shrink_0(), + ), + ) + .child( + div() + .overflow_hidden() + .when_some(self.title, |this, title| { + this.child( + div() + .w_full() + .truncate() + .mb_1() + .font_semibold() + .child(title), + ) + }) + .child(div().overflow_hidden().child(self.message)), ), - ) - .child( - div() - .overflow_hidden() - .when_some(self.title, |this, title| { - this.child( - div() - .w_full() - .truncate() - .mb_1() - .font_semibold() - .child(title), - ) - }) - .child(div().overflow_hidden().child(self.message)), - ) + ) } }