BlueGit

ict rusty-teams

Files

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

shared_tab.rs

shared_tab.rs 20.2 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 .flex_none()
352 .px(px(SIDE_PADDING))
353 .gap(px(12.))
354 .items_center()
355 .child(
356 h_flex()
357 .flex_1()
358 .gap(px(8.))
359 .items_center()
360 .child(
361 div()
362 .size(px(BADGE_SIZE))
363 .rounded(px(7.))
364 .bg(theme::surface_raised()),
365 )
366 .child(bar(140. + 30. * (index % 3) as f32)),
367 )
368 .child(column(Some(MODIFIED_WIDTH)).child(bar(50.)))
369 .child(column(Some(MODIFIED_BY_WIDTH)).child(bar(90.)))
370 .child(column(Some(SIZE_WIDTH)).child(bar(40.)))
371 }))
372}
373
374fn centered() -> Div {
375 div()
376 .w_full()
377 .flex_1()
378 .flex()
379 .items_center()
380 .justify_center()
381 .text_size(px(13.))
382 .text_color(theme::text_muted())
383}
384
385fn centered_note(text: &'static str) -> Div {
386 centered().child(text)
387}
388
389fn new_folder_row(input: &Entity<InputState>) -> Div {
390 h_flex()
391 .w_full()
392 .flex_none()
393 .px(px(SIDE_PADDING))
394 .pb(px(8.))
395 .gap(px(8.))
396 .items_center()
397 .child(icon(IconName::Folder, 18., theme::accent_text()))
398 .child(
399 div()
400 .w(px(NEW_FOLDER_WIDTH))
401 .h(px(30.))
402 .px(px(2.))
403 .flex()
404 .items_center()
405 .rounded(px(8.))
406 .bg(theme::surface())
407 .border_1()
408 .border_color(theme::accent())
409 .child(Input::new(input).appearance(false).bordered(false)),
410 )
411 .child(
412 div()
413 .text_size(px(11.5))
414 .text_color(theme::text_faint())
415 .child("Enter to create, Esc to cancel"),
416 )
417}
418
419pub fn render_library(view: LibraryView, run: &SharedRun) -> AnyElement {
420 let toolbar = view.path.map(|path| {
421 h_flex()
422 .flex_1()
423 .min_w_0()
424 .gap(px(4.))
425 .items_center()
426 .child(crumb_trail(path, run))
427 .child(tool_button(
428 "library-new-folder",
429 "New folder",
430 IconName::Plus,
431 SharedCommand::ToggleNewFolder,
432 run,
433 ))
434 .child(tool_button(
435 "library-upload",
436 "Upload",
437 IconName::Upload,
438 SharedCommand::Upload,
439 run,
440 ))
441 .child(tool_button(
442 "library-open-sharepoint",
443 "Open in SharePoint \u{2197}",
444 IconName::ExternalLink,
445 SharedCommand::OpenSharePoint,
446 run,
447 ))
448 .into_any_element()
449 });
450 let body = match view.listing {
451 Listing::Loading => skeleton_rows().into_any_element(),
452 Listing::Failed(reason) => {
453 let retry = run.clone();
454 centered()
455 .flex_col()
456 .gap(px(8.))
457 .child(
458 div()
459 .text_color(theme::red_soft())
460 .child(format!("Could not load the files: {reason}")),
461 )
462 .child(
463 Button::new("library-retry")
464 .ghost()
465 .compact()
466 .label("Retry")
467 .on_click(move |_, window, cx| retry(SharedCommand::Retry, window, cx)),
468 )
469 .into_any_element()
470 }
471 Listing::Ready(entries) if entries.is_empty() => {
472 centered_note(EMPTY_LIBRARY).into_any_element()
473 }
474 Listing::Ready(entries) => v_flex()
475 .id("library-rows")
476 .w_full()
477 .flex_1()
478 .min_h_0()
479 .overflow_y_scroll()
480 .children(entries.iter().enumerate().map(|(index, entry)| {
481 let download = view
482 .download_states
483 .get(index)
484 .and_then(|state| download_label(state.as_ref()));
485 entry_row(index, entry, download, run)
486 }))
487 .into_any_element(),
488 };
489 v_flex()
490 .size_full()
491 .child(header(SharedScope::Library, toolbar, run))
492 .children(view.new_folder.map(new_folder_row))
493 .children(view.status.map(|status| {
494 div()
495 .px(px(SIDE_PADDING))
496 .pb(px(6.))
497 .text_size(px(12.))
498 .text_color(theme::accent_text())
499 .child(status.to_owned())
500 }))
501 .child(column_header())
502 .child(body)
503 .into_any_element()
504}
505
506fn chip(
507 id: &'static str,
508 label: &'static str,
509 active: bool,
510 command: SharedCommand,
511 run: &SharedRun,
512) -> Stateful<Div> {
513 let run = run.clone();
514 div()
515 .id(id)
516 .px(px(12.))
517 .py(px(4.))
518 .rounded_full()
519 .cursor_pointer()
520 .text_size(px(12.))
521 .bg(if active {
522 theme::accent()
523 } else {
524 theme::surface_raised()
525 })
526 .text_color(if active {
527 theme::on_accent()
528 } else {
529 theme::text_soft()
530 })
531 .on_click(move |_, window, cx| run(command, window, cx))
532 .child(label)
533}
534
535fn chips(filter: MessageFilter, run: &SharedRun) -> AnyElement {
536 h_flex()
537 .gap(px(8.))
538 .child(chip(
539 "shared-filter-all",
540 "All",
541 filter == MessageFilter::All,
542 SharedCommand::Filter(MessageFilter::All),
543 run,
544 ))
545 .child(chip(
546 "shared-filter-files",
547 "Files",
548 filter == MessageFilter::Files,
549 SharedCommand::Filter(MessageFilter::Files),
550 run,
551 ))
552 .child(chip(
553 "shared-filter-links",
554 "Links",
555 filter == MessageFilter::Links,
556 SharedCommand::Filter(MessageFilter::Links),
557 run,
558 ))
559 .into_any_element()
560}
561
562fn item_badge(item: &SharedItem) -> Div {
563 match &item.content {
564 SharedContent::File(card) => file_badge(card.kind, BADGE_SIZE),
565 SharedContent::Link(_) => div()
566 .size(px(BADGE_SIZE))
567 .flex_none()
568 .flex()
569 .items_center()
570 .justify_center()
571 .rounded(px(7.))
572 .bg(theme::surface_raised())
573 .child(icon(IconName::Link, 14., theme::text_muted())),
574 }
575}
576
577pub fn item_row(
578 index: usize,
579 item: &SharedItem,
580 download: Option<String>,
581 run: &SharedRun,
582) -> Stateful<Div> {
583 let (open_run, post_run) = (run.clone(), run.clone());
584 let post =
585 (!item.post_label.is_empty()).then(|| format!("\u{201c}{}\u{201d}", item.post_label));
586 let source = h_flex()
587 .gap(px(4.))
588 .text_size(px(12.))
589 .text_color(theme::text_muted())
590 .child(format!("{} in", item.author))
591 .children(post.map(|label| {
592 div()
593 .id(ElementId::NamedInteger("shared-post".into(), index as u64))
594 .truncate()
595 .cursor_pointer()
596 .hover(|post| post.text_color(theme::accent_text()))
597 .on_click(move |_, window, cx| {
598 cx.stop_propagation();
599 post_run(SharedCommand::JumpToPost(index), window, cx)
600 })
601 .child(label)
602 }));
603 h_flex()
604 .id(ElementId::NamedInteger("shared-item".into(), index as u64))
605 .w_full()
606 .flex_none()
607 .px(px(SIDE_PADDING))
608 .py(px(6.))
609 .gap(px(8.))
610 .items_center()
611 .cursor_pointer()
612 .hover(|row| row.bg(theme::row_hover()))
613 .on_click(move |_, window, cx| open_run(SharedCommand::OpenItem(index), window, cx))
614 .child(item_badge(item))
615 .child(
616 v_flex()
617 .flex_1()
618 .min_w_0()
619 .child(
620 div()
621 .truncate()
622 .text_size(px(13.))
623 .text_color(theme::text())
624 .child(item.name().to_owned()),
625 )
626 .child(source),
627 )
628 .children(download.map(|label| {
629 div()
630 .flex_none()
631 .text_size(px(11.5))
632 .text_color(theme::accent_text())
633 .child(label)
634 }))
635}
636
637pub fn render_messages(view: MessagesView, run: &SharedRun) -> AnyElement {
638 let now = Local::now();
639 let groups = grouped_items(
640 view.items,
641 view.filter,
642 now.date_naive(),
643 now.offset().fix(),
644 );
645 let body = if groups.is_empty() {
646 centered_note(EMPTY_MESSAGES).into_any_element()
647 } else {
648 v_flex()
649 .id("shared-messages")
650 .w_full()
651 .flex_1()
652 .min_h_0()
653 .overflow_y_scroll()
654 .children(groups.into_iter().map(|(group, indices)| {
655 v_flex()
656 .w_full()
657 .child(
658 div()
659 .px(px(SIDE_PADDING))
660 .pt(px(12.))
661 .pb(px(4.))
662 .text_size(px(11.5))
663 .font_weight(FontWeight::SEMIBOLD)
664 .text_color(theme::text_faint())
665 .child(group.label()),
666 )
667 .children(indices.into_iter().map(|index| {
668 let download = view
669 .download_states
670 .get(index)
671 .and_then(|state| download_label(state.as_ref()));
672 item_row(index, &view.items[index], download, run)
673 }))
674 }))
675 .into_any_element()
676 };
677 v_flex()
678 .size_full()
679 .child(header(
680 SharedScope::Messages,
681 Some(chips(view.filter, run)),
682 run,
683 ))
684 .child(body)
685 .into_any_element()
686}