BlueGit

ict rusty-teams

Files

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

pin_banner.rs

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