BlueGit

ict rusty-teams

Files

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

shared_tab.rs

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