BlueGit

ict rusty-teams

Files

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

sidebar.rs

271952ed History
sidebar.rs 35.7 KiB Download Raw
1use std::collections::HashSet;
2
3use chrono::{Local, Offset, Utc};
4use gpui_kit::assets::IconName;
5use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenuItem};
6use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
7use gpui_kit::prelude::FluentBuilder as _;
8use gpui_kit::*;
9use store::{ChannelRecord, SidebarTeam, TeamRecord};
10
11use super::avatar::{spec_avatar, square_avatar, with_presence};
12use super::widgets::{count_badge, dot, icon, unread_marker};
13use crate::app_state::{AppEvent, AppState, Selection};
14use crate::data::{Directory, FolderKind};
15use crate::format;
16use crate::sidebar_model::{
17 AvatarSpec, ChatItem, DELETED_PREVIEW, EMPTY_FOLDER_HINT, Preview, Section, SectionInput,
18 SectionKind, any_unread_channel, build_sections, unread_chat_count,
19};
20use crate::theme;
21
22pub const SIDEBAR_WIDTH: f32 = 312.;
23const ROW_HEIGHT: f32 = 54.;
24const PINNED_CHANNEL_HEIGHT: f32 = 48.;
25const SECTION_HEIGHT: f32 = 30.;
26const SECTION_GAP: f32 = 4.;
27const HINT_HEIGHT: f32 = 26.;
28const FALLBACK_VIEWPORT: f32 = 900.;
29const VIEWPORT_MARGIN: f32 = 300.;
30const AVATAR_SIZE: f32 = 36.;
31const PINNED_LABEL: &str = "Pinned";
32const TEAMS_LABEL: &str = "Teams";
33const HIDDEN_TEAMS_LABEL: &str = "Hidden teams";
34const TEAM_ROW_HEIGHT: f32 = 40.;
35const REVEAL_ROW_HEIGHT: f32 = 32.;
36const NEW_CHAT_BUTTON_SIZE: f32 = 34.;
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39enum SidebarTab {
40 Chats,
41 Channels,
42}
43
44#[derive(Clone)]
45struct DraggedChat {
46 id: String,
47 title: String,
48}
49
50struct DragGhost {
51 title: String,
52}
53
54impl Render for DragGhost {
55 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
56 div()
57 .px(px(12.))
58 .py(px(6.))
59 .rounded(px(8.))
60 .bg(theme::surface_raised())
61 .border_1()
62 .border_color(theme::accent())
63 .text_size(px(13.))
64 .text_color(theme::text())
65 .child(self.title.clone())
66 }
67}
68
69#[derive(Clone)]
70struct MenuContext {
71 state: Entity<AppState>,
72 folders: Vec<(String, String)>,
73 favorites_id: Option<String>,
74}
75
76pub struct SidebarView {
77 state: Entity<AppState>,
78 tab: SidebarTab,
79 expanded_teams: HashSet<String>,
80 revealed_channel_teams: HashSet<String>,
81 hidden_teams_open: bool,
82 chats_scroll: ScrollHandle,
83 channels_scroll: ScrollHandle,
84 _subscription: Subscription,
85}
86
87fn preview_text(preview: &Preview) -> Option<(String, bool)> {
88 match preview {
89 Preview::Empty => None,
90 Preview::Deleted => Some((DELETED_PREVIEW.to_owned(), true)),
91 Preview::Text {
92 prefix: Some(prefix),
93 text,
94 } => Some((format!("{prefix}: {text}"), false)),
95 Preview::Text { prefix: None, text } => Some((text.clone(), false)),
96 }
97}
98
99fn preview_icon(text: &str) -> Option<IconName> {
100 match text.rsplit(": ").next()? {
101 "Image" => Some(IconName::Image),
102 "File" => Some(IconName::File),
103 _ => None,
104 }
105}
106
107impl SidebarView {
108 pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {
109 let subscription = cx.subscribe(&state, |this, state, event: &AppEvent, cx| {
110 if matches!(event, AppEvent::Selection) && state.read(cx).new_chat {
111 this.tab = SidebarTab::Chats;
112 }
113 if matches!(
114 event,
115 AppEvent::Sidebar | AppEvent::Selection | AppEvent::Directory
116 ) {
117 cx.notify();
118 }
119 });
120 let tab = if state.read(cx).start_on_channels {
121 SidebarTab::Channels
122 } else {
123 SidebarTab::Chats
124 };
125 SidebarView {
126 state,
127 tab,
128 expanded_teams: HashSet::new(),
129 revealed_channel_teams: HashSet::new(),
130 hidden_teams_open: false,
131 chats_scroll: ScrollHandle::new(),
132 channels_scroll: ScrollHandle::new(),
133 _subscription: subscription,
134 }
135 }
136
137 fn tab_button(
138 &self,
139 tab: SidebarTab,
140 label: &str,
141 marker: Option<AnyElement>,
142 cx: &mut Context<Self>,
143 ) -> impl IntoElement {
144 let active = self.tab == tab;
145 h_flex()
146 .id(SharedString::from(format!("tab-{label}")))
147 .flex_1()
148 .h(px(28.))
149 .rounded(px(6.))
150 .items_center()
151 .justify_center()
152 .gap(px(6.))
153 .cursor_pointer()
154 .text_size(px(13.))
155 .when(active, |tab| {
156 tab.bg(theme::surface_raised())
157 .font_weight(FontWeight::SEMIBOLD)
158 .text_color(theme::text())
159 })
160 .when(!active, |tab| {
161 tab.text_color(theme::text_muted())
162 .hover(|tab| tab.text_color(theme::text()))
163 })
164 .child(label.to_owned())
165 .children(marker)
166 .on_click(cx.listener(move |this, _, _, cx| {
167 this.tab = tab;
168 cx.notify();
169 }))
170 }
171
172 fn tab_bar(
173 &self,
174 unread_chats: u32,
175 channel_dot: bool,
176 cx: &mut Context<Self>,
177 ) -> impl IntoElement {
178 let chat_marker =
179 (unread_chats > 0).then(|| count_badge(unread_chats, false).into_any_element());
180 let channel_marker = channel_dot.then(|| dot(7.).into_any_element());
181 h_flex()
182 .w_full()
183 .px(px(12.))
184 .pt(px(12.))
185 .pb(px(8.))
186 .gap(px(8.))
187 .child(
188 h_flex()
189 .flex_1()
190 .min_w_0()
191 .gap(px(4.))
192 .p(px(3.))
193 .rounded(px(8.))
194 .bg(theme::background())
195 .border_1()
196 .border_color(theme::border())
197 .child(self.tab_button(SidebarTab::Chats, "Chats", chat_marker, cx))
198 .child(self.tab_button(SidebarTab::Channels, "Channels", channel_marker, cx)),
199 )
200 .child(self.new_chat_button(cx))
201 }
202
203 fn new_chat_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
204 let active = self.state.read(cx).new_chat;
205 div()
206 .id("new-chat")
207 .size(px(NEW_CHAT_BUTTON_SIZE))
208 .flex_none()
209 .flex()
210 .items_center()
211 .justify_center()
212 .rounded(px(8.))
213 .border_1()
214 .border_color(theme::border())
215 .cursor_pointer()
216 .when(active, |button| button.bg(theme::surface_raised()))
217 .hover(|button| button.bg(theme::row_hover()))
218 .tooltip(|window, cx| Tooltip::new("New chat (Ctrl+N)").build(window, cx))
219 .child(icon(IconName::Pencil, 16., theme::text_soft()))
220 .on_click(cx.listener(|this, _, _, cx| {
221 this.state.update(cx, |state, cx| state.start_new_chat(cx));
222 }))
223 }
224
225 fn draft_row(&self) -> impl IntoElement {
226 h_flex()
227 .id("new-chat-draft")
228 .mx(px(8.))
229 .h(px(ROW_HEIGHT))
230 .px(px(8.))
231 .gap(px(10.))
232 .items_center()
233 .rounded(px(8.))
234 .bg(theme::surface_raised())
235 .child(
236 div()
237 .size(px(AVATAR_SIZE))
238 .flex_none()
239 .flex()
240 .items_center()
241 .justify_center()
242 .rounded_full()
243 .bg(theme::border_strong())
244 .child(icon(IconName::Pencil, 16., theme::text())),
245 )
246 .child(
247 v_flex()
248 .flex_1()
249 .min_w_0()
250 .gap(px(2.))
251 .child(
252 div()
253 .truncate()
254 .text_size(px(14.))
255 .text_color(theme::text())
256 .child("New chat"),
257 )
258 .child(
259 div()
260 .truncate()
261 .text_size(px(12.5))
262 .text_color(theme::text_muted())
263 .child("Draft"),
264 ),
265 )
266 }
267
268 fn section_header(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
269 let drop_target = (section.kind != SectionKind::Others).then(|| section.id.clone());
270 let toggle_id = section.id.clone();
271 h_flex()
272 .id(SharedString::from(format!("section-{}", section.id)))
273 .h(px(SECTION_HEIGHT))
274 .mt(px(SECTION_GAP))
275 .mx(px(4.))
276 .pl(px(8.))
277 .pr(px(10.))
278 .gap(px(6.))
279 .items_center()
280 .rounded(px(6.))
281 .cursor_pointer()
282 .hover(|header| header.bg(theme::row_hover()))
283 .drag_over::<DraggedChat>(|style, _, _, _| {
284 style
285 .bg(theme::drop_background())
286 .border_1()
287 .border_dashed()
288 .border_color(theme::accent())
289 })
290 .on_drop(cx.listener(move |this, dragged: &DraggedChat, _, cx| {
291 let target = drop_target.clone();
292 this.state.update(cx, |state, cx| {
293 state.move_chat(&dragged.id, target.as_deref(), cx)
294 });
295 }))
296 .child(icon(
297 if section.collapsed {
298 IconName::ChevronRight
299 } else {
300 IconName::ChevronDown
301 },
302 12.,
303 theme::text_muted(),
304 ))
305 .child(
306 div()
307 .flex_1()
308 .min_w_0()
309 .truncate()
310 .text_size(px(12.))
311 .font_weight(FontWeight::SEMIBOLD)
312 .text_color(theme::text_soft())
313 .child(section.name.clone()),
314 )
315 .when(section.collapsed && section.unread_chats > 0, |header| {
316 header.child(count_badge(section.unread_chats, false))
317 })
318 .child(
319 div()
320 .text_size(px(11.))
321 .text_color(theme::text_faint())
322 .child(section.count.to_string()),
323 )
324 .on_click(cx.listener(move |this, _, _, cx| {
325 let id = toggle_id.clone();
326 this.state
327 .update(cx, |state, cx| state.toggle_collapsed(&id, cx));
328 }))
329 }
330
331 fn chat_row(
332 &self,
333 item: &ChatItem,
334 selected: bool,
335 directory: &Directory,
336 menu: &MenuContext,
337 ) -> AnyElement {
338 let ring = if selected {
339 theme::surface_raised()
340 } else {
341 theme::surface()
342 };
343 let unread = item.unread.is_unread();
344 let presence = item
345 .presence_user
346 .as_deref()
347 .map(|user_id| directory.presence_of(user_id));
348 let avatar = spec_avatar(directory, &item.avatar, AVATAR_SIZE, ring);
349 let avatar = match presence {
350 Some(kind) => with_presence(avatar, kind, AVATAR_SIZE, ring).into_any_element(),
351 None => avatar,
352 };
353 let name = div()
354 .flex_1()
355 .min_w_0()
356 .truncate()
357 .text_size(px(14.))
358 .font_weight(if unread {
359 FontWeight::BOLD
360 } else {
361 FontWeight::NORMAL
362 })
363 .text_color(theme::text())
364 .child(item.title.clone());
365 let time = div()
366 .flex_none()
367 .text_size(px(11.))
368 .text_color(if unread {
369 theme::accent_text()
370 } else {
371 theme::text_muted()
372 })
373 .child(item.time_label.clone());
374 let preview = preview_text(&item.preview).map(|(text, italic)| {
375 let glyph = preview_icon(&text);
376 h_flex()
377 .flex_1()
378 .min_w_0()
379 .overflow_hidden()
380 .gap(px(4.))
381 .items_center()
382 .text_size(px(12.5))
383 .when(italic, |preview| preview.italic())
384 .text_color(if unread {
385 theme::text_strong()
386 } else {
387 theme::text_muted()
388 })
389 .children(glyph.map(|name| {
390 icon(
391 name,
392 12.,
393 if unread {
394 theme::text_strong()
395 } else {
396 theme::text_muted()
397 },
398 )
399 }))
400 .child(div().truncate().child(text))
401 });
402 let lines = v_flex()
403 .flex_1()
404 .min_w_0()
405 .gap(px(2.))
406 .child(
407 h_flex()
408 .gap(px(8.))
409 .items_baseline()
410 .child(name)
411 .child(time),
412 )
413 .child(
414 h_flex()
415 .h(px(18.))
416 .gap(px(8.))
417 .items_center()
418 .child(preview.unwrap_or_else(|| div().flex_1()))
419 .children(unread_marker(item.unread)),
420 );
421 let state = self.state.clone();
422 let chat_id = item.id.clone();
423 let dragged = DraggedChat {
424 id: item.id.clone(),
425 title: item.title.clone(),
426 };
427 let pinned = menu.favorites_id.is_some() && item.folder_id == menu.favorites_id;
428 let menu_context = menu.clone();
429 let menu_chat_id = item.id.clone();
430 h_flex()
431 .id(SharedString::from(format!("chat-{}", item.id)))
432 .mx(px(8.))
433 .h(px(ROW_HEIGHT))
434 .px(px(8.))
435 .gap(px(10.))
436 .items_center()
437 .rounded(px(8.))
438 .cursor_pointer()
439 .when(selected, |row| row.bg(theme::surface_raised()))
440 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
441 .on_drag(dragged, |dragged, _, _, cx| {
442 let title = dragged.title.clone();
443 cx.new(|_| DragGhost { title })
444 })
445 .child(avatar)
446 .child(lines)
447 .on_click(move |_, _, cx| {
448 let selection = Selection::Chat(chat_id.clone());
449 state.update(cx, |state, cx| state.select(selection, cx));
450 })
451 .context_menu(move |popup, window, cx| {
452 let context = menu_context.clone();
453 let chat_id = menu_chat_id.clone();
454 let pin_state = context.state.clone();
455 let pin_id = chat_id.clone();
456 let mut popup = popup.item(
457 PopupMenuItem::new(if pinned { "Unpin" } else { "Pin" }).on_click(
458 move |_, _, cx| {
459 pin_state.update(cx, |state, cx| {
460 if pinned {
461 state.move_chat(&pin_id, None, cx);
462 } else {
463 state.pin_chat(&pin_id, cx);
464 }
465 });
466 },
467 ),
468 );
469 if !context.folders.is_empty() {
470 let folders = context.folders.clone();
471 let move_state = context.state.clone();
472 let move_id = chat_id.clone();
473 popup = popup.submenu("Move to folder", window, cx, move |submenu, _, _| {
474 folders.iter().fold(submenu, |submenu, (folder_id, name)| {
475 let (state, chat_id, folder_id) =
476 (move_state.clone(), move_id.clone(), folder_id.clone());
477 submenu.item(PopupMenuItem::new(name.clone()).on_click(
478 move |_, _, cx| {
479 state.update(cx, |state, cx| {
480 state.move_chat(&chat_id, Some(&folder_id), cx)
481 });
482 },
483 ))
484 })
485 });
486 }
487 let read_state = context.state.clone();
488 popup.item(
489 PopupMenuItem::new("Mark as read")
490 .disabled(!unread)
491 .on_click(move |_, _, cx| {
492 read_state.update(cx, |state, cx| state.mark_chat_read(&chat_id, cx));
493 }),
494 )
495 })
496 .into_any_element()
497 }
498
499 fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement {
500 let (sections, menu) = {
501 let state = self.state.read(cx);
502 let now = Utc::now();
503 let offset = Local::now().offset().fix();
504 let input = SectionInput {
505 chats: &state.sidebar.chats,
506 directory: &state.directory,
507 collapsed: &state.collapsed,
508 now,
509 offset,
510 };
511 let sections = build_sections(&input);
512 let menu = MenuContext {
513 state: self.state.clone(),
514 folders: state
515 .directory
516 .folders
517 .iter()
518 .filter(|folder| folder.kind == FolderKind::UserCreated)
519 .map(|folder| (folder.id.clone(), folder.name.clone()))
520 .collect(),
521 favorites_id: state.directory.favorites().map(|folder| folder.id.clone()),
522 };
523 (sections, menu)
524 };
525
526 let top = -f32::from(self.chats_scroll.offset().y);
527 let measured: f32 = f32::from(self.chats_scroll.bounds().size.height);
528 let height = if measured > 1. {
529 measured
530 } else {
531 window_height.max(FALLBACK_VIEWPORT)
532 };
533 let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
534
535 let (selected, drafting) = {
536 let state = self.state.read(cx);
537 (
538 state.selection.clone().filter(|_| !state.new_chat),
539 state.new_chat,
540 )
541 };
542 let mut faces: Vec<String> = Vec::new();
543 let mut presence_users: Vec<String> = Vec::new();
544 let mut y = 0.;
545 let mut list = v_flex().w_full().pb(px(12.));
546 let mut skipped_height = 0.;
547 let directory_state = self.state.clone();
548 if drafting {
549 list = list.child(self.draft_row());
550 y += ROW_HEIGHT;
551 }
552 for section in &sections {
553 list = with_spacer(list, &mut skipped_height);
554 list = list.child(self.section_header(section, cx));
555 y += SECTION_GAP + SECTION_HEIGHT;
556 if section.collapsed {
557 continue;
558 }
559 for item in &section.items {
560 let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to;
561 if on_screen {
562 collect_faces(&item.avatar, &mut faces);
563 presence_users.extend(item.presence_user.clone());
564 }
565 if on_screen {
566 list = with_spacer(list, &mut skipped_height);
567 let is_selected = selected == Some(Selection::Chat(item.id.clone()));
568 let directory = &directory_state.read(cx).directory;
569 list = list.child(self.chat_row(item, is_selected, directory, &menu));
570 } else {
571 skipped_height += ROW_HEIGHT;
572 }
573 y += ROW_HEIGHT;
574 }
575 if section.show_empty_hint {
576 list = with_spacer(list, &mut skipped_height);
577 list = list.child(
578 div()
579 .ml(px(44.))
580 .mr(px(20.))
581 .h(px(HINT_HEIGHT))
582 .flex()
583 .items_center()
584 .text_size(px(12.))
585 .italic()
586 .text_color(theme::text_faint())
587 .child(EMPTY_FOLDER_HINT),
588 );
589 y += HINT_HEIGHT;
590 }
591 }
592 list = with_spacer(list, &mut skipped_height);
593 self.request_lazy_data(faces, presence_users, cx);
594 div()
595 .id("chats-scroll")
596 .flex_1()
597 .min_h_0()
598 .overflow_y_scroll()
599 .track_scroll(&self.chats_scroll)
600 .child(list)
601 .into_any_element()
602 }
603
604 fn request_lazy_data(
605 &self,
606 faces: Vec<String>,
607 presence_users: Vec<String>,
608 cx: &mut Context<Self>,
609 ) {
610 if faces.is_empty() && presence_users.is_empty() {
611 return;
612 }
613 let state = self.state.clone();
614 cx.defer(move |cx| {
615 state.update(cx, |state, cx| {
616 state.request_avatars(faces, cx);
617 state.request_presence(presence_users, cx);
618 });
619 });
620 }
621
622 fn channel_row(
623 &self,
624 channel_id: &str,
625 team: &TeamRecord,
626 channel: &ChannelRecord,
627 selected: bool,
628 ) -> AnyElement {
629 let unread = channel.unread;
630 let state = self.state.clone();
631 let selection = Selection::Channel(channel_id.to_owned());
632 let now = Utc::now();
633 let offset = Local::now().offset().fix();
634 let time = channel
635 .last_message_at
636 .map(|time| {
637 format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset)
638 })
639 .unwrap_or_default();
640 h_flex()
641 .id(SharedString::from(format!("pinned-{channel_id}")))
642 .mx(px(8.))
643 .h(px(PINNED_CHANNEL_HEIGHT))
644 .px(px(8.))
645 .gap(px(10.))
646 .items_center()
647 .rounded(px(8.))
648 .cursor_pointer()
649 .when(selected, |row| row.bg(theme::surface_raised()))
650 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
651 .child(square_avatar(&team.name, &team.id, 28., 7.))
652 .child(
653 v_flex()
654 .flex_1()
655 .min_w_0()
656 .gap(px(1.))
657 .child(
658 h_flex()
659 .gap(px(8.))
660 .items_baseline()
661 .child(
662 div()
663 .flex_1()
664 .min_w_0()
665 .truncate()
666 .text_size(px(13.5))
667 .font_weight(if unread {
668 FontWeight::BOLD
669 } else {
670 FontWeight::NORMAL
671 })
672 .text_color(theme::text())
673 .child(format!("{} / {}", team.name, channel.name)),
674 )
675 .child(
676 div()
677 .flex_none()
678 .text_size(px(11.))
679 .text_color(if unread {
680 theme::accent_text()
681 } else {
682 theme::text_muted()
683 })
684 .child(time),
685 ),
686 )
687 .child(
688 h_flex()
689 .gap(px(8.))
690 .items_center()
691 .child(
692 div()
693 .flex_1()
694 .min_w_0()
695 .truncate()
696 .text_size(px(12.))
697 .text_color(theme::text_muted())
698 .child(team.name.clone()),
699 )
700 .when(unread, |line| line.child(dot(7.))),
701 ),
702 )
703 .on_click(move |_, _, cx| {
704 let selection = selection.clone();
705 state.update(cx, |state, cx| state.select(selection, cx));
706 })
707 .into_any_element()
708 }
709
710 fn plain_header(&self, label: &str) -> impl IntoElement {
711 div()
712 .h(px(SECTION_HEIGHT))
713 .mt(px(SECTION_GAP))
714 .px(px(12.))
715 .flex()
716 .items_center()
717 .text_size(px(12.))
718 .font_weight(FontWeight::SEMIBOLD)
719 .text_color(theme::text_soft())
720 .child(label.to_owned())
721 }
722
723 fn team_block(
724 &self,
725 mut list: Div,
726 entry: &SidebarTeam,
727 selected: Option<&Selection>,
728 cx: &mut Context<Self>,
729 ) -> Div {
730 let team_id = entry.team.id.clone();
731 let expanded = team_holds(entry, selected) || self.expanded_teams.contains(&team_id);
732 let unread = entry.channels.iter().any(|channel| channel.unread);
733 let toggle_id = team_id.clone();
734 list = list.child(
735 h_flex()
736 .id(SharedString::from(format!("team-{team_id}")))
737 .mx(px(8.))
738 .h(px(TEAM_ROW_HEIGHT))
739 .px(px(8.))
740 .gap(px(8.))
741 .items_center()
742 .rounded(px(8.))
743 .cursor_pointer()
744 .hover(|row| row.bg(theme::row_hover()))
745 .child(icon(
746 if expanded {
747 IconName::ChevronDown
748 } else {
749 IconName::ChevronRight
750 },
751 14.,
752 theme::text_muted(),
753 ))
754 .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.))
755 .child(
756 div()
757 .flex_1()
758 .min_w_0()
759 .truncate()
760 .text_size(px(13.5))
761 .font_weight(if unread || expanded {
762 FontWeight::SEMIBOLD
763 } else {
764 FontWeight::NORMAL
765 })
766 .text_color(theme::text())
767 .child(entry.team.name.clone()),
768 )
769 .when(unread && !expanded, |row| row.child(dot(7.)))
770 .on_click(cx.listener(move |this, _, _, cx| {
771 if !this.expanded_teams.remove(&toggle_id) {
772 this.expanded_teams.insert(toggle_id.clone());
773 }
774 cx.notify();
775 })),
776 );
777 if !expanded {
778 return list;
779 }
780 let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id);
781 let is_selected =
782 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
783 for channel in entry
784 .channels
785 .iter()
786 .filter(|channel| !is_hidden(channel) || is_selected(channel))
787 {
788 list = list.child(self.team_channel_row(channel, is_selected(channel)));
789 }
790 let hidden: Vec<&ChannelRecord> = entry
791 .channels
792 .iter()
793 .filter(|channel| is_hidden(channel) && !is_selected(channel))
794 .collect();
795 if hidden.is_empty() {
796 return list;
797 }
798 let revealed = self.revealed_channel_teams.contains(&team_id);
799 let toggle_id = team_id.clone();
800 list = list.child(self.reveal_row(
801 SharedString::from(format!("hidden-channels-{team_id}")),
802 hidden_channels_label(hidden.len()),
803 revealed,
804 40.,
805 cx.listener(move |this, _, _, cx| {
806 if !this.revealed_channel_teams.remove(&toggle_id) {
807 this.revealed_channel_teams.insert(toggle_id.clone());
808 }
809 cx.notify();
810 }),
811 ));
812 if revealed {
813 for channel in hidden {
814 list = list.child(self.team_channel_row(channel, false));
815 }
816 }
817 list
818 }
819
820 fn reveal_row(
821 &self,
822 id: SharedString,
823 label: String,
824 open: bool,
825 indent: f32,
826 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
827 ) -> impl IntoElement {
828 h_flex()
829 .id(id)
830 .mx(px(8.))
831 .h(px(REVEAL_ROW_HEIGHT))
832 .pl(px(indent))
833 .pr(px(8.))
834 .gap(px(8.))
835 .items_center()
836 .rounded(px(8.))
837 .cursor_pointer()
838 .hover(|row| row.bg(theme::row_hover()))
839 .child(icon(
840 if open {
841 IconName::ChevronDown
842 } else {
843 IconName::ChevronRight
844 },
845 12.,
846 theme::text_faint(),
847 ))
848 .child(
849 div()
850 .text_size(px(12.5))
851 .text_color(theme::text_muted())
852 .child(label),
853 )
854 .on_click(on_click)
855 }
856
857 fn team_channel_row(&self, channel: &ChannelRecord, is_selected: bool) -> impl IntoElement {
858 let state = self.state.clone();
859 let selection = Selection::Channel(channel.id.clone());
860 h_flex()
861 .id(SharedString::from(format!("channel-{}", channel.id)))
862 .mx(px(8.))
863 .h(px(TEAM_ROW_HEIGHT))
864 .pl(px(40.))
865 .pr(px(8.))
866 .gap(px(8.))
867 .items_center()
868 .rounded(px(8.))
869 .cursor_pointer()
870 .when(is_selected, |row| row.bg(theme::surface_raised()))
871 .when(!is_selected, |row| {
872 row.hover(|row| row.bg(theme::row_hover()))
873 })
874 .child(
875 div()
876 .w(px(14.))
877 .flex()
878 .justify_center()
879 .text_size(px(15.))
880 .text_color(theme::text_faint())
881 .child("#"),
882 )
883 .child(
884 div()
885 .flex_1()
886 .min_w_0()
887 .truncate()
888 .text_size(px(13.5))
889 .font_weight(if channel.unread {
890 FontWeight::BOLD
891 } else {
892 FontWeight::NORMAL
893 })
894 .text_color(theme::text())
895 .child(channel.name.clone()),
896 )
897 .when(channel.unread, |row| row.child(dot(7.)))
898 .on_click(move |_, _, cx| {
899 let selection = selection.clone();
900 state.update(cx, |state, cx| state.select(selection, cx));
901 })
902 }
903
904 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
905 let (sidebar, pinned_ids, selected) = {
906 let state = self.state.read(cx);
907 (
908 state.sidebar.clone(),
909 state.directory.pinned_channels.clone(),
910 state.selection.clone().filter(|_| !state.new_chat),
911 )
912 };
913 let mut list = v_flex().w_full().pb(px(12.));
914 let pinned: Vec<(&TeamRecord, &ChannelRecord)> = pinned_ids
915 .iter()
916 .filter_map(|id| {
917 sidebar.teams.iter().find_map(|entry| {
918 entry
919 .channels
920 .iter()
921 .find(|channel| &channel.id == id)
922 .map(|channel| (&entry.team, channel))
923 })
924 })
925 .collect();
926 if !pinned.is_empty() {
927 list = list.child(self.plain_header(PINNED_LABEL));
928 for (team, channel) in pinned {
929 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
930 list = list.child(self.channel_row(&channel.id, team, channel, is_selected));
931 }
932 }
933 list = list.child(self.plain_header(TEAMS_LABEL));
934 let (shown_teams, hidden_teams): (Vec<&SidebarTeam>, Vec<&SidebarTeam>) =
935 sidebar.teams.iter().partition(|entry| !entry.hidden);
936 for entry in shown_teams {
937 list = self.team_block(list, entry, selected.as_ref(), cx);
938 }
939 if !hidden_teams.is_empty() {
940 let holds_selection = hidden_teams
941 .iter()
942 .any(|entry| team_holds(entry, selected.as_ref()));
943 let open = self.hidden_teams_open || holds_selection;
944 list = list.child(self.reveal_row(
945 "hidden-teams".into(),
946 format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()),
947 open,
948 8.,
949 cx.listener(|this, _, _, cx| {
950 this.hidden_teams_open = !this.hidden_teams_open;
951 cx.notify();
952 }),
953 ));
954 if open {
955 for entry in hidden_teams {
956 list = self.team_block(list, entry, selected.as_ref(), cx);
957 }
958 }
959 }
960 div()
961 .id("channels-scroll")
962 .flex_1()
963 .min_h_0()
964 .overflow_y_scroll()
965 .track_scroll(&self.channels_scroll)
966 .child(list)
967 .into_any_element()
968 }
969}
970
971fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
972 entry
973 .channels
974 .iter()
975 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
976}
977
978fn hidden_channels_label(count: usize) -> String {
979 if count == 1 {
980 "1 hidden channel".to_owned()
981 } else {
982 format!("{count} hidden channels")
983 }
984}
985
986fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
987 match spec {
988 AvatarSpec::Single(face) => faces.extend(face.user_id.clone()),
989 AvatarSpec::Pair(first, second) => {
990 faces.extend(first.user_id.clone());
991 faces.extend(second.user_id.clone());
992 }
993 }
994}
995
996fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
997 let height = std::mem::take(skipped_height);
998 if height > 0. {
999 list.child(div().h(px(height)).flex_none())
1000 } else {
1001 list
1002 }
1003}
1004
1005impl Render for SidebarView {
1006 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1007 let (unread_chats, channel_dot) = {
1008 let state = self.state.read(cx);
1009 (
1010 unread_chat_count(&state.sidebar.chats),
1011 any_unread_channel(&state.sidebar),
1012 )
1013 };
1014 let window_height = f32::from(window.viewport_size().height);
1015 let body = match self.tab {
1016 SidebarTab::Chats => self.chats_body(window_height, cx),
1017 SidebarTab::Channels => self.channels_body(cx),
1018 };
1019 v_flex()
1020 .w(px(SIDEBAR_WIDTH))
1021 .h_full()
1022 .flex_none()
1023 .bg(theme::surface())
1024 .border_r_1()
1025 .border_color(theme::border())
1026 .overflow_hidden()
1027 .child(self.tab_bar(unread_chats, channel_dot, cx))
1028 .child(body)
1029 }
1030}