BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / badge.rs

badge.rs

badge.rs 5.0 KiB Download Raw
1pub const BADGE_SIZE: usize = 32;
2const GLYPH_WIDTH: usize = 5;
3const GLYPH_HEIGHT: usize = 7;
4const GLYPH_SCALE: usize = 2;
5const GLYPH_GAP: usize = 1;
6const NEUTRAL: [u8; 3] = [0xf4, 0xf4, 0xf5];
7const MENTION: [u8; 3] = [0xef, 0x44, 0x44];
8const INK: [u8; 3] = [0x0a, 0x0a, 0x0a];
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Badge {
12 pub text: String,
13 pub mention: bool,
14}
15
16pub fn badge_for(unread_chats: usize, unread_mention: bool) -> Option<Badge> {
17 if unread_chats == 0 {
18 return None;
19 }
20 let text = if unread_chats >= 10 {
21 "9+".to_owned()
22 } else {
23 unread_chats.to_string()
24 };
25 Some(Badge {
26 text,
27 mention: unread_mention,
28 })
29}
30
31const GLYPHS: [[u8; GLYPH_HEIGHT]; 11] = [
32 [0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110],
33 [0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110],
34 [0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111],
35 [0b11110, 0b00001, 0b00001, 0b01110, 0b00001, 0b00001, 0b11110],
36 [0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010],
37 [0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110],
38 [0b00110, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110],
39 [0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000],
40 [0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110],
41 [0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b01100],
42 [0b00000, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0b00000],
43];
44
45fn glyph_for(character: char) -> Option<&'static [u8; GLYPH_HEIGHT]> {
46 match character {
47 '0'..='9' => GLYPHS.get(character as usize - '0' as usize),
48 '+' => GLYPHS.get(10),
49 _ => None,
50 }
51}
52
53pub fn text_width(text: &str) -> usize {
54 let glyphs = text.chars().count();
55 if glyphs == 0 {
56 return 0;
57 }
58 glyphs * GLYPH_WIDTH * GLYPH_SCALE + (glyphs - 1) * GLYPH_GAP * GLYPH_SCALE
59}
60
61/// BGRA, premultiplied, top-down, `BADGE_SIZE` square.
62pub fn render_badge(badge: &Badge) -> Vec<u8> {
63 let fill = if badge.mention { MENTION } else { NEUTRAL };
64 let mut pixels = vec![0u8; BADGE_SIZE * BADGE_SIZE * 4];
65 let center = (BADGE_SIZE as f32 - 1.) / 2.;
66 let radius = BADGE_SIZE as f32 / 2.;
67 for row in 0..BADGE_SIZE {
68 for column in 0..BADGE_SIZE {
69 let distance = ((column as f32 - center).powi(2) + (row as f32 - center).powi(2)).sqrt();
70 if distance <= radius - 0.5 {
71 put(&mut pixels, column, row, fill);
72 }
73 }
74 }
75 let width = text_width(&badge.text);
76 let left = (BADGE_SIZE - width.min(BADGE_SIZE)) / 2;
77 let top = (BADGE_SIZE - GLYPH_HEIGHT * GLYPH_SCALE) / 2;
78 for (index, character) in badge.text.chars().enumerate() {
79 let Some(glyph) = glyph_for(character) else {
80 continue;
81 };
82 let origin = left + index * (GLYPH_WIDTH + GLYPH_GAP) * GLYPH_SCALE;
83 for (glyph_row, bits) in glyph.iter().enumerate() {
84 for glyph_column in 0..GLYPH_WIDTH {
85 if bits & (1 << (GLYPH_WIDTH - 1 - glyph_column)) == 0 {
86 continue;
87 }
88 for dy in 0..GLYPH_SCALE {
89 for dx in 0..GLYPH_SCALE {
90 put(
91 &mut pixels,
92 origin + glyph_column * GLYPH_SCALE + dx,
93 top + glyph_row * GLYPH_SCALE + dy,
94 INK,
95 );
96 }
97 }
98 }
99 }
100 }
101 pixels
102}
103
104fn put(pixels: &mut [u8], column: usize, row: usize, rgb: [u8; 3]) {
105 if column >= BADGE_SIZE || row >= BADGE_SIZE {
106 return;
107 }
108 let offset = (row * BADGE_SIZE + column) * 4;
109 pixels[offset..offset + 4].copy_from_slice(&[rgb[2], rgb[1], rgb[0], 0xff]);
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn no_unread_means_no_badge() {
118 assert_eq!(badge_for(0, true), None);
119 }
120
121 #[test]
122 fn counts_chats_and_caps_at_nine_plus() {
123 assert_eq!(badge_for(3, false).unwrap().text, "3");
124 assert_eq!(badge_for(9, false).unwrap().text, "9");
125 assert_eq!(badge_for(10, false).unwrap().text, "9+");
126 assert_eq!(badge_for(250, false).unwrap().text, "9+");
127 }
128
129 #[test]
130 fn mention_turns_badge_red() {
131 assert!(badge_for(1, true).unwrap().mention);
132 assert!(!badge_for(1, false).unwrap().mention);
133 }
134
135 #[test]
136 fn rendered_badge_has_fill_ink_and_transparent_corner() {
137 let badge = badge_for(3, true).unwrap();
138 let pixels = render_badge(&badge);
139 assert_eq!(pixels.len(), BADGE_SIZE * BADGE_SIZE * 4);
140 assert_eq!(pixels[3], 0);
141 let has = |blue: u8, green: u8, red: u8| {
142 pixels
143 .chunks(4)
144 .any(|pixel| pixel == [blue, green, red, 0xff])
145 };
146 assert!(has(0x44, 0x44, 0xef));
147 assert!(has(0x0a, 0x0a, 0x0a));
148 }
149
150 #[test]
151 fn two_glyphs_fit_the_badge() {
152 assert!(text_width("9+") < BADGE_SIZE);
153 }
154}