agent_notification.rs

  1use gpui::{
  2    App, Context, EventEmitter, IntoElement, PlatformDisplay, Size, Window,
  3    WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind, WindowOptions,
  4    linear_color_stop, linear_gradient, point,
  5};
  6use release_channel::ReleaseChannel;
  7use std::rc::Rc;
  8use theme;
  9use ui::{Render, prelude::*};
 10
 11pub struct AgentNotification {
 12    title: SharedString,
 13    caption: SharedString,
 14    icon: IconName,
 15    project_name: Option<SharedString>,
 16}
 17
 18impl AgentNotification {
 19    pub fn new(
 20        title: impl Into<SharedString>,
 21        caption: impl Into<SharedString>,
 22        icon: IconName,
 23        project_name: Option<impl Into<SharedString>>,
 24    ) -> Self {
 25        Self {
 26            title: title.into(),
 27            caption: caption.into(),
 28            icon,
 29            project_name: project_name.map(|name| name.into()),
 30        }
 31    }
 32
 33    pub fn window_options(screen: Rc<dyn PlatformDisplay>, cx: &App) -> WindowOptions {
 34        let size = Size {
 35            width: px(450.),
 36            height: px(72.),
 37        };
 38
 39        let notification_margin_width = px(16.);
 40        let notification_margin_height = px(-48.);
 41
 42        let bounds = gpui::Bounds::<Pixels> {
 43            origin: screen.bounds().top_right()
 44                - point(
 45                    size.width + notification_margin_width,
 46                    notification_margin_height,
 47                ),
 48            size,
 49        };
 50
 51        let app_id = ReleaseChannel::global(cx).app_id();
 52
 53        WindowOptions {
 54            window_bounds: Some(WindowBounds::Windowed(bounds)),
 55            titlebar: None,
 56            focus: false,
 57            show: true,
 58            kind: WindowKind::PopUp,
 59            is_movable: false,
 60            display_id: Some(screen.id()),
 61            window_background: WindowBackgroundAppearance::Transparent,
 62            app_id: Some(app_id.to_owned()),
 63            window_min_size: None,
 64            window_decorations: Some(WindowDecorations::Client),
 65            tabbing_identifier: None,
 66            ..Default::default()
 67        }
 68    }
 69}
 70
 71pub enum AgentNotificationEvent {
 72    Accepted,
 73    Dismissed,
 74}
 75
 76impl EventEmitter<AgentNotificationEvent> for AgentNotification {}
 77
 78impl Render for AgentNotification {
 79    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 80        let ui_font = theme::setup_ui_font(window, cx);
 81        let line_height = window.line_height();
 82
 83        let bg = cx.theme().colors().elevated_surface_background;
 84        let gradient_overflow = || {
 85            div()
 86                .h_full()
 87                .absolute()
 88                .w_8()
 89                .bottom_0()
 90                .right_0()
 91                .bg(linear_gradient(
 92                    90.,
 93                    linear_color_stop(bg, 1.),
 94                    linear_color_stop(bg.opacity(0.2), 0.),
 95                ))
 96        };
 97
 98        h_flex()
 99            .id("agent-notification")
100            .size_full()
101            .p_3()
102            .gap_4()
103            .justify_between()
104            .elevation_3(cx)
105            .text_ui(cx)
106            .font(ui_font)
107            .border_color(cx.theme().colors().border)
108            .rounded_xl()
109            .on_click(cx.listener(|_, _, _, cx| {
110                cx.emit(AgentNotificationEvent::Accepted);
111            }))
112            .child(
113                h_flex()
114                    .items_start()
115                    .gap_2()
116                    .flex_1()
117                    .child(
118                        h_flex().h(line_height).justify_center().child(
119                            Icon::new(self.icon)
120                                .color(Color::Muted)
121                                .size(IconSize::Small),
122                        ),
123                    )
124                    .child(
125                        v_flex()
126                            .flex_1()
127                            .max_w(px(300.))
128                            .child(
129                                div()
130                                    .relative()
131                                    .text_size(px(14.))
132                                    .text_color(cx.theme().colors().text)
133                                    .truncate()
134                                    .child(self.title.clone())
135                                    .child(gradient_overflow()),
136                            )
137                            .child(
138                                h_flex()
139                                    .relative()
140                                    .gap_1p5()
141                                    .text_size(px(12.))
142                                    .text_color(cx.theme().colors().text_muted)
143                                    .truncate()
144                                    .when_some(
145                                        self.project_name.clone(),
146                                        |description, project_name| {
147                                            description.child(
148                                                h_flex()
149                                                    .gap_1p5()
150                                                    .child(
151                                                        div()
152                                                            .max_w_16()
153                                                            .truncate()
154                                                            .child(project_name),
155                                                    )
156                                                    .child(
157                                                        div().size(px(3.)).rounded_full().bg(cx
158                                                            .theme()
159                                                            .colors()
160                                                            .text
161                                                            .opacity(0.5)),
162                                                    ),
163                                            )
164                                        },
165                                    )
166                                    .child(self.caption.clone())
167                                    .child(gradient_overflow()),
168                            ),
169                    ),
170            )
171            .child(
172                v_flex()
173                    .gap_1()
174                    .items_center()
175                    .child(
176                        Button::new("open", "View Panel")
177                            .style(ButtonStyle::Tinted(ui::TintColor::Accent))
178                            .full_width()
179                            .on_click({
180                                cx.listener(move |_this, _event, _, cx| {
181                                    cx.emit(AgentNotificationEvent::Accepted);
182                                })
183                            }),
184                    )
185                    .child(Button::new("dismiss", "Dismiss").full_width().on_click({
186                        cx.listener(move |_, _event, _, cx| {
187                            cx.emit(AgentNotificationEvent::Dismissed);
188                        })
189                    })),
190            )
191    }
192}