BlueGit

ict rusty-teams

Files

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

windows_impl.rs

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