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            .child(
110                h_flex()
111                    .items_start()
112                    .gap_2()
113                    .flex_1()
114                    .child(
115                        h_flex().h(line_height).justify_center().child(
116                            Icon::new(self.icon)
117                                .color(Color::Muted)
118                                .size(IconSize::Small),
119                        ),
120                    )
121                    .child(
122                        v_flex()
123                            .flex_1()
124                            .max_w(px(300.))
125                            .child(
126                                div()
127                                    .relative()
128                                    .text_size(px(14.))
129                                    .text_color(cx.theme().colors().text)
130                                    .truncate()
131                                    .child(self.title.clone())
132                                    .child(gradient_overflow()),
133                            )
134                            .child(
135                                h_flex()
136                                    .relative()
137                                    .gap_1p5()
138                                    .text_size(px(12.))
139                                    .text_color(cx.theme().colors().text_muted)
140                                    .truncate()
141                                    .when_some(
142                                        self.project_name.clone(),
143                                        |description, project_name| {
144                                            description.child(
145                                                h_flex()
146                                                    .gap_1p5()
147                                                    .child(
148                                                        div()
149                                                            .max_w_16()
150                                                            .truncate()
151                                                            .child(project_name),
152                                                    )
153                                                    .child(
154                                                        div().size(px(3.)).rounded_full().bg(cx
155                                                            .theme()
156                                                            .colors()
157                                                            .text
158                                                            .opacity(0.5)),
159                                                    ),
160                                            )
161                                        },
162                                    )
163                                    .child(self.caption.clone())
164                                    .child(gradient_overflow()),
165                            ),
166                    ),
167            )
168            .child(
169                v_flex()
170                    .gap_1()
171                    .items_center()
172                    .child(
173                        Button::new("open", "View Panel")
174                            .style(ButtonStyle::Tinted(ui::TintColor::Accent))
175                            .full_width()
176                            .on_click({
177                                cx.listener(move |_this, _event, _, cx| {
178                                    cx.emit(AgentNotificationEvent::Accepted);
179                                })
180                            }),
181                    )
182                    .child(Button::new("dismiss", "Dismiss").full_width().on_click({
183                        cx.listener(move |_, _event, _, cx| {
184                            cx.emit(AgentNotificationEvent::Dismissed);
185                        })
186                    })),
187            )
188    }
189}