BlueGit

ict rusty-teams

Files

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

pin_banner.rs

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