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