1use crate::ViewReleaseNotes;
2use gpui::{
3 elements::{Flex, MouseEventHandler, Padding, ParentElement, Svg, Text},
4 platform::{AppVersion, CursorStyle, MouseButton},
5 Element, 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::AnyElement<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 )
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 })
65 .with_padding(Padding::uniform(5.))
66 .on_click(MouseButton::Left, move |_, this, cx| {
67 this.dismiss(&Default::default(), cx)
68 })
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 ),
76 )
77 .with_child({
78 let style = theme.action_message.style_for(state, false);
79 Text::new("View the release notes", style.text.clone())
80 .contained()
81 .with_style(style.container)
82 })
83 .contained()
84 })
85 .with_cursor_style(CursorStyle::PointingHand)
86 .on_click(MouseButton::Left, |_, _, cx| {
87 crate::view_release_notes(&Default::default(), cx)
88 })
89 .into_any_named("update notification")
90 }
91}
92
93impl Notification for UpdateNotification {
94 fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
95 matches!(event, Event::Dismiss)
96 }
97}
98
99impl UpdateNotification {
100 pub fn new(version: AppVersion) -> Self {
101 Self { version }
102 }
103
104 pub fn dismiss(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
105 cx.emit(Event::Dismiss);
106 }
107}