BlueGit

ict rusty-teams

Files

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

widgets.rs

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