BlueGit

ict rusty-teams

Files

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

sidebar.rs

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