BlueGit

ict rusty-teams

Files

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

sidebar.rs

sidebar.rs 52.5 KiB Download Raw
1use std::collections::HashSet;
2
3use chrono::{Local, Offset, Utc};
4use gpui_kit::assets::IconName;
5use gpui_kit::component::button::{Button, ButtonVariants as _};
6use gpui_kit::component::dialog::DialogFooter;
7use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenu, PopupMenuItem};
8use gpui_kit::component::{WindowExt as _, h_flex, tooltip::Tooltip, v_flex};
9use gpui_kit::prelude::FluentBuilder as _;
10use gpui_kit::*;
11use store::{
12 ChannelNotificationLevel, ChannelNotifications, ChannelRecord, SidebarTeam, Store, TeamRecord,
13};
14
15use super::avatar::{spec_avatar, square_avatar, with_presence};
16use super::calls_tab::calls_body;
17use super::widgets::{count_badge, dot, icon, unread_marker};
18use crate::app_state::{AppEvent, AppState, Selection};
19use crate::chat_actions::TITLE_LIMIT;
20use crate::data::{Directory, FolderKind};
21use crate::format;
22use crate::local_previews::LocalPreview;
23use crate::notice::truncated;
24use crate::sidebar_model::{
25 AvatarSpec, ChannelOrderInput, ChatItem, DELETED_PREVIEW, EMPTY_FOLDER_HINT, Preview, Section,
26 SectionInput, SectionKind, Step, any_unread_channel, build_sections, step_id, unread_chat_count,
27 visible_channel_ids,
28};
29use crate::theme;
30
31const COLLAPSED_TEAMS_META_KEY: &str = "sidebar_collapsed_teams";
32pub const SIDEBAR_WIDTH: f32 = 312.;
33const ROW_HEIGHT: f32 = 54.;
34const PINNED_CHANNEL_HEIGHT: f32 = 48.;
35const SECTION_HEIGHT: f32 = 30.;
36const SECTION_GAP: f32 = 4.;
37const HINT_HEIGHT: f32 = 26.;
38const SEE_MORE_HEIGHT: f32 = 28.;
39const FALLBACK_VIEWPORT: f32 = 900.;
40const VIEWPORT_MARGIN: f32 = 300.;
41const AVATAR_SIZE: f32 = 36.;
42const PINNED_LABEL: &str = "Pinned";
43const TEAMS_LABEL: &str = "Teams";
44const HIDDEN_TEAMS_LABEL: &str = "Hidden teams";
45const TEAM_ROW_HEIGHT: f32 = 40.;
46const REVEAL_ROW_HEIGHT: f32 = 32.;
47const NEW_CHAT_BUTTON_SIZE: f32 = 34.;
48const MENU_ICON_SIZE: f32 = 15.;
49const MUTED_ICON_SIZE: f32 = 13.;
50const NOTIFICATION_ICON_SIZE: f32 = 13.;
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53enum SidebarTab {
54 Chats,
55 Channels,
56 Calls,
57}
58
59#[derive(Clone)]
60struct DraggedChat {
61 id: String,
62 title: String,
63}
64
65struct DragGhost {
66 title: String,
67}
68
69impl Render for DragGhost {
70 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
71 div()
72 .px(px(12.))
73 .py(px(6.))
74 .rounded(px(8.))
75 .bg(theme::surface_raised())
76 .border_1()
77 .border_color(theme::accent())
78 .text_size(px(13.))
79 .text_color(theme::text())
80 .child(self.title.clone())
81 }
82}
83
84struct ChatMenuTarget {
85 id: String,
86 title: String,
87 pinned: bool,
88 unread: bool,
89 muted: bool,
90 is_group: bool,
91 member_count: usize,
92}
93
94#[derive(Clone)]
95struct MenuContext {
96 state: Entity<AppState>,
97 folders: Vec<(String, String)>,
98 favorites_id: Option<String>,
99}
100
101fn menu_icon(name: IconName) -> gpui_kit::component::Icon {
102 icon(name, MENU_ICON_SIZE, theme::text_muted())
103}
104
105fn chat_menu(
106 popup: PopupMenu,
107 window: &mut Window,
108 cx: &mut Context<PopupMenu>,
109 context: &MenuContext,
110 target: &ChatMenuTarget,
111) -> PopupMenu {
112 let pinned = target.pinned;
113 let pin_state = context.state.clone();
114 let pin_id = target.id.clone();
115 let mut popup = popup.item(
116 PopupMenuItem::new(if pinned { "Unpin" } else { "Pin" })
117 .icon(menu_icon(if pinned {
118 IconName::PinOff
119 } else {
120 IconName::Pin
121 }))
122 .on_click(move |_, _, cx| {
123 pin_state.update(cx, |state, cx| {
124 if pinned {
125 state.move_chat(&pin_id, None, cx);
126 } else {
127 state.pin_chat(&pin_id, cx);
128 }
129 });
130 }),
131 );
132 if !context.folders.is_empty() {
133 let folders = context.folders.clone();
134 let move_state = context.state.clone();
135 let move_id = target.id.clone();
136 popup = popup.submenu_with_icon(
137 Some(menu_icon(IconName::Folder)),
138 "Move to folder",
139 window,
140 cx,
141 move |submenu, _, _| {
142 folders.iter().fold(submenu, |submenu, (folder_id, name)| {
143 let (state, chat_id, folder_id) =
144 (move_state.clone(), move_id.clone(), folder_id.clone());
145 submenu.item(PopupMenuItem::new(name.clone()).on_click(move |_, _, cx| {
146 state.update(cx, |state, cx| {
147 state.move_chat(&chat_id, Some(&folder_id), cx)
148 });
149 }))
150 })
151 },
152 );
153 }
154 let read_state = context.state.clone();
155 let read_id = target.id.clone();
156 let unread = target.unread;
157 let mute_state = context.state.clone();
158 let mute_id = target.id.clone();
159 let muted = target.muted;
160 let hide_state = context.state.clone();
161 let hide_id = target.id.clone();
162 popup = popup
163 .separator()
164 .item(
165 PopupMenuItem::new(if unread {
166 "Mark as read"
167 } else {
168 "Mark as unread"
169 })
170 .icon(menu_icon(if unread {
171 IconName::MailOpen
172 } else {
173 IconName::Mail
174 }))
175 .on_click(move |_, _, cx| {
176 read_state.update(cx, |state, cx| {
177 if unread {
178 state.mark_chat_read(&read_id, cx);
179 } else {
180 state.mark_chat_unread(&read_id, cx);
181 }
182 });
183 }),
184 )
185 .item(
186 PopupMenuItem::new(if muted { "Unmute" } else { "Mute" })
187 .icon(menu_icon(if muted {
188 IconName::Bell
189 } else {
190 IconName::BellOff
191 }))
192 .on_click(move |_, _, cx| {
193 mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));
194 }),
195 )
196 .separator()
197 .item(
198 PopupMenuItem::new("Hide")
199 .icon(menu_icon(IconName::EyeOff))
200 .on_click(move |_, _, cx| {
201 hide_state.update(cx, |state, cx| state.hide_chat(&hide_id, cx));
202 }),
203 );
204 if target.is_group {
205 let leave_state = context.state.clone();
206 let leave_id = target.id.clone();
207 let title = target.title.clone();
208 let member_count = target.member_count;
209 popup = popup.item(
210 PopupMenuItem::element(|_, _| {
211 div().text_color(theme::red_soft()).child("Leave chat...")
212 })
213 .icon(icon(IconName::LogOut, MENU_ICON_SIZE, theme::red_soft()))
214 .on_click(move |_, window, cx| {
215 confirm_leave(&leave_state, &leave_id, &title, member_count, window, cx);
216 }),
217 );
218 }
219 popup
220}
221
222fn leave_description(member_count: usize) -> String {
223 let who = match member_count {
224 0 | 1 => "The other members".to_owned(),
225 2 => "The other member".to_owned(),
226 count => format!("The {} other members", count - 1),
227 };
228 let verb = if member_count == 2 { "sees" } else { "see" };
229 format!("{who} {verb} that you left. You can only come back if someone adds you.")
230}
231
232fn confirm_leave(
233 state: &Entity<AppState>,
234 chat_id: &str,
235 title: &str,
236 member_count: usize,
237 window: &mut Window,
238 cx: &mut App,
239) {
240 let state = state.clone();
241 let chat_id = chat_id.to_owned();
242 let heading = format!("Leave \"{}\"?", truncated(title, TITLE_LIMIT));
243 let description = leave_description(member_count);
244 window.open_alert_dialog(cx, move |alert, _, _| {
245 let state = state.clone();
246 let chat_id = chat_id.clone();
247 let cancel = Button::new("leave-cancel")
248 .label("Cancel")
249 .on_click(|_, window, cx| window.close_dialog(cx));
250 let leave = Button::new("leave-confirm")
251 .label("Leave")
252 .danger()
253 .on_click(move |_, window, cx| {
254 state.update(cx, |state, cx| state.leave_chat(&chat_id, cx));
255 window.close_dialog(cx);
256 });
257 alert
258 .title(heading.clone())
259 .description(description.clone())
260 .footer(DialogFooter::new().child(cancel).child(leave))
261 .on_ok(|_, _, _| false)
262 });
263}
264
265pub struct SidebarView {
266 state: Entity<AppState>,
267 tab: SidebarTab,
268 collapsed_teams: HashSet<String>,
269 revealed_channel_teams: HashSet<String>,
270 hidden_teams_open: bool,
271 chats_scroll: ScrollHandle,
272 channels_scroll: ScrollHandle,
273 calls_scroll: ScrollHandle,
274 _subscription: Subscription,
275 _state_observation: Subscription,
276}
277
278fn preview_text(preview: &Preview) -> Option<(String, bool)> {
279 match preview {
280 Preview::Empty => None,
281 Preview::Deleted => Some((DELETED_PREVIEW.to_owned(), true)),
282 Preview::Typing(text) => Some((text.clone(), true)),
283 Preview::Text {
284 prefix: Some(prefix),
285 text,
286 } => Some((format!("{prefix}: {text}"), false)),
287 Preview::Text { prefix: None, text } => Some((text.clone(), false)),
288 }
289}
290
291fn preview_icon(text: &str) -> Option<IconName> {
292 match text.rsplit(": ").next()? {
293 "Image" => Some(IconName::Image),
294 "File" => Some(IconName::File),
295 _ => None,
296 }
297}
298
299fn local_preview_row(preview: &LocalPreview, color: Hsla) -> Div {
300 let row = h_flex()
301 .flex_1()
302 .min_w_0()
303 .overflow_hidden()
304 .gap(px(4.))
305 .items_center()
306 .text_size(px(12.5))
307 .text_color(color);
308 match preview {
309 LocalPreview::Draft(text) => row
310 .child(
311 div()
312 .flex_none()
313 .text_color(theme::accent_text())
314 .child("Draft:"),
315 )
316 .child(div().truncate().child(text.clone())),
317 LocalPreview::NotSent => row.child(
318 div()
319 .flex_none()
320 .text_color(theme::red_soft())
321 .child("Not sent"),
322 ),
323 }
324}
325
326impl SidebarView {
327 pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {
328 let subscription = cx.subscribe(&state, |this, state, event: &AppEvent, cx| {
329 if matches!(event, AppEvent::Selection) {
330 if state.read(cx).new_chat {
331 this.tab = SidebarTab::Chats;
332 }
333 this.expand_team_of_selection(cx);
334 }
335 if matches!(
336 event,
337 AppEvent::Sidebar
338 | AppEvent::Selection
339 | AppEvent::Directory
340 | AppEvent::Typing
341 | AppEvent::LocalPreviews
342 ) {
343 cx.notify();
344 }
345 });
346 let state_observation = cx.observe(&state, |_, _, cx| cx.notify());
347 let tab = if state.read(cx).start_on_channels {
348 SidebarTab::Channels
349 } else {
350 SidebarTab::Chats
351 };
352 let collapsed_teams = load_collapsed_teams(&state.read(cx).store);
353 SidebarView {
354 state,
355 tab,
356 collapsed_teams,
357 revealed_channel_teams: HashSet::new(),
358 hidden_teams_open: false,
359 chats_scroll: ScrollHandle::new(),
360 channels_scroll: ScrollHandle::new(),
361 calls_scroll: ScrollHandle::new(),
362 _subscription: subscription,
363 _state_observation: state_observation,
364 }
365 }
366
367 pub fn show_chats(&mut self, cx: &mut Context<Self>) {
368 self.tab = SidebarTab::Chats;
369 cx.notify();
370 }
371
372 pub fn show_channels(&mut self, cx: &mut Context<Self>) {
373 self.tab = SidebarTab::Channels;
374 cx.notify();
375 }
376
377 pub fn show_calls(&mut self, cx: &mut Context<Self>) {
378 self.open_tab(SidebarTab::Calls, cx);
379 }
380
381 fn open_tab(&mut self, tab: SidebarTab, cx: &mut Context<Self>) {
382 self.tab = tab;
383 if tab == SidebarTab::Calls {
384 self.state.update(cx, |state, cx| state.refresh_call_history(cx));
385 }
386 cx.notify();
387 }
388
389 pub fn select_adjacent(&mut self, step: Step, cx: &mut Context<Self>) {
390 let selection = match self.tab {
391 SidebarTab::Chats => self
392 .state
393 .read(cx)
394 .adjacent_chat_id(step)
395 .map(Selection::Chat),
396 SidebarTab::Channels => self.adjacent_channel_id(step, cx).map(Selection::Channel),
397 SidebarTab::Calls => None,
398 };
399 if let Some(selection) = selection {
400 self.state.update(cx, |state, cx| state.select(selection, cx));
401 }
402 }
403
404 fn adjacent_channel_id(&self, step: Step, cx: &mut Context<Self>) -> Option<String> {
405 let state = self.state.read(cx);
406 let selected_channel = match &state.selection {
407 Some(Selection::Channel(channel_id)) => Some(channel_id.as_str()),
408 _ => None,
409 };
410 let input = ChannelOrderInput {
411 teams: &state.sidebar.teams,
412 pinned_ids: &state.directory.pinned_channels,
413 collapsed_teams: &self.collapsed_teams,
414 revealed_channel_teams: &self.revealed_channel_teams,
415 hidden_teams_open: self.hidden_teams_open,
416 selected_channel,
417 };
418 step_id(&visible_channel_ids(&input), selected_channel, step)
419 }
420
421 fn expand_team_of_selection(&mut self, cx: &mut Context<Self>) {
422 let state = self.state.read(cx);
423 let Some(Selection::Channel(channel_id)) = state.selection.clone() else {
424 return;
425 };
426 let team_id = state
427 .sidebar
428 .teams
429 .iter()
430 .find(|entry| {
431 entry
432 .channels
433 .iter()
434 .any(|channel| channel.id == channel_id)
435 })
436 .map(|entry| entry.team.id.clone());
437 if let Some(team_id) = team_id
438 && self.collapsed_teams.remove(&team_id)
439 {
440 save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams);
441 }
442 }
443
444 fn tab_button(
445 &self,
446 tab: SidebarTab,
447 label: &str,
448 marker: Option<AnyElement>,
449 cx: &mut Context<Self>,
450 ) -> impl IntoElement {
451 let active = self.tab == tab;
452 h_flex()
453 .id(SharedString::from(format!("tab-{label}")))
454 .flex_1()
455 .h(px(28.))
456 .rounded(px(6.))
457 .items_center()
458 .justify_center()
459 .gap(px(6.))
460 .cursor_pointer()
461 .text_size(px(13.))
462 .when(active, |tab| {
463 tab.bg(theme::surface_raised())
464 .font_weight(FontWeight::SEMIBOLD)
465 .text_color(theme::text())
466 })
467 .when(!active, |tab| {
468 tab.text_color(theme::text_muted())
469 .hover(|tab| tab.text_color(theme::text()))
470 })
471 .child(label.to_owned())
472 .children(marker)
473 .on_click(cx.listener(move |this, _, _, cx| this.open_tab(tab, cx)))
474 }
475
476 fn tab_bar(
477 &self,
478 unread_chats: u32,
479 channel_dot: bool,
480 cx: &mut Context<Self>,
481 ) -> impl IntoElement {
482 let chat_marker =
483 (unread_chats > 0).then(|| count_badge(unread_chats, false).into_any_element());
484 let channel_marker = channel_dot.then(|| dot(7.).into_any_element());
485 h_flex()
486 .w_full()
487 .px(px(12.))
488 .pt(px(12.))
489 .pb(px(8.))
490 .gap(px(8.))
491 .child(
492 h_flex()
493 .flex_1()
494 .min_w_0()
495 .gap(px(4.))
496 .p(px(3.))
497 .rounded(px(8.))
498 .bg(theme::background())
499 .border_1()
500 .border_color(theme::border())
501 .child(self.tab_button(SidebarTab::Chats, "Chats", chat_marker, cx))
502 .child(self.tab_button(SidebarTab::Channels, "Channels", channel_marker, cx))
503 .child(self.tab_button(SidebarTab::Calls, "Calls", None, cx)),
504 )
505 .child(self.new_chat_button(cx))
506 }
507
508 fn new_chat_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
509 let active = self.state.read(cx).new_chat;
510 div()
511 .id("new-chat")
512 .size(px(NEW_CHAT_BUTTON_SIZE))
513 .flex_none()
514 .flex()
515 .items_center()
516 .justify_center()
517 .rounded(px(8.))
518 .border_1()
519 .border_color(theme::border())
520 .cursor_pointer()
521 .when(active, |button| button.bg(theme::surface_raised()))
522 .hover(|button| button.bg(theme::row_hover()))
523 .tooltip(|window, cx| Tooltip::new("New chat (Ctrl+N)").build(window, cx))
524 .child(icon(IconName::Pencil, 16., theme::text_soft()))
525 .on_click(cx.listener(|this, _, _, cx| {
526 this.state.update(cx, |state, cx| state.start_new_chat(cx));
527 }))
528 }
529
530 fn draft_row(&self) -> impl IntoElement {
531 h_flex()
532 .id("new-chat-draft")
533 .mx(px(8.))
534 .h(px(ROW_HEIGHT))
535 .px(px(8.))
536 .gap(px(10.))
537 .items_center()
538 .rounded(px(8.))
539 .bg(theme::surface_raised())
540 .child(
541 div()
542 .size(px(AVATAR_SIZE))
543 .flex_none()
544 .flex()
545 .items_center()
546 .justify_center()
547 .rounded_full()
548 .bg(theme::border_strong())
549 .child(icon(IconName::Pencil, 16., theme::text())),
550 )
551 .child(
552 v_flex()
553 .flex_1()
554 .min_w_0()
555 .gap(px(2.))
556 .child(
557 div()
558 .truncate()
559 .text_size(px(14.))
560 .text_color(theme::text())
561 .child("New chat"),
562 )
563 .child(
564 div()
565 .truncate()
566 .text_size(px(12.5))
567 .text_color(theme::text_muted())
568 .child("Draft"),
569 ),
570 )
571 }
572
573 fn section_header(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
574 let drop_target = matches!(section.kind, SectionKind::Favorites | SectionKind::Folder)
575 .then(|| section.id.clone());
576 let toggle_id = section.id.clone();
577 h_flex()
578 .id(SharedString::from(format!("section-{}", section.id)))
579 .h(px(SECTION_HEIGHT))
580 .mt(px(SECTION_GAP))
581 .mx(px(4.))
582 .pl(px(8.))
583 .pr(px(10.))
584 .gap(px(6.))
585 .items_center()
586 .rounded(px(6.))
587 .cursor_pointer()
588 .hover(|header| header.bg(theme::row_hover()))
589 .drag_over::<DraggedChat>(|style, _, _, _| {
590 style
591 .bg(theme::drop_background())
592 .border_1()
593 .border_dashed()
594 .border_color(theme::accent())
595 })
596 .on_drop(cx.listener(move |this, dragged: &DraggedChat, _, cx| {
597 let target = drop_target.clone();
598 this.state.update(cx, |state, cx| {
599 state.move_chat(&dragged.id, target.as_deref(), cx)
600 });
601 }))
602 .child(icon(
603 if section.collapsed {
604 IconName::ChevronRight
605 } else {
606 IconName::ChevronDown
607 },
608 12.,
609 theme::text_muted(),
610 ))
611 .child(
612 div()
613 .flex_1()
614 .min_w_0()
615 .truncate()
616 .text_size(px(12.))
617 .font_weight(FontWeight::SEMIBOLD)
618 .text_color(theme::text_soft())
619 .child(section.name.clone()),
620 )
621 .when(section.collapsed && section.unread_chats > 0, |header| {
622 header.child(count_badge(section.unread_chats, false))
623 })
624 .child(
625 div()
626 .text_size(px(11.))
627 .text_color(theme::text_faint())
628 .child(section.count.to_string()),
629 )
630 .on_click(cx.listener(move |this, _, _, cx| {
631 let id = toggle_id.clone();
632 this.state
633 .update(cx, |state, cx| state.toggle_collapsed(&id, cx));
634 }))
635 }
636
637 fn see_more_row(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
638 let toggle_id = section.id.clone();
639 let label = if section.hidden_count > 0 {
640 format!("See more ({})", section.hidden_count)
641 } else {
642 "See less".to_owned()
643 };
644 div()
645 .id(SharedString::from(format!("see-more-{}", section.id)))
646 .mx(px(8.))
647 .pl(px(54.))
648 .h(px(SEE_MORE_HEIGHT))
649 .flex()
650 .items_center()
651 .rounded(px(6.))
652 .cursor_pointer()
653 .text_size(px(12.5))
654 .text_color(theme::accent_text())
655 .hover(|row| row.bg(theme::row_hover()))
656 .child(label)
657 .on_click(cx.listener(move |this, _, _, cx| {
658 let id = toggle_id.clone();
659 this.state
660 .update(cx, |state, cx| state.toggle_list_expanded(&id, cx));
661 }))
662 }
663
664 fn chat_row(
665 &self,
666 item: &ChatItem,
667 selected: bool,
668 local_preview: Option<&LocalPreview>,
669 directory: &Directory,
670 menu: &MenuContext,
671 ) -> AnyElement {
672 let ring = if selected {
673 theme::surface_raised()
674 } else {
675 theme::surface()
676 };
677 let unread = item.unread.is_unread();
678 let presence = item
679 .presence_user
680 .as_deref()
681 .map(|user_id| directory.presence_of(user_id));
682 let avatar = spec_avatar(directory, &item.avatar, AVATAR_SIZE, ring);
683 let avatar = match presence {
684 Some(kind) => with_presence(avatar, kind, AVATAR_SIZE, ring).into_any_element(),
685 None => avatar,
686 };
687 let name = div()
688 .flex_1()
689 .min_w_0()
690 .truncate()
691 .text_size(px(14.))
692 .font_weight(if unread {
693 FontWeight::BOLD
694 } else {
695 FontWeight::NORMAL
696 })
697 .text_color(theme::text())
698 .child(item.title.clone());
699 let time = h_flex()
700 .flex_none()
701 .gap(px(4.))
702 .items_center()
703 .text_size(px(11.))
704 .text_color(if unread {
705 theme::accent_text()
706 } else {
707 theme::text_muted()
708 })
709 .when(item.muted, |time| {
710 time.child(icon(
711 IconName::BellOff,
712 MUTED_ICON_SIZE,
713 theme::text_faint(),
714 ))
715 })
716 .child(item.time_label.clone());
717 let preview_color = if matches!(item.preview, Preview::Typing(_)) {
718 theme::accent_text()
719 } else if item.muted {
720 theme::text_faint()
721 } else if unread {
722 theme::text_strong()
723 } else {
724 theme::text_muted()
725 };
726 let preview = match local_preview {
727 Some(local) => Some(local_preview_row(local, preview_color)),
728 None => preview_text(&item.preview).map(|(text, italic)| {
729 let glyph = preview_icon(&text);
730 h_flex()
731 .flex_1()
732 .min_w_0()
733 .overflow_hidden()
734 .gap(px(4.))
735 .items_center()
736 .text_size(px(12.5))
737 .when(italic, |preview| preview.italic())
738 .text_color(preview_color)
739 .children(glyph.map(|name| icon(name, 12., preview_color)))
740 .child(div().truncate().child(text))
741 }),
742 };
743 let lines = v_flex()
744 .flex_1()
745 .min_w_0()
746 .gap(px(2.))
747 .child(
748 h_flex()
749 .gap(px(8.))
750 .items_baseline()
751 .child(name)
752 .child(time),
753 )
754 .child(
755 h_flex()
756 .h(px(18.))
757 .gap(px(8.))
758 .items_center()
759 .child(preview.unwrap_or_else(|| div().flex_1()))
760 .children(unread_marker(item.unread, item.muted)),
761 );
762 let state = self.state.clone();
763 let chat_id = item.id.clone();
764 let dragged = DraggedChat {
765 id: item.id.clone(),
766 title: item.title.clone(),
767 };
768 let pinned = menu.favorites_id.is_some() && item.folder_id == menu.favorites_id;
769 let menu_context = menu.clone();
770 let target = ChatMenuTarget {
771 id: item.id.clone(),
772 title: item.title.clone(),
773 pinned,
774 unread,
775 muted: item.muted,
776 is_group: item.is_group,
777 member_count: item.member_count,
778 };
779 h_flex()
780 .id(SharedString::from(format!("chat-{}", item.id)))
781 .mx(px(8.))
782 .h(px(ROW_HEIGHT))
783 .px(px(8.))
784 .gap(px(10.))
785 .items_center()
786 .rounded(px(8.))
787 .cursor_pointer()
788 .when(selected, |row| row.bg(theme::surface_raised()))
789 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
790 .on_drag(dragged, |dragged, _, _, cx| {
791 let title = dragged.title.clone();
792 cx.new(|_| DragGhost { title })
793 })
794 .child(avatar)
795 .child(lines)
796 .on_click(move |_, _, cx| {
797 let selection = Selection::Chat(chat_id.clone());
798 state.update(cx, |state, cx| state.select(selection, cx));
799 })
800 .context_menu(move |popup, window, cx| {
801 chat_menu(popup, window, cx, &menu_context, &target)
802 })
803 .into_any_element()
804 }
805
806 fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement {
807 let (sections, menu, local_previews) = {
808 let state = self.state.read(cx);
809 let now = Utc::now();
810 let offset = Local::now().offset().fix();
811 let input = SectionInput {
812 chats: &state.sidebar.chats,
813 directory: &state.directory,
814 collapsed: &state.collapsed,
815 expanded_lists: &state.expanded_lists,
816 typing: &state.typing,
817 now,
818 offset,
819 };
820 let sections = build_sections(&input);
821 let menu = MenuContext {
822 state: self.state.clone(),
823 folders: state
824 .directory
825 .folders
826 .iter()
827 .filter(|folder| folder.kind == FolderKind::UserCreated)
828 .map(|folder| (folder.id.clone(), folder.name.clone()))
829 .collect(),
830 favorites_id: state.directory.favorites().map(|folder| folder.id.clone()),
831 };
832 (sections, menu, state.local_previews.clone())
833 };
834
835 let top = -f32::from(self.chats_scroll.offset().y);
836 let measured: f32 = f32::from(self.chats_scroll.bounds().size.height);
837 let height = if measured > 1. {
838 measured
839 } else {
840 window_height.max(FALLBACK_VIEWPORT)
841 };
842 let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
843
844 let (selected, drafting) = {
845 let state = self.state.read(cx);
846 (
847 state.selection.clone().filter(|_| !state.new_chat),
848 state.new_chat,
849 )
850 };
851 let mut faces: Vec<String> = Vec::new();
852 let mut presence_users: Vec<String> = Vec::new();
853 let mut y = 0.;
854 let mut list = v_flex().w_full().pb(px(12.));
855 let mut skipped_height = 0.;
856 let directory_state = self.state.clone();
857 if drafting {
858 list = list.child(self.draft_row());
859 y += ROW_HEIGHT;
860 }
861 for section in &sections {
862 list = with_spacer(list, &mut skipped_height);
863 list = list.child(self.section_header(section, cx));
864 y += SECTION_GAP + SECTION_HEIGHT;
865 if section.collapsed {
866 continue;
867 }
868 for item in &section.items {
869 let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to;
870 if on_screen {
871 collect_faces(&item.avatar, &mut faces);
872 presence_users.extend(item.presence_user.clone());
873 }
874 if on_screen {
875 list = with_spacer(list, &mut skipped_height);
876 let is_selected = selected == Some(Selection::Chat(item.id.clone()));
877 let directory = &directory_state.read(cx).directory;
878 let local_preview = local_previews.get(&item.id).filter(|_| !is_selected);
879 list = list.child(self.chat_row(
880 item,
881 is_selected,
882 local_preview,
883 directory,
884 &menu,
885 ));
886 } else {
887 skipped_height += ROW_HEIGHT;
888 }
889 y += ROW_HEIGHT;
890 }
891 if section.hidden_count > 0 || section.list_expanded {
892 list = with_spacer(list, &mut skipped_height);
893 list = list.child(self.see_more_row(section, cx));
894 y += SEE_MORE_HEIGHT;
895 }
896 if section.show_empty_hint {
897 list = with_spacer(list, &mut skipped_height);
898 list = list.child(
899 div()
900 .ml(px(44.))
901 .mr(px(20.))
902 .h(px(HINT_HEIGHT))
903 .flex()
904 .items_center()
905 .text_size(px(12.))
906 .italic()
907 .text_color(theme::text_faint())
908 .child(EMPTY_FOLDER_HINT),
909 );
910 y += HINT_HEIGHT;
911 }
912 }
913 list = with_spacer(list, &mut skipped_height);
914 self.request_lazy_data(faces, presence_users, cx);
915 div()
916 .id("chats-scroll")
917 .flex_1()
918 .min_h_0()
919 .overflow_y_scroll()
920 .track_scroll(&self.chats_scroll)
921 .child(list)
922 .into_any_element()
923 }
924
925 fn request_lazy_data(
926 &self,
927 faces: Vec<String>,
928 presence_users: Vec<String>,
929 cx: &mut Context<Self>,
930 ) {
931 if faces.is_empty() && presence_users.is_empty() {
932 return;
933 }
934 let state = self.state.clone();
935 cx.defer(move |cx| {
936 state.update(cx, |state, cx| {
937 state.request_avatars(faces, cx);
938 state.request_presence(presence_users, cx);
939 });
940 });
941 }
942
943 fn channel_row(
944 &self,
945 channel_id: &str,
946 team: &TeamRecord,
947 channel: &ChannelRecord,
948 selected: bool,
949 notifications: ChannelNotifications,
950 ) -> AnyElement {
951 let unread = channel.unread;
952 let state = self.state.clone();
953 let menu_state = self.state.clone();
954 let menu_channel_id = channel_id.to_owned();
955 let selection = Selection::Channel(channel_id.to_owned());
956 let now = Utc::now();
957 let offset = Local::now().offset().fix();
958 let time = channel
959 .last_message_at
960 .map(|time| {
961 format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset)
962 })
963 .unwrap_or_default();
964 h_flex()
965 .id(SharedString::from(format!("pinned-{channel_id}")))
966 .mx(px(8.))
967 .h(px(PINNED_CHANNEL_HEIGHT))
968 .px(px(8.))
969 .gap(px(10.))
970 .items_center()
971 .rounded(px(8.))
972 .cursor_pointer()
973 .when(selected, |row| row.bg(theme::surface_raised()))
974 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
975 .child(square_avatar(&team.name, &team.id, 28., 7.))
976 .child(
977 v_flex()
978 .flex_1()
979 .min_w_0()
980 .gap(px(1.))
981 .child(
982 h_flex()
983 .gap(px(8.))
984 .items_baseline()
985 .child(
986 div()
987 .flex_1()
988 .min_w_0()
989 .truncate()
990 .text_size(px(13.5))
991 .font_weight(if unread {
992 FontWeight::BOLD
993 } else {
994 FontWeight::NORMAL
995 })
996 .text_color(theme::text())
997 .child(format!("{} / {}", team.name, channel.name)),
998 )
999 .children(notification_badge(notifications.level))
1000 .child(
1001 div()
1002 .flex_none()
1003 .text_size(px(11.))
1004 .text_color(if unread {
1005 theme::accent_text()
1006 } else {
1007 theme::text_muted()
1008 })
1009 .child(time),
1010 ),
1011 )
1012 .child(
1013 h_flex()
1014 .gap(px(8.))
1015 .items_center()
1016 .child(
1017 div()
1018 .flex_1()
1019 .min_w_0()
1020 .truncate()
1021 .text_size(px(12.))
1022 .text_color(theme::text_muted())
1023 .child(team.name.clone()),
1024 )
1025 .when(unread, |line| line.child(dot(7.))),
1026 ),
1027 )
1028 .on_click(move |_, _, cx| {
1029 let selection = selection.clone();
1030 state.update(cx, |state, cx| state.select(selection, cx));
1031 })
1032 .context_menu(move |popup, window, cx| {
1033 channel_menu(
1034 popup,
1035 window,
1036 cx,
1037 menu_state.clone(),
1038 &menu_channel_id,
1039 notifications,
1040 )
1041 })
1042 .into_any_element()
1043 }
1044
1045 fn plain_header(&self, label: &str) -> impl IntoElement {
1046 div()
1047 .h(px(SECTION_HEIGHT))
1048 .mt(px(SECTION_GAP))
1049 .px(px(12.))
1050 .flex()
1051 .items_center()
1052 .text_size(px(12.))
1053 .font_weight(FontWeight::SEMIBOLD)
1054 .text_color(theme::text_soft())
1055 .child(label.to_owned())
1056 }
1057
1058 fn team_block(
1059 &self,
1060 mut list: Div,
1061 entry: &SidebarTeam,
1062 selected: Option<&Selection>,
1063 cx: &mut Context<Self>,
1064 ) -> Div {
1065 let team_id = entry.team.id.clone();
1066 let expanded = team_expanded(&team_id, &self.collapsed_teams);
1067 let unread = entry.channels.iter().any(|channel| channel.unread);
1068 let toggle_id = team_id.clone();
1069 list = list.child(
1070 h_flex()
1071 .id(SharedString::from(format!("team-{team_id}")))
1072 .mx(px(8.))
1073 .h(px(TEAM_ROW_HEIGHT))
1074 .px(px(8.))
1075 .gap(px(8.))
1076 .items_center()
1077 .rounded(px(8.))
1078 .cursor_pointer()
1079 .hover(|row| row.bg(theme::row_hover()))
1080 .child(icon(
1081 if expanded {
1082 IconName::ChevronDown
1083 } else {
1084 IconName::ChevronRight
1085 },
1086 14.,
1087 theme::text_muted(),
1088 ))
1089 .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.))
1090 .child(
1091 div()
1092 .flex_1()
1093 .min_w_0()
1094 .truncate()
1095 .text_size(px(13.5))
1096 .font_weight(if unread || expanded {
1097 FontWeight::SEMIBOLD
1098 } else {
1099 FontWeight::NORMAL
1100 })
1101 .text_color(theme::text())
1102 .child(entry.team.name.clone()),
1103 )
1104 .when(unread && !expanded, |row| row.child(dot(7.)))
1105 .on_click(cx.listener(move |this, _, _, cx| {
1106 if !this.collapsed_teams.remove(&toggle_id) {
1107 this.collapsed_teams.insert(toggle_id.clone());
1108 }
1109 save_collapsed_teams(&this.state.read(cx).store, &this.collapsed_teams);
1110 cx.notify();
1111 })),
1112 );
1113 if !expanded {
1114 return list;
1115 }
1116 let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id);
1117 let is_selected =
1118 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
1119 for channel in entry
1120 .channels
1121 .iter()
1122 .filter(|channel| !is_hidden(channel) || is_selected(channel))
1123 {
1124 list = list.child(self.team_channel_row(
1125 channel,
1126 is_selected(channel),
1127 entry.channel_notifications(&channel.id),
1128 ));
1129 }
1130 let hidden: Vec<&ChannelRecord> = entry
1131 .channels
1132 .iter()
1133 .filter(|channel| is_hidden(channel) && !is_selected(channel))
1134 .collect();
1135 if hidden.is_empty() {
1136 return list;
1137 }
1138 let revealed = self.revealed_channel_teams.contains(&team_id);
1139 let toggle_id = team_id.clone();
1140 list = list.child(self.reveal_row(
1141 SharedString::from(format!("hidden-channels-{team_id}")),
1142 hidden_channels_label(hidden.len()),
1143 revealed,
1144 40.,
1145 cx.listener(move |this, _, _, cx| {
1146 if !this.revealed_channel_teams.remove(&toggle_id) {
1147 this.revealed_channel_teams.insert(toggle_id.clone());
1148 }
1149 cx.notify();
1150 }),
1151 ));
1152 if revealed {
1153 for channel in hidden {
1154 list = list.child(self.team_channel_row(
1155 channel,
1156 false,
1157 entry.channel_notifications(&channel.id),
1158 ));
1159 }
1160 }
1161 list
1162 }
1163
1164 fn reveal_row(
1165 &self,
1166 id: SharedString,
1167 label: String,
1168 open: bool,
1169 indent: f32,
1170 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1171 ) -> impl IntoElement {
1172 h_flex()
1173 .id(id)
1174 .mx(px(8.))
1175 .h(px(REVEAL_ROW_HEIGHT))
1176 .pl(px(indent))
1177 .pr(px(8.))
1178 .gap(px(8.))
1179 .items_center()
1180 .rounded(px(8.))
1181 .cursor_pointer()
1182 .hover(|row| row.bg(theme::row_hover()))
1183 .child(icon(
1184 if open {
1185 IconName::ChevronDown
1186 } else {
1187 IconName::ChevronRight
1188 },
1189 12.,
1190 theme::text_faint(),
1191 ))
1192 .child(
1193 div()
1194 .text_size(px(12.5))
1195 .text_color(theme::text_muted())
1196 .child(label),
1197 )
1198 .on_click(on_click)
1199 }
1200
1201 fn team_channel_row(
1202 &self,
1203 channel: &ChannelRecord,
1204 is_selected: bool,
1205 notifications: ChannelNotifications,
1206 ) -> impl IntoElement {
1207 let state = self.state.clone();
1208 let menu_state = self.state.clone();
1209 let menu_channel_id = channel.id.clone();
1210 let selection = Selection::Channel(channel.id.clone());
1211 h_flex()
1212 .id(SharedString::from(format!("channel-{}", channel.id)))
1213 .mx(px(8.))
1214 .h(px(TEAM_ROW_HEIGHT))
1215 .pl(px(40.))
1216 .pr(px(8.))
1217 .gap(px(8.))
1218 .items_center()
1219 .rounded(px(8.))
1220 .cursor_pointer()
1221 .when(is_selected, |row| row.bg(theme::surface_raised()))
1222 .when(!is_selected, |row| {
1223 row.hover(|row| row.bg(theme::row_hover()))
1224 })
1225 .child(
1226 div()
1227 .w(px(14.))
1228 .flex()
1229 .justify_center()
1230 .text_size(px(15.))
1231 .text_color(theme::text_faint())
1232 .child("#"),
1233 )
1234 .child(
1235 div()
1236 .flex_1()
1237 .min_w_0()
1238 .truncate()
1239 .text_size(px(13.5))
1240 .font_weight(if channel.unread {
1241 FontWeight::BOLD
1242 } else {
1243 FontWeight::NORMAL
1244 })
1245 .text_color(theme::text())
1246 .child(channel.name.clone()),
1247 )
1248 .children(notification_badge(notifications.level))
1249 .when(channel.unread, |row| row.child(dot(7.)))
1250 .on_click(move |_, _, cx| {
1251 let selection = selection.clone();
1252 state.update(cx, |state, cx| state.select(selection, cx));
1253 })
1254 .context_menu(move |popup, window, cx| {
1255 channel_menu(
1256 popup,
1257 window,
1258 cx,
1259 menu_state.clone(),
1260 &menu_channel_id,
1261 notifications,
1262 )
1263 })
1264 }
1265
1266 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
1267 let (sidebar, pinned_ids, selected) = {
1268 let state = self.state.read(cx);
1269 (
1270 state.sidebar.clone(),
1271 state.directory.pinned_channels.clone(),
1272 state.selection.clone().filter(|_| !state.new_chat),
1273 )
1274 };
1275 let mut list = v_flex().w_full().pb(px(12.));
1276 let pinned: Vec<(&SidebarTeam, &ChannelRecord)> = pinned_ids
1277 .iter()
1278 .filter_map(|id| {
1279 sidebar.teams.iter().find_map(|entry| {
1280 entry
1281 .channels
1282 .iter()
1283 .find(|channel| &channel.id == id)
1284 .map(|channel| (entry, channel))
1285 })
1286 })
1287 .collect();
1288 if !pinned.is_empty() {
1289 list = list.child(self.plain_header(PINNED_LABEL));
1290 for (entry, channel) in pinned {
1291 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
1292 list = list.child(self.channel_row(
1293 &channel.id,
1294 &entry.team,
1295 channel,
1296 is_selected,
1297 entry.channel_notifications(&channel.id),
1298 ));
1299 }
1300 }
1301 list = list.child(self.plain_header(TEAMS_LABEL));
1302 let (shown_teams, hidden_teams): (Vec<&SidebarTeam>, Vec<&SidebarTeam>) =
1303 sidebar.teams.iter().partition(|entry| !entry.hidden);
1304 for entry in shown_teams {
1305 list = self.team_block(list, entry, selected.as_ref(), cx);
1306 }
1307 if !hidden_teams.is_empty() {
1308 let holds_selection = hidden_teams
1309 .iter()
1310 .any(|entry| team_holds(entry, selected.as_ref()));
1311 let open = self.hidden_teams_open || holds_selection;
1312 list = list.child(self.reveal_row(
1313 "hidden-teams".into(),
1314 format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()),
1315 open,
1316 8.,
1317 cx.listener(|this, _, _, cx| {
1318 this.hidden_teams_open = !this.hidden_teams_open;
1319 cx.notify();
1320 }),
1321 ));
1322 if open {
1323 for entry in hidden_teams {
1324 list = self.team_block(list, entry, selected.as_ref(), cx);
1325 }
1326 }
1327 }
1328 div()
1329 .id("channels-scroll")
1330 .flex_1()
1331 .min_h_0()
1332 .overflow_y_scroll()
1333 .track_scroll(&self.channels_scroll)
1334 .child(list)
1335 .into_any_element()
1336 }
1337}
1338
1339fn notification_badge(level: ChannelNotificationLevel) -> Option<impl IntoElement> {
1340 let name = match level {
1341 ChannelNotificationLevel::BannerAndFeed => IconName::Bell,
1342 ChannelNotificationLevel::Off => IconName::BellOff,
1343 ChannelNotificationLevel::Feed => return None,
1344 };
1345 Some(icon(name, NOTIFICATION_ICON_SIZE, theme::text_muted()))
1346}
1347
1348fn channel_menu(
1349 popup: PopupMenu,
1350 window: &mut Window,
1351 cx: &mut Context<PopupMenu>,
1352 state: Entity<AppState>,
1353 channel_id: &str,
1354 notifications: ChannelNotifications,
1355) -> PopupMenu {
1356 let channel_id = channel_id.to_owned();
1357 popup.submenu_with_icon(
1358 Some(menu_icon(IconName::Bell)),
1359 "Notifications",
1360 window,
1361 cx,
1362 move |submenu, _, _| {
1363 let level_item = |label: &'static str, level: ChannelNotificationLevel| {
1364 let (state, channel_id) = (state.clone(), channel_id.clone());
1365 PopupMenuItem::new(label)
1366 .checked(notifications.level == level)
1367 .on_click(move |_, _, cx| {
1368 let changed = ChannelNotifications {
1369 level,
1370 include_replies: notifications.include_replies
1371 && level != ChannelNotificationLevel::Off,
1372 };
1373 state.update(cx, |state, cx| {
1374 state.set_channel_notifications(&channel_id, changed, cx)
1375 });
1376 })
1377 };
1378 let (replies_state, replies_id) = (state.clone(), channel_id.clone());
1379 submenu
1380 .item(level_item(
1381 "Banner and activity",
1382 ChannelNotificationLevel::BannerAndFeed,
1383 ))
1384 .item(level_item("Activity only", ChannelNotificationLevel::Feed))
1385 .item(level_item("Off", ChannelNotificationLevel::Off))
1386 .separator()
1387 .item(
1388 PopupMenuItem::new("Include thread replies")
1389 .checked(notifications.include_replies)
1390 .disabled(notifications.level == ChannelNotificationLevel::Off)
1391 .on_click(move |_, _, cx| {
1392 let changed = ChannelNotifications {
1393 level: notifications.level,
1394 include_replies: !notifications.include_replies,
1395 };
1396 replies_state.update(cx, |state, cx| {
1397 state.set_channel_notifications(&replies_id, changed, cx)
1398 });
1399 }),
1400 )
1401 },
1402 )
1403}
1404
1405fn team_expanded(team_id: &str, collapsed_teams: &HashSet<String>) -> bool {
1406 !collapsed_teams.contains(team_id)
1407}
1408
1409fn load_collapsed_teams(store: &Store) -> HashSet<String> {
1410 store
1411 .meta(COLLAPSED_TEAMS_META_KEY)
1412 .ok()
1413 .flatten()
1414 .map(|value| parse_collapsed_teams(&value))
1415 .unwrap_or_default()
1416}
1417
1418fn save_collapsed_teams(store: &Store, collapsed_teams: &HashSet<String>) {
1419 let _ = store.set_meta(
1420 COLLAPSED_TEAMS_META_KEY,
1421 &serialize_collapsed_teams(collapsed_teams),
1422 );
1423}
1424
1425fn serialize_collapsed_teams(collapsed_teams: &HashSet<String>) -> String {
1426 let mut team_ids: Vec<&String> = collapsed_teams.iter().collect();
1427 team_ids.sort_unstable();
1428 serde_json::to_string(&team_ids).unwrap_or_default()
1429}
1430
1431fn parse_collapsed_teams(value: &str) -> HashSet<String> {
1432 serde_json::from_str(value).unwrap_or_default()
1433}
1434
1435fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
1436 entry
1437 .channels
1438 .iter()
1439 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
1440}
1441
1442fn hidden_channels_label(count: usize) -> String {
1443 if count == 1 {
1444 "1 hidden channel".to_owned()
1445 } else {
1446 format!("{count} hidden channels")
1447 }
1448}
1449
1450fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
1451 match spec {
1452 AvatarSpec::Single(face) => faces.extend(face.user_id.clone()),
1453 AvatarSpec::Pair(first, second) => {
1454 faces.extend(first.user_id.clone());
1455 faces.extend(second.user_id.clone());
1456 }
1457 }
1458}
1459
1460fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
1461 let height = std::mem::take(skipped_height);
1462 if height > 0. {
1463 list.child(div().h(px(height)).flex_none())
1464 } else {
1465 list
1466 }
1467}
1468
1469impl Render for SidebarView {
1470 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1471 let (unread_chats, channel_dot) = {
1472 let state = self.state.read(cx);
1473 (
1474 unread_chat_count(&state.sidebar.chats),
1475 any_unread_channel(&state.sidebar),
1476 )
1477 };
1478 let window_height = f32::from(window.viewport_size().height);
1479 let body = match self.tab {
1480 SidebarTab::Chats => self.chats_body(window_height, cx),
1481 SidebarTab::Channels => self.channels_body(cx),
1482 SidebarTab::Calls => calls_body(&self.state, &self.calls_scroll, cx),
1483 };
1484 v_flex()
1485 .w(px(SIDEBAR_WIDTH))
1486 .h_full()
1487 .flex_none()
1488 .bg(theme::surface())
1489 .border_r_1()
1490 .border_color(theme::border())
1491 .overflow_hidden()
1492 .child(self.tab_bar(unread_chats, channel_dot, cx))
1493 .child(body)
1494 }
1495}
1496
1497#[cfg(test)]
1498mod tests {
1499 use std::collections::HashSet;
1500
1501 use super::{
1502 leave_description, parse_collapsed_teams, serialize_collapsed_teams, team_expanded,
1503 };
1504
1505 #[test]
1506 fn teams_start_expanded() {
1507 assert!(team_expanded("t1", &HashSet::new()));
1508 }
1509
1510 #[test]
1511 fn collapsed_team_stays_closed() {
1512 let collapsed = HashSet::from(["t1".to_owned()]);
1513 assert!(!team_expanded("t1", &collapsed));
1514 assert!(team_expanded("t2", &collapsed));
1515 }
1516
1517 #[test]
1518 fn collapsed_teams_round_trip_as_json() {
1519 let collapsed = HashSet::from(["b".to_owned(), "a".to_owned()]);
1520 let json = serialize_collapsed_teams(&collapsed);
1521 assert_eq!(json, r#"["a","b"]"#);
1522 assert_eq!(parse_collapsed_teams(&json), collapsed);
1523 assert!(parse_collapsed_teams("garbage").is_empty());
1524 }
1525
1526 #[test]
1527 fn leave_text_counts_the_other_members() {
1528 assert_eq!(
1529 leave_description(4),
1530 "The 3 other members see that you left. You can only come back if someone adds you."
1531 );
1532 assert!(leave_description(2).starts_with("The other member sees"));
1533 assert!(leave_description(0).starts_with("The other members see"));
1534 }
1535}