BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / notify / platform / windows_impl.rs

windows_impl.rs

windows_impl.rs 21.1 KiB Download Raw
1use std::ffi::c_void;
2use std::io::Cursor;
3use std::sync::Arc;
4use std::sync::atomic::{AtomicBool, Ordering};
5use std::time::Duration;
6
7use calling::ShareKind;
8use gpui_kit::Window;
9use raw_window_handle::{HasWindowHandle, RawWindowHandle};
10use tray_icon::menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem};
11use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
12use windows::Win32::Foundation::{COLORREF, HWND, POINT, RECT};
13use windows::Win32::Graphics::Dwm::{
14 DWMWA_BORDER_COLOR, DWMWA_CLOAKED, DWMWA_COLOR_NONE, DWMWA_EXTENDED_FRAME_BOUNDS,
15 DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND, DwmGetWindowAttribute,
16 DwmSetWindowAttribute,
17};
18use windows::Win32::Graphics::Gdi::{
19 CreateBitmap, DEVMODEW, DISPLAY_DEVICE_ACTIVE, DISPLAY_DEVICEW, DeleteObject,
20 ENUM_CURRENT_SETTINGS, EnumDisplayDevicesW, EnumDisplaySettingsW, GetMonitorInfoW, HGDIOBJ,
21 MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromPoint, MonitorFromRect, MonitorFromWindow,
22};
23use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance};
24use windows::Win32::UI::HiDpi::{GetDpiForMonitor, GetDpiForWindow, MDT_EFFECTIVE_DPI};
25use windows::Win32::UI::Shell::{
26 ITaskbarList3, QUNS_BUSY, QUNS_PRESENTATION_MODE, QUNS_QUIET_TIME,
27 QUNS_RUNNING_D3D_FULL_SCREEN, SHQueryUserNotificationState, TaskbarList,
28};
29use windows::Win32::UI::WindowsAndMessaging::{
30 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
31 FlashWindowEx, GW_HWNDPREV, GWL_EXSTYLE, GWL_STYLE, GetWindow, GetWindowLongPtrW,
32 GetWindowRect, HICON, HWND_NOTOPMOST, HWND_TOPMOST, ICONINFO, IsIconic, IsWindowVisible,
33 LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW, SW_SHOWNOACTIVATE,
34 SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE, SWP_NOZORDER,
35 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SetForegroundWindow, SetLayeredWindowAttributes,
36 SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos, ShowWindow, SystemParametersInfoW,
37 WDA_EXCLUDEFROMCAPTURE, WS_CAPTION, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOPMOST,
38 WS_EX_TRANSPARENT, WS_POPUP, WS_THICKFRAME,
39};
40use windows::core::{BOOL, PCWSTR, w};
41
42use super::stack_plan::{StackPlan, stack_plan};
43use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};
44use crate::notify::badge::{BADGE_SIZE, Badge, render_badge};
45
46const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3");
47const TRAY_ICON_PNG: &[u8] = include_bytes!("../../../../app/assets/icon/teams-fast-256.png");
48const TRAY_ICON_SIZE: u32 = 32;
49const TRAY_DOT_RADIUS: f32 = 6.;
50const BASE_DPI: f32 = 96.;
51const RING_POLL: Duration = Duration::from_millis(20);
52
53fn hwnd(handle: NativeHandle) -> HWND {
54 HWND(handle as *mut c_void)
55}
56
57pub fn native_handle(window: &Window) -> Option<NativeHandle> {
58 match HasWindowHandle::window_handle(window).ok()?.as_raw() {
59 RawWindowHandle::Win32(handle) => Some(handle.hwnd.get()),
60 _ => None,
61 }
62}
63
64pub fn prepare_toast_window(handle: NativeHandle) {
65 unsafe {
66 // GPUI creates popups with style 0, and Windows adds a caption frame to that on its own.
67 let frame = (WS_CAPTION | WS_THICKFRAME).0 as isize;
68 let style = GetWindowLongPtrW(hwnd(handle), GWL_STYLE);
69 SetWindowLongPtrW(
70 hwnd(handle),
71 GWL_STYLE,
72 (style & !frame) | WS_POPUP.0 as isize,
73 );
74 let _ = SetWindowPos(
75 hwnd(handle),
76 None,
77 0,
78 0,
79 0,
80 0,
81 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE,
82 );
83 let style = GetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE);
84 SetWindowLongPtrW(
85 hwnd(handle),
86 GWL_EXSTYLE,
87 style | WS_EX_NOACTIVATE.0 as isize,
88 );
89 let no_border = DWMWA_COLOR_NONE;
90 let _ = DwmSetWindowAttribute(
91 hwnd(handle),
92 DWMWA_BORDER_COLOR,
93 &no_border as *const _ as *const c_void,
94 size_of::<u32>() as u32,
95 );
96 let preference = DWMWCP_DONOTROUND;
97 let _ = DwmSetWindowAttribute(
98 hwnd(handle),
99 DWMWA_WINDOW_CORNER_PREFERENCE,
100 &preference as *const _ as *const c_void,
101 size_of_val(&preference) as u32,
102 );
103 }
104}
105
106pub fn set_activatable(handle: NativeHandle, activatable: bool) {
107 unsafe {
108 let style = GetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE);
109 let flag = WS_EX_NOACTIVATE.0 as isize;
110 let updated = if activatable {
111 style & !flag
112 } else {
113 style | flag
114 };
115 SetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE, updated);
116 if activatable {
117 let _ = SetForegroundWindow(hwnd(handle));
118 }
119 }
120}
121
122pub fn work_area(handle: NativeHandle) -> Option<WorkArea> {
123 unsafe {
124 let monitor = MonitorFromWindow(hwnd(handle), MONITOR_DEFAULTTONEAREST);
125 let mut info = MONITORINFO {
126 cbSize: size_of::<MONITORINFO>() as u32,
127 ..Default::default()
128 };
129 if !GetMonitorInfoW(monitor, &mut info).as_bool() {
130 return None;
131 }
132 let RECT {
133 left,
134 top,
135 right,
136 bottom,
137 } = info.rcWork;
138 let dpi = GetDpiForWindow(hwnd(handle));
139 Some(WorkArea {
140 left,
141 top,
142 right,
143 bottom,
144 scale: if dpi == 0 { 1. } else { dpi as f32 / BASE_DPI },
145 })
146 }
147}
148
149pub fn work_area_at(x: i32, y: i32) -> Option<WorkArea> {
150 unsafe {
151 let monitor = MonitorFromPoint(POINT { x, y }, MONITOR_DEFAULTTONEAREST);
152 let mut info = MONITORINFO {
153 cbSize: size_of::<MONITORINFO>() as u32,
154 ..Default::default()
155 };
156 if !GetMonitorInfoW(monitor, &mut info).as_bool() {
157 return None;
158 }
159 let (mut dpi_x, mut dpi_y) = (0, 0);
160 let scale = match GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &mut dpi_x, &mut dpi_y) {
161 Ok(()) if dpi_x > 0 => dpi_x as f32 / BASE_DPI,
162 _ => 1.,
163 };
164 let RECT {
165 left,
166 top,
167 right,
168 bottom,
169 } = info.rcWork;
170 Some(WorkArea {
171 left,
172 top,
173 right,
174 bottom,
175 scale,
176 })
177 }
178}
179
180pub fn place(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) {
181 unsafe {
182 let _ = SetWindowPos(
183 hwnd(handle),
184 Some(HWND_TOPMOST),
185 x,
186 y,
187 width,
188 height,
189 SWP_NOACTIVATE | SWP_NOOWNERZORDER,
190 );
191 }
192}
193
194fn is_topmost(window: HWND) -> bool {
195 unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) & WS_EX_TOPMOST.0 as isize != 0 }
196}
197
198fn band_after(shared_topmost: bool, previous: Option<HWND>) -> HWND {
199 match previous {
200 Some(previous) if shared_topmost || !is_topmost(previous) => previous,
201 _ if shared_topmost => HWND_TOPMOST,
202 _ => HWND_NOTOPMOST,
203 }
204}
205
206fn windows_above(shared: HWND, group: &[NativeHandle]) -> Vec<NativeHandle> {
207 let mut above = Vec::new();
208 let mut current = shared;
209 while let Ok(previous) = unsafe { GetWindow(current, GW_HWNDPREV) } {
210 above.push(previous.0 as NativeHandle);
211 if !group.contains(&(previous.0 as NativeHandle)) {
212 break;
213 }
214 current = previous;
215 }
216 above
217}
218
219pub fn place_rect(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) {
220 unsafe {
221 let _ = SetWindowPos(
222 hwnd(handle),
223 None,
224 x,
225 y,
226 width,
227 height,
228 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER,
229 );
230 }
231}
232
233pub fn stack_group_above(shared: NativeHandle, group: &[NativeHandle]) {
234 let shared = hwnd(shared);
235 let above = windows_above(shared, group);
236 let StackPlan::Rebuild { after } = stack_plan(&above, group) else {
237 return;
238 };
239 let mut insert_after = band_after(is_topmost(shared), after.map(hwnd));
240 for handle in group {
241 unsafe {
242 let _ = SetWindowPos(
243 hwnd(*handle),
244 Some(insert_after),
245 0,
246 0,
247 0,
248 0,
249 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,
250 );
251 }
252 insert_after = hwnd(*handle);
253 }
254}
255
256pub fn show_window_inactive(handle: NativeHandle) {
257 unsafe {
258 let _ = ShowWindow(hwnd(handle), SW_SHOWNOACTIVATE);
259 }
260}
261
262pub fn exclude_from_capture(handle: NativeHandle) {
263 unsafe {
264 let _ = SetWindowDisplayAffinity(hwnd(handle), WDA_EXCLUDEFROMCAPTURE);
265 }
266}
267
268pub fn make_window_click_through(window: &Window) {
269 if let Some(handle) = native_handle(window) {
270 make_click_through(handle);
271 }
272}
273
274fn make_click_through(handle: NativeHandle) {
275 unsafe {
276 let style = GetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE);
277 let added = (WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE).0 as isize;
278 SetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE, style | added);
279 // A layered window stays invisible until its alpha is set.
280 let _ = SetLayeredWindowAttributes(hwnd(handle), COLORREF(0), u8::MAX, LWA_ALPHA);
281 }
282}
283
284pub fn window_minimized(handle: NativeHandle) -> bool {
285 unsafe { IsIconic(hwnd(handle)).as_bool() }
286}
287
288fn screen_rect(rect: RECT) -> ScreenRect {
289 ScreenRect {
290 left: rect.left,
291 top: rect.top,
292 right: rect.right,
293 bottom: rect.bottom,
294 }
295}
296
297pub fn window_rect(handle: NativeHandle) -> Option<ScreenRect> {
298 let mut rect = RECT::default();
299 unsafe { GetWindowRect(hwnd(handle), &mut rect).ok()? };
300 Some(screen_rect(rect))
301}
302
303fn visible_frame(window: HWND) -> Option<ScreenRect> {
304 let mut rect = RECT::default();
305 unsafe {
306 if IsIconic(window).as_bool() || !IsWindowVisible(window).as_bool() {
307 return None;
308 }
309 let mut cloaked = 0u32;
310 let queried = DwmGetWindowAttribute(
311 window,
312 DWMWA_CLOAKED,
313 &mut cloaked as *mut _ as *mut c_void,
314 size_of::<u32>() as u32,
315 );
316 if queried.is_ok() && cloaked != 0 {
317 return None;
318 }
319 // Excludes the invisible resize margin that GetWindowRect includes.
320 let framed = DwmGetWindowAttribute(
321 window,
322 DWMWA_EXTENDED_FRAME_BOUNDS,
323 &mut rect as *mut _ as *mut c_void,
324 size_of::<RECT>() as u32,
325 );
326 if framed.is_err() {
327 GetWindowRect(window, &mut rect).ok()?;
328 }
329 }
330 Some(screen_rect(rect))
331}
332
333fn screen_frame(index: u32) -> Option<ScreenRect> {
334 unsafe {
335 let mut device = DISPLAY_DEVICEW {
336 cb: size_of::<DISPLAY_DEVICEW>() as u32,
337 ..Default::default()
338 };
339 if !EnumDisplayDevicesW(PCWSTR::null(), index, &mut device, 0).as_bool()
340 || device.StateFlags & DISPLAY_DEVICE_ACTIVE == Default::default()
341 {
342 return None;
343 }
344 let mut mode = DEVMODEW {
345 dmSize: size_of::<DEVMODEW>() as u16,
346 ..Default::default()
347 };
348 if !EnumDisplaySettingsW(
349 PCWSTR(device.DeviceName.as_ptr()),
350 ENUM_CURRENT_SETTINGS,
351 &mut mode,
352 )
353 .as_bool()
354 {
355 return None;
356 }
357 let position = mode.Anonymous1.Anonymous2.dmPosition;
358 Some(ScreenRect {
359 left: position.x,
360 top: position.y,
361 right: position.x + mode.dmPelsWidth as i32,
362 bottom: position.y + mode.dmPelsHeight as i32,
363 })
364 }
365}
366
367fn geometry_around(target: ScreenRect) -> Option<ShareGeometry> {
368 unsafe {
369 let rect = RECT {
370 left: target.left,
371 top: target.top,
372 right: target.right,
373 bottom: target.bottom,
374 };
375 let monitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
376 let mut info = MONITORINFO {
377 cbSize: size_of::<MONITORINFO>() as u32,
378 ..Default::default()
379 };
380 if !GetMonitorInfoW(monitor, &mut info).as_bool() {
381 return None;
382 }
383 let (mut dpi_x, mut dpi_y) = (0, 0);
384 let scale = match GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &mut dpi_x, &mut dpi_y) {
385 Ok(()) if dpi_x > 0 => dpi_x as f32 / BASE_DPI,
386 _ => 1.,
387 };
388 let RECT {
389 left,
390 top,
391 right,
392 bottom,
393 } = info.rcWork;
394 Some(ShareGeometry {
395 target,
396 work: WorkArea {
397 left,
398 top,
399 right,
400 bottom,
401 scale,
402 },
403 })
404 }
405}
406
407pub fn share_geometry(kind: ShareKind, id: u64) -> Option<ShareGeometry> {
408 let target = match kind {
409 ShareKind::Screen => screen_frame(u32::try_from(id).ok()?)?,
410 ShareKind::Window => visible_frame(hwnd(id as NativeHandle))?,
411 };
412 geometry_around(target)
413}
414
415pub fn system_quiet() -> bool {
416 let state = unsafe { SHQueryUserNotificationState() };
417 matches!(
418 state,
419 Ok(QUNS_BUSY | QUNS_RUNNING_D3D_FULL_SCREEN | QUNS_PRESENTATION_MODE | QUNS_QUIET_TIME)
420 )
421}
422
423pub fn restore_and_raise(handle: NativeHandle) {
424 unsafe {
425 let command = if IsIconic(hwnd(handle)).as_bool() {
426 SW_RESTORE
427 } else {
428 SW_SHOW
429 };
430 let _ = ShowWindow(hwnd(handle), command);
431 let _ = SetForegroundWindow(hwnd(handle));
432 }
433}
434
435pub fn hide_window(handle: NativeHandle) {
436 unsafe {
437 let _ = ShowWindow(hwnd(handle), SW_HIDE);
438 }
439}
440
441pub fn flash(handle: NativeHandle) {
442 let info = FLASHWINFO {
443 cbSize: size_of::<FLASHWINFO>() as u32,
444 hwnd: hwnd(handle),
445 dwFlags: FLASHW_TRAY | FLASHW_TIMERNOFG,
446 uCount: 0,
447 dwTimeout: 0,
448 };
449 unsafe {
450 let _ = FlashWindowEx(&info);
451 }
452}
453
454pub fn stop_flash(handle: NativeHandle) {
455 let info = FLASHWINFO {
456 cbSize: size_of::<FLASHWINFO>() as u32,
457 hwnd: hwnd(handle),
458 dwFlags: FLASHW_STOP,
459 uCount: 0,
460 dwTimeout: 0,
461 };
462 unsafe {
463 let _ = FlashWindowEx(&info);
464 }
465}
466
467pub fn set_badge(handle: NativeHandle, badge: Option<&Badge>) {
468 unsafe {
469 let Ok(taskbar) =
470 CoCreateInstance::<_, ITaskbarList3>(&TaskbarList, None, CLSCTX_INPROC_SERVER)
471 else {
472 return;
473 };
474 if taskbar.HrInit().is_err() {
475 return;
476 }
477 match badge {
478 None => {
479 let _ = taskbar.SetOverlayIcon(hwnd(handle), HICON::default(), w!(""));
480 }
481 Some(badge) => {
482 let Some(icon) = icon_from_badge(badge) else {
483 return;
484 };
485 let _ = taskbar.SetOverlayIcon(hwnd(handle), icon, w!("Unread chats"));
486 let _ = DestroyIcon(icon);
487 }
488 }
489 }
490}
491
492unsafe fn icon_from_badge(badge: &Badge) -> Option<HICON> {
493 let pixels = render_badge(badge);
494 unsafe {
495 let color = CreateBitmap(
496 BADGE_SIZE as i32,
497 BADGE_SIZE as i32,
498 1,
499 32,
500 Some(pixels.as_ptr() as *const c_void),
501 );
502 let mask = CreateBitmap(BADGE_SIZE as i32, BADGE_SIZE as i32, 1, 1, None);
503 let info = ICONINFO {
504 fIcon: BOOL(1),
505 xHotspot: 0,
506 yHotspot: 0,
507 hbmMask: mask,
508 hbmColor: color,
509 };
510 let icon = CreateIconIndirect(&info).ok();
511 let _ = DeleteObject(HGDIOBJ(color.0));
512 let _ = DeleteObject(HGDIOBJ(mask.0));
513 icon
514 }
515}
516
517pub fn play_mp3_until(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) {
518 std::thread::spawn(move || {
519 let Ok(mut sink) = rodio::DeviceSinkBuilder::open_default_sink() else {
520 return;
521 };
522 sink.log_on_drop(false);
523 loop {
524 let Ok(player) = rodio::play(sink.mixer(), Cursor::new(bytes.clone())) else {
525 return;
526 };
527 while !player.empty() {
528 if stop.load(Ordering::SeqCst) {
529 player.stop();
530 return;
531 }
532 std::thread::sleep(RING_POLL);
533 }
534 if !looped || stop.load(Ordering::SeqCst) {
535 return;
536 }
537 }
538 });
539}
540
541pub fn play_mp3(bytes: Vec<u8>) {
542 std::thread::spawn(move || {
543 let Ok(mut sink) = rodio::DeviceSinkBuilder::open_default_sink() else {
544 return;
545 };
546 sink.log_on_drop(false);
547 if let Ok(player) = rodio::play(sink.mixer(), Cursor::new(bytes)) {
548 player.sleep_until_end();
549 }
550 });
551}
552
553pub fn play_sound() {
554 std::thread::spawn(|| {
555 let Ok(mut sink) = rodio::DeviceSinkBuilder::open_default_sink() else {
556 return;
557 };
558 sink.log_on_drop(false);
559 if let Ok(player) = rodio::play(sink.mixer(), Cursor::new(SOUND_BYTES)) {
560 player.sleep_until_end();
561 }
562 });
563}
564
565const ID_OPEN: &str = "open";
566const ID_SOUND: &str = "sound";
567const ID_DO_NOT_DISTURB: &str = "do_not_disturb";
568const ID_SETTINGS: &str = "settings";
569const ID_QUIT: &str = "quit";
570
571pub struct Tray {
572 icon: TrayIcon,
573 sound: CheckMenuItem,
574 do_not_disturb: CheckMenuItem,
575 plain: Icon,
576 with_dot: Icon,
577}
578
579impl Tray {
580 pub fn new(sound_on: bool, do_not_disturb: bool) -> Option<Self> {
581 let (plain, with_dot) = tray_icons()?;
582 let open = MenuItem::with_id(ID_OPEN, "Open", true, None);
583 let sound = CheckMenuItem::with_id(ID_SOUND, "Mute sound", true, !sound_on, None);
584 let dnd = CheckMenuItem::with_id(
585 ID_DO_NOT_DISTURB,
586 "Do not disturb",
587 true,
588 do_not_disturb,
589 None,
590 );
591 let settings = MenuItem::with_id(ID_SETTINGS, "Notifications", true, None);
592 let quit = MenuItem::with_id(ID_QUIT, "Quit", true, None);
593 let menu = Menu::new();
594 menu.append_items(&[
595 &open,
596 &PredefinedMenuItem::separator(),
597 &sound,
598 &dnd,
599 &settings,
600 &PredefinedMenuItem::separator(),
601 &quit,
602 ])
603 .ok()?;
604 let icon = TrayIconBuilder::new()
605 .with_icon(plain.clone())
606 .with_tooltip(crate::APP_NAME)
607 .with_menu(Box::new(menu))
608 .with_menu_on_left_click(false)
609 .build()
610 .ok()?;
611 Some(Tray {
612 icon,
613 sound,
614 do_not_disturb: dnd,
615 plain,
616 with_dot,
617 })
618 }
619
620 pub fn poll(&self) -> Vec<TrayCommand> {
621 let mut commands = Vec::new();
622 while let Ok(event) = MenuEvent::receiver().try_recv() {
623 let command = match event.id.0.as_str() {
624 ID_OPEN => TrayCommand::Open,
625 ID_SOUND => TrayCommand::ToggleSound,
626 ID_DO_NOT_DISTURB => TrayCommand::ToggleDoNotDisturb,
627 ID_SETTINGS => TrayCommand::Settings,
628 ID_QUIT => TrayCommand::Quit,
629 _ => continue,
630 };
631 commands.push(command);
632 }
633 while let Ok(event) = TrayIconEvent::receiver().try_recv() {
634 if let TrayIconEvent::Click {
635 button: MouseButton::Left,
636 button_state: MouseButtonState::Up,
637 ..
638 } = event
639 {
640 commands.push(TrayCommand::Toggle);
641 }
642 }
643 commands
644 }
645
646 pub fn sync(&self, sound_on: bool, do_not_disturb: bool, mention: bool) {
647 self.sound.set_checked(!sound_on);
648 self.do_not_disturb.set_checked(do_not_disturb);
649 let icon = if mention { &self.with_dot } else { &self.plain };
650 let _ = self.icon.set_icon(Some(icon.clone()));
651 }
652}
653
654fn tray_icons() -> Option<(Icon, Icon)> {
655 let decoded = image::load_from_memory(TRAY_ICON_PNG).ok()?;
656 let resized = decoded
657 .resize_exact(
658 TRAY_ICON_SIZE,
659 TRAY_ICON_SIZE,
660 image::imageops::FilterType::Lanczos3,
661 )
662 .to_rgba8();
663 let plain = resized.clone().into_raw();
664 let mut dotted = resized;
665 let size = TRAY_ICON_SIZE as f32;
666 let center = (size - TRAY_DOT_RADIUS - 1., size - TRAY_DOT_RADIUS - 1.);
667 for (column, row, pixel) in dotted.enumerate_pixels_mut() {
668 let distance =
669 ((column as f32 - center.0).powi(2) + (row as f32 - center.1).powi(2)).sqrt();
670 if distance <= TRAY_DOT_RADIUS {
671 *pixel = image::Rgba([0xef, 0x44, 0x44, 0xff]);
672 }
673 }
674 Some((
675 Icon::from_rgba(plain, TRAY_ICON_SIZE, TRAY_ICON_SIZE).ok()?,
676 Icon::from_rgba(dotted.into_raw(), TRAY_ICON_SIZE, TRAY_ICON_SIZE).ok()?,
677 ))
678}
679
680pub fn animations_enabled() -> bool {
681 let mut enabled = BOOL(1);
682 let read = unsafe {
683 SystemParametersInfoW(
684 SPI_GETCLIENTAREAANIMATION,
685 0,
686 Some(&mut enabled as *mut BOOL as *mut c_void),
687 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
688 )
689 };
690 read.is_err() || enabled.as_bool()
691}