BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-ui-kit / src / widgets.rs

widgets.rs

widgets.rs 6.4 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::Icon;
3use gpui_kit::*;
4
5use app_base::format;
6use app_base::theme;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Unread {
10 None,
11 Silent,
12 Dot,
13 Count(u32),
14}
15
16impl Unread {
17 pub fn is_unread(self) -> bool {
18 self != Unread::None
19 }
20}
21
22pub fn icon(name: IconName, size: f32, color: Hsla) -> Icon {
23 Icon::new(name).size(px(size)).text_color(color)
24}
25
26pub fn menu_icon(name: IconName) -> Icon {
27 Icon::new(name)
28}
29
30pub fn symbol(name: &str, size: f32, color: Hsla) -> Icon {
31 Icon::empty()
32 .path(format!("symbols/{name}.svg"))
33 .size(px(size))
34 .text_color(color)
35}
36
37#[derive(Clone, Copy)]
38pub enum Corner {
39 TopRight,
40 BottomRight,
41}
42
43pub fn corner_badge(corner: Corner, overhang: f32) -> Div {
44 let badge = div().absolute().right(px(-overhang));
45 match corner {
46 Corner::TopRight => badge.top(px(-overhang)),
47 Corner::BottomRight => badge.bottom(px(-overhang)),
48 }
49}
50
51#[derive(Clone, Copy)]
52pub struct BadgeMetrics {
53 pub height: f32,
54 pub padding_x: f32,
55 pub font_size: f32,
56}
57
58pub const COUNT_BADGE: BadgeMetrics = BadgeMetrics {
59 height: 18.,
60 padding_x: 5.,
61 font_size: 11.,
62};
63
64pub const RAIL_BADGE: BadgeMetrics = BadgeMetrics {
65 height: 16.,
66 padding_x: 4.,
67 font_size: 10.,
68};
69
70pub fn badge_selector(text: &str) -> String {
71 format!("count-badge/{text}")
72}
73
74pub fn badge_text_selector(text: &str) -> String {
75 format!("count-badge/{text}/text")
76}
77
78pub fn badge_pill(count: u32, metrics: BadgeMetrics, background: Hsla, foreground: Hsla) -> Div {
79 let text = format::badge_text(count);
80 div()
81 .debug_selector(|| badge_selector(&text))
82 .relative()
83 .min_w(px(metrics.height))
84 .h(px(metrics.height))
85 .px(px(metrics.padding_x))
86 .flex()
87 .flex_none()
88 .items_center()
89 .justify_center()
90 .rounded(px(metrics.height / 2.))
91 .bg(background)
92 .text_size(px(metrics.font_size))
93 .line_height(px(metrics.height))
94 .font_weight(FontWeight::BOLD)
95 .child(
96 div()
97 .debug_selector(|| badge_text_selector(&text))
98 .flex_none()
99 .text_color(transparent_black())
100 .child(text.clone()),
101 )
102 .child(badge_ink(text, metrics.font_size, foreground))
103}
104
105fn badge_ink(text: String, font_size: f32, color: Hsla) -> impl IntoElement {
106 canvas(
107 |_, _, _| {},
108 move |bounds, _, window, cx| {
109 let run = TextRun {
110 len: text.len(),
111 font: badge_font(window),
112 color,
113 background_color: None,
114 underline: None,
115 strikethrough: None,
116 };
117 let line =
118 window
119 .text_system()
120 .shape_line(text.clone().into(), px(font_size), &[run], None);
121 let origin = badge_text_origin(window, &text, font_size, bounds);
122 let _ = line.paint(
123 point(origin.x, origin.y - line.ascent),
124 line.ascent + line.descent,
125 TextAlign::Left,
126 None,
127 window,
128 cx,
129 );
130 },
131 )
132 .absolute()
133 .inset_0()
134}
135
136fn badge_font(window: &Window) -> Font {
137 let mut font = window.text_style().font();
138 font.weight = FontWeight::BOLD;
139 font
140}
141
142/// Pen origin on the baseline that puts the ink centre of `text` on the centre of `bounds`.
143pub fn badge_text_origin(
144 window: &Window,
145 text: &str,
146 font_size: f32,
147 bounds: Bounds<Pixels>,
148) -> Point<Pixels> {
149 let (ink_centre, cap_height) = badge_ink_centre(window, text, font_size);
150 let centre = bounds.center();
151 point(
152 px(f32::from(centre.x) - ink_centre),
153 px(f32::from(centre.y) + cap_height / 2.),
154 )
155}
156
157/// Horizontal ink centre from the pen origin, and the cap height.
158pub fn badge_ink_centre(window: &Window, text: &str, font_size: f32) -> (f32, f32) {
159 let text_system = window.text_system();
160 let font_id = text_system.resolve_font(&badge_font(window));
161 let size = px(font_size);
162 let cap_height = f32::from(text_system.cap_height(font_id, size));
163 let mut pen = 0.;
164 let mut ink_left = None;
165 let mut ink_right = 0.;
166 for character in text.chars() {
167 let advance = text_system
168 .advance(font_id, size, character)
169 .map_or(0., |advance| f32::from(advance.width));
170 if let Ok(ink) = text_system.typographic_bounds(font_id, size, character) {
171 let left = pen + f32::from(ink.origin.x);
172 ink_left.get_or_insert(left);
173 ink_right = left + f32::from(ink.size.width);
174 }
175 pen += advance;
176 }
177 let ink_centre = ink_left.map_or(pen / 2., |left| (left + ink_right) / 2.);
178 (ink_centre, cap_height)
179}
180
181pub fn count_badge(count: u32, muted: bool) -> Div {
182 let (background, foreground) = if muted {
183 (theme::badge_muted(), theme::text_strong())
184 } else {
185 (theme::accent(), theme::on_accent())
186 };
187 badge_pill(count, COUNT_BADGE, background, foreground)
188}
189
190pub fn page_header(title: &str) -> Div {
191 div()
192 .h(px(52.))
193 .flex()
194 .flex_none()
195 .items_center()
196 .gap(px(6.))
197 .pl(px(16.))
198 .pr(px(12.))
199 .child(
200 div()
201 .flex_1()
202 .text_size(px(16.))
203 .font_weight(FontWeight::BOLD)
204 .child(title.to_owned()),
205 )
206}
207
208pub fn dot(size: f32) -> Div {
209 div()
210 .size(px(size))
211 .flex_none()
212 .rounded_full()
213 .bg(theme::accent())
214}
215
216pub fn unread_marker(unread: Unread, muted: bool) -> Option<Div> {
217 match unread {
218 Unread::None | Unread::Silent => None,
219 Unread::Dot if muted => Some(dot(8.).bg(theme::badge_muted())),
220 Unread::Dot => Some(dot(8.)),
221 Unread::Count(count) => Some(count_badge(count, muted)),
222 }
223}
224
225const SMILE_SVG: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"/><path d="M8.5 14.5a4.5 4.5 0 0 0 7 0M9 9.5h.01M15 9.5h.01"/></svg>"#;
226
227#[allow(dead_code)]
228pub fn smile_icon(size: f32, color: Hsla) -> Icon {
229 Icon::default()
230 .data(SMILE_SVG)
231 .size(px(size))
232 .text_color(color)
233}