BlueGit

ict rusty-teams

Files

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

shared_tab.rs

shared_tab.rs 20.1 KiB Download Raw
1use std::rc::Rc;
2
3use chrono::{Local, Offset as _};
4use gpui_kit::component::scroll::ScrollableElement as _;
5use gpui_kit::assets::IconName;
6use gpui_kit::component::{
7 button::{Button, ButtonVariants as _},
8 h_flex,
9 input::{Input, InputState},
10 menu::{ContextMenuExt as _, PopupMenuItem},
11 v_flex,
12};
13use gpui_kit::prelude::FluentBuilder as _;
14use gpui_kit::*;
15use teams_core::{DriveEntry, FileKind};
16
17use super::attachments::file_badge;
18use super::widgets::{icon, menu_icon};
19use crate::channel_files::{
20 LibraryPath, MessageFilter, SharedContent, SharedItem, grouped_items, size_label,
21};
22use crate::downloads::{DownloadState, percent_label};
23use crate::format;
24use crate::theme;
25
26const SIDE_PADDING: f32 = 20.;
27const ROW_HEIGHT: f32 = 40.;
28const BADGE_SIZE: f32 = 26.;
29const MODIFIED_WIDTH: f32 = 110.;
30const MODIFIED_BY_WIDTH: f32 = 160.;
31const SIZE_WIDTH: f32 = 90.;
32const SKELETON_ROWS: usize = 6;
33const NEW_FOLDER_WIDTH: f32 = 220.;
34
35pub const EMPTY_LIBRARY: &str = "No files yet. Upload or drop files here.";
36pub const EMPTY_MESSAGES: &str = "No files or links shared in this channel yet.";
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum SharedScope {
40 Library,
41 Messages,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum SharedCommand {
46 Scope(SharedScope),
47 Filter(MessageFilter),
48 Crumb(usize),
49 Open(usize),
50 OpenInBrowser(usize),
51 Download(usize),
52 CopyLink(usize),
53 Retry,
54 ToggleNewFolder,
55 Upload,
56 OpenSharePoint,
57 OpenItem(usize),
58 JumpToPost(usize),
59}
60
61pub type SharedRun = Rc<dyn Fn(SharedCommand, &mut Window, &mut App)>;
62
63pub enum Listing {
64 Loading,
65 Failed(String),
66 Ready(Vec<DriveEntry>),
67}
68
69pub struct LibraryView<'a> {
70 pub path: Option<&'a LibraryPath>,
71 pub listing: &'a Listing,
72 pub new_folder: Option<&'a Entity<InputState>>,
73 pub status: Option<&'a str>,
74 pub download_states: Vec<Option<DownloadState>>,
75}
76
77pub struct MessagesView<'a> {
78 pub items: &'a [SharedItem],
79 pub filter: MessageFilter,
80 pub download_states: Vec<Option<DownloadState>>,
81}
82
83pub fn download_label(state: Option<&DownloadState>) -> Option<String> {
84 match state? {
85 DownloadState::Saving(percent) => Some(percent_label(*percent)),
86 DownloadState::Saved(_) => Some("Saved".to_owned()),
87 DownloadState::Failed(_) => Some("Failed, click to retry".to_owned()),
88 }
89}
90
91fn segment(
92 id: &'static str,
93 label: &'static str,
94 active: bool,
95 command: SharedCommand,
96 run: &SharedRun,
97) -> Stateful<Div> {
98 let run = run.clone();
99 div()
100 .id(id)
101 .px(px(12.))
102 .py(px(4.))
103 .rounded(px(6.))
104 .cursor_pointer()
105 .text_size(px(12.5))
106 .font_weight(if active {
107 FontWeight::SEMIBOLD
108 } else {
109 FontWeight::NORMAL
110 })
111 .text_color(if active {
112 theme::text()
113 } else {
114 theme::text_muted()
115 })
116 .when(active, |segment| segment.bg(theme::border_strong()))
117 .on_click(move |_, window, cx| run(command, window, cx))
118 .child(label)
119}
120
121fn switch(scope: SharedScope, run: &SharedRun) -> Div {
122 h_flex()
123 .p(px(2.))
124 .gap(px(2.))
125 .rounded(px(8.))
126 .bg(theme::surface_raised())
127 .child(segment(
128 "shared-in-library",
129 "In library",
130 scope == SharedScope::Library,
131 SharedCommand::Scope(SharedScope::Library),
132 run,
133 ))
134 .child(segment(
135 "shared-in-messages",
136 "In messages",
137 scope == SharedScope::Messages,
138 SharedCommand::Scope(SharedScope::Messages),
139 run,
140 ))
141}
142
143fn tool_button(
144 id: &'static str,
145 label: &'static str,
146 glyph: IconName,
147 command: SharedCommand,
148 run: &SharedRun,
149) -> impl IntoElement {
150 let run = run.clone();
151 Button::new(id)
152 .ghost()
153 .compact()
154 .icon(glyph)
155 .label(label)
156 .on_click(move |_, window, cx| run(command, window, cx))
157}
158
159fn header(scope: SharedScope, trailing: Option<AnyElement>, run: &SharedRun) -> Div {
160 h_flex()
161 .w_full()
162 .flex_none()
163 .px(px(SIDE_PADDING))
164 .py(px(8.))
165 .gap(px(12.))
166 .items_center()
167 .child(switch(scope, run))
168 .children(trailing)
169}
170
171fn crumb_trail(path: &LibraryPath, run: &SharedRun) -> Div {
172 let last = path.crumbs().len() - 1;
173 let mut trail = h_flex().flex_1().min_w_0().gap(px(4.)).items_center();
174 for (index, crumb) in path.crumbs().iter().enumerate() {
175 if index > 0 {
176 trail = trail.child(icon(IconName::ChevronRight, 12., theme::text_faint()));
177 }
178 let run = run.clone();
179 let current = index == last;
180 trail = trail.child(
181 div()
182 .id(ElementId::NamedInteger(
183 "library-crumb".into(),
184 index as u64,
185 ))
186 .px(px(4.))
187 .py(px(2.))
188 .rounded(px(4.))
189 .truncate()
190 .text_size(px(13.))
191 .font_weight(if current {
192 FontWeight::SEMIBOLD
193 } else {
194 FontWeight::NORMAL
195 })
196 .text_color(if current {
197 theme::text()
198 } else {
199 theme::text_muted()
200 })
201 .when(!current, |crumb| {
202 crumb
203 .cursor_pointer()
204 .hover(|crumb| crumb.bg(theme::row_hover()))
205 .on_click(move |_, window, cx| run(SharedCommand::Crumb(index), window, cx))
206 })
207 .child(crumb.name.clone()),
208 );
209 }
210 trail
211}
212
213fn column(width: Option<f32>) -> Div {
214 match width {
215 Some(width) => div().w(px(width)).flex_none().truncate(),
216 None => div().flex_1().min_w_0().truncate(),
217 }
218}
219
220fn column_header() -> Div {
221 h_flex()
222 .w_full()
223 .flex_none()
224 .h(px(28.))
225 .px(px(SIDE_PADDING))
226 .gap(px(12.))
227 .items_center()
228 .text_size(px(11.5))
229 .font_weight(FontWeight::SEMIBOLD)
230 .text_color(theme::text_faint())
231 .child(column(None).pl(px(BADGE_SIZE + 8.)).child("Name"))
232 .child(column(Some(MODIFIED_WIDTH)).child("Modified"))
233 .child(column(Some(MODIFIED_BY_WIDTH)).child("Modified by"))
234 .child(column(Some(SIZE_WIDTH)).child("Size"))
235}
236
237fn entry_badge(entry: &DriveEntry) -> Div {
238 if entry.is_folder() {
239 return div()
240 .size(px(BADGE_SIZE))
241 .flex_none()
242 .flex()
243 .items_center()
244 .justify_center()
245 .child(icon(IconName::Folder, 22., theme::accent_text()));
246 }
247 file_badge(FileKind::from_name(&entry.name), BADGE_SIZE)
248}
249
250fn modified_text(entry: &DriveEntry) -> String {
251 entry.modified_at.map_or_else(String::new, |time| {
252 let now = Local::now();
253 format::list_time_label(time, now.date_naive(), now.offset().fix())
254 })
255}
256
257fn entry_row(
258 index: usize,
259 entry: &DriveEntry,
260 download: Option<String>,
261 run: &SharedRun,
262) -> impl IntoElement {
263 let folder = entry.is_folder();
264 let (open_run, menu_run) = (run.clone(), run.clone());
265 let name = h_flex()
266 .flex_1()
267 .min_w_0()
268 .gap(px(8.))
269 .items_center()
270 .child(entry_badge(entry))
271 .child(
272 div()
273 .truncate()
274 .text_size(px(13.))
275 .text_color(theme::text())
276 .child(entry.name.clone()),
277 )
278 .children(download.map(|label| {
279 div()
280 .flex_none()
281 .text_size(px(11.5))
282 .text_color(theme::accent_text())
283 .child(label)
284 }));
285 h_flex()
286 .id(ElementId::NamedInteger("library-row".into(), index as u64))
287 .w_full()
288 .flex_none()
289 .h(px(ROW_HEIGHT))
290 .px(px(SIDE_PADDING))
291 .gap(px(12.))
292 .items_center()
293 .cursor_pointer()
294 .text_size(px(12.5))
295 .text_color(theme::text_muted())
296 .hover(|row| row.bg(theme::row_hover()))
297 .on_click(move |_, window, cx| open_run(SharedCommand::Open(index), window, cx))
298 .child(name)
299 .child(column(Some(MODIFIED_WIDTH)).child(modified_text(entry)))
300 .child(column(Some(MODIFIED_BY_WIDTH)).child(entry.modified_by.clone().unwrap_or_default()))
301 .child(column(Some(SIZE_WIDTH)).child(size_label(entry)))
302 .context_menu(move |popup, _, _| {
303 let item = |label: &'static str, glyph: IconName, command: SharedCommand| {
304 let run = menu_run.clone();
305 PopupMenuItem::new(label)
306 .icon(menu_icon(glyph))
307 .on_click(move |_, window, cx| run(command, window, cx))
308 };
309 let popup = popup.item(item(
310 "Open in browser",
311 IconName::ExternalLink,
312 SharedCommand::OpenInBrowser(index),
313 ));
314 let popup = if folder {
315 popup
316 } else {
317 popup.item(item(
318 "Download",
319 IconName::Download,
320 SharedCommand::Download(index),
321 ))
322 };
323 popup.item(item(
324 "Copy link",
325 IconName::Link,
326 SharedCommand::CopyLink(index),
327 ))
328 })
329}
330
331fn skeleton_rows() -> impl IntoElement {
332 v_flex().w_full().children((0..SKELETON_ROWS).map(|index| {
333 let bar = |width: f32| {
334 div()
335 .h(px(10.))
336 .w(px(width))
337 .rounded(px(5.))
338 .bg(theme::surface_raised())
339 };
340 h_flex()
341 .id(ElementId::NamedInteger(
342 "library-skeleton".into(),
343 index as u64,
344 ))
345 .w_full()
346 .h(px(ROW_HEIGHT))
347 .flex_none()
348 .px(px(SIDE_PADDING))
349 .gap(px(12.))
350 .items_center()
351 .child(
352 h_flex()
353 .flex_1()
354 .gap(px(8.))
355 .items_center()
356 .child(
357 div()
358 .size(px(BADGE_SIZE))
359 .rounded(px(7.))
360 .bg(theme::surface_raised()),
361 )
362 .child(bar(140. + 30. * (index % 3) as f32)),
363 )
364 .child(column(Some(MODIFIED_WIDTH)).child(bar(50.)))
365 .child(column(Some(MODIFIED_BY_WIDTH)).child(bar(90.)))
366 .child(column(Some(SIZE_WIDTH)).child(bar(40.)))
367 }))
368}
369
370fn centered() -> Div {
371 div()
372 .w_full()
373 .flex_1()
374 .flex()
375 .items_center()
376 .justify_center()
377 .text_size(px(13.))
378 .text_color(theme::text_muted())
379}
380
381fn centered_note(text: &'static str) -> Div {
382 centered().child(text)
383}
384
385fn new_folder_row(input: &Entity<InputState>) -> Div {
386 h_flex()
387 .w_full()
388 .flex_none()
389 .px(px(SIDE_PADDING))
390 .pb(px(8.))
391 .gap(px(8.))
392 .items_center()
393 .child(icon(IconName::Folder, 18., theme::accent_text()))
394 .child(
395 div()
396 .w(px(NEW_FOLDER_WIDTH))
397 .h(px(30.))
398 .px(px(2.))
399 .flex()
400 .items_center()
401 .rounded(px(8.))
402 .bg(theme::surface())
403 .border_1()
404 .border_color(theme::accent())
405 .child(Input::new(input).appearance(false).bordered(false)),
406 )
407 .child(
408 div()
409 .text_size(px(11.5))
410 .text_color(theme::text_faint())
411 .child("Enter to create, Esc to cancel"),
412 )
413}
414
415pub fn render_library(view: LibraryView, run: &SharedRun) -> AnyElement {
416 let toolbar = view.path.map(|path| {
417 h_flex()
418 .flex_1()
419 .min_w_0()
420 .gap(px(4.))
421 .items_center()
422 .child(crumb_trail(path, run))
423 .child(tool_button(
424 "library-new-folder",
425 "New folder",
426 IconName::Plus,
427 SharedCommand::ToggleNewFolder,
428 run,
429 ))
430 .child(tool_button(
431 "library-upload",
432 "Upload",
433 IconName::Upload,
434 SharedCommand::Upload,
435 run,
436 ))
437 .child(tool_button(
438 "library-open-sharepoint",
439 "Open in SharePoint \u{2197}",
440 IconName::ExternalLink,
441 SharedCommand::OpenSharePoint,
442 run,
443 ))
444 .into_any_element()
445 });
446 let body = match view.listing {
447 Listing::Loading => skeleton_rows().into_any_element(),
448 Listing::Failed(reason) => {
449 let retry = run.clone();
450 centered()
451 .flex_col()
452 .gap(px(8.))
453 .child(
454 div()
455 .text_color(theme::red_soft())
456 .child(format!("Could not load the files: {reason}")),
457 )
458 .child(
459 Button::new("library-retry")
460 .ghost()
461 .compact()
462 .label("Retry")
463 .on_click(move |_, window, cx| retry(SharedCommand::Retry, window, cx)),
464 )
465 .into_any_element()
466 }
467 Listing::Ready(entries) if entries.is_empty() => {
468 centered_note(EMPTY_LIBRARY).into_any_element()
469 }
470 Listing::Ready(entries) => v_flex()
471 .id("library-rows")
472 .w_full()
473 .flex_1()
474 .min_h_0()
475 .overflow_y_scrollbar()
476 .children(entries.iter().enumerate().map(|(index, entry)| {
477 let download = view
478 .download_states
479 .get(index)
480 .and_then(|state| download_label(state.as_ref()));
481 entry_row(index, entry, download, run)
482 }))
483 .into_any_element(),
484 };
485 v_flex()
486 .size_full()
487 .child(header(SharedScope::Library, toolbar, run))
488 .children(view.new_folder.map(new_folder_row))
489 .children(view.status.map(|status| {
490 div()
491 .px(px(SIDE_PADDING))
492 .pb(px(6.))
493 .text_size(px(12.))
494 .text_color(theme::accent_text())
495 .child(status.to_owned())
496 }))
497 .child(column_header())
498 .child(body)
499 .into_any_element()
500}
501
502fn chip(
503 id: &'static str,
504 label: &'static str,
505 active: bool,
506 command: SharedCommand,
507 run: &SharedRun,
508) -> Stateful<Div> {
509 let run = run.clone();
510 div()
511 .id(id)
512 .px(px(12.))
513 .py(px(4.))
514 .rounded_full()
515 .cursor_pointer()
516 .text_size(px(12.))
517 .bg(if active {
518 theme::accent()
519 } else {
520 theme::surface_raised()
521 })
522 .text_color(if active {
523 theme::on_accent()
524 } else {
525 theme::text_soft()
526 })
527 .on_click(move |_, window, cx| run(command, window, cx))
528 .child(label)
529}
530
531fn chips(filter: MessageFilter, run: &SharedRun) -> AnyElement {
532 h_flex()
533 .gap(px(8.))
534 .child(chip(
535 "shared-filter-all",
536 "All",
537 filter == MessageFilter::All,
538 SharedCommand::Filter(MessageFilter::All),
539 run,
540 ))
541 .child(chip(
542 "shared-filter-files",
543 "Files",
544 filter == MessageFilter::Files,
545 SharedCommand::Filter(MessageFilter::Files),
546 run,
547 ))
548 .child(chip(
549 "shared-filter-links",
550 "Links",
551 filter == MessageFilter::Links,
552 SharedCommand::Filter(MessageFilter::Links),
553 run,
554 ))
555 .into_any_element()
556}
557
558fn item_badge(item: &SharedItem) -> Div {
559 match &item.content {
560 SharedContent::File(card) => file_badge(card.kind, BADGE_SIZE),
561 SharedContent::Link(_) => div()
562 .size(px(BADGE_SIZE))
563 .flex_none()
564 .flex()
565 .items_center()
566 .justify_center()
567 .rounded(px(7.))
568 .bg(theme::surface_raised())
569 .child(icon(IconName::Link, 14., theme::text_muted())),
570 }
571}
572
573pub fn item_row(
574 index: usize,
575 item: &SharedItem,
576 download: Option<String>,
577 run: &SharedRun,
578) -> Stateful<Div> {
579 let (open_run, post_run) = (run.clone(), run.clone());
580 let post =
581 (!item.post_label.is_empty()).then(|| format!("\u{201c}{}\u{201d}", item.post_label));
582 let source = h_flex()
583 .gap(px(4.))
584 .text_size(px(12.))
585 .text_color(theme::text_muted())
586 .child(format!("{} in", item.author))
587 .children(post.map(|label| {
588 div()
589 .id(ElementId::NamedInteger("shared-post".into(), index as u64))
590 .truncate()
591 .cursor_pointer()
592 .hover(|post| post.text_color(theme::accent_text()))
593 .on_click(move |_, window, cx| {
594 cx.stop_propagation();
595 post_run(SharedCommand::JumpToPost(index), window, cx)
596 })
597 .child(label)
598 }));
599 h_flex()
600 .id(ElementId::NamedInteger("shared-item".into(), index as u64))
601 .w_full()
602 .flex_none()
603 .px(px(SIDE_PADDING))
604 .py(px(6.))
605 .gap(px(8.))
606 .items_center()
607 .cursor_pointer()
608 .hover(|row| row.bg(theme::row_hover()))
609 .on_click(move |_, window, cx| open_run(SharedCommand::OpenItem(index), window, cx))
610 .child(item_badge(item))
611 .child(
612 v_flex()
613 .flex_1()
614 .min_w_0()
615 .child(
616 div()
617 .truncate()
618 .text_size(px(13.))
619 .text_color(theme::text())
620 .child(item.name().to_owned()),
621 )
622 .child(source),
623 )
624 .children(download.map(|label| {
625 div()
626 .flex_none()
627 .text_size(px(11.5))
628 .text_color(theme::accent_text())
629 .child(label)
630 }))
631}
632
633pub fn render_messages(view: MessagesView, run: &SharedRun) -> AnyElement {
634 let now = Local::now();
635 let groups = grouped_items(
636 view.items,
637 view.filter,
638 now.date_naive(),
639 now.offset().fix(),
640 );
641 let body = if groups.is_empty() {
642 centered_note(EMPTY_MESSAGES).into_any_element()
643 } else {
644 v_flex()
645 .id("shared-messages")
646 .w_full()
647 .flex_1()
648 .min_h_0()
649 .overflow_y_scrollbar()
650 .children(groups.into_iter().map(|(group, indices)| {
651 v_flex()
652 .w_full()
653 .child(
654 div()
655 .px(px(SIDE_PADDING))
656 .pt(px(12.))
657 .pb(px(4.))
658 .text_size(px(11.5))
659 .font_weight(FontWeight::SEMIBOLD)
660 .text_color(theme::text_faint())
661 .child(group.label()),
662 )
663 .children(indices.into_iter().map(|index| {
664 let download = view
665 .download_states
666 .get(index)
667 .and_then(|state| download_label(state.as_ref()));
668 item_row(index, &view.items[index], download, run)
669 }))
670 }))
671 .into_any_element()
672 };
673 v_flex()
674 .size_full()
675 .child(header(
676 SharedScope::Messages,
677 Some(chips(view.filter, run)),
678 run,
679 ))
680 .child(body)
681 .into_any_element()
682}