BlueGit

ict rusty-teams

Files

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

card_media.rs

card_media.rs 2.4 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::tooltip::Tooltip;
3use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::*;
5use teams_core::CardMedia;
6
7use super::widgets::icon;
8use crate::theme;
9
10const MEDIA_WIDTH: f32 = 360.;
11const MEDIA_HEIGHT: f32 = 202.;
12const MEDIA_RADIUS: f32 = 6.;
13const PLAY_BUTTON: f32 = 52.;
14const PLAY_GLYPH: f32 = 24.;
15const PLAY_BACKDROP: f32 = 0.6;
16const HOVER_TINT: f32 = 0.12;
17
18pub fn media_view(media: &CardMedia, id: &str) -> AnyElement {
19 let source_url = media.source_url.clone();
20 let alt_text = media.alt_text.clone();
21 let poster = match &media.poster_url {
22 Some(url) => img(url.clone())
23 .size_full()
24 .rounded(px(MEDIA_RADIUS))
25 .object_fit(ObjectFit::Cover)
26 .with_loading(placeholder)
27 .with_fallback(placeholder)
28 .into_any_element(),
29 None => placeholder(),
30 };
31 div()
32 .id(ElementId::Name(format!("{id}-media").into()))
33 .group(SharedString::from(format!("{id}-media")))
34 .relative()
35 .w(px(MEDIA_WIDTH))
36 .h(px(MEDIA_HEIGHT))
37 .max_w(relative(1.))
38 .flex_none()
39 .overflow_hidden()
40 .rounded(px(MEDIA_RADIUS))
41 .cursor_pointer()
42 .when_some(alt_text, |frame, alt_text| {
43 frame.tooltip(move |window, cx| Tooltip::new(alt_text.clone()).build(window, cx))
44 })
45 .on_click(move |_, _, cx| {
46 cx.stop_propagation();
47 cx.open_url(&source_url);
48 })
49 .child(poster)
50 .child(
51 div()
52 .absolute()
53 .inset_0()
54 .flex()
55 .items_center()
56 .justify_center()
57 .group_hover(SharedString::from(format!("{id}-media")), |tint| {
58 tint.bg(gpui_kit::white().opacity(HOVER_TINT))
59 })
60 .child(
61 div()
62 .size(px(PLAY_BUTTON))
63 .flex()
64 .items_center()
65 .justify_center()
66 .rounded_full()
67 .bg(gpui_kit::black().opacity(PLAY_BACKDROP))
68 .child(icon(IconName::Play, PLAY_GLYPH, gpui_kit::white())),
69 ),
70 )
71 .into_any_element()
72}
73
74fn placeholder() -> AnyElement {
75 div()
76 .size_full()
77 .bg(theme::surface_raised())
78 .into_any_element()
79}