BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / notifications.rs

notifications.rs

notifications.rs 7.0 KiB Download Raw
1use gpui_kit::component::{h_flex, v_flex};
2use gpui_kit::*;
3
4use super::rows::{Choice, caption, control_row, select, text_button, toggle_row};
5use crate::notify::NotificationCenter;
6use crate::notify::rules::{Corner, Settings};
7use crate::notify::sounds::AlertSound;
8use crate::theme;
9
10type Accessor = fn(&mut Settings) -> &mut bool;
11type SoundAccessor = fn(&mut Settings) -> &mut AlertSound;
12
13struct Toggle {
14 id: &'static str,
15 title: &'static str,
16 detail: &'static str,
17 field: Accessor,
18}
19
20const APPEARANCE: [Toggle; 2] = [
21 Toggle {
22 id: "settings-preview",
23 title: "Show message preview",
24 detail: "Off: only \"New message\"",
25 field: |settings| &mut settings.preview,
26 },
27 Toggle {
28 id: "settings-flash",
29 title: "Flash taskbar when notifications are received",
30 detail: "On new messages, until you open the window",
31 field: |settings| &mut settings.flash,
32 },
33];
34
35const NOTIFICATION_SOUND: Toggle = Toggle {
36 id: "settings-notification-sound",
37 title: "Play sounds with notifications",
38 detail: "",
39 field: |settings| &mut settings.sounds.notification_sound,
40};
41
42const URGENT_SOUND: Toggle = Toggle {
43 id: "settings-urgent-sound",
44 title: "Play sounds with urgent and priority contact notifications",
45 detail: "",
46 field: |settings| &mut settings.sounds.urgent_sound,
47};
48
49const CALL_SOUNDS: [Toggle; 2] = [
50 Toggle {
51 id: "settings-call-sound",
52 title: "Play sounds with incoming calls, requests to join, and meeting start notifications",
53 detail: "",
54 field: |settings| &mut settings.sounds.call_and_meeting_sound,
55 },
56 Toggle {
57 id: "settings-busy-sound",
58 title: "Play sounds when I'm busy or in a meeting",
59 detail: "",
60 field: |settings| &mut settings.sounds.sound_when_busy,
61 },
62];
63
64const MENTIONS_ONLY: Toggle = Toggle {
65 id: "settings-mentions-only",
66 title: "Mentions only",
67 detail: "Direct messages and @mentions",
68 field: |settings| &mut settings.mentions_only,
69};
70
71const HIDE_IN_CALLS: Toggle = Toggle {
72 id: "settings-hide-in-calls",
73 title: "Hide notifications during calls and meetings",
74 detail: "No message toasts while a call is active",
75 field: |settings| &mut settings.mute_in_calls,
76};
77
78pub fn render(center: &Entity<NotificationCenter>, cx: &App) -> Div {
79 let mut current = center.read(cx).settings().clone();
80 let corner = current.corner;
81 let sounds = current.sounds.clone();
82 let mut toggle = |toggle: &Toggle| {
83 let on = *(toggle.field)(&mut current);
84 toggle_element(center, toggle, on)
85 };
86 let appearance: Vec<Stateful<Div>> = APPEARANCE.iter().map(&mut toggle).collect();
87 let notification_sound = toggle(&NOTIFICATION_SOUND);
88 let urgent_sound = toggle(&URGENT_SOUND);
89 let call_sounds: Vec<Stateful<Div>> = CALL_SOUNDS.iter().map(&mut toggle).collect();
90 let mentions_only = toggle(&MENTIONS_ONLY);
91 let hide_in_calls = toggle(&HIDE_IN_CALLS);
92 let test_center = center.clone();
93 v_flex()
94 .w_full()
95 .child(caption("APPEARANCE AND SOUND"))
96 .children(appearance)
97 .child(control_row(
98 "Position on the screen",
99 "Corner of the main screen",
100 select(
101 "settings-position",
102 corner.label(),
103 false,
104 corner_choices(center, corner),
105 ),
106 ))
107 .child(control_row(
108 "Test notification",
109 "Shows a toast with the settings above",
110 text_button("settings-test-notification", "Send test notification").on_click(
111 move |_, _, cx| {
112 test_center.update(cx, |center, cx| center.send_test_notification(cx))
113 },
114 ),
115 ))
116 .child(caption("SOUND"))
117 .child(notification_sound)
118 .child(sound_row(
119 center,
120 (
121 "settings-notification-sound-type",
122 "settings-notification-sound-preview",
123 ),
124 "Notification sound",
125 &AlertSound::NORMAL,
126 sounds.notification_sound_type,
127 sounds.notification_sound,
128 |settings| &mut settings.sounds.notification_sound_type,
129 ))
130 .child(urgent_sound)
131 .child(sound_row(
132 center,
133 (
134 "settings-urgent-sound-type",
135 "settings-urgent-sound-preview",
136 ),
137 "Urgent sound",
138 &AlertSound::URGENT,
139 sounds.urgent_sound_type,
140 sounds.urgent_sound,
141 |settings| &mut settings.sounds.urgent_sound_type,
142 ))
143 .children(call_sounds)
144 .child(caption("WHAT YOU GET NOTIFIED FOR"))
145 .child(mentions_only)
146 .child(caption("MEETINGS AND CALLS"))
147 .child(hide_in_calls)
148}
149
150fn toggle_element(center: &Entity<NotificationCenter>, toggle: &Toggle, on: bool) -> Stateful<Div> {
151 let center = center.clone();
152 let field = toggle.field;
153 toggle_row(
154 toggle.id,
155 toggle.title,
156 toggle.detail,
157 theme::text_muted(),
158 on,
159 )
160 .on_click(move |_, _, cx| {
161 center.update(cx, |center, cx| {
162 center.update_settings(cx, |settings| {
163 let value = field(settings);
164 *value = !*value;
165 })
166 })
167 })
168}
169
170fn corner_choices(center: &Entity<NotificationCenter>, current: Corner) -> Vec<Choice> {
171 Corner::ALL
172 .into_iter()
173 .map(|corner| {
174 let center = center.clone();
175 Choice::new(corner.label(), corner == current, move |cx| {
176 center.update(cx, |center, cx| {
177 center.update_settings(cx, |settings| settings.corner = corner)
178 })
179 })
180 })
181 .collect()
182}
183
184fn sound_row(
185 center: &Entity<NotificationCenter>,
186 (id, preview_id): (&'static str, &'static str),
187 title: &'static str,
188 options: &'static [AlertSound],
189 current: AlertSound,
190 enabled: bool,
191 field: SoundAccessor,
192) -> Div {
193 let choices = options
194 .iter()
195 .map(|sound| {
196 let (center, sound) = (center.clone(), *sound);
197 Choice::new(sound.label(), sound == current, move |cx| {
198 center.update(cx, |center, cx| {
199 center.update_settings(cx, |settings| *field(settings) = sound);
200 center.preview_sound(sound.file_name(), cx);
201 })
202 })
203 })
204 .collect();
205 let preview_center = center.clone();
206 control_row(
207 title,
208 "",
209 h_flex()
210 .gap(px(8.))
211 .items_center()
212 .child(select(id, current.label(), !enabled, choices))
213 .child(
214 text_button(preview_id, "Preview").on_click(move |_, _, cx| {
215 preview_center.update(cx, |center, cx| {
216 center.preview_sound(current.file_name(), cx)
217 })
218 }),
219 ),
220 )
221}