BlueGit

ict rusty-teams

Files

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

shared_tab.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
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::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::theme;
25
26const SIDE_PADDING: f32 = 20.;
27const ROW_HEIGHT: f32 = 40.;
28const BADGE_SIZE: f32 = 26.;
29const MODIFIED_WIDTH: f32 = 110.;
30const MODIFIED_BY_WIDTH: f32 = 160.;
31const SIZE_WIDTH: f32 = 90.;
32const SKELETON_ROWS: usize = 6;
33const NEW_FOLDER_WIDTH: f32 = 220.;
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 pub has_library: bool,
82}
83
84pub fn download_label(state: Option<&DownloadState>) -> Option<String> {
85 match state? {
86 DownloadState::Saving(percent) => Some(percent_label(*percent)),
87 DownloadState::Saved(_) => Some("Saved".to_owned()),
88 DownloadState::Failed(_) => Some("Failed, click to retry".to_owned()),
89 }
90}
91
92fn segment(
93 id: &'static str,
94 label: &'static str,
95 active: bool,
96 command: SharedCommand,
97 run: &SharedRun,
98) -> Stateful<Div> {
99 let run = run.clone();
100 div()
101 .id(id)
102 .px(px(12.))
103 .py(px(4.))
104 .rounded(px(6.))
105 .cursor_pointer()
106 .text_size(px(12.5))
107 .font_weight(if active {
108 FontWeight::SEMIBOLD
109 } else {
110 FontWeight::NORMAL
111 })
112 .text_color(if active {
113 theme::text()
114 } else {
115 theme::text_muted()
116 })
117 .when(active, |segment| segment.bg(theme::border_strong()))
118 .on_click(move |_, window, cx| run(command, window, cx))
119 .child(label)
120}
121
122fn switch(scope: SharedScope, run: &SharedRun) -> Div {
123 h_flex()
124 .p(px(2.))
125 .gap(px(2.))
126 .rounded(px(8.))
127 .bg(theme::surface_raised())
128 .child(segment(
129 "shared-in-library",
130 "In library",
131 scope == SharedScope::Library,
132 SharedCommand::Scope(SharedScope::Library),
133 run,
134 ))
135 .child(segment(
136 "shared-in-messages",
137 "In messages",
138 scope == SharedScope::Messages,
139 SharedCommand::Scope(SharedScope::Messages),
140 run,
141 ))
142}
143
144fn tool_button(
145 id: &'static str,
146 label: &'static str,
147 glyph: IconName,
148 command: SharedCommand,
149 run: &SharedRun,
150) -> impl IntoElement {
151 let run = run.clone();
152 Button::new(id)
153 .ghost()
154 .compact()
155 .icon(glyph)
156 .label(label)
157 .on_click(move |_, window, cx| run(command, window, cx))
158}
159
160fn header(
161 scope: SharedScope,
162 has_library: bool,
163 trailing: Option<AnyElement>,
164 run: &SharedRun,
165) -> Div {
166 h_flex()
167 .w_full()
168 .flex_none()
169 .px(px(SIDE_PADDING))
170 .py(px(8.))
171 .gap(px(12.))
172 .items_center()
173 .when(has_library, |row| row.child(switch(scope, run)))
174 .children(trailing)
175}
176
177fn crumb_trail(path: &LibraryPath, run: &SharedRun) -> Div {
178 let last = path.crumbs().len() - 1;
179 let mut trail = h_flex().flex_1().min_w_0().gap(px(4.)).items_center();
180 for (index, crumb) in path.crumbs().iter().enumerate() {
181 if index > 0 {
182 trail = trail.child(icon(IconName::ChevronRight, 12., theme::text_faint()));
183 }
184 let run = run.clone();
185 let current = index == last;
186 trail = trail.child(
187 div()
188 .id(ElementId::NamedInteger(
189 "library-crumb".into(),
190 index as u64,
191 ))
192 .px(px(4.))
193 .py(px(2.))
194 .rounded(px(4.))
195 .truncate()
196 .text_size(px(13.))
197 .font_weight(if current {
198 FontWeight::SEMIBOLD
199 } else {
200 FontWeight::NORMAL
201 })
202 .text_color(if current {
203 theme::text()
204 } else {
205 theme::text_muted()
206 })
207 .when(!current, |crumb| {
208 crumb
209 .cursor_pointer()
210 .hover(|crumb| crumb.bg(theme::row_hover()))
211 .on_click(move |_, window, cx| run(SharedCommand::Crumb(index), window, cx))
212 })
213 .child(crumb.name.clone()),
214 );
215 }
216 trail
217}
218
219fn column(width: Option<f32>) -> Div {
220 match width {
221 Some(width) => div().w(px(width)).flex_none().truncate(),
222 None => div().flex_1().min_w_0().truncate(),
223 }
224}
225
226fn column_header() -> Div {
227 h_flex()
228 .w_full()
229 .flex_none()
230 .h(px(28.))
231 .px(px(SIDE_PADDING))
232 .gap(px(12.))
233 .items_center()
234 .text_size(px(11.5))
235 .font_weight(FontWeight::SEMIBOLD)
236 .text_color(theme::text_faint())
237 .child(column(None).pl(px(BADGE_SIZE + 8.)).child("Name"))
238 .child(column(Some(MODIFIED_WIDTH)).child("Modified"))
239 .child(column(Some(MODIFIED_BY_WIDTH)).child("Modified by"))
240 .child(column(Some(SIZE_WIDTH)).child("Size"))
241}
242
243pub(crate) fn entry_badge(entry: &DriveEntry) -> Div {
244 if entry.is_folder() {
245 return div()
246 .size(px(BADGE_SIZE))
247 .flex_none()
248 .flex()
249 .items_center()
250 .justify_center()
251 .child(icon(IconName::Folder, 22., theme::accent_text()));
252 }
253 file_badge(FileKind::from_name(&entry.name), BADGE_SIZE)
254}
255
256pub(crate) fn modified_text(entry: &DriveEntry) -> String {
257 entry.modified_at.map_or_else(String::new, |time| {
258 let now = Local::now();
259 format::list_time_label(time, now.date_naive(), now.offset().fix())
260 })
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(8.))
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 .flex_none()
354 .px(px(SIDE_PADDING))
355 .gap(px(12.))
356 .items_center()
357 .child(
358 h_flex()
359 .flex_1()
360 .gap(px(8.))
361 .items_center()
362 .child(
363 div()
364 .size(px(BADGE_SIZE))
365 .rounded(px(7.))
366 .bg(theme::surface_raised()),
367 )
368 .child(bar(140. + 30. * (index % 3) as f32)),
369 )
370 .child(column(Some(MODIFIED_WIDTH)).child(bar(50.)))
371 .child(column(Some(MODIFIED_BY_WIDTH)).child(bar(90.)))
372 .child(column(Some(SIZE_WIDTH)).child(bar(40.)))
373 }))
374}
375
376fn centered() -> Div {
377 div()
378 .w_full()
379 .flex_1()
380 .flex()
381 .items_center()
382 .justify_center()
383 .text_size(px(13.))
384 .text_color(theme::text_muted())
385}
386
387fn centered_note(text: &'static str) -> Div {
388 centered().child(text)
389}
390
391fn new_folder_row(input: &Entity<InputState>) -> Div {
392 h_flex()
393 .w_full()
394 .flex_none()
395 .px(px(SIDE_PADDING))
396 .pb(px(8.))
397 .gap(px(8.))
398 .items_center()
399 .child(icon(IconName::Folder, 18., theme::accent_text()))
400 .child(
401 div()
402 .w(px(NEW_FOLDER_WIDTH))
403 .h(px(30.))
404 .px(px(2.))
405 .flex()
406 .items_center()
407 .rounded(px(8.))
408 .bg(theme::surface())
409 .border_1()
410 .border_color(theme::accent())
411 .child(Input::new(input).appearance(false).bordered(false)),
412 )
413 .child(
414 div()
415 .text_size(px(11.5))
416 .text_color(theme::text_faint())
417 .child("Enter to create, Esc to cancel"),
418 )
419}
420
421pub fn render_library(view: LibraryView, run: &SharedRun) -> AnyElement {
422 let toolbar = view.path.map(|path| {
423 h_flex()
424 .flex_1()
425 .min_w_0()
426 .gap(px(4.))
427 .items_center()
428 .child(crumb_trail(path, run))
429 .child(tool_button(
430 "library-new-folder",
431 "New folder",
432 IconName::Plus,
433 SharedCommand::ToggleNewFolder,
434 run,
435 ))
436 .child(tool_button(
437 "library-upload",
438 "Upload",
439 IconName::Upload,
440 SharedCommand::Upload,
441 run,
442 ))
443 .child(tool_button(
444 "library-open-sharepoint",
445 "Open in SharePoint \u{2197}",
446 IconName::ExternalLink,
447 SharedCommand::OpenSharePoint,
448 run,
449 ))
450 .into_any_element()
451 });
452 let body = match view.listing {
453 Listing::Loading => skeleton_rows().into_any_element(),
454 Listing::Failed(reason) => {
455 let retry = run.clone();
456 centered()
457 .flex_col()
458 .gap(px(8.))
459 .child(
460 div()
461 .text_color(theme::red_soft())
462 .child(format!("Could not load the files: {reason}")),
463 )
464 .child(
465 Button::new("library-retry")
466 .ghost()
467 .compact()
468 .label("Retry")
469 .on_click(move |_, window, cx| retry(SharedCommand::Retry, window, cx)),
470 )
471 .into_any_element()
472 }
473 Listing::Ready(entries) if entries.is_empty() => {
474 centered_note(EMPTY_LIBRARY).into_any_element()
475 }
476 Listing::Ready(entries) => v_flex()
477 .id("library-rows")
478 .w_full()
479 .flex_1()
480 .min_h_0()
481 .overflow_y_scrollbar()
482 .children(entries.iter().enumerate().map(|(index, entry)| {
483 let download = view
484 .download_states
485 .get(index)
486 .and_then(|state| download_label(state.as_ref()));
487 entry_row(index, entry, download, run)
488 }))
489 .into_any_element(),
490 };
491 v_flex()
492 .size_full()
493 .child(header(SharedScope::Library, true, toolbar, run))
494 .children(view.new_folder.map(new_folder_row))
495 .children(view.status.map(|status| {
496 div()
497 .px(px(SIDE_PADDING))
498 .pb(px(6.))
499 .text_size(px(12.))
500 .text_color(theme::accent_text())
501 .child(status.to_owned())
502 }))
503 .child(column_header())
504 .child(body)
505 .into_any_element()
506}
507
508fn chip(
509 id: &'static str,
510 label: &'static str,
511 active: bool,
512 command: SharedCommand,
513 run: &SharedRun,
514) -> Stateful<Div> {
515 let run = run.clone();
516 div()
517 .id(id)
518 .px(px(12.))
519 .py(px(4.))
520 .rounded_full()
521 .cursor_pointer()
522 .text_size(px(12.))
523 .bg(if active {
524 theme::accent()
525 } else {
526 theme::surface_raised()
527 })
528 .text_color(if active {
529 theme::on_accent()
530 } else {
531 theme::text_soft()
532 })
533 .on_click(move |_, window, cx| run(command, window, cx))
534 .child(label)
535}
536
537fn chips(filter: MessageFilter, run: &SharedRun) -> AnyElement {
538 h_flex()
539 .gap(px(8.))
540 .child(chip(
541 "shared-filter-all",
542 "All",
543 filter == MessageFilter::All,
544 SharedCommand::Filter(MessageFilter::All),
545 run,
546 ))
547 .child(chip(
548 "shared-filter-files",
549 "Files",
550 filter == MessageFilter::Files,
551 SharedCommand::Filter(MessageFilter::Files),
552 run,
553 ))
554 .child(chip(
555 "shared-filter-links",
556 "Links",
557 filter == MessageFilter::Links,
558 SharedCommand::Filter(MessageFilter::Links),
559 run,
560 ))
561 .into_any_element()
562}
563
564fn item_badge(item: &SharedItem) -> Div {
565 match &item.content {
566 SharedContent::File(card) => file_badge(card.kind, BADGE_SIZE),
567 SharedContent::Link(_) => div()
568 .size(px(BADGE_SIZE))
569 .flex_none()
570 .flex()
571 .items_center()
572 .justify_center()
573 .rounded(px(7.))
574 .bg(theme::surface_raised())
575 .child(icon(IconName::Link, 14., theme::text_muted())),
576 }
577}
578
579pub fn item_row(
580 index: usize,
581 item: &SharedItem,
582 download: Option<String>,
583 run: &SharedRun,
584) -> Stateful<Div> {
585 let (open_run, post_run) = (run.clone(), run.clone());
586 let post =
587 (!item.post_label.is_empty()).then(|| format!("\u{201c}{}\u{201d}", item.post_label));
588 let source = h_flex()
589 .gap(px(4.))
590 .text_size(px(12.))
591 .text_color(theme::text_muted())
592 .child(format!("{} in", item.author))
593 .children(post.map(|label| {
594 div()
595 .id(ElementId::NamedInteger("shared-post".into(), index as u64))
596 .truncate()
597 .cursor_pointer()
598 .hover(|post| post.text_color(theme::accent_text()))
599 .on_click(move |_, window, cx| {
600 cx.stop_propagation();
601 post_run(SharedCommand::JumpToPost(index), window, cx)
602 })
603 .child(label)
604 }));
605 h_flex()
606 .id(ElementId::NamedInteger("shared-item".into(), index as u64))
607 .w_full()
608 .flex_none()
609 .px(px(SIDE_PADDING))
610 .py(px(6.))
611 .gap(px(8.))
612 .items_center()
613 .cursor_pointer()
614 .hover(|row| row.bg(theme::row_hover()))
615 .on_click(move |_, window, cx| open_run(SharedCommand::OpenItem(index), window, cx))
616 .child(item_badge(item))
617 .child(
618 v_flex()
619 .flex_1()
620 .min_w_0()
621 .child(
622 div()
623 .truncate()
624 .text_size(px(13.))
625 .text_color(theme::text())
626 .child(item.name().to_owned()),
627 )
628 .child(source),
629 )
630 .children(download.map(|label| {
631 div()
632 .flex_none()
633 .text_size(px(11.5))
634 .text_color(theme::accent_text())
635 .child(label)
636 }))
637}
638
639pub fn render_messages(view: MessagesView, run: &SharedRun) -> AnyElement {
640 let now = Local::now();
641 let groups = grouped_items(
642 view.items,
643 view.filter,
644 now.date_naive(),
645 now.offset().fix(),
646 );
647 let body = if groups.is_empty() {
648 centered_note(EMPTY_MESSAGES).into_any_element()
649 } else {
650 v_flex()
651 .id("shared-messages")
652 .w_full()
653 .flex_1()
654 .min_h_0()
655 .overflow_y_scrollbar()
656 .children(groups.into_iter().map(|(group, indices)| {
657 v_flex()
658 .w_full()
659 .child(
660 div()
661 .px(px(SIDE_PADDING))
662 .pt(px(12.))
663 .pb(px(4.))
664 .text_size(px(11.5))
665 .font_weight(FontWeight::SEMIBOLD)
666 .text_color(theme::text_faint())
667 .child(group.label()),
668 )
669 .children(indices.into_iter().map(|index| {
670 let download = view
671 .download_states
672 .get(index)
673 .and_then(|state| download_label(state.as_ref()));
674 item_row(index, &view.items[index], download, run)
675 }))
676 }))
677 .into_any_element()
678 };
679 v_flex()
680 .size_full()
681 .child(header(
682 SharedScope::Messages,
683 view.has_library,
684 Some(chips(view.filter, run)),
685 run,
686 ))
687 .child(body)
688 .into_any_element()
689}