agent_notification.rs

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