BlueGit

ict rusty-teams

Files

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

chat_menu.rs

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