BlueGit

ict rusty-teams

Files

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

pin_banner.rs

pin_banner.rs 5.0 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 .bg(theme::row_hover())
99 .shadow(theme::toast_shadow())
100 .cursor_pointer()
101 .hover(|banner| banner.bg(theme::surface_raised()))
102 .on_click(move |_, window, cx| open(window, cx))
103 .child(icon(IconName::Pin, 14., theme::accent_text()))
104 .child(
105 div()
106 .flex_none()
107 .text_size(px(12.))
108 .text_color(theme::text_muted())
109 .child("Pinned"),
110 )
111 .when_some(author, |banner, author| {
112 banner.child(
113 div()
114 .flex_none()
115 .text_size(px(13.))
116 .font_weight(FontWeight::SEMIBOLD)
117 .text_color(theme::text())
118 .child(author),
119 )
120 })
121 .child(
122 div()
123 .flex_1()
124 .min_w_0()
125 .truncate()
126 .text_size(px(13.))
127 .text_color(theme::text_soft())
128 .child(preview.unwrap_or_else(|| FALLBACK_PREVIEW.to_owned())),
129 )
130 .when(total > 1, |banner| {
131 banner
132 .child(
133 div()
134 .flex_none()
135 .text_size(px(12.))
136 .text_color(theme::text_muted())
137 .child(position_label(position, total)),
138 )
139 .child(icon_button("pin-previous", IconName::ChevronUp, previous))
140 .child(icon_button("pin-next", IconName::ChevronDown, next))
141 })
142 .child(
143 icon_button("pin-unpin", IconName::X, unpin)
144 .opacity(0.)
145 .group_hover("pin-banner", |button| button.opacity(1.))
146 .tooltip(|window, cx| Tooltip::new("Unpin for everyone").build(window, cx)),
147 )
148 .into_any_element()
149}
150
151#[cfg(test)]
152mod tests {
153 use super::{BANNER_HEIGHT, cycle_pin, pin_banner_inset, position_label};
154 use gpui_kit::px;
155
156 #[test]
157 fn cycling_wraps_around_both_ends() {
158 assert_eq!(cycle_pin(0, 3, 1), 1);
159 assert_eq!(cycle_pin(2, 3, 1), 0);
160 assert_eq!(cycle_pin(0, 3, -1), 2);
161 assert_eq!(cycle_pin(1, 3, -1), 0);
162 }
163
164 #[test]
165 fn cycling_an_empty_or_single_list_stays_put() {
166 assert_eq!(cycle_pin(0, 0, 1), 0);
167 assert_eq!(cycle_pin(0, 1, 1), 0);
168 assert_eq!(cycle_pin(0, 1, -1), 0);
169 }
170
171 #[test]
172 fn inset_clears_the_banner_only_while_it_shows() {
173 assert!(pin_banner_inset(true) > px(BANNER_HEIGHT));
174 assert_eq!(pin_banner_inset(false), px(0.));
175 }
176
177 #[test]
178 fn position_is_one_based() {
179 assert_eq!(position_label(0, 3), "1 of 3");
180 }
181}