BlueGit

ict rusty-teams

Files

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

sidebar.rs

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