BlueGit

ict rusty-teams

Files

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

chat_menu.rs

chat_menu.rs 13.6 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::button::{Button, ButtonVariants as _};
3use gpui_kit::component::dialog::DialogFooter;
4use gpui_kit::component::menu::{PopupMenu, PopupMenuItem};
5use gpui_kit::component::WindowExt as _;
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::i18n::tr;
12use crate::app_state::{AppState, chat_title};
13use crate::chat_actions::TITLE_LIMIT;
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.state.read(cx).is_presence_followed(&partner.user_id);
170 popup = popup.item(
171 PopupMenuItem::new(if follow {
172 NOTIFY_WHEN_AVAILABLE
173 } else {
174 TURN_OFF_NOTIFICATIONS
175 })
176 .icon(menu_icon(if follow {
177 IconName::BellRing
178 } else {
179 IconName::BellOff
180 }))
181 .on_click(move |_, _, cx| {
182 follow_state.update(cx, |state, cx| {
183 state.set_presence_followed(&partner.user_id, &partner.name, follow, cx)
184 });
185 }),
186 );
187 }
188 let (link_state, link_id) = (context.state.clone(), target.id.clone());
189 popup = popup
190 .item(
191 PopupMenuItem::new("Copy link")
192 .icon(menu_icon(IconName::Link))
193 .on_click(move |_, _, cx| {
194 cx.write_to_clipboard(ClipboardItem::new_string(chat_link(&link_id)));
195 link_state.update(cx, |state, cx| {
196 state.raise_notice("Link copied".to_owned(), None, cx)
197 });
198 }),
199 )
200 .item(pop_out_item(context, target))
201 .separator()
202 .item(hide_item);
203 if !target.is_group {
204 let remove_state = context.state.clone();
205 let remove_id = target.id.clone();
206 popup = popup.item(
207 PopupMenuItem::new(REMOVE_HISTORY_LABEL)
208 .icon(menu_icon(IconName::Trash))
209 .on_click(move |_, window, cx| {
210 confirm_remove_history(&remove_state, &remove_id, window, cx);
211 }),
212 );
213 }
214 if target.is_group {
215 let leave_state = context.state.clone();
216 let leave_id = target.id.clone();
217 let title = target.title.clone();
218 let member_count = target.member_count;
219 popup = popup.item(
220 PopupMenuItem::element(|_, _| {
221 div().text_color(theme::red_soft()).child("Leave chat...")
222 })
223 .icon(menu_icon(IconName::LogOut).text_color(theme::red_soft()))
224 .on_click(move |_, window, cx| {
225 confirm_leave(&leave_state, &leave_id, &title, member_count, window, cx);
226 }),
227 );
228 }
229 popup
230}
231
232fn pop_out_item(context: &MenuContext, target: &ChatMenuTarget) -> PopupMenuItem {
233 let (state, chat_id) = (context.state.clone(), target.id.clone());
234 PopupMenuItem::new(chat_pop_out::POP_OUT_CHAT_LABEL)
235 .icon(menu_icon(IconName::ExternalLink))
236 .on_click(move |_, _, cx| chat_pop_out::pop_out_chat(&state, &chat_id, cx))
237}
238
239const CHAT_NOTIFICATION_MODES: [(&str, ChatNotificationMode); 2] = [
240 ("All new messages", ChatNotificationMode::AllNewMessages),
241 ("Mute", ChatNotificationMode::Mute),
242];
243
244const DESKTOP_NOTIFICATIONS: [(&str, DesktopNotifications); 2] = [
245 (
246 "Match settings used for all chats",
247 DesktopNotifications::MatchAllChats,
248 ),
249 ("Don't get desktop notifications", DesktopNotifications::Off),
250];
251
252fn chat_notifications_submenu(
253 popup: PopupMenu,
254 window: &mut Window,
255 cx: &mut Context<PopupMenu>,
256 context: &MenuContext,
257 target: &ChatMenuTarget,
258) -> PopupMenu {
259 let state = context.state.clone();
260 let chat_id = target.id.clone();
261 let mode = if target.muted {
262 ChatNotificationMode::Mute
263 } else {
264 ChatNotificationMode::AllNewMessages
265 };
266 let desktop = state.read(cx).chat_desktop_notifications(&chat_id);
267 popup.submenu_with_icon(
268 Some(menu_icon(IconName::Bell)),
269 "Manage notifications",
270 window,
271 cx,
272 move |submenu, _, _| {
273 let choose = |chosen_mode: ChatNotificationMode,
274 chosen_desktop: DesktopNotifications| {
275 let (state, chat_id) = (state.clone(), chat_id.clone());
276 move |_: &ClickEvent, _: &mut Window, cx: &mut App| {
277 state.update(cx, |state, cx| {
278 state.set_chat_notifications(&chat_id, chosen_mode, chosen_desktop, cx)
279 });
280 }
281 };
282 let submenu = submenu.label("Chat notification mode");
283 let submenu =
284 CHAT_NOTIFICATION_MODES
285 .iter()
286 .fold(submenu, |submenu, (label, value)| {
287 submenu.item(
288 PopupMenuItem::new(*label)
289 .checked(*value == mode)
290 .on_click(choose(*value, desktop)),
291 )
292 });
293 let submenu = submenu
294 .separator()
295 .label("Set a desktop notification preference");
296 DESKTOP_NOTIFICATIONS
297 .iter()
298 .fold(submenu, |submenu, (label, value)| {
299 submenu.item(
300 PopupMenuItem::new(*label)
301 .checked(*value == desktop)
302 .on_click(choose(mode, *value)),
303 )
304 })
305 },
306 )
307}
308
309pub(crate) fn leave_description(member_count: usize) -> String {
310 let who = match member_count {
311 0 | 1 => "The other members".to_owned(),
312 2 => "The other member".to_owned(),
313 count => format!("The {} other members", count - 1),
314 };
315 let verb = if member_count == 2 { "sees" } else { "see" };
316 format!("{who} {verb} that you left. You can only come back if someone adds you.")
317}
318
319const REMOVE_HISTORY_LABEL: &str = "Remove chat history";
320const REMOVE_HISTORY_TITLE: &str = "Remove chat history?";
321const REMOVE_HISTORY_TEXT: &str =
322 "This will remove the chat from your list and search. It affects you only.";
323
324fn confirm_remove_history(
325 state: &Entity<AppState>,
326 chat_id: &str,
327 window: &mut Window,
328 cx: &mut App,
329) {
330 let state = state.clone();
331 let chat_id = chat_id.to_owned();
332 window.open_alert_dialog(cx, move |alert, _, _| {
333 let state = state.clone();
334 let chat_id = chat_id.clone();
335 let cancel = Button::new("remove-history-cancel")
336 .label(tr("common.cancel"))
337 .on_click(|_, window, cx| window.close_dialog(cx));
338 let remove = Button::new("remove-history-confirm")
339 .label(tr("common.remove"))
340 .primary()
341 .on_click(move |_, window, cx| {
342 state.update(cx, |state, cx| state.remove_chat_history(&chat_id, cx));
343 window.close_dialog(cx);
344 });
345 alert
346 .title(REMOVE_HISTORY_TITLE)
347 .description(REMOVE_HISTORY_TEXT)
348 .footer(DialogFooter::new().child(cancel).child(remove))
349 .on_ok(|_, _, _| false)
350 });
351}
352
353pub(crate) fn confirm_leave(
354 state: &Entity<AppState>,
355 chat_id: &str,
356 title: &str,
357 member_count: usize,
358 window: &mut Window,
359 cx: &mut App,
360) {
361 let state = state.clone();
362 let chat_id = chat_id.to_owned();
363 let heading = format!("Leave \"{}\"?", truncated(title, TITLE_LIMIT));
364 let description = leave_description(member_count);
365 window.open_alert_dialog(cx, move |alert, _, _| {
366 let state = state.clone();
367 let chat_id = chat_id.clone();
368 let cancel = Button::new("leave-cancel")
369 .label(tr("common.cancel"))
370 .on_click(|_, window, cx| window.close_dialog(cx));
371 let leave = Button::new("leave-confirm")
372 .label(tr("common.leave"))
373 .danger()
374 .on_click(move |_, window, cx| {
375 state.update(cx, |state, cx| state.leave_chat(&chat_id, cx));
376 window.close_dialog(cx);
377 });
378 alert
379 .title(heading.clone())
380 .description(description.clone())
381 .footer(DialogFooter::new().child(cancel).child(leave))
382 .on_ok(|_, _, _| false)
383 });
384}
385
386#[cfg(test)]
387mod tests {
388 use super::leave_description;
389
390 #[test]
391 fn leave_text_counts_the_other_members() {
392 assert_eq!(
393 leave_description(4),
394 "The 3 other members see that you left. You can only come back if someone adds you."
395 );
396 assert!(leave_description(2).starts_with("The other member sees"));
397 assert!(leave_description(0).starts_with("The other members see"));
398 }
399}