BlueGit

ict rusty-teams

fix(notify): smooth taskbar badge like Teams

- badge drawn as SVG with the app font, anti-aliased, Teams red
- rendered at the taskbar overlay size for the window DPI, no downscale

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-06 · 3 weeks ago

Hash
46a977071a2497049fa34b9dbf49cbcbf74d4cfd
Parent
26163bad
4 files changed +84 −113
Cargo.lock +1 −0
@@ -320,6 +320,7 @@ dependencies = [
320 320 "log",
321 321 "mcp",
322 322 "raw-window-handle",
323 "resvg 0.46.0",
323 324 "rodio",
324 325 "serde",
325 326 "serde_json",
@@ -320,6 +320,7 @@ dependencies = [
320 "log",
320 "log",
321 "mcp",
321 "mcp",
322 "raw-window-handle",
322 "raw-window-handle",
323 "resvg 0.46.0",
323 "rodio",
324 "rodio",
324 "serde",
325 "serde",
325 "serde_json",
326 "serde_json",
crates/app-base/Cargo.toml +1 −0
@@ -28,6 +28,7 @@ teams-core.workspace = true
28 28 tokio.workspace = true
29 29 image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg", "png", "webp"] }
30 30 raw-window-handle = "0.6"
31 resvg = { version = "0.46", default-features = false, features = ["text"] }
31 32
32 33 [target.'cfg(target_os = "linux")'.dependencies]
33 34 x11rb = { version = "0.13", features = ["shape"] }
@@ -28,6 +28,7 @@ teams-core.workspace = true
28tokio.workspace = true
28tokio.workspace = true
29image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg", "png", "webp"] }
29image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg", "png", "webp"] }
30raw-window-handle = "0.6"
30raw-window-handle = "0.6"
31resvg = { version = "0.46", default-features = false, features = ["text"] }
31
32
32[target.'cfg(target_os = "linux")'.dependencies]
33[target.'cfg(target_os = "linux")'.dependencies]
33x11rb = { version = "0.13", features = ["shape"] }
34x11rb = { version = "0.13", features = ["shape"] }
crates/app-base/src/notify/badge.rs +70 −107
@@ -1,10 +1,14 @@
1 pub const BADGE_SIZE: usize = 32;
2 const GLYPH_WIDTH: usize = 5;
3 const GLYPH_HEIGHT: usize = 7;
4 const GLYPH_SCALE: usize = 2;
5 const GLYPH_GAP: usize = 1;
6 const RED: [u8; 3] = [0xef, 0x44, 0x44];
7 const INK: [u8; 3] = [0xff, 0xff, 0xff];
1 use std::sync::{Arc, OnceLock};
2
3 use resvg::{tiny_skia, usvg};
4
5 const FILL: &str = "#C4314B";
6 const INK: &str = "#FFFFFF";
7 const FONT: &[u8] = include_bytes!("../../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf");
8 const FONT_FAMILY: &str = "Google Sans Flex";
9 const SINGLE_GLYPH_SCALE: f32 = 0.62;
10 const WIDE_TEXT_SCALE: f32 = 0.5;
11 const BASELINE_SHIFT: f32 = 0.36;
8 12
9 13 #[derive(Debug, Clone, PartialEq, Eq)]
10 14 pub struct Badge {
@@ -23,109 +27,48 @@ pub fn badge_for(unread_chats: usize) -> Option<Badge> {
23 27 Some(Badge { text })
24 28 }
25 29
26 const 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
62 fn 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 }
30 fn options() -> &'static usvg::Options<'static> {
31 static OPTIONS: OnceLock<usvg::Options<'static>> = OnceLock::new();
32 OPTIONS.get_or_init(|| {
33 let mut fonts = usvg::fontdb::Database::new();
34 fonts.load_font_data(FONT.to_vec());
35 usvg::Options {
36 font_family: FONT_FAMILY.to_owned(),
37 fontdb: Arc::new(fonts),
38 ..usvg::Options::default()
39 }
40 })
68 41 }
69 42
70 pub 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
43 fn badge_svg(text: &str, size: u32) -> String {
44 let size = size as f32;
45 let radius = size / 2.;
46 let font_size = size
47 * if text.chars().count() > 1 {
48 WIDE_TEXT_SCALE
49 } else {
50 SINGLE_GLYPH_SCALE
51 };
52 let baseline = radius + font_size * BASELINE_SHIFT;
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>"#
55 )
76 56 }
77 57
78 /// BGRA, premultiplied, top-down, `BADGE_SIZE` square.
79 pub 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 }
58 /// BGRA, premultiplied, top-down, `size` square.
59 pub fn render_badge(badge: &Badge, size: u32) -> Vec<u8> {
60 let size = size.max(1);
61 let mut pixmap = tiny_skia::Pixmap::new(size, size).expect("badge size is not zero");
62 if let Ok(tree) = usvg::Tree::from_str(&badge_svg(&badge.text, size), options()) {
63 resvg::render(&tree, tiny_skia::Transform::identity(), &mut pixmap.as_mut());
91 64 }
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 }
65 let mut pixels = pixmap.take();
66 for pixel in pixels.chunks_exact_mut(4) {
67 pixel.swap(0, 2);
117 68 }
118 69 pixels
119 70 }
120 71
121 fn 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 72 #[cfg(test)]
130 73 mod tests {
131 74 use super::*;
@@ -143,23 +86,43 @@ mod tests {
143 86 assert_eq!(badge_for(250).unwrap().text, "9+");
144 87 }
145 88
89 fn rendered(text: &str, size: u32) -> Vec<u8> {
90 render_badge(
91 &Badge {
92 text: text.to_owned(),
93 },
94 size,
95 )
96 }
97
146 98 #[test]
147 99 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);
100 let pixels = rendered("3", 32);
101 assert_eq!(pixels.len(), 32 * 32 * 4);
151 102 assert_eq!(pixels[3], 0);
152 103 let has = |blue: u8, green: u8, red: u8| {
153 104 pixels
154 105 .chunks(4)
155 106 .any(|pixel| pixel == [blue, green, red, 0xff])
156 107 };
157 assert!(has(0x44, 0x44, 0xef));
108 assert!(has(0x4b, 0x31, 0xc4));
158 109 assert!(has(0xff, 0xff, 0xff));
159 110 }
160 111
161 112 #[test]
162 fn two_glyphs_fit_the_badge() {
163 assert!(text_width("9+") < BADGE_SIZE);
113 fn nine_plus_stays_inside_the_circle() {
114 let size = 32;
115 let pixels = rendered("9+", size);
116 let ink_columns: Vec<u32> = (0..size)
117 .filter(|column| {
118 (0..size).any(|row| {
119 let offset = ((row * size + column) * 4) as usize;
120 pixels[offset..offset + 3] == [0xff, 0xff, 0xff]
121 })
122 })
123 .collect();
124 assert!(ink_columns.len() > 8);
125 assert!(ink_columns.first().is_some_and(|column| *column >= 4));
126 assert!(ink_columns.last().is_some_and(|column| *column <= size - 5));
164 127 }
165 128 }
@@ -1,10 +1,14 @@
1pub const BADGE_SIZE: usize = 32;
1use std::sync::{Arc, OnceLock};
2const GLYPH_WIDTH: usize = 5;
2
3const GLYPH_HEIGHT: usize = 7;
3use resvg::{tiny_skia, usvg};
4const GLYPH_SCALE: usize = 2;
4
5const GLYPH_GAP: usize = 1;
5const FILL: &str = "#C4314B";
6const RED: [u8; 3] = [0xef, 0x44, 0x44];
6const INK: &str = "#FFFFFF";
7const INK: [u8; 3] = [0xff, 0xff, 0xff];
7const FONT: &[u8] = include_bytes!("../../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf");
8const FONT_FAMILY: &str = "Google Sans Flex";
9const SINGLE_GLYPH_SCALE: f32 = 0.62;
10const WIDE_TEXT_SCALE: f32 = 0.5;
11const BASELINE_SHIFT: f32 = 0.36;
8
12
9#[derive(Debug, Clone, PartialEq, Eq)]
13#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Badge {
14pub struct Badge {
@@ -23,109 +27,48 @@ pub fn badge_for(unread_chats: usize) -> Option<Badge> {
23 Some(Badge { text })
27 Some(Badge { text })
24}
28}
25
29
26const GLYPHS: [[u8; GLYPH_HEIGHT]; 11] = [
30fn options() -> &'static usvg::Options<'static> {
27 [
31 static OPTIONS: OnceLock<usvg::Options<'static>> = OnceLock::new();
28 0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110,
32 OPTIONS.get_or_init(|| {
29 ],
33 let mut fonts = usvg::fontdb::Database::new();
30 [
34 fonts.load_font_data(FONT.to_vec());
31 0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
35 usvg::Options {
32 ],
36 font_family: FONT_FAMILY.to_owned(),
33 [
37 fontdb: Arc::new(fonts),
34 0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111,
38 ..usvg::Options::default()
35 ],
39 }
36 [
40 })
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}
41}
69
42
70pub fn text_width(text: &str) -> usize {
43fn badge_svg(text: &str, size: u32) -> String {
71 let glyphs = text.chars().count();
44 let size = size as f32;
72 if glyphs == 0 {
45 let radius = size / 2.;
73 return 0;
46 let font_size = size
74 }
47 * if text.chars().count() > 1 {
75 glyphs * GLYPH_WIDTH * GLYPH_SCALE + (glyphs - 1) * GLYPH_GAP * GLYPH_SCALE
48 WIDE_TEXT_SCALE
49 } else {
50 SINGLE_GLYPH_SCALE
51 };
52 let baseline = radius + font_size * BASELINE_SHIFT;
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>"#
55 )
76}
56}
77
57
78/// BGRA, premultiplied, top-down, `BADGE_SIZE` square.
58/// BGRA, premultiplied, top-down, `size` square.
79pub fn render_badge(badge: &Badge) -> Vec<u8> {
59pub fn render_badge(badge: &Badge, size: u32) -> Vec<u8> {
80 let mut pixels = vec![0u8; BADGE_SIZE * BADGE_SIZE * 4];
60 let size = size.max(1);
81 let center = (BADGE_SIZE as f32 - 1.) / 2.;
61 let mut pixmap = tiny_skia::Pixmap::new(size, size).expect("badge size is not zero");
82 let radius = BADGE_SIZE as f32 / 2.;
62 if let Ok(tree) = usvg::Tree::from_str(&badge_svg(&badge.text, size), options()) {
83 for row in 0..BADGE_SIZE {
63 resvg::render(&tree, tiny_skia::Transform::identity(), &mut pixmap.as_mut());
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 }
64 }
92 let width = text_width(&badge.text);
65 let mut pixels = pixmap.take();
93 let left = (BADGE_SIZE - width.min(BADGE_SIZE)) / 2;
66 for pixel in pixels.chunks_exact_mut(4) {
94 let top = (BADGE_SIZE - GLYPH_HEIGHT * GLYPH_SCALE) / 2;
67 pixel.swap(0, 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 }
68 }
118 pixels
69 pixels
119}
70}
120
71
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)]
72#[cfg(test)]
130mod tests {
73mod tests {
131 use super::*;
74 use super::*;
@@ -143,23 +86,43 @@ mod tests {
143 assert_eq!(badge_for(250).unwrap().text, "9+");
86 assert_eq!(badge_for(250).unwrap().text, "9+");
144 }
87 }
145
88
89 fn rendered(text: &str, size: u32) -> Vec<u8> {
90 render_badge(
91 &Badge {
92 text: text.to_owned(),
93 },
94 size,
95 )
96 }
97
146 #[test]
98 #[test]
147 fn rendered_badge_has_fill_ink_and_transparent_corner() {
99 fn rendered_badge_has_fill_ink_and_transparent_corner() {
148 let badge = badge_for(3).unwrap();
100 let pixels = rendered("3", 32);
149 let pixels = render_badge(&badge);
101 assert_eq!(pixels.len(), 32 * 32 * 4);
150 assert_eq!(pixels.len(), BADGE_SIZE * BADGE_SIZE * 4);
151 assert_eq!(pixels[3], 0);
102 assert_eq!(pixels[3], 0);
152 let has = |blue: u8, green: u8, red: u8| {
103 let has = |blue: u8, green: u8, red: u8| {
153 pixels
104 pixels
154 .chunks(4)
105 .chunks(4)
155 .any(|pixel| pixel == [blue, green, red, 0xff])
106 .any(|pixel| pixel == [blue, green, red, 0xff])
156 };
107 };
157 assert!(has(0x44, 0x44, 0xef));
108 assert!(has(0x4b, 0x31, 0xc4));
158 assert!(has(0xff, 0xff, 0xff));
109 assert!(has(0xff, 0xff, 0xff));
159 }
110 }
160
111
161 #[test]
112 #[test]
162 fn two_glyphs_fit_the_badge() {
113 fn nine_plus_stays_inside_the_circle() {
163 assert!(text_width("9+") < BADGE_SIZE);
114 let size = 32;
115 let pixels = rendered("9+", size);
116 let ink_columns: Vec<u32> = (0..size)
117 .filter(|column| {
118 (0..size).any(|row| {
119 let offset = ((row * size + column) * 4) as usize;
120 pixels[offset..offset + 3] == [0xff, 0xff, 0xff]
121 })
122 })
123 .collect();
124 assert!(ink_columns.len() > 8);
125 assert!(ink_columns.first().is_some_and(|column| *column >= 4));
126 assert!(ink_columns.last().is_some_and(|column| *column <= size - 5));
164 }
127 }
165}
128}
crates/app-base/src/notify/platform/windows_impl.rs +12 −6
@@ -21,7 +21,9 @@ use windows::Win32::Graphics::Gdi::{
21 21 MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromPoint, MonitorFromRect, MonitorFromWindow,
22 22 };
23 23 use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance};
24 use windows::Win32::UI::HiDpi::{GetDpiForMonitor, GetDpiForWindow, MDT_EFFECTIVE_DPI};
24 use windows::Win32::UI::HiDpi::{
25 GetDpiForMonitor, GetDpiForWindow, GetSystemMetricsForDpi, MDT_EFFECTIVE_DPI,
26 };
25 27 use windows::Win32::UI::Shell::{
26 28 ITaskbarList3, QUNS_BUSY, QUNS_PRESENTATION_MODE, QUNS_QUIET_TIME,
27 29 QUNS_RUNNING_D3D_FULL_SCREEN, SHQueryUserNotificationState, TaskbarList,
@@ -30,7 +32,7 @@ use windows::Win32::UI::WindowsAndMessaging::{
30 32 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
31 33 FlashWindowEx, GW_HWNDPREV, GWL_EXSTYLE, GWL_STYLE, GetWindow, GetWindowLongPtrW,
32 34 GetWindowRect, HICON, HWND_NOTOPMOST, HWND_TOPMOST, ICON_BIG, ICON_SMALL, ICONINFO, IsIconic,
33 IsWindowVisible, LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW,
35 IsWindowVisible, LWA_ALPHA, SM_CXSMICON, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW,
34 36 SW_SHOWNOACTIVATE, SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE,
35 37 SWP_NOZORDER, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SendMessageW, SetForegroundWindow,
36 38 SetLayeredWindowAttributes, SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos,
@@ -41,7 +43,7 @@ use windows::core::{BOOL, PCWSTR, w};
41 43
42 44 use super::stack_plan::{StackPlan, stack_plan};
43 45 use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};
44 use crate::notify::badge::{BADGE_SIZE, Badge, render_badge};
46 use crate::notify::badge::{Badge, render_badge};
45 47
46 48 const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3");
47 49 const TRAY_ICON_SIZE: u32 = 32;
@@ -492,7 +494,7 @@ pub fn set_badge(handle: NativeHandle, badge: Option<&Badge>) {
492 494 let _ = taskbar.SetOverlayIcon(hwnd(handle), HICON::default(), w!(""));
493 495 }
494 496 Some(badge) => {
495 let Some(icon) = icon_from_badge(badge) else {
497 let Some(icon) = icon_from_badge(badge, handle) else {
496 498 return;
497 499 };
498 500 let _ = taskbar.SetOverlayIcon(hwnd(handle), icon, w!("Unread chats"));
@@ -502,8 +504,12 @@ pub fn set_badge(handle: NativeHandle, badge: Option<&Badge>) {
502 504 }
503 505 }
504 506
505 unsafe fn icon_from_badge(badge: &Badge) -> Option<HICON> {
506 unsafe { icon_from_bgra(&render_badge(badge), BADGE_SIZE as u32) }
507 unsafe fn icon_from_badge(badge: &Badge, handle: NativeHandle) -> Option<HICON> {
508 unsafe {
509 let dpi = GetDpiForWindow(hwnd(handle));
510 let size = GetSystemMetricsForDpi(SM_CXSMICON, dpi).max(16) as u32;
511 icon_from_bgra(&render_badge(badge, size), size)
512 }
507 513 }
508 514
509 515 unsafe fn icon_from_bgra(pixels: &[u8], size: u32) -> Option<HICON> {
@@ -21,7 +21,9 @@ use windows::Win32::Graphics::Gdi::{
21 MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromPoint, MonitorFromRect, MonitorFromWindow,
21 MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromPoint, MonitorFromRect, MonitorFromWindow,
22};
22};
23use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance};
23use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance};
24use windows::Win32::UI::HiDpi::{GetDpiForMonitor, GetDpiForWindow, MDT_EFFECTIVE_DPI};
24use windows::Win32::UI::HiDpi::{
25 GetDpiForMonitor, GetDpiForWindow, GetSystemMetricsForDpi, MDT_EFFECTIVE_DPI,
26};
25use windows::Win32::UI::Shell::{
27use windows::Win32::UI::Shell::{
26 ITaskbarList3, QUNS_BUSY, QUNS_PRESENTATION_MODE, QUNS_QUIET_TIME,
28 ITaskbarList3, QUNS_BUSY, QUNS_PRESENTATION_MODE, QUNS_QUIET_TIME,
27 QUNS_RUNNING_D3D_FULL_SCREEN, SHQueryUserNotificationState, TaskbarList,
29 QUNS_RUNNING_D3D_FULL_SCREEN, SHQueryUserNotificationState, TaskbarList,
@@ -30,7 +32,7 @@ use windows::Win32::UI::WindowsAndMessaging::{
30 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
32 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
31 FlashWindowEx, GW_HWNDPREV, GWL_EXSTYLE, GWL_STYLE, GetWindow, GetWindowLongPtrW,
33 FlashWindowEx, GW_HWNDPREV, GWL_EXSTYLE, GWL_STYLE, GetWindow, GetWindowLongPtrW,
32 GetWindowRect, HICON, HWND_NOTOPMOST, HWND_TOPMOST, ICON_BIG, ICON_SMALL, ICONINFO, IsIconic,
34 GetWindowRect, HICON, HWND_NOTOPMOST, HWND_TOPMOST, ICON_BIG, ICON_SMALL, ICONINFO, IsIconic,
33 IsWindowVisible, LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW,
35 IsWindowVisible, LWA_ALPHA, SM_CXSMICON, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW,
34 SW_SHOWNOACTIVATE, SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE,
36 SW_SHOWNOACTIVATE, SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE,
35 SWP_NOZORDER, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SendMessageW, SetForegroundWindow,
37 SWP_NOZORDER, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SendMessageW, SetForegroundWindow,
36 SetLayeredWindowAttributes, SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos,
38 SetLayeredWindowAttributes, SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos,
@@ -41,7 +43,7 @@ use windows::core::{BOOL, PCWSTR, w};
41
43
42use super::stack_plan::{StackPlan, stack_plan};
44use super::stack_plan::{StackPlan, stack_plan};
43use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};
45use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};
44use crate::notify::badge::{BADGE_SIZE, Badge, render_badge};
46use crate::notify::badge::{Badge, render_badge};
45
47
46const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3");
48const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3");
47const TRAY_ICON_SIZE: u32 = 32;
49const TRAY_ICON_SIZE: u32 = 32;
@@ -492,7 +494,7 @@ pub fn set_badge(handle: NativeHandle, badge: Option<&Badge>) {
492 let _ = taskbar.SetOverlayIcon(hwnd(handle), HICON::default(), w!(""));
494 let _ = taskbar.SetOverlayIcon(hwnd(handle), HICON::default(), w!(""));
493 }
495 }
494 Some(badge) => {
496 Some(badge) => {
495 let Some(icon) = icon_from_badge(badge) else {
497 let Some(icon) = icon_from_badge(badge, handle) else {
496 return;
498 return;
497 };
499 };
498 let _ = taskbar.SetOverlayIcon(hwnd(handle), icon, w!("Unread chats"));
500 let _ = taskbar.SetOverlayIcon(hwnd(handle), icon, w!("Unread chats"));
@@ -502,8 +504,12 @@ pub fn set_badge(handle: NativeHandle, badge: Option<&Badge>) {
502 }
504 }
503}
505}
504
506
505unsafe fn icon_from_badge(badge: &Badge) -> Option<HICON> {
507unsafe fn icon_from_badge(badge: &Badge, handle: NativeHandle) -> Option<HICON> {
506 unsafe { icon_from_bgra(&render_badge(badge), BADGE_SIZE as u32) }
508 unsafe {
509 let dpi = GetDpiForWindow(hwnd(handle));
510 let size = GetSystemMetricsForDpi(SM_CXSMICON, dpi).max(16) as u32;
511 icon_from_bgra(&render_badge(badge, size), size)
512 }
507}
513}
508
514
509unsafe fn icon_from_bgra(pixels: &[u8], size: u32) -> Option<HICON> {
515unsafe fn icon_from_bgra(pixels: &[u8], size: u32) -> Option<HICON> {