BlueGit

ict rusty-teams

Files

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

chat_menu.rs

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