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 = match *cx.global::<ReleaseChannel>() {
33 ReleaseChannel::Dev => "Zed Dev",
34 ReleaseChannel::Preview => "Zed Preview",
35 ReleaseChannel::Stable => "Zed",
36 };
37
38 MouseEventHandler::<ViewReleaseNotes>::new(0, cx, |state, cx| {
39 Flex::column()
40 .with_child(
41 Flex::row()
42 .with_child(
43 Text::new(
44 format!("Updated to {app_name} {}", self.version),
45 theme.message.text.clone(),
46 )
47 .contained()
48 .with_style(theme.message.container)
49 .aligned()
50 .top()
51 .left()
52 .flex(1., true)
53 .boxed(),
54 )
55 .with_child(
56 MouseEventHandler::<Cancel>::new(0, cx, |state, _| {
57 let style = theme.dismiss_button.style_for(state, false);
58 Svg::new("icons/x_mark_thin_8.svg")
59 .with_color(style.color)
60 .constrained()
61 .with_width(style.icon_width)
62 .aligned()
63 .contained()
64 .with_style(style.container)
65 .constrained()
66 .with_width(style.button_width)
67 .with_height(style.button_width)
68 .boxed()
69 })
70 .with_padding(Padding::uniform(5.))
71 .on_click(MouseButton::Left, move |_, cx| cx.dispatch_action(Cancel))
72 .aligned()
73 .constrained()
74 .with_height(cx.font_cache().line_height(theme.message.text.font_size))
75 .aligned()
76 .top()
77 .flex_float()
78 .boxed(),
79 )
80 .boxed(),
81 )
82 .with_child({
83 let style = theme.action_message.style_for(state, false);
84 Text::new("View the release notes".to_string(), style.text.clone())
85 .contained()
86 .with_style(style.container)
87 .boxed()
88 })
89 .contained()
90 .boxed()
91 })
92 .with_cursor_style(CursorStyle::PointingHand)
93 .on_click(MouseButton::Left, |_, cx| {
94 cx.dispatch_action(ViewReleaseNotes)
95 })
96 .boxed()
97 }
98}
99
100impl Notification for UpdateNotification {
101 fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
102 matches!(event, Event::Dismiss)
103 }
104}
105
106impl UpdateNotification {
107 pub fn new(version: AppVersion) -> Self {
108 Self { version }
109 }
110
111 pub fn dismiss(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
112 cx.emit(Event::Dismiss);
113 }
114}