BlueGit

ict rusty-teams

Files

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

widgets.rs

widgets.rs 5.5 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 .min_w(px(metrics.height))
83 .h(px(metrics.height))
84 .px(px(metrics.padding_x))
85 .flex()
86 .flex_none()
87 .items_center()
88 .justify_center()
89 .rounded(px(metrics.height / 2.))
90 .bg(background)
91 .text_color(foreground)
92 .text_size(px(metrics.font_size))
93 .line_height(px(metrics.height))
94 .font_weight(FontWeight::BOLD)
95 .child(BadgeText {
96 text,
97 font_size: metrics.font_size,
98 line_height: metrics.height,
99 })
100}
101
102#[derive(IntoElement)]
103struct BadgeText {
104 text: String,
105 font_size: f32,
106 line_height: f32,
107}
108
109impl RenderOnce for BadgeText {
110 fn render(self, window: &mut Window, _: &mut App) -> impl IntoElement {
111 let (shift_x, shift_y) = badge_ink_offset(window, &self.text, self.font_size);
112 div()
113 .debug_selector(|| badge_text_selector(&self.text))
114 .relative()
115 .left(px(-shift_x))
116 .top(px(-shift_y))
117 .flex_none()
118 .h(px(self.line_height))
119 .child(self.text)
120 }
121}
122
123pub fn badge_ink_offset(window: &Window, text: &str, font_size: f32) -> (f32, f32) {
124 let mut font = window.text_style().font();
125 font.weight = FontWeight::BOLD;
126 let text_system = window.text_system();
127 let font_id = text_system.resolve_font(&font);
128 let size = px(font_size);
129 let ascent = f32::from(text_system.ascent(font_id, size));
130 let descent = f32::from(text_system.descent(font_id, size)).abs();
131 let cap_height = f32::from(text_system.cap_height(font_id, size));
132 let shift_y = (ascent - descent - cap_height) / 2.;
133 let mut pen = 0.;
134 let mut ink_left = None;
135 let mut ink_right = 0.;
136 for character in text.chars() {
137 let advance = text_system
138 .advance(font_id, size, character)
139 .map_or(0., |advance| f32::from(advance.width));
140 if let Ok(bounds) = text_system.typographic_bounds(font_id, size, character) {
141 let left = pen + f32::from(bounds.origin.x);
142 ink_left.get_or_insert(left);
143 ink_right = left + f32::from(bounds.size.width);
144 }
145 pen += advance;
146 }
147 let shift_x = ink_left.map_or(0., |left| (left + ink_right) / 2. - pen / 2.);
148 (shift_x, shift_y)
149}
150
151pub fn count_badge(count: u32, muted: bool) -> Div {
152 let (background, foreground) = if muted {
153 (theme::badge_muted(), theme::text_strong())
154 } else {
155 (theme::accent(), theme::on_accent())
156 };
157 badge_pill(count, COUNT_BADGE, background, foreground)
158}
159
160pub fn page_header(title: &str) -> Div {
161 div()
162 .h(px(52.))
163 .flex()
164 .flex_none()
165 .items_center()
166 .gap(px(6.))
167 .pl(px(16.))
168 .pr(px(12.))
169 .child(
170 div()
171 .flex_1()
172 .text_size(px(16.))
173 .font_weight(FontWeight::BOLD)
174 .child(title.to_owned()),
175 )
176}
177
178pub fn dot(size: f32) -> Div {
179 div()
180 .size(px(size))
181 .flex_none()
182 .rounded_full()
183 .bg(theme::accent())
184}
185
186pub fn unread_marker(unread: Unread, muted: bool) -> Option<Div> {
187 match unread {
188 Unread::None | Unread::Silent => None,
189 Unread::Dot if muted => Some(dot(8.).bg(theme::badge_muted())),
190 Unread::Dot => Some(dot(8.)),
191 Unread::Count(count) => Some(count_badge(count, muted)),
192 }
193}
194
195const 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>"#;
196
197#[allow(dead_code)]
198pub fn smile_icon(size: f32, color: Hsla) -> Icon {
199 Icon::default()
200 .data(SMILE_SVG)
201 .size(px(size))
202 .text_color(color)
203}