| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use chrono::{Local, Offset, Utc}; |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenuItem}; |
| 6 | use gpui_kit::component::{h_flex, v_flex}; |
| 7 | use gpui_kit::prelude::FluentBuilder as _; |
| 8 | use gpui_kit::*; |
| 9 | use store::{ChannelRecord, SidebarTeam, TeamRecord}; |
| 10 | |
| 11 | use super::avatar::{spec_avatar, square_avatar, with_presence}; |
| 12 | use super::widgets::{count_badge, dot, icon, unread_marker}; |
| 13 | use crate::app_state::{AppEvent, AppState, Selection}; |
| 14 | use crate::data::{Directory, FolderKind}; |
| 15 | use crate::format; |
| 16 | use 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 | }; |
| 20 | use crate::theme; |
| 21 | |
| 22 | pub const SIDEBAR_WIDTH: f32 = 312.; |
| 23 | const ROW_HEIGHT: f32 = 54.; |
| 24 | const PINNED_CHANNEL_HEIGHT: f32 = 48.; |
| 25 | const SECTION_HEIGHT: f32 = 30.; |
| 26 | const SECTION_GAP: f32 = 4.; |
| 27 | const HINT_HEIGHT: f32 = 26.; |
| 28 | const FALLBACK_VIEWPORT: f32 = 900.; |
| 29 | const VIEWPORT_MARGIN: f32 = 300.; |
| 30 | const AVATAR_SIZE: f32 = 36.; |
| 31 | const PINNED_LABEL: &str = "Angeheftet"; |
| 32 | const TEAMS_LABEL: &str = "Teams"; |
| 33 | const HIDDEN_TEAMS_LABEL: &str = "Ausgeblendete Teams"; |
| 34 | const TEAM_ROW_HEIGHT: f32 = 40.; |
| 35 | const REVEAL_ROW_HEIGHT: f32 = 32.; |
| 36 | |
| 37 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 38 | enum SidebarTab { |
| 39 | Chats, |
| 40 | Channels, |
| 41 | } |
| 42 | |
| 43 | #[derive(Clone)] |
| 44 | struct DraggedChat { |
| 45 | id: String, |
| 46 | title: String, |
| 47 | } |
| 48 | |
| 49 | struct DragGhost { |
| 50 | title: String, |
| 51 | } |
| 52 | |
| 53 | impl 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)] |
| 69 | struct MenuContext { |
| 70 | state: Entity<AppState>, |
| 71 | folders: Vec<(String, String)>, |
| 72 | favorites_id: Option<String>, |
| 73 | } |
| 74 | |
| 75 | pub 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 | |
| 86 | fn 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 | |
| 98 | fn preview_icon(text: &str) -> Option<IconName> { |
| 99 | match text.rsplit(": ").next()? { |
| 100 | "Bild" => Some(IconName::Image), |
| 101 | "Datei" => Some(IconName::File), |
| 102 | _ => None, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl 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 { "Lösen" } else { "Anheften" }).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 = |
| 397 | popup.submenu("In Ordner verschieben", window, cx, move |submenu, _, _| { |
| 398 | folders.iter().fold(submenu, |submenu, (folder_id, name)| { |
| 399 | let (state, chat_id, folder_id) = |
| 400 | (move_state.clone(), move_id.clone(), folder_id.clone()); |
| 401 | submenu.item(PopupMenuItem::new(name.clone()).on_click( |
| 402 | move |_, _, cx| { |
| 403 | state.update(cx, |state, cx| { |
| 404 | state.move_chat(&chat_id, Some(&folder_id), cx) |
| 405 | }); |
| 406 | }, |
| 407 | )) |
| 408 | }) |
| 409 | }); |
| 410 | } |
| 411 | let read_state = context.state.clone(); |
| 412 | popup.item( |
| 413 | PopupMenuItem::new("Als gelesen markieren") |
| 414 | .disabled(!unread) |
| 415 | .on_click(move |_, _, cx| { |
| 416 | read_state.update(cx, |state, cx| state.mark_chat_read(&chat_id, cx)); |
| 417 | }), |
| 418 | ) |
| 419 | }) |
| 420 | .into_any_element() |
| 421 | } |
| 422 | |
| 423 | fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement { |
| 424 | let (sections, menu) = { |
| 425 | let state = self.state.read(cx); |
| 426 | let now = Utc::now(); |
| 427 | let offset = Local::now().offset().fix(); |
| 428 | let input = SectionInput { |
| 429 | chats: &state.sidebar.chats, |
| 430 | directory: &state.directory, |
| 431 | collapsed: &state.collapsed, |
| 432 | now, |
| 433 | offset, |
| 434 | }; |
| 435 | let sections = build_sections(&input); |
| 436 | let menu = MenuContext { |
| 437 | state: self.state.clone(), |
| 438 | folders: state |
| 439 | .directory |
| 440 | .folders |
| 441 | .iter() |
| 442 | .filter(|folder| folder.kind == FolderKind::UserCreated) |
| 443 | .map(|folder| (folder.id.clone(), folder.name.clone())) |
| 444 | .collect(), |
| 445 | favorites_id: state.directory.favorites().map(|folder| folder.id.clone()), |
| 446 | }; |
| 447 | (sections, menu) |
| 448 | }; |
| 449 | |
| 450 | let top = -f32::from(self.chats_scroll.offset().y); |
| 451 | let measured: f32 = f32::from(self.chats_scroll.bounds().size.height); |
| 452 | let height = if measured > 1. { |
| 453 | measured |
| 454 | } else { |
| 455 | window_height.max(FALLBACK_VIEWPORT) |
| 456 | }; |
| 457 | let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN); |
| 458 | |
| 459 | let selected = self.state.read(cx).selection.clone(); |
| 460 | let mut faces: Vec<String> = Vec::new(); |
| 461 | let mut presence_users: Vec<String> = Vec::new(); |
| 462 | let mut y = 0.; |
| 463 | let mut list = v_flex().w_full().pb(px(12.)); |
| 464 | let mut skipped_height = 0.; |
| 465 | let directory_state = self.state.clone(); |
| 466 | for section in §ions { |
| 467 | list = with_spacer(list, &mut skipped_height); |
| 468 | list = list.child(self.section_header(section, cx)); |
| 469 | y += SECTION_GAP + SECTION_HEIGHT; |
| 470 | if section.collapsed { |
| 471 | continue; |
| 472 | } |
| 473 | for item in §ion.items { |
| 474 | let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to; |
| 475 | if on_screen { |
| 476 | collect_faces(&item.avatar, &mut faces); |
| 477 | presence_users.extend(item.presence_user.clone()); |
| 478 | } |
| 479 | if on_screen { |
| 480 | list = with_spacer(list, &mut skipped_height); |
| 481 | let is_selected = selected == Some(Selection::Chat(item.id.clone())); |
| 482 | let directory = &directory_state.read(cx).directory; |
| 483 | list = list.child(self.chat_row(item, is_selected, directory, &menu)); |
| 484 | } else { |
| 485 | skipped_height += ROW_HEIGHT; |
| 486 | } |
| 487 | y += ROW_HEIGHT; |
| 488 | } |
| 489 | if section.show_empty_hint { |
| 490 | list = with_spacer(list, &mut skipped_height); |
| 491 | list = list.child( |
| 492 | div() |
| 493 | .ml(px(44.)) |
| 494 | .mr(px(20.)) |
| 495 | .h(px(HINT_HEIGHT)) |
| 496 | .flex() |
| 497 | .items_center() |
| 498 | .text_size(px(12.)) |
| 499 | .italic() |
| 500 | .text_color(theme::text_faint()) |
| 501 | .child(EMPTY_FOLDER_HINT), |
| 502 | ); |
| 503 | y += HINT_HEIGHT; |
| 504 | } |
| 505 | } |
| 506 | list = with_spacer(list, &mut skipped_height); |
| 507 | self.request_lazy_data(faces, presence_users, cx); |
| 508 | div() |
| 509 | .id("chats-scroll") |
| 510 | .flex_1() |
| 511 | .min_h_0() |
| 512 | .overflow_y_scroll() |
| 513 | .track_scroll(&self.chats_scroll) |
| 514 | .child(list) |
| 515 | .into_any_element() |
| 516 | } |
| 517 | |
| 518 | fn request_lazy_data( |
| 519 | &self, |
| 520 | faces: Vec<String>, |
| 521 | presence_users: Vec<String>, |
| 522 | cx: &mut Context<Self>, |
| 523 | ) { |
| 524 | if faces.is_empty() && presence_users.is_empty() { |
| 525 | return; |
| 526 | } |
| 527 | let state = self.state.clone(); |
| 528 | cx.defer(move |cx| { |
| 529 | state.update(cx, |state, cx| { |
| 530 | state.request_avatars(faces, cx); |
| 531 | state.request_presence(presence_users, cx); |
| 532 | }); |
| 533 | }); |
| 534 | } |
| 535 | |
| 536 | fn channel_row( |
| 537 | &self, |
| 538 | channel_id: &str, |
| 539 | team: &TeamRecord, |
| 540 | channel: &ChannelRecord, |
| 541 | selected: bool, |
| 542 | ) -> AnyElement { |
| 543 | let unread = channel.unread; |
| 544 | let state = self.state.clone(); |
| 545 | let selection = Selection::Channel(channel_id.to_owned()); |
| 546 | let now = Utc::now(); |
| 547 | let offset = Local::now().offset().fix(); |
| 548 | let time = channel |
| 549 | .last_message_at |
| 550 | .map(|time| { |
| 551 | format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset) |
| 552 | }) |
| 553 | .unwrap_or_default(); |
| 554 | h_flex() |
| 555 | .id(SharedString::from(format!("pinned-{channel_id}"))) |
| 556 | .mx(px(8.)) |
| 557 | .h(px(PINNED_CHANNEL_HEIGHT)) |
| 558 | .px(px(8.)) |
| 559 | .gap(px(10.)) |
| 560 | .items_center() |
| 561 | .rounded(px(8.)) |
| 562 | .cursor_pointer() |
| 563 | .when(selected, |row| row.bg(theme::surface_raised())) |
| 564 | .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover()))) |
| 565 | .child(square_avatar(&team.name, &team.id, 28., 7.)) |
| 566 | .child( |
| 567 | v_flex() |
| 568 | .flex_1() |
| 569 | .min_w_0() |
| 570 | .gap(px(1.)) |
| 571 | .child( |
| 572 | h_flex() |
| 573 | .gap(px(8.)) |
| 574 | .items_baseline() |
| 575 | .child( |
| 576 | div() |
| 577 | .flex_1() |
| 578 | .min_w_0() |
| 579 | .truncate() |
| 580 | .text_size(px(13.5)) |
| 581 | .font_weight(if unread { |
| 582 | FontWeight::BOLD |
| 583 | } else { |
| 584 | FontWeight::NORMAL |
| 585 | }) |
| 586 | .text_color(theme::text()) |
| 587 | .child(format!("{} / {}", team.name, channel.name)), |
| 588 | ) |
| 589 | .child( |
| 590 | div() |
| 591 | .flex_none() |
| 592 | .text_size(px(11.)) |
| 593 | .text_color(if unread { |
| 594 | theme::accent_text() |
| 595 | } else { |
| 596 | theme::text_muted() |
| 597 | }) |
| 598 | .child(time), |
| 599 | ), |
| 600 | ) |
| 601 | .child( |
| 602 | h_flex() |
| 603 | .gap(px(8.)) |
| 604 | .items_center() |
| 605 | .child( |
| 606 | div() |
| 607 | .flex_1() |
| 608 | .min_w_0() |
| 609 | .truncate() |
| 610 | .text_size(px(12.)) |
| 611 | .text_color(theme::text_muted()) |
| 612 | .child(team.name.clone()), |
| 613 | ) |
| 614 | .when(unread, |line| line.child(dot(7.))), |
| 615 | ), |
| 616 | ) |
| 617 | .on_click(move |_, _, cx| { |
| 618 | let selection = selection.clone(); |
| 619 | state.update(cx, |state, cx| state.select(selection, cx)); |
| 620 | }) |
| 621 | .into_any_element() |
| 622 | } |
| 623 | |
| 624 | fn plain_header(&self, label: &str) -> impl IntoElement { |
| 625 | div() |
| 626 | .h(px(SECTION_HEIGHT)) |
| 627 | .mt(px(SECTION_GAP)) |
| 628 | .px(px(12.)) |
| 629 | .flex() |
| 630 | .items_center() |
| 631 | .text_size(px(12.)) |
| 632 | .font_weight(FontWeight::SEMIBOLD) |
| 633 | .text_color(theme::text_soft()) |
| 634 | .child(label.to_owned()) |
| 635 | } |
| 636 | |
| 637 | fn team_block( |
| 638 | &self, |
| 639 | mut list: Div, |
| 640 | entry: &SidebarTeam, |
| 641 | selected: Option<&Selection>, |
| 642 | cx: &mut Context<Self>, |
| 643 | ) -> Div { |
| 644 | let team_id = entry.team.id.clone(); |
| 645 | let expanded = team_holds(entry, selected) || self.expanded_teams.contains(&team_id); |
| 646 | let unread = entry.channels.iter().any(|channel| channel.unread); |
| 647 | let toggle_id = team_id.clone(); |
| 648 | list = list.child( |
| 649 | h_flex() |
| 650 | .id(SharedString::from(format!("team-{team_id}"))) |
| 651 | .mx(px(8.)) |
| 652 | .h(px(TEAM_ROW_HEIGHT)) |
| 653 | .px(px(8.)) |
| 654 | .gap(px(8.)) |
| 655 | .items_center() |
| 656 | .rounded(px(8.)) |
| 657 | .cursor_pointer() |
| 658 | .hover(|row| row.bg(theme::row_hover())) |
| 659 | .child(icon( |
| 660 | if expanded { |
| 661 | IconName::ChevronDown |
| 662 | } else { |
| 663 | IconName::ChevronRight |
| 664 | }, |
| 665 | 14., |
| 666 | theme::text_muted(), |
| 667 | )) |
| 668 | .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.)) |
| 669 | .child( |
| 670 | div() |
| 671 | .flex_1() |
| 672 | .min_w_0() |
| 673 | .truncate() |
| 674 | .text_size(px(13.5)) |
| 675 | .font_weight(if unread || expanded { |
| 676 | FontWeight::SEMIBOLD |
| 677 | } else { |
| 678 | FontWeight::NORMAL |
| 679 | }) |
| 680 | .text_color(theme::text()) |
| 681 | .child(entry.team.name.clone()), |
| 682 | ) |
| 683 | .when(unread && !expanded, |row| row.child(dot(7.))) |
| 684 | .on_click(cx.listener(move |this, _, _, cx| { |
| 685 | if !this.expanded_teams.remove(&toggle_id) { |
| 686 | this.expanded_teams.insert(toggle_id.clone()); |
| 687 | } |
| 688 | cx.notify(); |
| 689 | })), |
| 690 | ); |
| 691 | if !expanded { |
| 692 | return list; |
| 693 | } |
| 694 | let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id); |
| 695 | let is_selected = |
| 696 | |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone())); |
| 697 | for channel in entry |
| 698 | .channels |
| 699 | .iter() |
| 700 | .filter(|channel| !is_hidden(channel) || is_selected(channel)) |
| 701 | { |
| 702 | list = list.child(self.team_channel_row(channel, is_selected(channel))); |
| 703 | } |
| 704 | let hidden: Vec<&ChannelRecord> = entry |
| 705 | .channels |
| 706 | .iter() |
| 707 | .filter(|channel| is_hidden(channel) && !is_selected(channel)) |
| 708 | .collect(); |
| 709 | if hidden.is_empty() { |
| 710 | return list; |
| 711 | } |
| 712 | let revealed = self.revealed_channel_teams.contains(&team_id); |
| 713 | let toggle_id = team_id.clone(); |
| 714 | list = list.child(self.reveal_row( |
| 715 | SharedString::from(format!("hidden-channels-{team_id}")), |
| 716 | hidden_channels_label(hidden.len()), |
| 717 | revealed, |
| 718 | 40., |
| 719 | cx.listener(move |this, _, _, cx| { |
| 720 | if !this.revealed_channel_teams.remove(&toggle_id) { |
| 721 | this.revealed_channel_teams.insert(toggle_id.clone()); |
| 722 | } |
| 723 | cx.notify(); |
| 724 | }), |
| 725 | )); |
| 726 | if revealed { |
| 727 | for channel in hidden { |
| 728 | list = list.child(self.team_channel_row(channel, false)); |
| 729 | } |
| 730 | } |
| 731 | list |
| 732 | } |
| 733 | |
| 734 | fn reveal_row( |
| 735 | &self, |
| 736 | id: SharedString, |
| 737 | label: String, |
| 738 | open: bool, |
| 739 | indent: f32, |
| 740 | on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 741 | ) -> impl IntoElement { |
| 742 | h_flex() |
| 743 | .id(id) |
| 744 | .mx(px(8.)) |
| 745 | .h(px(REVEAL_ROW_HEIGHT)) |
| 746 | .pl(px(indent)) |
| 747 | .pr(px(8.)) |
| 748 | .gap(px(8.)) |
| 749 | .items_center() |
| 750 | .rounded(px(8.)) |
| 751 | .cursor_pointer() |
| 752 | .hover(|row| row.bg(theme::row_hover())) |
| 753 | .child(icon( |
| 754 | if open { |
| 755 | IconName::ChevronDown |
| 756 | } else { |
| 757 | IconName::ChevronRight |
| 758 | }, |
| 759 | 12., |
| 760 | theme::text_faint(), |
| 761 | )) |
| 762 | .child( |
| 763 | div() |
| 764 | .text_size(px(12.5)) |
| 765 | .text_color(theme::text_muted()) |
| 766 | .child(label), |
| 767 | ) |
| 768 | .on_click(on_click) |
| 769 | } |
| 770 | |
| 771 | fn team_channel_row(&self, channel: &ChannelRecord, is_selected: bool) -> impl IntoElement { |
| 772 | let state = self.state.clone(); |
| 773 | let selection = Selection::Channel(channel.id.clone()); |
| 774 | h_flex() |
| 775 | .id(SharedString::from(format!("channel-{}", channel.id))) |
| 776 | .mx(px(8.)) |
| 777 | .h(px(TEAM_ROW_HEIGHT)) |
| 778 | .pl(px(40.)) |
| 779 | .pr(px(8.)) |
| 780 | .gap(px(8.)) |
| 781 | .items_center() |
| 782 | .rounded(px(8.)) |
| 783 | .cursor_pointer() |
| 784 | .when(is_selected, |row| row.bg(theme::surface_raised())) |
| 785 | .when(!is_selected, |row| { |
| 786 | row.hover(|row| row.bg(theme::row_hover())) |
| 787 | }) |
| 788 | .child( |
| 789 | div() |
| 790 | .w(px(14.)) |
| 791 | .flex() |
| 792 | .justify_center() |
| 793 | .text_size(px(15.)) |
| 794 | .text_color(theme::text_faint()) |
| 795 | .child("#"), |
| 796 | ) |
| 797 | .child( |
| 798 | div() |
| 799 | .flex_1() |
| 800 | .min_w_0() |
| 801 | .truncate() |
| 802 | .text_size(px(13.5)) |
| 803 | .font_weight(if channel.unread { |
| 804 | FontWeight::BOLD |
| 805 | } else { |
| 806 | FontWeight::NORMAL |
| 807 | }) |
| 808 | .text_color(theme::text()) |
| 809 | .child(channel.name.clone()), |
| 810 | ) |
| 811 | .when(channel.unread, |row| row.child(dot(7.))) |
| 812 | .on_click(move |_, _, cx| { |
| 813 | let selection = selection.clone(); |
| 814 | state.update(cx, |state, cx| state.select(selection, cx)); |
| 815 | }) |
| 816 | } |
| 817 | |
| 818 | fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement { |
| 819 | let (sidebar, pinned_ids, selected) = { |
| 820 | let state = self.state.read(cx); |
| 821 | ( |
| 822 | state.sidebar.clone(), |
| 823 | state.directory.pinned_channels.clone(), |
| 824 | state.selection.clone(), |
| 825 | ) |
| 826 | }; |
| 827 | let mut list = v_flex().w_full().pb(px(12.)); |
| 828 | let pinned: Vec<(&TeamRecord, &ChannelRecord)> = pinned_ids |
| 829 | .iter() |
| 830 | .filter_map(|id| { |
| 831 | sidebar.teams.iter().find_map(|entry| { |
| 832 | entry |
| 833 | .channels |
| 834 | .iter() |
| 835 | .find(|channel| &channel.id == id) |
| 836 | .map(|channel| (&entry.team, channel)) |
| 837 | }) |
| 838 | }) |
| 839 | .collect(); |
| 840 | if !pinned.is_empty() { |
| 841 | list = list.child(self.plain_header(PINNED_LABEL)); |
| 842 | for (team, channel) in pinned { |
| 843 | let is_selected = selected == Some(Selection::Channel(channel.id.clone())); |
| 844 | list = list.child(self.channel_row(&channel.id, team, channel, is_selected)); |
| 845 | } |
| 846 | } |
| 847 | list = list.child(self.plain_header(TEAMS_LABEL)); |
| 848 | let (shown_teams, hidden_teams): (Vec<&SidebarTeam>, Vec<&SidebarTeam>) = |
| 849 | sidebar.teams.iter().partition(|entry| !entry.hidden); |
| 850 | for entry in shown_teams { |
| 851 | list = self.team_block(list, entry, selected.as_ref(), cx); |
| 852 | } |
| 853 | if !hidden_teams.is_empty() { |
| 854 | let holds_selection = hidden_teams |
| 855 | .iter() |
| 856 | .any(|entry| team_holds(entry, selected.as_ref())); |
| 857 | let open = self.hidden_teams_open || holds_selection; |
| 858 | list = list.child(self.reveal_row( |
| 859 | "hidden-teams".into(), |
| 860 | format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()), |
| 861 | open, |
| 862 | 8., |
| 863 | cx.listener(|this, _, _, cx| { |
| 864 | this.hidden_teams_open = !this.hidden_teams_open; |
| 865 | cx.notify(); |
| 866 | }), |
| 867 | )); |
| 868 | if open { |
| 869 | for entry in hidden_teams { |
| 870 | list = self.team_block(list, entry, selected.as_ref(), cx); |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | div() |
| 875 | .id("channels-scroll") |
| 876 | .flex_1() |
| 877 | .min_h_0() |
| 878 | .overflow_y_scroll() |
| 879 | .track_scroll(&self.channels_scroll) |
| 880 | .child(list) |
| 881 | .into_any_element() |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool { |
| 886 | entry |
| 887 | .channels |
| 888 | .iter() |
| 889 | .any(|channel| selected == Some(&Selection::Channel(channel.id.clone()))) |
| 890 | } |
| 891 | |
| 892 | fn hidden_channels_label(count: usize) -> String { |
| 893 | if count == 1 { |
| 894 | "1 ausgeblendeter Channel".to_owned() |
| 895 | } else { |
| 896 | format!("{count} ausgeblendete Channels") |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) { |
| 901 | match spec { |
| 902 | AvatarSpec::Single(face) => faces.extend(face.user_id.clone()), |
| 903 | AvatarSpec::Pair(first, second) => { |
| 904 | faces.extend(first.user_id.clone()); |
| 905 | faces.extend(second.user_id.clone()); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | fn with_spacer(list: Div, skipped_height: &mut f32) -> Div { |
| 911 | let height = std::mem::take(skipped_height); |
| 912 | if height > 0. { |
| 913 | list.child(div().h(px(height)).flex_none()) |
| 914 | } else { |
| 915 | list |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | impl Render for SidebarView { |
| 920 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 921 | let (unread_chats, channel_dot) = { |
| 922 | let state = self.state.read(cx); |
| 923 | ( |
| 924 | unread_chat_count(&state.sidebar.chats), |
| 925 | any_unread_channel(&state.sidebar), |
| 926 | ) |
| 927 | }; |
| 928 | let window_height = f32::from(window.viewport_size().height); |
| 929 | let body = match self.tab { |
| 930 | SidebarTab::Chats => self.chats_body(window_height, cx), |
| 931 | SidebarTab::Channels => self.channels_body(cx), |
| 932 | }; |
| 933 | v_flex() |
| 934 | .w(px(SIDEBAR_WIDTH)) |
| 935 | .h_full() |
| 936 | .flex_none() |
| 937 | .bg(theme::surface()) |
| 938 | .border_r_1() |
| 939 | .border_color(theme::border()) |
| 940 | .overflow_hidden() |
| 941 | .child(self.tab_bar(unread_chats, channel_dot, cx)) |
| 942 | .child(body) |
| 943 | } |
| 944 | } |