| 1 | use std::ffi::c_void; |
| 2 | use std::io::Cursor; |
| 3 | |
| 4 | use gpui_kit::Window; |
| 5 | use raw_window_handle::{HasWindowHandle, RawWindowHandle}; |
| 6 | use tray_icon::menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem}; |
| 7 | use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent}; |
| 8 | use windows::Win32::Foundation::{HWND, RECT}; |
| 9 | use windows::Win32::Graphics::Dwm::{ |
| 10 | DWMWA_BORDER_COLOR, DWMWA_COLOR_NONE, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND, |
| 11 | DwmSetWindowAttribute, |
| 12 | }; |
| 13 | use windows::Win32::Graphics::Gdi::{ |
| 14 | CreateBitmap, DeleteObject, GetMonitorInfoW, HGDIOBJ, MONITOR_DEFAULTTONEAREST, MONITORINFO, |
| 15 | MonitorFromWindow, |
| 16 | }; |
| 17 | use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance}; |
| 18 | use windows::Win32::UI::HiDpi::GetDpiForWindow; |
| 19 | use windows::Win32::UI::Shell::{ |
| 20 | ITaskbarList3, QUNS_BUSY, QUNS_PRESENTATION_MODE, QUNS_QUIET_TIME, QUNS_RUNNING_D3D_FULL_SCREEN, |
| 21 | SHQueryUserNotificationState, TaskbarList, |
| 22 | }; |
| 23 | use windows::Win32::UI::WindowsAndMessaging::{ |
| 24 | CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO, FlashWindowEx, GWL_EXSTYLE, GWL_STYLE, |
| 25 | GetWindowLongPtrW, HICON, HWND_TOPMOST, ICONINFO, IsIconic, SW_HIDE, SW_RESTORE, SW_SHOW, SWP_NOACTIVATE, |
| 26 | SPI_GETCLIENTAREAANIMATION, SWP_FRAMECHANGED, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE, |
| 27 | SWP_NOZORDER, WS_CAPTION, WS_POPUP, WS_THICKFRAME, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SetForegroundWindow, |
| 28 | SetWindowLongPtrW, SetWindowPos, ShowWindow, SystemParametersInfoW, |
| 29 | WS_EX_NOACTIVATE, |
| 30 | }; |
| 31 | use windows::core::{BOOL, w}; |
| 32 | |
| 33 | use super::{NativeHandle, TrayCommand, WorkArea}; |
| 34 | use crate::notify::badge::{BADGE_SIZE, Badge, render_badge}; |
| 35 | |
| 36 | const SOUND_BYTES: &[u8] = include_bytes!("../../../assets/sounds/notification.mp3"); |
| 37 | const TRAY_ICON_PNG: &[u8] = include_bytes!("../../../assets/icon/teams-fast-256.png"); |
| 38 | const TRAY_ICON_SIZE: u32 = 32; |
| 39 | const TRAY_DOT_RADIUS: f32 = 6.; |
| 40 | const BASE_DPI: f32 = 96.; |
| 41 | |
| 42 | fn hwnd(handle: NativeHandle) -> HWND { |
| 43 | HWND(handle as *mut c_void) |
| 44 | } |
| 45 | |
| 46 | pub fn native_handle(window: &Window) -> Option<NativeHandle> { |
| 47 | match HasWindowHandle::window_handle(window).ok()?.as_raw() { |
| 48 | RawWindowHandle::Win32(handle) => Some(handle.hwnd.get()), |
| 49 | _ => None, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn prepare_toast_window(handle: NativeHandle) { |
| 54 | unsafe { |
| 55 | // GPUI creates popups with style 0, and Windows adds a caption frame to that on its own. |
| 56 | let frame = (WS_CAPTION | WS_THICKFRAME).0 as isize; |
| 57 | let style = GetWindowLongPtrW(hwnd(handle), GWL_STYLE); |
| 58 | SetWindowLongPtrW( |
| 59 | hwnd(handle), |
| 60 | GWL_STYLE, |
| 61 | (style & !frame) | WS_POPUP.0 as isize, |
| 62 | ); |
| 63 | let _ = SetWindowPos( |
| 64 | hwnd(handle), |
| 65 | None, |
| 66 | 0, |
| 67 | 0, |
| 68 | 0, |
| 69 | 0, |
| 70 | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE, |
| 71 | ); |
| 72 | let style = GetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE); |
| 73 | SetWindowLongPtrW( |
| 74 | hwnd(handle), |
| 75 | GWL_EXSTYLE, |
| 76 | style | WS_EX_NOACTIVATE.0 as isize, |
| 77 | ); |
| 78 | let no_border = DWMWA_COLOR_NONE; |
| 79 | let _ = DwmSetWindowAttribute( |
| 80 | hwnd(handle), |
| 81 | DWMWA_BORDER_COLOR, |
| 82 | &no_border as *const _ as *const c_void, |
| 83 | size_of::<u32>() as u32, |
| 84 | ); |
| 85 | let preference = DWMWCP_DONOTROUND; |
| 86 | let _ = DwmSetWindowAttribute( |
| 87 | hwnd(handle), |
| 88 | DWMWA_WINDOW_CORNER_PREFERENCE, |
| 89 | &preference as *const _ as *const c_void, |
| 90 | size_of_val(&preference) as u32, |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn set_activatable(handle: NativeHandle, activatable: bool) { |
| 96 | unsafe { |
| 97 | let style = GetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE); |
| 98 | let flag = WS_EX_NOACTIVATE.0 as isize; |
| 99 | let updated = if activatable { style & !flag } else { style | flag }; |
| 100 | SetWindowLongPtrW(hwnd(handle), GWL_EXSTYLE, updated); |
| 101 | if activatable { |
| 102 | let _ = SetForegroundWindow(hwnd(handle)); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pub fn work_area(handle: NativeHandle) -> Option<WorkArea> { |
| 108 | unsafe { |
| 109 | let monitor = MonitorFromWindow(hwnd(handle), MONITOR_DEFAULTTONEAREST); |
| 110 | let mut info = MONITORINFO { |
| 111 | cbSize: size_of::<MONITORINFO>() as u32, |
| 112 | ..Default::default() |
| 113 | }; |
| 114 | if !GetMonitorInfoW(monitor, &mut info).as_bool() { |
| 115 | return None; |
| 116 | } |
| 117 | let RECT { |
| 118 | left, |
| 119 | top, |
| 120 | right, |
| 121 | bottom, |
| 122 | } = info.rcWork; |
| 123 | let dpi = GetDpiForWindow(hwnd(handle)); |
| 124 | Some(WorkArea { |
| 125 | left, |
| 126 | top, |
| 127 | right, |
| 128 | bottom, |
| 129 | scale: if dpi == 0 { 1. } else { dpi as f32 / BASE_DPI }, |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | pub fn place(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) { |
| 135 | unsafe { |
| 136 | let _ = SetWindowPos( |
| 137 | hwnd(handle), |
| 138 | Some(HWND_TOPMOST), |
| 139 | x, |
| 140 | y, |
| 141 | width, |
| 142 | height, |
| 143 | SWP_NOACTIVATE | SWP_NOOWNERZORDER, |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | pub fn system_quiet() -> bool { |
| 149 | let state = unsafe { SHQueryUserNotificationState() }; |
| 150 | matches!( |
| 151 | state, |
| 152 | Ok(QUNS_BUSY | QUNS_RUNNING_D3D_FULL_SCREEN | QUNS_PRESENTATION_MODE | QUNS_QUIET_TIME) |
| 153 | ) |
| 154 | } |
| 155 | |
| 156 | pub fn restore_and_raise(handle: NativeHandle) { |
| 157 | unsafe { |
| 158 | let command = if IsIconic(hwnd(handle)).as_bool() { |
| 159 | SW_RESTORE |
| 160 | } else { |
| 161 | SW_SHOW |
| 162 | }; |
| 163 | let _ = ShowWindow(hwnd(handle), command); |
| 164 | let _ = SetForegroundWindow(hwnd(handle)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub fn hide_window(handle: NativeHandle) { |
| 169 | unsafe { |
| 170 | let _ = ShowWindow(hwnd(handle), SW_HIDE); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | pub fn flash(handle: NativeHandle) { |
| 175 | let info = FLASHWINFO { |
| 176 | cbSize: size_of::<FLASHWINFO>() as u32, |
| 177 | hwnd: hwnd(handle), |
| 178 | dwFlags: FLASHW_TRAY | FLASHW_TIMERNOFG, |
| 179 | uCount: 0, |
| 180 | dwTimeout: 0, |
| 181 | }; |
| 182 | unsafe { |
| 183 | let _ = FlashWindowEx(&info); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | pub fn stop_flash(handle: NativeHandle) { |
| 188 | let info = FLASHWINFO { |
| 189 | cbSize: size_of::<FLASHWINFO>() as u32, |
| 190 | hwnd: hwnd(handle), |
| 191 | dwFlags: FLASHW_STOP, |
| 192 | uCount: 0, |
| 193 | dwTimeout: 0, |
| 194 | }; |
| 195 | unsafe { |
| 196 | let _ = FlashWindowEx(&info); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | pub fn set_badge(handle: NativeHandle, badge: Option<&Badge>) { |
| 201 | unsafe { |
| 202 | let Ok(taskbar) = CoCreateInstance::<_, ITaskbarList3>(&TaskbarList, None, CLSCTX_INPROC_SERVER) |
| 203 | else { |
| 204 | return; |
| 205 | }; |
| 206 | if taskbar.HrInit().is_err() { |
| 207 | return; |
| 208 | } |
| 209 | match badge { |
| 210 | None => { |
| 211 | let _ = taskbar.SetOverlayIcon(hwnd(handle), HICON::default(), w!("")); |
| 212 | } |
| 213 | Some(badge) => { |
| 214 | let Some(icon) = icon_from_badge(badge) else { |
| 215 | return; |
| 216 | }; |
| 217 | let _ = taskbar.SetOverlayIcon(hwnd(handle), icon, w!("Unread chats")); |
| 218 | let _ = DestroyIcon(icon); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | unsafe fn icon_from_badge(badge: &Badge) -> Option<HICON> { |
| 225 | let pixels = render_badge(badge); |
| 226 | unsafe { |
| 227 | let color = CreateBitmap( |
| 228 | BADGE_SIZE as i32, |
| 229 | BADGE_SIZE as i32, |
| 230 | 1, |
| 231 | 32, |
| 232 | Some(pixels.as_ptr() as *const c_void), |
| 233 | ); |
| 234 | let mask = CreateBitmap(BADGE_SIZE as i32, BADGE_SIZE as i32, 1, 1, None); |
| 235 | let info = ICONINFO { |
| 236 | fIcon: BOOL(1), |
| 237 | xHotspot: 0, |
| 238 | yHotspot: 0, |
| 239 | hbmMask: mask, |
| 240 | hbmColor: color, |
| 241 | }; |
| 242 | let icon = CreateIconIndirect(&info).ok(); |
| 243 | let _ = DeleteObject(HGDIOBJ(color.0)); |
| 244 | let _ = DeleteObject(HGDIOBJ(mask.0)); |
| 245 | icon |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | pub fn play_sound() { |
| 250 | std::thread::spawn(|| { |
| 251 | let Ok(mut sink) = rodio::DeviceSinkBuilder::open_default_sink() else { |
| 252 | return; |
| 253 | }; |
| 254 | sink.log_on_drop(false); |
| 255 | if let Ok(player) = rodio::play(sink.mixer(), Cursor::new(SOUND_BYTES)) { |
| 256 | player.sleep_until_end(); |
| 257 | } |
| 258 | }); |
| 259 | } |
| 260 | |
| 261 | const ID_OPEN: &str = "open"; |
| 262 | const ID_SOUND: &str = "sound"; |
| 263 | const ID_DO_NOT_DISTURB: &str = "do_not_disturb"; |
| 264 | const ID_SETTINGS: &str = "settings"; |
| 265 | const ID_QUIT: &str = "quit"; |
| 266 | |
| 267 | pub struct Tray { |
| 268 | icon: TrayIcon, |
| 269 | sound: CheckMenuItem, |
| 270 | do_not_disturb: CheckMenuItem, |
| 271 | plain: Icon, |
| 272 | with_dot: Icon, |
| 273 | } |
| 274 | |
| 275 | impl Tray { |
| 276 | pub fn new(sound_on: bool, do_not_disturb: bool) -> Option<Self> { |
| 277 | let (plain, with_dot) = tray_icons()?; |
| 278 | let open = MenuItem::with_id(ID_OPEN, "Open", true, None); |
| 279 | let sound = CheckMenuItem::with_id(ID_SOUND, "Mute sound", true, !sound_on, None); |
| 280 | let dnd = CheckMenuItem::with_id( |
| 281 | ID_DO_NOT_DISTURB, |
| 282 | "Do not disturb", |
| 283 | true, |
| 284 | do_not_disturb, |
| 285 | None, |
| 286 | ); |
| 287 | let settings = MenuItem::with_id(ID_SETTINGS, "Notifications", true, None); |
| 288 | let quit = MenuItem::with_id(ID_QUIT, "Quit", true, None); |
| 289 | let menu = Menu::new(); |
| 290 | menu.append_items(&[ |
| 291 | &open, |
| 292 | &PredefinedMenuItem::separator(), |
| 293 | &sound, |
| 294 | &dnd, |
| 295 | &settings, |
| 296 | &PredefinedMenuItem::separator(), |
| 297 | &quit, |
| 298 | ]) |
| 299 | .ok()?; |
| 300 | let icon = TrayIconBuilder::new() |
| 301 | .with_icon(plain.clone()) |
| 302 | .with_tooltip(crate::APP_NAME) |
| 303 | .with_menu(Box::new(menu)) |
| 304 | .with_menu_on_left_click(false) |
| 305 | .build() |
| 306 | .ok()?; |
| 307 | Some(Tray { |
| 308 | icon, |
| 309 | sound, |
| 310 | do_not_disturb: dnd, |
| 311 | plain, |
| 312 | with_dot, |
| 313 | }) |
| 314 | } |
| 315 | |
| 316 | pub fn poll(&self) -> Vec<TrayCommand> { |
| 317 | let mut commands = Vec::new(); |
| 318 | while let Ok(event) = MenuEvent::receiver().try_recv() { |
| 319 | let command = match event.id.0.as_str() { |
| 320 | ID_OPEN => TrayCommand::Open, |
| 321 | ID_SOUND => TrayCommand::ToggleSound, |
| 322 | ID_DO_NOT_DISTURB => TrayCommand::ToggleDoNotDisturb, |
| 323 | ID_SETTINGS => TrayCommand::Settings, |
| 324 | ID_QUIT => TrayCommand::Quit, |
| 325 | _ => continue, |
| 326 | }; |
| 327 | commands.push(command); |
| 328 | } |
| 329 | while let Ok(event) = TrayIconEvent::receiver().try_recv() { |
| 330 | if let TrayIconEvent::Click { |
| 331 | button: MouseButton::Left, |
| 332 | button_state: MouseButtonState::Up, |
| 333 | .. |
| 334 | } = event |
| 335 | { |
| 336 | commands.push(TrayCommand::Toggle); |
| 337 | } |
| 338 | } |
| 339 | commands |
| 340 | } |
| 341 | |
| 342 | pub fn sync(&self, sound_on: bool, do_not_disturb: bool, mention: bool) { |
| 343 | self.sound.set_checked(!sound_on); |
| 344 | self.do_not_disturb.set_checked(do_not_disturb); |
| 345 | let icon = if mention { &self.with_dot } else { &self.plain }; |
| 346 | let _ = self.icon.set_icon(Some(icon.clone())); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | fn tray_icons() -> Option<(Icon, Icon)> { |
| 351 | let decoded = image::load_from_memory(TRAY_ICON_PNG).ok()?; |
| 352 | let resized = decoded |
| 353 | .resize_exact( |
| 354 | TRAY_ICON_SIZE, |
| 355 | TRAY_ICON_SIZE, |
| 356 | image::imageops::FilterType::Lanczos3, |
| 357 | ) |
| 358 | .to_rgba8(); |
| 359 | let plain = resized.clone().into_raw(); |
| 360 | let mut dotted = resized; |
| 361 | let size = TRAY_ICON_SIZE as f32; |
| 362 | let center = (size - TRAY_DOT_RADIUS - 1., size - TRAY_DOT_RADIUS - 1.); |
| 363 | for (column, row, pixel) in dotted.enumerate_pixels_mut() { |
| 364 | let distance = ((column as f32 - center.0).powi(2) + (row as f32 - center.1).powi(2)).sqrt(); |
| 365 | if distance <= TRAY_DOT_RADIUS { |
| 366 | *pixel = image::Rgba([0xef, 0x44, 0x44, 0xff]); |
| 367 | } |
| 368 | } |
| 369 | Some(( |
| 370 | Icon::from_rgba(plain, TRAY_ICON_SIZE, TRAY_ICON_SIZE).ok()?, |
| 371 | Icon::from_rgba(dotted.into_raw(), TRAY_ICON_SIZE, TRAY_ICON_SIZE).ok()?, |
| 372 | )) |
| 373 | } |
| 374 | |
| 375 | pub fn animations_enabled() -> bool { |
| 376 | let mut enabled = BOOL(1); |
| 377 | let read = unsafe { |
| 378 | SystemParametersInfoW( |
| 379 | SPI_GETCLIENTAREAANIMATION, |
| 380 | 0, |
| 381 | Some(&mut enabled as *mut BOOL as *mut c_void), |
| 382 | SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0), |
| 383 | ) |
| 384 | }; |
| 385 | read.is_err() || enabled.as_bool() |
| 386 | } |