BlueGit

ict rusty-teams

Files

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

badge.rs

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