update_notification.rs

  1use crate::ViewReleaseNotes;
  2use gpui::{
  3    elements::{Flex, MouseEventHandler, Padding, ParentElement, Svg, Text},
  4    platform::{AppVersion, CursorStyle, MouseButton},
  5    Drawable, Entity, View, ViewContext,
  6};
  7use menu::Cancel;
  8use settings::Settings;
  9use util::channel::ReleaseChannel;
 10use workspace::notifications::Notification;
 11
 12pub struct UpdateNotification {
 13    version: AppVersion,
 14}
 15
 16pub enum Event {
 17    Dismiss,
 18}
 19
 20impl Entity for UpdateNotification {
 21    type Event = Event;
 22}
 23
 24impl View for UpdateNotification {
 25    fn ui_name() -> &'static str {
 26        "UpdateNotification"
 27    }
 28
 29    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> gpui::Element<Self> {
 30        let theme = cx.global::<Settings>().theme.clone();
 31        let theme = &theme.update_notification;
 32
 33        let app_name = cx.global::<ReleaseChannel>().display_name();
 34
 35        MouseEventHandler::<ViewReleaseNotes, _>::new(0, cx, |state, cx| {
 36            Flex::column()
 37                .with_child(
 38                    Flex::row()
 39                        .with_child(
 40                            Text::new(
 41                                format!("Updated to {app_name} {}", self.version),
 42                                theme.message.text.clone(),
 43                            )
 44                            .contained()
 45                            .with_style(theme.message.container)
 46                            .aligned()
 47                            .top()
 48                            .left()
 49                            .flex(1., true)
 50                            .boxed(),
 51                        )
 52                        .with_child(
 53                            MouseEventHandler::<Cancel, _>::new(0, cx, |state, _| {
 54                                let style = theme.dismiss_button.style_for(state, false);
 55                                Svg::new("icons/x_mark_8.svg")
 56                                    .with_color(style.color)
 57                                    .constrained()
 58                                    .with_width(style.icon_width)
 59                                    .aligned()
 60                                    .contained()
 61                                    .with_style(style.container)
 62                                    .constrained()
 63                                    .with_width(style.button_width)
 64                                    .with_height(style.button_width)
 65                                    .boxed()
 66                            })
 67                            .with_padding(Padding::uniform(5.))
 68                            .on_click(MouseButton::Left, move |_, _, cx| {
 69                                cx.dispatch_action(Cancel)
 70                            })
 71                            .aligned()
 72                            .constrained()
 73                            .with_height(cx.font_cache().line_height(theme.message.text.font_size))
 74                            .aligned()
 75                            .top()
 76                            .flex_float()
 77                            .boxed(),
 78                        )
 79                        .boxed(),
 80                )
 81                .with_child({
 82                    let style = theme.action_message.style_for(state, false);
 83                    Text::new("View the release notes", style.text.clone())
 84                        .contained()
 85                        .with_style(style.container)
 86                        .boxed()
 87                })
 88                .contained()
 89                .boxed()
 90        })
 91        .with_cursor_style(CursorStyle::PointingHand)
 92        .on_click(MouseButton::Left, |_, _, cx| {
 93            cx.dispatch_action(ViewReleaseNotes)
 94        })
 95        .boxed()
 96    }
 97}
 98
 99impl Notification for UpdateNotification {
100    fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
101        matches!(event, Event::Dismiss)
102    }
103}
104
105impl UpdateNotification {
106    pub fn new(version: AppVersion) -> Self {
107        Self { version }
108    }
109
110    pub fn dismiss(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
111        cx.emit(Event::Dismiss);
112    }
113}