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