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            ..Default::default()
 66        }
 67    }
 68}
 69
 70pub enum AgentNotificationEvent {
 71    Accepted,
 72    Dismissed,
 73}
 74
 75impl EventEmitter<AgentNotificationEvent> for AgentNotification {}
 76
 77impl Render for AgentNotification {
 78    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 79        let ui_font = theme::setup_ui_font(window, cx);
 80        let line_height = window.line_height();
 81
 82        let bg = cx.theme().colors().elevated_surface_background;
 83        let gradient_overflow = || {
 84            div()
 85                .h_full()
 86                .absolute()
 87                .w_8()
 88                .bottom_0()
 89                .right_0()
 90                .bg(linear_gradient(
 91                    90.,
 92                    linear_color_stop(bg, 1.),
 93                    linear_color_stop(bg.opacity(0.2), 0.),
 94                ))
 95        };
 96
 97        h_flex()
 98            .id("agent-notification")
 99            .size_full()
100            .p_3()
101            .gap_4()
102            .justify_between()
103            .elevation_3(cx)
104            .text_ui(cx)
105            .font(ui_font)
106            .border_color(cx.theme().colors().border)
107            .rounded_xl()
108            .on_click(cx.listener(|_, _, _, cx| {
109                cx.emit(AgentNotificationEvent::Accepted);
110            }))
111            .child(
112                h_flex()
113                    .items_start()
114                    .gap_2()
115                    .flex_1()
116                    .child(
117                        h_flex().h(line_height).justify_center().child(
118                            Icon::new(self.icon)
119                                .color(Color::Muted)
120                                .size(IconSize::Small),
121                        ),
122                    )
123                    .child(
124                        v_flex()
125                            .flex_1()
126                            .max_w(px(300.))
127                            .child(
128                                div()
129                                    .relative()
130                                    .text_size(px(14.))
131                                    .text_color(cx.theme().colors().text)
132                                    .truncate()
133                                    .child(self.title.clone())
134                                    .child(gradient_overflow()),
135                            )
136                            .child(
137                                h_flex()
138                                    .relative()
139                                    .gap_1p5()
140                                    .text_size(px(12.))
141                                    .text_color(cx.theme().colors().text_muted)
142                                    .truncate()
143                                    .when_some(
144                                        self.project_name.clone(),
145                                        |description, project_name| {
146                                            description.child(
147                                                h_flex()
148                                                    .gap_1p5()
149                                                    .child(
150                                                        div()
151                                                            .max_w_16()
152                                                            .truncate()
153                                                            .child(project_name),
154                                                    )
155                                                    .child(
156                                                        div().size(px(3.)).rounded_full().bg(cx
157                                                            .theme()
158                                                            .colors()
159                                                            .text
160                                                            .opacity(0.5)),
161                                                    ),
162                                            )
163                                        },
164                                    )
165                                    .child(self.caption.clone())
166                                    .child(gradient_overflow()),
167                            ),
168                    ),
169            )
170            .child(
171                v_flex()
172                    .gap_1()
173                    .items_center()
174                    .child(
175                        Button::new("open", "View Panel")
176                            .style(ButtonStyle::Tinted(ui::TintColor::Accent))
177                            .full_width()
178                            .on_click({
179                                cx.listener(move |_this, _event, _, cx| {
180                                    cx.emit(AgentNotificationEvent::Accepted);
181                                })
182                            }),
183                    )
184                    .child(Button::new("dismiss", "Dismiss").full_width().on_click({
185                        cx.listener(move |_, _event, _, cx| {
186                            cx.emit(AgentNotificationEvent::Dismissed);
187                        })
188                    })),
189            )
190    }
191}