BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / chat_menu.rs

chat_menu.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
chat_menu.rs 13.6 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::WindowExt as _;
3use gpui_kit::component::button::{Button, ButtonVariants as _};
4use gpui_kit::component::dialog::DialogFooter;
5use gpui_kit::component::menu::{PopupMenu, PopupMenuItem};
6use gpui_kit::*;
7use teams_core::{ChatNotificationMode, DesktopNotifications, chat_link, is_notes_chat};
8
9use super::chat_pop_out;
10use super::widgets::menu_icon;
11use crate::app_state::{AppState, chat_title};
12use crate::chat_actions::TITLE_LIMIT;
13use crate::notice::truncated;
14use crate::presence_follow::{NOTIFY_WHEN_AVAILABLE, TURN_OFF_NOTIFICATIONS};
15use crate::theme;
16
17const GROUP_KIND: &str = "group";
18
19#[derive(Clone)]
20pub(crate) struct ChatMenuTarget {
21 pub id: String,
22 pub title: String,
23 pub folder_id: Option<String>,
24 pub unread: bool,
25 pub muted: bool,
26 pub is_group: bool,
27 pub member_count: usize,
28}
29
30impl ChatMenuTarget {
31 pub(crate) fn for_chat(state: &AppState, chat_id: &str) -> Option<Self> {
32 let chat = state.sidebar.chats.iter().find(|chat| chat.id == chat_id)?;
33 Some(ChatMenuTarget {
34 id: chat.id.clone(),
35 title: chat_title(chat),
36 folder_id: state
37 .directory
38 .folder_of(chat_id)
39 .map(|folder| folder.id.clone()),
40 unread: chat.unread,
41 muted: chat.muted,
42 is_group: chat.kind == GROUP_KIND,
43 member_count: chat.members.len(),
44 })
45 }
46}
47
48#[derive(Clone)]
49pub(crate) struct MenuContext {
50 pub state: Entity<AppState>,
51 pub folders: Vec<(String, String)>,
52}
53
54impl MenuContext {
55 pub(crate) fn new(state: &Entity<AppState>, cx: &App) -> Self {
56 MenuContext {
57 state: state.clone(),
58 folders: state.read(cx).directory.move_targets(),
59 }
60 }
61}
62
63pub(crate) fn chat_menu(
64 popup: PopupMenu,
65 window: &mut Window,
66 cx: &mut Context<PopupMenu>,
67 context: &MenuContext,
68 target: &ChatMenuTarget,
69) -> PopupMenu {
70 let mut popup = popup;
71 let read_state = context.state.clone();
72 let read_id = target.id.clone();
73 let unread = target.unread;
74 let mute_state = context.state.clone();
75 let mute_id = target.id.clone();
76 let muted = target.muted;
77 let hide_state = context.state.clone();
78 let hide_id = target.id.clone();
79 let notes = is_notes_chat(&target.id);
80 popup = popup.item(
81 PopupMenuItem::new(if unread {
82 "Mark as read"
83 } else {
84 "Mark as unread"
85 })
86 .icon(menu_icon(if unread {
87 IconName::MailOpen
88 } else {
89 IconName::Mail
90 }))
91 .on_click(move |_, _, cx| {
92 read_state.update(cx, |state, cx| {
93 if unread {
94 state.mark_chat_read(&read_id, cx);
95 } else {
96 state.mark_chat_unread(&read_id, cx);
97 }
98 });
99 }),
100 );
101 let current = target.folder_id.clone();
102 let folders: Vec<(String, String)> = context
103 .folders
104 .iter()
105 .filter(|(folder_id, _)| current.as_ref() != Some(folder_id))
106 .cloned()
107 .collect();
108 if !folders.is_empty() {
109 let move_state = context.state.clone();
110 let move_id = target.id.clone();
111 popup = popup.submenu_with_icon(
112 Some(menu_icon(IconName::Folder)),
113 "Move to section",
114 window,
115 cx,
116 move |submenu, _, _| {
117 folders.iter().fold(submenu, |submenu, (folder_id, name)| {
118 let (state, chat_id, folder_id) =
119 (move_state.clone(), move_id.clone(), folder_id.clone());
120 submenu.item(PopupMenuItem::new(name.clone()).on_click(move |_, _, cx| {
121 state.update(cx, |state, cx| {
122 state.move_chat(&chat_id, Some(&folder_id), cx)
123 });
124 }))
125 })
126 },
127 );
128 }
129 if current.is_some() {
130 let remove_state = context.state.clone();
131 let remove_id = target.id.clone();
132 popup = popup.item(
133 PopupMenuItem::new("Remove from section")
134 .icon(menu_icon(IconName::FolderMinus))
135 .on_click(move |_, _, cx| {
136 remove_state.update(cx, |state, cx| state.move_chat(&remove_id, None, cx));
137 }),
138 );
139 }
140 let hide_item = PopupMenuItem::new("Hide")
141 .icon(menu_icon(IconName::EyeOff))
142 .on_click(move |_, _, cx| {
143 hide_state.update(cx, |state, cx| state.hide_chat(&hide_id, cx));
144 });
145 if notes {
146 return popup
147 .item(pop_out_item(context, target))
148 .separator()
149 .item(hide_item);
150 }
151 popup = popup.item(
152 PopupMenuItem::new(if muted { "Unmute" } else { "Mute" })
153 .icon(menu_icon(if muted {
154 IconName::Bell
155 } else {
156 IconName::BellOff
157 }))
158 .on_click(move |_, _, cx| {
159 mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));
160 }),
161 );
162 popup = chat_notifications_submenu(popup, window, cx, context, target);
163 let partner = (!target.is_group)
164 .then(|| context.state.read(cx).one_on_one_partner(&target.id))
165 .flatten();
166 if let Some(partner) = partner {
167 let follow_state = context.state.clone();
168 let follow = !context
169 .state
170 .read(cx)
171 .is_presence_followed(&partner.user_id);
172 popup = popup.item(
173 PopupMenuItem::new(if follow {
174 NOTIFY_WHEN_AVAILABLE
175 } else {
176 TURN_OFF_NOTIFICATIONS
177 })
178 .icon(menu_icon(if follow {
179 IconName::BellRing
180 } else {
181 IconName::BellOff
182 }))
183 .on_click(move |_, _, cx| {
184 follow_state.update(cx, |state, cx| {
185 state.set_presence_followed(&partner.user_id, &partner.name, follow, cx)
186 });
187 }),
188 );
189 }
190 let (link_state, link_id) = (context.state.clone(), target.id.clone());
191 popup = popup
192 .item(
193 PopupMenuItem::new("Copy link")
194 .icon(menu_icon(IconName::Link))
195 .on_click(move |_, _, cx| {
196 cx.write_to_clipboard(ClipboardItem::new_string(chat_link(&link_id)));
197 link_state.update(cx, |state, cx| {
198 state.raise_notice("Link copied".to_owned(), None, cx)
199 });
200 }),
201 )
202 .item(pop_out_item(context, target))
203 .separator()
204 .item(hide_item);
205 if !target.is_group {
206 let remove_state = context.state.clone();
207 let remove_id = target.id.clone();
208 popup = popup.item(
209 PopupMenuItem::new(REMOVE_HISTORY_LABEL)
210 .icon(menu_icon(IconName::Trash))
211 .on_click(move |_, window, cx| {
212 confirm_remove_history(&remove_state, &remove_id, window, cx);
213 }),
214 );
215 }
216 if target.is_group {
217 let leave_state = context.state.clone();
218 let leave_id = target.id.clone();
219 let title = target.title.clone();
220 let member_count = target.member_count;
221 popup = popup.item(
222 PopupMenuItem::element(|_, _| {
223 div().text_color(theme::red_soft()).child("Leave chat...")
224 })
225 .icon(menu_icon(IconName::LogOut).text_color(theme::red_soft()))
226 .on_click(move |_, window, cx| {
227 confirm_leave(&leave_state, &leave_id, &title, member_count, window, cx);
228 }),
229 );
230 }
231 popup
232}
233
234fn pop_out_item(context: &MenuContext, target: &ChatMenuTarget) -> PopupMenuItem {
235 let (state, chat_id) = (context.state.clone(), target.id.clone());
236 PopupMenuItem::new(chat_pop_out::POP_OUT_CHAT_LABEL)
237 .icon(menu_icon(IconName::ExternalLink))
238 .on_click(move |_, _, cx| chat_pop_out::pop_out_chat(&state, &chat_id, cx))
239}
240
241const CHAT_NOTIFICATION_MODES: [(&str, ChatNotificationMode); 2] = [
242 ("All new messages", ChatNotificationMode::AllNewMessages),
243 ("Mute", ChatNotificationMode::Mute),
244];
245
246const DESKTOP_NOTIFICATIONS: [(&str, DesktopNotifications); 2] = [
247 (
248 "Match settings used for all chats",
249 DesktopNotifications::MatchAllChats,
250 ),
251 ("Don't get desktop notifications", DesktopNotifications::Off),
252];
253
254fn chat_notifications_submenu(
255 popup: PopupMenu,
256 window: &mut Window,
257 cx: &mut Context<PopupMenu>,
258 context: &MenuContext,
259 target: &ChatMenuTarget,
260) -> PopupMenu {
261 let state = context.state.clone();
262 let chat_id = target.id.clone();
263 let mode = if target.muted {
264 ChatNotificationMode::Mute
265 } else {
266 ChatNotificationMode::AllNewMessages
267 };
268 let desktop = state.read(cx).chat_desktop_notifications(&chat_id);
269 popup.submenu_with_icon(
270 Some(menu_icon(IconName::Bell)),
271 "Manage notifications",
272 window,
273 cx,
274 move |submenu, _, _| {
275 let choose = |chosen_mode: ChatNotificationMode,
276 chosen_desktop: DesktopNotifications| {
277 let (state, chat_id) = (state.clone(), chat_id.clone());
278 move |_: &ClickEvent, _: &mut Window, cx: &mut App| {
279 state.update(cx, |state, cx| {
280 state.set_chat_notifications(&chat_id, chosen_mode, chosen_desktop, cx)
281 });
282 }
283 };
284 let submenu = submenu.label("Chat notification mode");
285 let submenu =
286 CHAT_NOTIFICATION_MODES
287 .iter()
288 .fold(submenu, |submenu, (label, value)| {
289 submenu.item(
290 PopupMenuItem::new(*label)
291 .checked(*value == mode)
292 .on_click(choose(*value, desktop)),
293 )
294 });
295 let submenu = submenu
296 .separator()
297 .label("Set a desktop notification preference");
298 DESKTOP_NOTIFICATIONS
299 .iter()
300 .fold(submenu, |submenu, (label, value)| {
301 submenu.item(
302 PopupMenuItem::new(*label)
303 .checked(*value == desktop)
304 .on_click(choose(mode, *value)),
305 )
306 })
307 },
308 )
309}
310
311pub(crate) fn leave_description(member_count: usize) -> String {
312 let who = match member_count {
313 0 | 1 => "The other members".to_owned(),
314 2 => "The other member".to_owned(),
315 count => format!("The {} other members", count - 1),
316 };
317 let verb = if member_count == 2 { "sees" } else { "see" };
318 format!("{who} {verb} that you left. You can only come back if someone adds you.")
319}
320
321const REMOVE_HISTORY_LABEL: &str = "Remove chat history";
322const REMOVE_HISTORY_TITLE: &str = "Remove chat history?";
323const REMOVE_HISTORY_TEXT: &str =
324 "This will remove the chat from your list and search. It affects you only.";
325
326fn confirm_remove_history(
327 state: &Entity<AppState>,
328 chat_id: &str,
329 window: &mut Window,
330 cx: &mut App,
331) {
332 let state = state.clone();
333 let chat_id = chat_id.to_owned();
334 window.open_alert_dialog(cx, move |alert, _, _| {
335 let state = state.clone();
336 let chat_id = chat_id.clone();
337 let cancel = Button::new("remove-history-cancel")
338 .label("Cancel")
339 .on_click(|_, window, cx| window.close_dialog(cx));
340 let remove = Button::new("remove-history-confirm")
341 .label("Remove")
342 .primary()
343 .on_click(move |_, window, cx| {
344 state.update(cx, |state, cx| state.remove_chat_history(&chat_id, cx));
345 window.close_dialog(cx);
346 });
347 alert
348 .title(REMOVE_HISTORY_TITLE)
349 .description(REMOVE_HISTORY_TEXT)
350 .footer(DialogFooter::new().child(cancel).child(remove))
351 .on_ok(|_, _, _| false)
352 });
353}
354
355pub(crate) fn confirm_leave(
356 state: &Entity<AppState>,
357 chat_id: &str,
358 title: &str,
359 member_count: usize,
360 window: &mut Window,
361 cx: &mut App,
362) {
363 let state = state.clone();
364 let chat_id = chat_id.to_owned();
365 let heading = format!("Leave \"{}\"?", truncated(title, TITLE_LIMIT));
366 let description = leave_description(member_count);
367 window.open_alert_dialog(cx, move |alert, _, _| {
368 let state = state.clone();
369 let chat_id = chat_id.clone();
370 let cancel = Button::new("leave-cancel")
371 .label("Cancel")
372 .on_click(|_, window, cx| window.close_dialog(cx));
373 let leave = Button::new("leave-confirm")
374 .label("Leave")
375 .danger()
376 .on_click(move |_, window, cx| {
377 state.update(cx, |state, cx| state.leave_chat(&chat_id, cx));
378 window.close_dialog(cx);
379 });
380 alert
381 .title(heading.clone())
382 .description(description.clone())
383 .footer(DialogFooter::new().child(cancel).child(leave))
384 .on_ok(|_, _, _| false)
385 });
386}
387
388#[cfg(test)]
389mod tests {
390 use super::leave_description;
391
392 #[test]
393 fn leave_text_counts_the_other_members() {
394 assert_eq!(
395 leave_description(4),
396 "The 3 other members see that you left. You can only come back if someone adds you."
397 );
398 assert!(leave_description(2).starts_with("The other member sees"));
399 assert!(leave_description(0).starts_with("The other members see"));
400 }
401}