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::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::RenderContext<'_, Self>) -> gpui::ElementBox {
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| cx.dispatch_action(Cancel))
69 .aligned()
70 .constrained()
71 .with_height(cx.font_cache().line_height(theme.message.text.font_size))
72 .aligned()
73 .top()
74 .flex_float()
75 .boxed(),
76 )
77 .boxed(),
78 )
79 .with_child({
80 let style = theme.action_message.style_for(state, false);
81 Text::new("View the release notes".to_string(), style.text.clone())
82 .contained()
83 .with_style(style.container)
84 .boxed()
85 })
86 .contained()
87 .boxed()
88 })
89 .with_cursor_style(CursorStyle::PointingHand)
90 .on_click(MouseButton::Left, |_, cx| {
91 cx.dispatch_action(ViewReleaseNotes)
92 })
93 .boxed()
94 }
95}
96
97impl Notification for UpdateNotification {
98 fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
99 matches!(event, Event::Dismiss)
100 }
101}
102
103impl UpdateNotification {
104 pub fn new(version: AppVersion) -> Self {
105 Self { version }
106 }
107
108 pub fn dismiss(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
109 cx.emit(Event::Dismiss);
110 }
111}