BlueGit

ict rusty-teams

Files

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

pin_banner.rs

pin_banner.rs 5.3 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{h_flex, tooltip::Tooltip};
3use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::*;
5
6use app_ui_kit::layout::ROW_PADDING;
7
8use super::message_actions::Action;
9use super::widgets::icon;
10use crate::theme;
11
12const BANNER_HEIGHT: f32 = 40.;
13pub const PIN_BANNER_TAG: &str = "shell/islands/main/pin-banner";
14const BANNER_GAP: f32 = 8.;
15const BANNER_PADDING: f32 = 12.;
16pub const BANNER_INSET: f32 = ROW_PADDING - BANNER_PADDING;
17const BUTTON_SIZE: f32 = 28.;
18const FALLBACK_PREVIEW: &str = "Pinned message";
19
20pub struct PinBannerData {
21 pub author: Option<String>,
22 pub preview: Option<String>,
23 pub position: usize,
24 pub total: usize,
25}
26
27pub struct PinBannerActions {
28 pub open: Action,
29 pub previous: Action,
30 pub next: Action,
31 pub unpin: Action,
32}
33
34pub fn pin_banner_inset(shown: bool) -> Pixels {
35 px(if shown {
36 BANNER_INSET + BANNER_HEIGHT + BANNER_GAP
37 } else {
38 0.
39 })
40}
41
42pub fn pin_banner_overlay(banner: AnyElement) -> AnyElement {
43 div()
44 .absolute()
45 .top(px(BANNER_INSET))
46 .left(px(BANNER_INSET))
47 .right(px(BANNER_INSET))
48 .child(banner)
49 .into_any_element()
50}
51
52pub fn cycle_pin(index: usize, length: usize, delta: isize) -> usize {
53 if length == 0 {
54 return 0;
55 }
56 (index as isize + delta).rem_euclid(length as isize) as usize
57}
58
59pub fn position_label(position: usize, total: usize) -> String {
60 format!("{} of {}", position + 1, total)
61}
62
63fn icon_button(id: &'static str, glyph: IconName, action: Action) -> Stateful<Div> {
64 div()
65 .id(id)
66 .size(px(BUTTON_SIZE))
67 .flex()
68 .flex_none()
69 .items_center()
70 .justify_center()
71 .rounded(px(7.))
72 .cursor_pointer()
73 .hover(|button| button.bg(theme::popup_hover()))
74 .child(icon(glyph, 14., theme::text_muted()))
75 .on_click(move |_, window, cx| {
76 cx.stop_propagation();
77 action(window, cx);
78 })
79}
80
81pub fn render_pin_banner(data: PinBannerData, actions: PinBannerActions) -> AnyElement {
82 let PinBannerData {
83 author,
84 preview,
85 position,
86 total,
87 } = data;
88 let PinBannerActions {
89 open,
90 previous,
91 next,
92 unpin,
93 } = actions;
94 h_flex()
95 .id("pin-banner")
96 .debug_selector(|| PIN_BANNER_TAG.into())
97 .group("pin-banner")
98 .occlude()
99 .w_full()
100 .h(px(BANNER_HEIGHT))
101 .pl(px(BANNER_PADDING))
102 .pr(px(6.))
103 .gap(px(8.))
104 .items_center()
105 .rounded(px(8.))
106 .bg(theme::row_hover())
107 .shadow(theme::toast_shadow())
108 .cursor_pointer()
109 .hover(|banner| banner.bg(theme::surface_raised()))
110 .on_click(move |_, window, cx| open(window, cx))
111 .child(icon(IconName::Pin, 14., theme::accent_text()))
112 .child(
113 div()
114 .flex_none()
115 .text_size(px(12.))
116 .text_color(theme::text_muted())
117 .child("Pinned"),
118 )
119 .when_some(author, |banner, author| {
120 banner.child(
121 div()
122 .flex_none()
123 .text_size(px(13.))
124 .font_weight(FontWeight::SEMIBOLD)
125 .text_color(theme::text())
126 .child(author),
127 )
128 })
129 .child(
130 div()
131 .flex_1()
132 .min_w_0()
133 .truncate()
134 .text_size(px(13.))
135 .text_color(theme::text_soft())
136 .child(preview.unwrap_or_else(|| FALLBACK_PREVIEW.to_owned())),
137 )
138 .when(total > 1, |banner| {
139 banner
140 .child(
141 div()
142 .flex_none()
143 .text_size(px(12.))
144 .text_color(theme::text_muted())
145 .child(position_label(position, total)),
146 )
147 .child(icon_button("pin-previous", IconName::ChevronUp, previous))
148 .child(icon_button("pin-next", IconName::ChevronDown, next))
149 })
150 .child(
151 icon_button("pin-unpin", IconName::X, unpin)
152 .opacity(0.)
153 .group_hover("pin-banner", |button| button.opacity(1.))
154 .tooltip(|window, cx| Tooltip::new("Unpin for everyone").build(window, cx)),
155 )
156 .into_any_element()
157}
158
159#[cfg(test)]
160mod tests {
161 use super::{BANNER_HEIGHT, cycle_pin, pin_banner_inset, position_label};
162 use gpui_kit::px;
163
164 #[test]
165 fn cycling_wraps_around_both_ends() {
166 assert_eq!(cycle_pin(0, 3, 1), 1);
167 assert_eq!(cycle_pin(2, 3, 1), 0);
168 assert_eq!(cycle_pin(0, 3, -1), 2);
169 assert_eq!(cycle_pin(1, 3, -1), 0);
170 }
171
172 #[test]
173 fn cycling_an_empty_or_single_list_stays_put() {
174 assert_eq!(cycle_pin(0, 0, 1), 0);
175 assert_eq!(cycle_pin(0, 1, 1), 0);
176 assert_eq!(cycle_pin(0, 1, -1), 0);
177 }
178
179 #[test]
180 fn inset_clears_the_banner_only_while_it_shows() {
181 assert!(pin_banner_inset(true) > px(BANNER_HEIGHT));
182 assert_eq!(pin_banner_inset(false), px(0.));
183 }
184
185 #[test]
186 fn position_is_one_based() {
187 assert_eq!(position_label(0, 3), "1 of 3");
188 }
189}