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