fix(ui): centre badge digits and match the Teams taskbar badge
- rail and count badge text painted at its ink centre; gpui rounds text box widths up, which pushed digits like 8 a pixel left - taskbar badge uses the Teams red, bold and larger digits Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -2,12 +2,12 @@ use std::sync::{Arc, OnceLock};
2
2
3
3
use resvg::{tiny_skia, usvg};
4
4
5
const FILL: &str = "#C4314B";
5
const FILL: &str = "#C42B1C";
6
6
const INK: &str = "#FFFFFF";
7
const FONT: &[u8] = include_bytes!("../../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf");
7
const FONT: &[u8] = include_bytes!("../../../app/assets/fonts/GoogleSansFlex-Bold.ttf");
8
8
const FONT_FAMILY: &str = "Google Sans Flex";
9
const SINGLE_GLYPH_SCALE: f32 = 0.62;
10
const WIDE_TEXT_SCALE: f32 = 0.5;
9
const SINGLE_GLYPH_SCALE: f32 = 0.68;
10
const WIDE_TEXT_SCALE: f32 = 0.58;
11
11
const BASELINE_SHIFT: f32 = 0.36;
12
12
13
13
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -51,7 +51,7 @@ fn badge_svg(text: &str, size: u32) -> String {
51
51
};
52
52
let baseline = radius + font_size * BASELINE_SHIFT;
53
53
format!(
54
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}"><circle cx="{radius}" cy="{radius}" r="{radius}" fill="{FILL}"/><text x="{radius}" y="{baseline}" text-anchor="middle" font-family="{FONT_FAMILY}" font-weight="600" font-size="{font_size}" fill="{INK}">{text}</text></svg>"#
54
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}"><circle cx="{radius}" cy="{radius}" r="{radius}" fill="{FILL}"/><text x="{radius}" y="{baseline}" text-anchor="middle" font-family="{FONT_FAMILY}" font-weight="700" font-size="{font_size}" fill="{INK}">{text}</text></svg>"#
55
55
)
56
56
}
57
57
@@ -105,7 +105,7 @@ mod tests {
105
105
.chunks(4)
106
106
.any(|pixel| pixel == [blue, green, red, 0xff])
107
107
};
108
assert!(has(0x4b, 0x31, 0xc4));
108
assert!(has(0x1c, 0x2b, 0xc4));
109
109
assert!(has(0xff, 0xff, 0xff));
110
110
}
111
111
@@ -2,12 +2,12 @@ use std::sync::{Arc, OnceLock};2
2
3
use resvg::{tiny_skia, usvg};3
use resvg::{tiny_skia, usvg};4
4
5
const FILL: &str = "#C4314B";5
const FILL: &str = "#C42B1C";6
const INK: &str = "#FFFFFF";6
const INK: &str = "#FFFFFF";7
const FONT: &[u8] = include_bytes!("../../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf");7
const FONT: &[u8] = include_bytes!("../../../app/assets/fonts/GoogleSansFlex-Bold.ttf");8
const FONT_FAMILY: &str = "Google Sans Flex";8
const FONT_FAMILY: &str = "Google Sans Flex";9
const SINGLE_GLYPH_SCALE: f32 = 0.62;9
const SINGLE_GLYPH_SCALE: f32 = 0.68;10
const WIDE_TEXT_SCALE: f32 = 0.5;10
const WIDE_TEXT_SCALE: f32 = 0.58;11
const BASELINE_SHIFT: f32 = 0.36;11
const BASELINE_SHIFT: f32 = 0.36;12
12
13
#[derive(Debug, Clone, PartialEq, Eq)]13
#[derive(Debug, Clone, PartialEq, Eq)]@@ -51,7 +51,7 @@ fn badge_svg(text: &str, size: u32) -> String {51
};51
};52
let baseline = radius + font_size * BASELINE_SHIFT;52
let baseline = radius + font_size * BASELINE_SHIFT;53
format!(53
format!(54
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}"><circle cx="{radius}" cy="{radius}" r="{radius}" fill="{FILL}"/><text x="{radius}" y="{baseline}" text-anchor="middle" font-family="{FONT_FAMILY}" font-weight="600" font-size="{font_size}" fill="{INK}">{text}</text></svg>"#54
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}"><circle cx="{radius}" cy="{radius}" r="{radius}" fill="{FILL}"/><text x="{radius}" y="{baseline}" text-anchor="middle" font-family="{FONT_FAMILY}" font-weight="700" font-size="{font_size}" fill="{INK}">{text}</text></svg>"#55
)55
)56
}56
}57
57
@@ -105,7 +105,7 @@ mod tests {105
.chunks(4)105
.chunks(4)106
.any(|pixel| pixel == [blue, green, red, 0xff])106
.any(|pixel| pixel == [blue, green, red, 0xff])107
};107
};108
assert!(has(0x4b, 0x31, 0xc4));108
assert!(has(0x1c, 0x2b, 0xc4));109
assert!(has(0xff, 0xff, 0xff));109
assert!(has(0xff, 0xff, 0xff));110
}110
}111
111
@@ -79,6 +79,7 @@ pub fn badge_pill(count: u32, metrics: BadgeMetrics, background: Hsla, foregroun
79
79
let text = format::badge_text(count);
80
80
div()
81
81
.debug_selector(|| badge_selector(&text))
82
.relative()
82
83
.min_w(px(metrics.height))
83
84
.h(px(metrics.height))
84
85
.px(px(metrics.padding_x))
@@ -88,48 +89,77 @@ pub fn badge_pill(count: u32, metrics: BadgeMetrics, background: Hsla, foregroun
88
89
.justify_center()
89
90
.rounded(px(metrics.height / 2.))
90
91
.bg(background)
91
.text_color(foreground)
92
92
.text_size(px(metrics.font_size))
93
93
.line_height(px(metrics.height))
94
94
.font_weight(FontWeight::BOLD)
95
.child(BadgeText {
96
text,
97
font_size: metrics.font_size,
98
line_height: metrics.height,
99
})
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
105
fn 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
136
fn badge_font(window: &Window) -> Font {
137
let mut font = window.text_style().font();
138
font.weight = FontWeight::BOLD;
139
font
100
140
}
101
141
102
#[derive(IntoElement)]
103
struct BadgeText {
104
text: String,
142
/// Pen origin on the baseline that puts the ink centre of `text` on the centre of `bounds`.
143
pub fn badge_text_origin(
144
window: &Window,
145
text: &str,
105
146
font_size: f32,
106
line_height: f32,
107
}
108
109
impl 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
123
pub 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;
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.
158
pub fn badge_ink_centre(window: &Window, text: &str, font_size: f32) -> (f32, f32) {
126
159
let text_system = window.text_system();
127
let font_id = text_system.resolve_font(&font);
160
let font_id = text_system.resolve_font(&badge_font(window));
128
161
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
162
let cap_height = f32::from(text_system.cap_height(font_id, size));
132
let shift_y = (ascent - descent - cap_height) / 2.;
133
163
let mut pen = 0.;
134
164
let mut ink_left = None;
135
165
let mut ink_right = 0.;
@@ -137,15 +167,15 @@ pub fn badge_ink_offset(window: &Window, text: &str, font_size: f32) -> (f32, f3
137
167
let advance = text_system
138
168
.advance(font_id, size, character)
139
169
.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);
170
if let Ok(ink) = text_system.typographic_bounds(font_id, size, character) {
171
let left = pen + f32::from(ink.origin.x);
142
172
ink_left.get_or_insert(left);
143
ink_right = left + f32::from(bounds.size.width);
173
ink_right = left + f32::from(ink.size.width);
144
174
}
145
175
pen += advance;
146
176
}
147
let shift_x = ink_left.map_or(0., |left| (left + ink_right) / 2. - pen / 2.);
148
(shift_x, shift_y)
177
let ink_centre = ink_left.map_or(pen / 2., |left| (left + ink_right) / 2.);
178
(ink_centre, cap_height)
149
179
}
150
180
151
181
pub fn count_badge(count: u32, muted: bool) -> Div {
@@ -79,6 +79,7 @@ pub fn badge_pill(count: u32, metrics: BadgeMetrics, background: Hsla, foregroun79
let text = format::badge_text(count);79
let text = format::badge_text(count);80
div()80
div()81
.debug_selector(|| badge_selector(&text))81
.debug_selector(|| badge_selector(&text))82
.relative()82
.min_w(px(metrics.height))83
.min_w(px(metrics.height))83
.h(px(metrics.height))84
.h(px(metrics.height))84
.px(px(metrics.padding_x))85
.px(px(metrics.padding_x))@@ -88,48 +89,77 @@ pub fn badge_pill(count: u32, metrics: BadgeMetrics, background: Hsla, foregroun88
.justify_center()89
.justify_center()89
.rounded(px(metrics.height / 2.))90
.rounded(px(metrics.height / 2.))90
.bg(background)91
.bg(background)91
.text_color(foreground)92
.text_size(px(metrics.font_size))92
.text_size(px(metrics.font_size))93
.line_height(px(metrics.height))93
.line_height(px(metrics.height))94
.font_weight(FontWeight::BOLD)94
.font_weight(FontWeight::BOLD)95
.child(BadgeText {95
.child(96
text,96
div()97
font_size: metrics.font_size,97
.debug_selector(|| badge_text_selector(&text))98
line_height: metrics.height,98
.flex_none()99
})99
.text_color(transparent_black())100
.child(text.clone()),101
)102
.child(badge_ink(text, metrics.font_size, foreground))103
}104
105
fn 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
window119
.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
136
fn badge_font(window: &Window) -> Font {137
let mut font = window.text_style().font();138
font.weight = FontWeight::BOLD;139
font100
}140
}101
141
102
#[derive(IntoElement)]142
/// Pen origin on the baseline that puts the ink centre of `text` on the centre of `bounds`.103
struct BadgeText {143
pub fn badge_text_origin(104
text: String,144
window: &Window,145
text: &str,105
font_size: f32,146
font_size: f32,106
line_height: f32,147
bounds: Bounds<Pixels>,107
}148
) -> Point<Pixels> {108
149
let (ink_centre, cap_height) = badge_ink_centre(window, text, font_size);109
impl RenderOnce for BadgeText {150
let centre = bounds.center();110
fn render(self, window: &mut Window, _: &mut App) -> impl IntoElement {151
point(111
let (shift_x, shift_y) = badge_ink_offset(window, &self.text, self.font_size);152
px(f32::from(centre.x) - ink_centre),112
div()153
px(f32::from(centre.y) + cap_height / 2.),113
.debug_selector(|| badge_text_selector(&self.text))154
)114
.relative()155
}115
.left(px(-shift_x))156
116
.top(px(-shift_y))157
/// Horizontal ink centre from the pen origin, and the cap height.117
.flex_none()158
pub fn badge_ink_centre(window: &Window, text: &str, font_size: f32) -> (f32, f32) {118
.h(px(self.line_height))119
.child(self.text)120
}121
}122
123
pub 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();159
let text_system = window.text_system();127
let font_id = text_system.resolve_font(&font);160
let font_id = text_system.resolve_font(&badge_font(window));128
let size = px(font_size);161
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));162
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.;163
let mut pen = 0.;134
let mut ink_left = None;164
let mut ink_left = None;135
let mut ink_right = 0.;165
let mut ink_right = 0.;@@ -137,15 +167,15 @@ pub fn badge_ink_offset(window: &Window, text: &str, font_size: f32) -> (f32, f3137
let advance = text_system167
let advance = text_system138
.advance(font_id, size, character)168
.advance(font_id, size, character)139
.map_or(0., |advance| f32::from(advance.width));169
.map_or(0., |advance| f32::from(advance.width));140
if let Ok(bounds) = text_system.typographic_bounds(font_id, size, character) {170
if let Ok(ink) = text_system.typographic_bounds(font_id, size, character) {141
let left = pen + f32::from(bounds.origin.x);171
let left = pen + f32::from(ink.origin.x);142
ink_left.get_or_insert(left);172
ink_left.get_or_insert(left);143
ink_right = left + f32::from(bounds.size.width);173
ink_right = left + f32::from(ink.size.width);144
}174
}145
pen += advance;175
pen += advance;146
}176
}147
let shift_x = ink_left.map_or(0., |left| (left + ink_right) / 2. - pen / 2.);177
let ink_centre = ink_left.map_or(pen / 2., |left| (left + ink_right) / 2.);148
(shift_x, shift_y)178
(ink_centre, cap_height)149
}179
}150
180
151
pub fn count_badge(count: u32, muted: bool) -> Div {181
pub fn count_badge(count: u32, muted: bool) -> Div {@@ -224,8 +224,8 @@ mod frames {
224
224
};
225
225
use crate::views::status_menu::{OWN_AVATAR_DISC, TITLE_AVATAR_SIZE};
226
226
use crate::views::widgets::{
227
COUNT_BADGE, RAIL_BADGE, badge_ink_offset, badge_pill, badge_selector, badge_text_selector,
228
count_badge,
227
COUNT_BADGE, RAIL_BADGE, badge_ink_centre, badge_pill, badge_selector, badge_text_origin,
228
badge_text_selector, count_badge,
229
229
};
230
230
use gpui_kit::MouseButton;
231
231
use gpui_kit::component::fluent::{
@@ -1994,7 +1994,14 @@ mod frames {
1994
1994
} else {
1995
1995
COUNT_BADGE.font_size
1996
1996
};
1997
let offset = visual.update(|window, _| badge_ink_offset(window, &text, font_size));
1997
let offset = visual.update(|window, _| {
1998
let origin = badge_text_origin(window, &text, font_size, pill);
1999
let (ink_centre, cap_height) = badge_ink_centre(window, &text, font_size);
2000
(
2001
f32::from(origin.x) + ink_centre - f32::from(label.center().x),
2002
f32::from(origin.y) - cap_height / 2. - f32::from(label.center().y),
2003
)
2004
});
1998
2005
found.extend(badge_violation(&text, pill, label, offset));
1999
2006
}
2000
2007
let found: Vec<String> = found
@@ -224,8 +224,8 @@ mod frames {224
};224
};225
use crate::views::status_menu::{OWN_AVATAR_DISC, TITLE_AVATAR_SIZE};225
use crate::views::status_menu::{OWN_AVATAR_DISC, TITLE_AVATAR_SIZE};226
use crate::views::widgets::{226
use crate::views::widgets::{227
COUNT_BADGE, RAIL_BADGE, badge_ink_offset, badge_pill, badge_selector, badge_text_selector,227
COUNT_BADGE, RAIL_BADGE, badge_ink_centre, badge_pill, badge_selector, badge_text_origin,228
count_badge,228
badge_text_selector, count_badge,229
};229
};230
use gpui_kit::MouseButton;230
use gpui_kit::MouseButton;231
use gpui_kit::component::fluent::{231
use gpui_kit::component::fluent::{@@ -1994,7 +1994,14 @@ mod frames {1994
} else {1994
} else {1995
COUNT_BADGE.font_size1995
COUNT_BADGE.font_size1996
};1996
};1997
let offset = visual.update(|window, _| badge_ink_offset(window, &text, font_size));1997
let offset = visual.update(|window, _| {1998
let origin = badge_text_origin(window, &text, font_size, pill);1999
let (ink_centre, cap_height) = badge_ink_centre(window, &text, font_size);2000
(2001
f32::from(origin.x) + ink_centre - f32::from(label.center().x),2002
f32::from(origin.y) - cap_height / 2. - f32::from(label.center().y),2003
)2004
});1998
found.extend(badge_violation(&text, pill, label, offset));2005
found.extend(badge_violation(&text, pill, label, offset));1999
}2006
}2000
let found: Vec<String> = found2007
let found: Vec<String> = found