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::scroll::ScrollableElement as _;
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::i18n::tr;
25use crate::theme;
26
27const SIDE_PADDING: f32 = 20.;
28const ROW_HEIGHT: f32 = 40.;
29const BADGE_SIZE: f32 = 26.;
30const MODIFIED_WIDTH: f32 = 110.;
31const MODIFIED_BY_WIDTH: f32 = 160.;
32const SIZE_WIDTH: f32 = 90.;
33const SKELETON_ROWS: usize = 6;
34const NEW_FOLDER_WIDTH: f32 = 220.;
35
36pub const EMPTY_LIBRARY: &str = "No files yet. Upload or drop files here.";
37pub const EMPTY_MESSAGES: &str = "No files or links shared in this channel yet.";
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum SharedScope {
41 Library,
42 Messages,
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum SharedCommand {
47 Scope(SharedScope),
48 Filter(MessageFilter),
49 Crumb(usize),
50 Open(usize),
51 OpenInBrowser(usize),
52 Download(usize),
53 CopyLink(usize),
54 Retry,
55 ToggleNewFolder,
56 Upload,
57 OpenSharePoint,
58 OpenItem(usize),
59 JumpToPost(usize),
60}
61
62pub type SharedRun = Rc<dyn Fn(SharedCommand, &mut Window, &mut App)>;
63
64pub enum Listing {
65 Loading,
66 Failed(String),
67 Ready(Vec<DriveEntry>),
68}
69
70pub struct LibraryView<'a> {
71 pub path: Option<&'a LibraryPath>,
72 pub listing: &'a Listing,
73 pub new_folder: Option<&'a Entity<InputState>>,
74 pub status: Option<&'a str>,
75 pub download_states: Vec<Option<DownloadState>>,
76}
77
78pub struct MessagesView<'a> {
79 pub items: &'a [SharedItem],
80 pub filter: MessageFilter,
81 pub download_states: Vec<Option<DownloadState>>,
82 pub has_library: bool,
83}
84
85pub fn download_label(state: Option<&DownloadState>) -> Option<String> {
86 match state? {
87 DownloadState::Saving(percent) => Some(percent_label(*percent)),
88 DownloadState::Saved(_) => Some("Saved".to_owned()),
89 DownloadState::Failed(_) => Some("Failed, click to retry".to_owned()),
90 }
91}
92
93fn segment(
94 id: &'static str,
95 label: &'static str,
96 active: bool,
97 command: SharedCommand,
98 run: &SharedRun,
99) -> Stateful<Div> {
100 let run = run.clone();
101 div()
102 .id(id)
103 .px(px(12.))
104 .py(px(4.))
105 .rounded(px(6.))
106 .cursor_pointer()
107 .text_size(px(12.5))
108 .font_weight(if active {
109 FontWeight::SEMIBOLD
110 } else {
111 FontWeight::NORMAL
112 })
113 .text_color(if active {
114 theme::text()
115 } else {
116 theme::text_muted()
117 })
118 .when(active, |segment| segment.bg(theme::border_strong()))
119 .on_click(move |_, window, cx| run(command, window, cx))
120 .child(label)
121}
122
123fn switch(scope: SharedScope, run: &SharedRun) -> Div {
124 h_flex()
125 .p(px(2.))
126 .gap(px(2.))
127 .rounded(px(8.))
128 .bg(theme::surface_raised())
129 .child(segment(
130 "shared-in-library",
131 "In library",
132 scope == SharedScope::Library,
133 SharedCommand::Scope(SharedScope::Library),
134 run,
135 ))
136 .child(segment(
137 "shared-in-messages",
138 "In messages",
139 scope == SharedScope::Messages,
140 SharedCommand::Scope(SharedScope::Messages),
141 run,
142 ))
143}
144
145fn tool_button(
146 id: &'static str,
147 label: &'static str,
148 glyph: IconName,
149 command: SharedCommand,
150 run: &SharedRun,
151) -> impl IntoElement {
152 let run = run.clone();
153 Button::new(id)
154 .ghost()
155 .compact()
156 .icon(glyph)
157 .label(label)
158 .on_click(move |_, window, cx| run(command, window, cx))
159}
160
161fn header(
162 scope: SharedScope,
163 has_library: bool,
164 trailing: Option<AnyElement>,
165 run: &SharedRun,
166) -> Div {
167 h_flex()
168 .w_full()
169 .flex_none()
170 .px(px(SIDE_PADDING))
171 .py(px(8.))
172 .gap(px(12.))
173 .items_center()
174 .when(has_library, |row| row.child(switch(scope, run)))
175 .children(trailing)
176}
177
178fn crumb_trail(path: &LibraryPath, run: &SharedRun) -> Div {
179 let last = path.crumbs().len() - 1;
180 let mut trail = h_flex().flex_1().min_w_0().gap(px(4.)).items_center();
181 for (index, crumb) in path.crumbs().iter().enumerate() {
182 if index > 0 {
183 trail = trail.child(icon(IconName::ChevronRight, 12., theme::text_faint()));
184 }
185 let run = run.clone();
186 let current = index == last;
187 trail = trail.child(
188 div()
189 .id(ElementId::NamedInteger(
190 "library-crumb".into(),
191 index as u64,
192 ))
193 .px(px(4.))
194 .py(px(2.))
195 .rounded(px(4.))
196 .truncate()
197 .text_size(px(13.))
198 .font_weight(if current {
199 FontWeight::SEMIBOLD
200 } else {
201 FontWeight::NORMAL
202 })
203 .text_color(if current {
204 theme::text()
205 } else {
206 theme::text_muted()
207 })
208 .when(!current, |crumb| {
209 crumb
210 .cursor_pointer()
211 .hover(|crumb| crumb.bg(theme::row_hover()))
212 .on_click(move |_, window, cx| run(SharedCommand::Crumb(index), window, cx))
213 })
214 .child(crumb.name.clone()),
215 );
216 }
217 trail
218}
219
220fn column(width: Option<f32>) -> Div {
221 match width {
222 Some(width) => div().w(px(width)).flex_none().truncate(),
223 None => div().flex_1().min_w_0().truncate(),
224 }
225}
226
227fn column_header() -> Div {
228 h_flex()
229 .w_full()
230 .flex_none()
231 .h(px(28.))
232 .px(px(SIDE_PADDING))
233 .gap(px(12.))
234 .items_center()
235 .text_size(px(11.5))
236 .font_weight(FontWeight::SEMIBOLD)
237 .text_color(theme::text_faint())
238 .child(column(None).pl(px(BADGE_SIZE + 8.)).child("Name"))
239 .child(column(Some(MODIFIED_WIDTH)).child("Modified"))
240 .child(column(Some(MODIFIED_BY_WIDTH)).child("Modified by"))
241 .child(column(Some(SIZE_WIDTH)).child("Size"))
242}
243
244pub(crate) fn entry_badge(entry: &DriveEntry) -> Div {
245 if entry.is_folder() {
246 return div()
247 .size(px(BADGE_SIZE))
248 .flex_none()
249 .flex()
250 .items_center()
251 .justify_center()
252 .child(icon(IconName::Folder, 22., theme::accent_text()));
253 }
254 file_badge(FileKind::from_name(&entry.name), BADGE_SIZE)
255}
256
257pub(crate) fn modified_text(entry: &DriveEntry) -> String {
258 entry.modified_at.map_or_else(String::new, |time| {
259 let now = Local::now();
260 format::list_time_label(time, now.date_naive(), now.offset().fix())
261 })
262}
263
264fn entry_row(
265 index: usize,
266 entry: &DriveEntry,
267 download: Option<String>,
268 run: &SharedRun,
269) -> impl IntoElement {
270 let folder = entry.is_folder();
271 let (open_run, menu_run) = (run.clone(), run.clone());
272 let name = h_flex()
273 .flex_1()
274 .min_w_0()
275 .gap(px(8.))
276 .items_center()
277 .child(entry_badge(entry))
278 .child(
279 div()
280 .truncate()
281 .text_size(px(13.))
282 .text_color(theme::text())
283 .child(entry.name.clone()),
284 )
285 .children(download.map(|label| {
286 div()
287 .flex_none()
288 .text_size(px(11.5))
289 .text_color(theme::accent_text())
290 .child(label)
291 }));
292 h_flex()
293 .id(ElementId::NamedInteger("library-row".into(), index as u64))
294 .w_full()
295 .flex_none()
296 .h(px(ROW_HEIGHT))
297 .px(px(SIDE_PADDING))
298 .gap(px(12.))
299 .items_center()
300 .cursor_pointer()
301 .text_size(px(12.5))
302 .text_color(theme::text_muted())
303 .hover(|row| row.bg(theme::row_hover()))
304 .on_click(move |_, window, cx| open_run(SharedCommand::Open(index), window, cx))
305 .child(name)
306 .child(column(Some(MODIFIED_WIDTH)).child(modified_text(entry)))
307 .child(column(Some(MODIFIED_BY_WIDTH)).child(entry.modified_by.clone().unwrap_or_default()))
308 .child(column(Some(SIZE_WIDTH)).child(size_label(entry)))
309 .context_menu(move |popup, _, _| {
310 let item = |label: &'static str, glyph: IconName, command: SharedCommand| {
311 let run = menu_run.clone();
312 PopupMenuItem::new(label)
313 .icon(menu_icon(glyph))
314 .on_click(move |_, window, cx| run(command, window, cx))
315 };
316 let popup = popup.item(item(
317 "Open in browser",
318 IconName::ExternalLink,
319 SharedCommand::OpenInBrowser(index),
320 ));
321 let popup = if folder {
322 popup
323 } else {
324 popup.item(item(
325 "Download",
326 IconName::Download,
327 SharedCommand::Download(index),
328 ))
329 };
330 popup.item(item(
331 "Copy link",
332 IconName::Link,
333 SharedCommand::CopyLink(index),
334 ))
335 })
336}
337
338fn skeleton_rows() -> impl IntoElement {
339 v_flex().w_full().children((0..SKELETON_ROWS).map(|index| {
340 let bar = |width: f32| {
341 div()
342 .h(px(10.))
343 .w(px(width))
344 .rounded(px(5.))
345 .bg(theme::surface_raised())
346 };
347 h_flex()
348 .id(ElementId::NamedInteger(
349 "library-skeleton".into(),
350 index as u64,
351 ))
352 .w_full()
353 .h(px(ROW_HEIGHT))
354 .flex_none()
355 .px(px(SIDE_PADDING))
356 .gap(px(12.))
357 .items_center()
358 .child(
359 h_flex()
360 .flex_1()
361 .gap(px(8.))
362 .items_center()
363 .child(
364 div()
365 .size(px(BADGE_SIZE))
366 .rounded(px(7.))
367 .bg(theme::surface_raised()),
368 )
369 .child(bar(140. + 30. * (index % 3) as f32)),
370 )
371 .child(column(Some(MODIFIED_WIDTH)).child(bar(50.)))
372 .child(column(Some(MODIFIED_BY_WIDTH)).child(bar(90.)))
373 .child(column(Some(SIZE_WIDTH)).child(bar(40.)))
374 }))
375}
376
377fn centered() -> Div {
378 div()
379 .w_full()
380 .flex_1()
381 .flex()
382 .items_center()
383 .justify_center()
384 .text_size(px(13.))
385 .text_color(theme::text_muted())
386}
387
388fn centered_note(text: &'static str) -> Div {
389 centered().child(text)
390}
391
392fn new_folder_row(input: &Entity<InputState>) -> Div {
393 h_flex()
394 .w_full()
395 .flex_none()
396 .px(px(SIDE_PADDING))
397 .pb(px(8.))
398 .gap(px(8.))
399 .items_center()
400 .child(icon(IconName::Folder, 18., theme::accent_text()))
401 .child(
402 div()
403 .w(px(NEW_FOLDER_WIDTH))
404 .h(px(30.))
405 .px(px(2.))
406 .flex()
407 .items_center()
408 .rounded(px(8.))
409 .bg(theme::surface())
410 .border_1()
411 .border_color(theme::accent())
412 .child(Input::new(input).appearance(false).bordered(false)),
413 )
414 .child(
415 div()
416 .text_size(px(11.5))
417 .text_color(theme::text_faint())
418 .child("Enter to create, Esc to cancel"),
419 )
420}
421
422pub fn render_library(view: LibraryView, run: &SharedRun) -> AnyElement {
423 let toolbar = view.path.map(|path| {
424 h_flex()
425 .flex_1()
426 .min_w_0()
427 .gap(px(4.))
428 .items_center()
429 .child(crumb_trail(path, run))
430 .child(tool_button(
431 "library-new-folder",
432 "New folder",
433 IconName::Plus,
434 SharedCommand::ToggleNewFolder,
435 run,
436 ))
437 .child(tool_button(
438 "library-upload",
439 "Upload",
440 IconName::Upload,
441 SharedCommand::Upload,
442 run,
443 ))
444 .child(tool_button(
445 "library-open-sharepoint",
446 "Open in SharePoint \u{2197}",
447 IconName::ExternalLink,
448 SharedCommand::OpenSharePoint,
449 run,
450 ))
451 .into_any_element()
452 });
453 let body = match view.listing {
454 Listing::Loading => skeleton_rows().into_any_element(),
455 Listing::Failed(reason) => {
456 let retry = run.clone();
457 centered()
458 .flex_col()
459 .gap(px(8.))
460 .child(
461 div()
462 .text_color(theme::red_soft())
463 .child(format!("Could not load the files: {reason}")),
464 )
465 .child(
466 Button::new("library-retry")
467 .ghost()
468 .compact()
469 .label(tr("common.retry"))
470 .on_click(move |_, window, cx| retry(SharedCommand::Retry, window, cx)),
471 )
472 .into_any_element()
473 }
474 Listing::Ready(entries) if entries.is_empty() => {
475 centered_note(EMPTY_LIBRARY).into_any_element()
476 }
477 Listing::Ready(entries) => v_flex()
478 .id("library-rows")
479 .w_full()
480 .flex_1()
481 .min_h_0()
482 .overflow_y_scrollbar()
483 .children(entries.iter().enumerate().map(|(index, entry)| {
484 let download = view
485 .download_states
486 .get(index)
487 .and_then(|state| download_label(state.as_ref()));
488 entry_row(index, entry, download, run)
489 }))
490 .into_any_element(),
491 };
492 v_flex()
493 .size_full()
494 .child(header(SharedScope::Library, true, toolbar, run))
495 .children(view.new_folder.map(new_folder_row))
496 .children(view.status.map(|status| {
497 div()
498 .px(px(SIDE_PADDING))
499 .pb(px(6.))
500 .text_size(px(12.))
501 .text_color(theme::accent_text())
502 .child(status.to_owned())
503 }))
504 .child(column_header())
505 .child(body)
506 .into_any_element()
507}
508
509fn chip(
510 id: &'static str,
511 label: &'static str,
512 active: bool,
513 command: SharedCommand,
514 run: &SharedRun,
515) -> Stateful<Div> {
516 let run = run.clone();
517 div()
518 .id(id)
519 .px(px(12.))
520 .py(px(4.))
521 .rounded_full()
522 .cursor_pointer()
523 .text_size(px(12.))
524 .bg(if active {
525 theme::accent()
526 } else {
527 theme::surface_raised()
528 })
529 .text_color(if active {
530 theme::on_accent()
531 } else {
532 theme::text_soft()
533 })
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
580pub fn 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(8.))
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_scrollbar()
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(12.))
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 view.has_library,
685 Some(chips(view.filter, run)),
686 run,
687 ))
688 .child(body)
689 .into_any_element()
690}