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