BlueGit

ict rusty-teams

Files

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

sidebar.rs

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