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::assets::IconName;
5use gpui_kit::component::{
6 button::{Button, ButtonVariants as _},
7 h_flex,
8 input::{Input, InputState},
9 menu::{ContextMenuExt as _, PopupMenuItem},
10 v_flex,
11};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::*;
14use teams_core::{DriveEntry, FileKind};
15
16use super::attachments::file_badge;
17use super::widgets::icon;
18use crate::channel_files::{
19 LibraryPath, MessageFilter, SharedContent, SharedItem, grouped_items, size_label,
20};
21use crate::downloads::{DownloadState, percent_label};
22use crate::format;
23use crate::theme;
24
25const SIDE_PADDING: f32 = 20.;
26const ROW_HEIGHT: f32 = 40.;
27const BADGE_SIZE: f32 = 26.;
28const MODIFIED_WIDTH: f32 = 110.;
29const MODIFIED_BY_WIDTH: f32 = 160.;
30const SIZE_WIDTH: f32 = 90.;
31const SKELETON_ROWS: usize = 6;
32const NEW_FOLDER_WIDTH: f32 = 220.;
33const MENU_ICON_SIZE: f32 = 14.;
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 menu_icon(glyph: IconName) -> gpui_kit::component::Icon {
258 icon(glyph, MENU_ICON_SIZE, theme::text_muted())
259}
260
261fn entry_row(
262 index: usize,
263 entry: &DriveEntry,
264 download: Option<String>,
265 run: &SharedRun,
266) -> impl IntoElement {
267 let folder = entry.is_folder();
268 let (open_run, menu_run) = (run.clone(), run.clone());
269 let name = h_flex()
270 .flex_1()
271 .min_w_0()
272 .gap(px(8.))
273 .items_center()
274 .child(entry_badge(entry))
275 .child(
276 div()
277 .truncate()
278 .text_size(px(13.))
279 .text_color(theme::text())
280 .child(entry.name.clone()),
281 )
282 .children(download.map(|label| {
283 div()
284 .flex_none()
285 .text_size(px(11.5))
286 .text_color(theme::accent_text())
287 .child(label)
288 }));
289 h_flex()
290 .id(ElementId::NamedInteger("library-row".into(), index as u64))
291 .w_full()
292 .flex_none()
293 .h(px(ROW_HEIGHT))
294 .px(px(SIDE_PADDING))
295 .gap(px(12.))
296 .items_center()
297 .cursor_pointer()
298 .text_size(px(12.5))
299 .text_color(theme::text_muted())
300 .hover(|row| row.bg(theme::row_hover()))
301 .on_click(move |_, window, cx| open_run(SharedCommand::Open(index), window, cx))
302 .child(name)
303 .child(column(Some(MODIFIED_WIDTH)).child(modified_text(entry)))
304 .child(column(Some(MODIFIED_BY_WIDTH)).child(entry.modified_by.clone().unwrap_or_default()))
305 .child(column(Some(SIZE_WIDTH)).child(size_label(entry)))
306 .context_menu(move |popup, _, _| {
307 let item = |label: &'static str, glyph: IconName, command: SharedCommand| {
308 let run = menu_run.clone();
309 PopupMenuItem::new(label)
310 .icon(menu_icon(glyph))
311 .on_click(move |_, window, cx| run(command, window, cx))
312 };
313 let popup = popup.item(item(
314 "Open in browser",
315 IconName::ExternalLink,
316 SharedCommand::OpenInBrowser(index),
317 ));
318 let popup = if folder {
319 popup
320 } else {
321 popup.item(item(
322 "Download",
323 IconName::Download,
324 SharedCommand::Download(index),
325 ))
326 };
327 popup.item(item(
328 "Copy link",
329 IconName::Link,
330 SharedCommand::CopyLink(index),
331 ))
332 })
333}
334
335fn skeleton_rows() -> impl IntoElement {
336 v_flex().w_full().children((0..SKELETON_ROWS).map(|index| {
337 let bar = |width: f32| {
338 div()
339 .h(px(10.))
340 .w(px(width))
341 .rounded(px(5.))
342 .bg(theme::surface_raised())
343 };
344 h_flex()
345 .id(ElementId::NamedInteger(
346 "library-skeleton".into(),
347 index as u64,
348 ))
349 .w_full()
350 .h(px(ROW_HEIGHT))
351 .px(px(SIDE_PADDING))
352 .gap(px(12.))
353 .items_center()
354 .child(
355 h_flex()
356 .flex_1()
357 .gap(px(8.))
358 .items_center()
359 .child(
360 div()
361 .size(px(BADGE_SIZE))
362 .rounded(px(7.))
363 .bg(theme::surface_raised()),
364 )
365 .child(bar(140. + 30. * (index % 3) as f32)),
366 )
367 .child(column(Some(MODIFIED_WIDTH)).child(bar(50.)))
368 .child(column(Some(MODIFIED_BY_WIDTH)).child(bar(90.)))
369 .child(column(Some(SIZE_WIDTH)).child(bar(40.)))
370 }))
371}
372
373fn centered() -> Div {
374 div()
375 .w_full()
376 .flex_1()
377 .flex()
378 .items_center()
379 .justify_center()
380 .text_size(px(13.))
381 .text_color(theme::text_muted())
382}
383
384fn centered_note(text: &'static str) -> Div {
385 centered().child(text)
386}
387
388fn new_folder_row(input: &Entity<InputState>) -> Div {
389 h_flex()
390 .w_full()
391 .flex_none()
392 .px(px(SIDE_PADDING))
393 .pb(px(8.))
394 .gap(px(8.))
395 .items_center()
396 .child(icon(IconName::Folder, 18., theme::accent_text()))
397 .child(
398 div()
399 .w(px(NEW_FOLDER_WIDTH))
400 .h(px(30.))
401 .px(px(2.))
402 .flex()
403 .items_center()
404 .rounded(px(8.))
405 .bg(theme::surface())
406 .border_1()
407 .border_color(theme::accent())
408 .child(Input::new(input).appearance(false).bordered(false)),
409 )
410 .child(
411 div()
412 .text_size(px(11.5))
413 .text_color(theme::text_faint())
414 .child("Enter to create, Esc to cancel"),
415 )
416}
417
418pub fn render_library(view: LibraryView, run: &SharedRun) -> AnyElement {
419 let toolbar = view.path.map(|path| {
420 h_flex()
421 .flex_1()
422 .min_w_0()
423 .gap(px(4.))
424 .items_center()
425 .child(crumb_trail(path, run))
426 .child(tool_button(
427 "library-new-folder",
428 "New folder",
429 IconName::Plus,
430 SharedCommand::ToggleNewFolder,
431 run,
432 ))
433 .child(tool_button(
434 "library-upload",
435 "Upload",
436 IconName::Upload,
437 SharedCommand::Upload,
438 run,
439 ))
440 .child(tool_button(
441 "library-open-sharepoint",
442 "Open in SharePoint \u{2197}",
443 IconName::ExternalLink,
444 SharedCommand::OpenSharePoint,
445 run,
446 ))
447 .into_any_element()
448 });
449 let body = match view.listing {
450 Listing::Loading => skeleton_rows().into_any_element(),
451 Listing::Failed(reason) => {
452 let retry = run.clone();
453 centered()
454 .flex_col()
455 .gap(px(8.))
456 .child(
457 div()
458 .text_color(theme::red_soft())
459 .child(format!("Could not load the files: {reason}")),
460 )
461 .child(
462 Button::new("library-retry")
463 .ghost()
464 .compact()
465 .label("Retry")
466 .on_click(move |_, window, cx| retry(SharedCommand::Retry, window, cx)),
467 )
468 .into_any_element()
469 }
470 Listing::Ready(entries) if entries.is_empty() => {
471 centered_note(EMPTY_LIBRARY).into_any_element()
472 }
473 Listing::Ready(entries) => v_flex()
474 .id("library-rows")
475 .w_full()
476 .flex_1()
477 .min_h_0()
478 .overflow_y_scroll()
479 .children(entries.iter().enumerate().map(|(index, entry)| {
480 let download = view
481 .download_states
482 .get(index)
483 .and_then(|state| download_label(state.as_ref()));
484 entry_row(index, entry, download, run)
485 }))
486 .into_any_element(),
487 };
488 v_flex()
489 .size_full()
490 .child(header(SharedScope::Library, toolbar, run))
491 .children(view.new_folder.map(new_folder_row))
492 .children(view.status.map(|status| {
493 div()
494 .px(px(SIDE_PADDING))
495 .pb(px(6.))
496 .text_size(px(12.))
497 .text_color(theme::accent_text())
498 .child(status.to_owned())
499 }))
500 .child(column_header())
501 .child(body)
502 .into_any_element()
503}
504
505fn chip(
506 id: &'static str,
507 label: &'static str,
508 active: bool,
509 command: SharedCommand,
510 run: &SharedRun,
511) -> Stateful<Div> {
512 let run = run.clone();
513 div()
514 .id(id)
515 .px(px(12.))
516 .py(px(4.))
517 .rounded_full()
518 .cursor_pointer()
519 .text_size(px(12.))
520 .bg(if active {
521 theme::accent()
522 } else {
523 theme::surface_raised()
524 })
525 .text_color(if active {
526 theme::on_accent()
527 } else {
528 theme::text_soft()
529 })
530 .on_click(move |_, window, cx| run(command, window, cx))
531 .child(label)
532}
533
534fn chips(filter: MessageFilter, run: &SharedRun) -> AnyElement {
535 h_flex()
536 .gap(px(8.))
537 .child(chip(
538 "shared-filter-all",
539 "All",
540 filter == MessageFilter::All,
541 SharedCommand::Filter(MessageFilter::All),
542 run,
543 ))
544 .child(chip(
545 "shared-filter-files",
546 "Files",
547 filter == MessageFilter::Files,
548 SharedCommand::Filter(MessageFilter::Files),
549 run,
550 ))
551 .child(chip(
552 "shared-filter-links",
553 "Links",
554 filter == MessageFilter::Links,
555 SharedCommand::Filter(MessageFilter::Links),
556 run,
557 ))
558 .into_any_element()
559}
560
561fn item_badge(item: &SharedItem) -> Div {
562 match &item.content {
563 SharedContent::File(card) => file_badge(card.kind, BADGE_SIZE),
564 SharedContent::Link(_) => div()
565 .size(px(BADGE_SIZE))
566 .flex_none()
567 .flex()
568 .items_center()
569 .justify_center()
570 .rounded(px(7.))
571 .bg(theme::surface_raised())
572 .child(icon(IconName::Link, 14., theme::text_muted())),
573 }
574}
575
576fn item_row(
577 index: usize,
578 item: &SharedItem,
579 download: Option<String>,
580 run: &SharedRun,
581) -> Stateful<Div> {
582 let (open_run, post_run) = (run.clone(), run.clone());
583 let post =
584 (!item.post_label.is_empty()).then(|| format!("\u{201c}{}\u{201d}", item.post_label));
585 let source = h_flex()
586 .gap(px(4.))
587 .text_size(px(12.))
588 .text_color(theme::text_muted())
589 .child(format!("{} in", item.author))
590 .children(post.map(|label| {
591 div()
592 .id(ElementId::NamedInteger("shared-post".into(), index as u64))
593 .truncate()
594 .cursor_pointer()
595 .hover(|post| post.text_color(theme::accent_text()))
596 .on_click(move |_, window, cx| {
597 cx.stop_propagation();
598 post_run(SharedCommand::JumpToPost(index), window, cx)
599 })
600 .child(label)
601 }));
602 h_flex()
603 .id(ElementId::NamedInteger("shared-item".into(), index as u64))
604 .w_full()
605 .flex_none()
606 .px(px(SIDE_PADDING))
607 .py(px(6.))
608 .gap(px(8.))
609 .items_center()
610 .cursor_pointer()
611 .hover(|row| row.bg(theme::row_hover()))
612 .on_click(move |_, window, cx| open_run(SharedCommand::OpenItem(index), window, cx))
613 .child(item_badge(item))
614 .child(
615 v_flex()
616 .flex_1()
617 .min_w_0()
618 .child(
619 div()
620 .truncate()
621 .text_size(px(13.))
622 .text_color(theme::text())
623 .child(item.name().to_owned()),
624 )
625 .child(source),
626 )
627 .children(download.map(|label| {
628 div()
629 .flex_none()
630 .text_size(px(11.5))
631 .text_color(theme::accent_text())
632 .child(label)
633 }))
634}
635
636pub fn render_messages(view: MessagesView, run: &SharedRun) -> AnyElement {
637 let now = Local::now();
638 let groups = grouped_items(
639 view.items,
640 view.filter,
641 now.date_naive(),
642 now.offset().fix(),
643 );
644 let body = if groups.is_empty() {
645 centered_note(EMPTY_MESSAGES).into_any_element()
646 } else {
647 v_flex()
648 .id("shared-messages")
649 .w_full()
650 .flex_1()
651 .min_h_0()
652 .overflow_y_scroll()
653 .children(groups.into_iter().map(|(group, indices)| {
654 v_flex()
655 .w_full()
656 .child(
657 div()
658 .px(px(SIDE_PADDING))
659 .pt(px(12.))
660 .pb(px(4.))
661 .text_size(px(11.5))
662 .font_weight(FontWeight::SEMIBOLD)
663 .text_color(theme::text_faint())
664 .child(group.label()),
665 )
666 .children(indices.into_iter().map(|index| {
667 let download = view
668 .download_states
669 .get(index)
670 .and_then(|state| download_label(state.as_ref()));
671 item_row(index, &view.items[index], download, run)
672 }))
673 }))
674 .into_any_element()
675 };
676 v_flex()
677 .size_full()
678 .child(header(
679 SharedScope::Messages,
680 Some(chips(view.filter, run)),
681 run,
682 ))
683 .child(body)
684 .into_any_element()
685}