taskbar-focus 0.5.0

A tiny Pomodoro/focus timer that lives in the Windows tray and toggles Do Not Disturb
//! The notification-area (tray) icon.
//!
//! Notifications are delivered as tray balloons via `Shell_NotifyIcon`. On
//! Windows 10/11 the shell renders these as ordinary toasts in the Action
//! Centre, which means they follow the user's notification settings - including
//! being suppressed by Do Not Disturb - without us needing to register an
//! AppUserModelID or install a Start Menu shortcut.

use windows::Win32::Foundation::HWND;
use windows::Win32::UI::Shell::{
    Shell_NotifyIconW, NIF_ICON, NIF_INFO, NIF_MESSAGE, NIF_TIP, NIIF_LARGE_ICON, NIIF_NOSOUND,
    NIIF_USER, NIM_ADD, NIM_DELETE, NIM_MODIFY, NOTIFYICONDATAW,
};
use windows::Win32::UI::WindowsAndMessaging::HICON;

/// Copy `s` into a fixed-size wide buffer, truncating safely.
fn fill(buf: &mut [u16], s: &str) {
    let src: Vec<u16> = s.encode_utf16().take(buf.len().saturating_sub(1)).collect();
    buf[..src.len()].copy_from_slice(&src);
    buf[src.len()] = 0;
    for c in buf.iter_mut().skip(src.len() + 1) {
        *c = 0;
    }
}

pub struct Tray {
    hwnd: HWND,
    id: u32,
    callback: u32,
    added: bool,
}

impl Tray {
    pub fn new(hwnd: HWND, id: u32, callback: u32) -> Self {
        Tray {
            hwnd,
            id,
            callback,
            added: false,
        }
    }

    fn base(&self) -> NOTIFYICONDATAW {
        NOTIFYICONDATAW {
            cbSize: std::mem::size_of::<NOTIFYICONDATAW>() as u32,
            hWnd: self.hwnd,
            uID: self.id,
            uCallbackMessage: self.callback,
            ..Default::default()
        }
    }

    /// Add or update the icon and tooltip.
    ///
    /// The icon is first added as the application icon and only then given
    /// `icon`. Windows takes the small icon in the header of this app's
    /// notifications from the icon it was first added with, and keeps it.
    pub fn set(&mut self, icon: HICON, tooltip: &str) {
        let mut data = self.base();
        data.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
        fill(&mut data.szTip, tooltip);
        if !self.added {
            data.hIcon = crate::ui::app::shared_icon();
            self.added = unsafe { Shell_NotifyIconW(NIM_ADD, &data) }.as_bool();
            if self.added {
                data.hIcon = icon;
                let _ = unsafe { Shell_NotifyIconW(NIM_MODIFY, &data) };
            }
            return;
        }
        data.hIcon = icon;
        self.added = unsafe { Shell_NotifyIconW(NIM_MODIFY, &data) }.as_bool();
    }

    /// Show a notification. `NIIF_NOSOUND` because we play our own cue, and
    /// letting Windows add a second one is grating.
    ///
    /// The notification carries the application icon. Without `NIIF_USER`
    /// Windows shows whatever the tray icon is at that moment, which is the
    /// progress ring, often half drawn or faded.
    ///
    /// Returns whether the shell accepted it.
    pub fn notify(&self, title: &str, body: &str) -> bool {
        if !self.added {
            return false;
        }
        let mut data = self.base();
        data.uFlags = NIF_INFO;
        data.dwInfoFlags = NIIF_NOSOUND | NIIF_USER | NIIF_LARGE_ICON;
        data.hBalloonIcon = crate::ui::app::shared_icon();
        fill(&mut data.szInfoTitle, title);
        fill(&mut data.szInfo, body);
        unsafe { Shell_NotifyIconW(NIM_MODIFY, &data) }.as_bool()
    }

    /// Called after Explorer restarts, which silently drops every tray icon.
    pub fn forget(&mut self) {
        self.added = false;
    }

    pub fn remove(&mut self) {
        if self.added {
            let data = self.base();
            unsafe {
                let _ = Shell_NotifyIconW(NIM_DELETE, &data);
            }
            self.added = false;
        }
    }
}

impl Drop for Tray {
    fn drop(&mut self) {
        self.remove();
    }
}