BlueGit

ict rusty-teams

Files

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

sidebar.rs

sidebar.rs 37.5 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, symbol, 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 followed: bool,
629 ) -> AnyElement {
630 let unread = channel.unread;
631 let state = self.state.clone();
632 let menu_state = self.state.clone();
633 let menu_channel_id = channel_id.to_owned();
634 let selection = Selection::Channel(channel_id.to_owned());
635 let now = Utc::now();
636 let offset = Local::now().offset().fix();
637 let time = channel
638 .last_message_at
639 .map(|time| {
640 format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset)
641 })
642 .unwrap_or_default();
643 h_flex()
644 .id(SharedString::from(format!("pinned-{channel_id}")))
645 .mx(px(8.))
646 .h(px(PINNED_CHANNEL_HEIGHT))
647 .px(px(8.))
648 .gap(px(10.))
649 .items_center()
650 .rounded(px(8.))
651 .cursor_pointer()
652 .when(selected, |row| row.bg(theme::surface_raised()))
653 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
654 .child(square_avatar(&team.name, &team.id, 28., 7.))
655 .child(
656 v_flex()
657 .flex_1()
658 .min_w_0()
659 .gap(px(1.))
660 .child(
661 h_flex()
662 .gap(px(8.))
663 .items_baseline()
664 .child(
665 div()
666 .flex_1()
667 .min_w_0()
668 .truncate()
669 .text_size(px(13.5))
670 .font_weight(if unread {
671 FontWeight::BOLD
672 } else {
673 FontWeight::NORMAL
674 })
675 .text_color(theme::text())
676 .child(format!("{} / {}", team.name, channel.name)),
677 )
678 .when(followed, |line| line.child(followed_bell()))
679 .child(
680 div()
681 .flex_none()
682 .text_size(px(11.))
683 .text_color(if unread {
684 theme::accent_text()
685 } else {
686 theme::text_muted()
687 })
688 .child(time),
689 ),
690 )
691 .child(
692 h_flex()
693 .gap(px(8.))
694 .items_center()
695 .child(
696 div()
697 .flex_1()
698 .min_w_0()
699 .truncate()
700 .text_size(px(12.))
701 .text_color(theme::text_muted())
702 .child(team.name.clone()),
703 )
704 .when(unread, |line| line.child(dot(7.))),
705 ),
706 )
707 .on_click(move |_, _, cx| {
708 let selection = selection.clone();
709 state.update(cx, |state, cx| state.select(selection, cx));
710 })
711 .context_menu(move |popup, _, _| {
712 popup.item(follow_item(
713 menu_state.clone(),
714 menu_channel_id.clone(),
715 followed,
716 ))
717 })
718 .into_any_element()
719 }
720
721 fn plain_header(&self, label: &str) -> impl IntoElement {
722 div()
723 .h(px(SECTION_HEIGHT))
724 .mt(px(SECTION_GAP))
725 .px(px(12.))
726 .flex()
727 .items_center()
728 .text_size(px(12.))
729 .font_weight(FontWeight::SEMIBOLD)
730 .text_color(theme::text_soft())
731 .child(label.to_owned())
732 }
733
734 fn team_block(
735 &self,
736 mut list: Div,
737 entry: &SidebarTeam,
738 selected: Option<&Selection>,
739 cx: &mut Context<Self>,
740 ) -> Div {
741 let followed = self.state.read(cx).followed_channels.clone();
742 let team_id = entry.team.id.clone();
743 let expanded = team_holds(entry, selected) || self.expanded_teams.contains(&team_id);
744 let unread = entry.channels.iter().any(|channel| channel.unread);
745 let toggle_id = team_id.clone();
746 list = list.child(
747 h_flex()
748 .id(SharedString::from(format!("team-{team_id}")))
749 .mx(px(8.))
750 .h(px(TEAM_ROW_HEIGHT))
751 .px(px(8.))
752 .gap(px(8.))
753 .items_center()
754 .rounded(px(8.))
755 .cursor_pointer()
756 .hover(|row| row.bg(theme::row_hover()))
757 .child(icon(
758 if expanded {
759 IconName::ChevronDown
760 } else {
761 IconName::ChevronRight
762 },
763 14.,
764 theme::text_muted(),
765 ))
766 .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.))
767 .child(
768 div()
769 .flex_1()
770 .min_w_0()
771 .truncate()
772 .text_size(px(13.5))
773 .font_weight(if unread || expanded {
774 FontWeight::SEMIBOLD
775 } else {
776 FontWeight::NORMAL
777 })
778 .text_color(theme::text())
779 .child(entry.team.name.clone()),
780 )
781 .when(unread && !expanded, |row| row.child(dot(7.)))
782 .on_click(cx.listener(move |this, _, _, cx| {
783 if !this.expanded_teams.remove(&toggle_id) {
784 this.expanded_teams.insert(toggle_id.clone());
785 }
786 cx.notify();
787 })),
788 );
789 if !expanded {
790 return list;
791 }
792 let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id);
793 let is_selected =
794 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
795 for channel in entry
796 .channels
797 .iter()
798 .filter(|channel| !is_hidden(channel) || is_selected(channel))
799 {
800 list = list.child(self.team_channel_row(
801 channel,
802 is_selected(channel),
803 followed.contains(&channel.id),
804 ));
805 }
806 let hidden: Vec<&ChannelRecord> = entry
807 .channels
808 .iter()
809 .filter(|channel| is_hidden(channel) && !is_selected(channel))
810 .collect();
811 if hidden.is_empty() {
812 return list;
813 }
814 let revealed = self.revealed_channel_teams.contains(&team_id);
815 let toggle_id = team_id.clone();
816 list = list.child(self.reveal_row(
817 SharedString::from(format!("hidden-channels-{team_id}")),
818 hidden_channels_label(hidden.len()),
819 revealed,
820 40.,
821 cx.listener(move |this, _, _, cx| {
822 if !this.revealed_channel_teams.remove(&toggle_id) {
823 this.revealed_channel_teams.insert(toggle_id.clone());
824 }
825 cx.notify();
826 }),
827 ));
828 if revealed {
829 for channel in hidden {
830 list = list.child(self.team_channel_row(
831 channel,
832 false,
833 followed.contains(&channel.id),
834 ));
835 }
836 }
837 list
838 }
839
840 fn reveal_row(
841 &self,
842 id: SharedString,
843 label: String,
844 open: bool,
845 indent: f32,
846 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
847 ) -> impl IntoElement {
848 h_flex()
849 .id(id)
850 .mx(px(8.))
851 .h(px(REVEAL_ROW_HEIGHT))
852 .pl(px(indent))
853 .pr(px(8.))
854 .gap(px(8.))
855 .items_center()
856 .rounded(px(8.))
857 .cursor_pointer()
858 .hover(|row| row.bg(theme::row_hover()))
859 .child(icon(
860 if open {
861 IconName::ChevronDown
862 } else {
863 IconName::ChevronRight
864 },
865 12.,
866 theme::text_faint(),
867 ))
868 .child(
869 div()
870 .text_size(px(12.5))
871 .text_color(theme::text_muted())
872 .child(label),
873 )
874 .on_click(on_click)
875 }
876
877 fn team_channel_row(
878 &self,
879 channel: &ChannelRecord,
880 is_selected: bool,
881 followed: bool,
882 ) -> impl IntoElement {
883 let state = self.state.clone();
884 let menu_state = self.state.clone();
885 let menu_channel_id = channel.id.clone();
886 let selection = Selection::Channel(channel.id.clone());
887 h_flex()
888 .id(SharedString::from(format!("channel-{}", channel.id)))
889 .mx(px(8.))
890 .h(px(TEAM_ROW_HEIGHT))
891 .pl(px(40.))
892 .pr(px(8.))
893 .gap(px(8.))
894 .items_center()
895 .rounded(px(8.))
896 .cursor_pointer()
897 .when(is_selected, |row| row.bg(theme::surface_raised()))
898 .when(!is_selected, |row| {
899 row.hover(|row| row.bg(theme::row_hover()))
900 })
901 .child(
902 div()
903 .w(px(14.))
904 .flex()
905 .justify_center()
906 .text_size(px(15.))
907 .text_color(theme::text_faint())
908 .child("#"),
909 )
910 .child(
911 div()
912 .flex_1()
913 .min_w_0()
914 .truncate()
915 .text_size(px(13.5))
916 .font_weight(if channel.unread {
917 FontWeight::BOLD
918 } else {
919 FontWeight::NORMAL
920 })
921 .text_color(theme::text())
922 .child(channel.name.clone()),
923 )
924 .when(followed, |row| row.child(followed_bell()))
925 .when(channel.unread, |row| row.child(dot(7.)))
926 .on_click(move |_, _, cx| {
927 let selection = selection.clone();
928 state.update(cx, |state, cx| state.select(selection, cx));
929 })
930 .context_menu(move |popup, _, _| {
931 popup.item(follow_item(
932 menu_state.clone(),
933 menu_channel_id.clone(),
934 followed,
935 ))
936 })
937 }
938
939 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
940 let (sidebar, pinned_ids, followed, selected) = {
941 let state = self.state.read(cx);
942 (
943 state.sidebar.clone(),
944 state.directory.pinned_channels.clone(),
945 state.followed_channels.clone(),
946 state.selection.clone().filter(|_| !state.new_chat),
947 )
948 };
949 let mut list = v_flex().w_full().pb(px(12.));
950 let pinned: Vec<(&TeamRecord, &ChannelRecord)> = pinned_ids
951 .iter()
952 .filter_map(|id| {
953 sidebar.teams.iter().find_map(|entry| {
954 entry
955 .channels
956 .iter()
957 .find(|channel| &channel.id == id)
958 .map(|channel| (&entry.team, channel))
959 })
960 })
961 .collect();
962 if !pinned.is_empty() {
963 list = list.child(self.plain_header(PINNED_LABEL));
964 for (team, channel) in pinned {
965 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
966 list = list.child(self.channel_row(
967 &channel.id,
968 team,
969 channel,
970 is_selected,
971 followed.contains(&channel.id),
972 ));
973 }
974 }
975 list = list.child(self.plain_header(TEAMS_LABEL));
976 let (shown_teams, hidden_teams): (Vec<&SidebarTeam>, Vec<&SidebarTeam>) =
977 sidebar.teams.iter().partition(|entry| !entry.hidden);
978 for entry in shown_teams {
979 list = self.team_block(list, entry, selected.as_ref(), cx);
980 }
981 if !hidden_teams.is_empty() {
982 let holds_selection = hidden_teams
983 .iter()
984 .any(|entry| team_holds(entry, selected.as_ref()));
985 let open = self.hidden_teams_open || holds_selection;
986 list = list.child(self.reveal_row(
987 "hidden-teams".into(),
988 format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()),
989 open,
990 8.,
991 cx.listener(|this, _, _, cx| {
992 this.hidden_teams_open = !this.hidden_teams_open;
993 cx.notify();
994 }),
995 ));
996 if open {
997 for entry in hidden_teams {
998 list = self.team_block(list, entry, selected.as_ref(), cx);
999 }
1000 }
1001 }
1002 div()
1003 .id("channels-scroll")
1004 .flex_1()
1005 .min_h_0()
1006 .overflow_y_scroll()
1007 .track_scroll(&self.channels_scroll)
1008 .child(list)
1009 .into_any_element()
1010 }
1011}
1012
1013fn followed_bell() -> impl IntoElement {
1014 symbol("notifications", 12., theme::text_muted())
1015}
1016
1017fn follow_item(state: Entity<AppState>, channel_id: String, followed: bool) -> PopupMenuItem {
1018 PopupMenuItem::new("Notify on all posts")
1019 .checked(followed)
1020 .on_click(move |_, _, cx| {
1021 state.update(cx, |state, cx| {
1022 state.toggle_followed_channel(&channel_id, cx)
1023 });
1024 })
1025}
1026
1027fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
1028 entry
1029 .channels
1030 .iter()
1031 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
1032}
1033
1034fn hidden_channels_label(count: usize) -> String {
1035 if count == 1 {
1036 "1 hidden channel".to_owned()
1037 } else {
1038 format!("{count} hidden channels")
1039 }
1040}
1041
1042fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
1043 match spec {
1044 AvatarSpec::Single(face) => faces.extend(face.user_id.clone()),
1045 AvatarSpec::Pair(first, second) => {
1046 faces.extend(first.user_id.clone());
1047 faces.extend(second.user_id.clone());
1048 }
1049 }
1050}
1051
1052fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
1053 let height = std::mem::take(skipped_height);
1054 if height > 0. {
1055 list.child(div().h(px(height)).flex_none())
1056 } else {
1057 list
1058 }
1059}
1060
1061impl Render for SidebarView {
1062 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1063 let (unread_chats, channel_dot) = {
1064 let state = self.state.read(cx);
1065 (
1066 unread_chat_count(&state.sidebar.chats),
1067 any_unread_channel(&state.sidebar),
1068 )
1069 };
1070 let window_height = f32::from(window.viewport_size().height);
1071 let body = match self.tab {
1072 SidebarTab::Chats => self.chats_body(window_height, cx),
1073 SidebarTab::Channels => self.channels_body(cx),
1074 };
1075 v_flex()
1076 .w(px(SIDEBAR_WIDTH))
1077 .h_full()
1078 .flex_none()
1079 .bg(theme::surface())
1080 .border_r_1()
1081 .border_color(theme::border())
1082 .overflow_hidden()
1083 .child(self.tab_bar(unread_chats, channel_dot, cx))
1084 .child(body)
1085 }
1086}