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