BlueGit

ict rusty-teams

Files

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

shared_tab.rs

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