update_notification.rs

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