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 util::channel::ReleaseChannel;
9use workspace::notifications::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::ViewContext<Self>) -> gpui::AnyElement<Self> {
29 let theme = theme::current(cx).clone();
30 let theme = &theme.update_notification;
31
32 let app_name = cx.global::<ReleaseChannel>().display_name();
33
34 MouseEventHandler::new::<ViewReleaseNotes, _>(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 )
50 .with_child(
51 MouseEventHandler::new::<Cancel, _>(0, cx, |state, _| {
52 let style = theme.dismiss_button.style_for(state);
53 Svg::new("icons/x.svg")
54 .with_color(style.color)
55 .constrained()
56 .with_width(style.icon_width)
57 .aligned()
58 .contained()
59 .with_style(style.container)
60 .constrained()
61 .with_width(style.button_width)
62 .with_height(style.button_width)
63 })
64 .with_padding(Padding::uniform(5.))
65 .on_click(MouseButton::Left, move |_, this, cx| {
66 this.dismiss(&Default::default(), cx)
67 })
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 ),
75 )
76 .with_child({
77 let style = theme.action_message.style_for(state);
78 Text::new("View the release notes", style.text.clone())
79 .contained()
80 .with_style(style.container)
81 })
82 .contained()
83 })
84 .with_cursor_style(CursorStyle::PointingHand)
85 .on_click(MouseButton::Left, |_, _, cx| {
86 crate::view_release_notes(&Default::default(), cx)
87 })
88 .into_any_named("update notification")
89 }
90}
91
92impl Notification for UpdateNotification {
93 fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
94 matches!(event, Event::Dismiss)
95 }
96}
97
98impl UpdateNotification {
99 pub fn new(version: AppVersion) -> Self {
100 Self { version }
101 }
102
103 pub fn dismiss(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
104 cx.emit(Event::Dismiss);
105 }
106}