Skip to content

Commit

Permalink
feat: add silent option (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Jun 20, 2023
1 parent 5ba7167 commit c3c0136
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/silent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"win7-notifications": "minor"
---

Add option to make the notification silent
26 changes: 21 additions & 5 deletions src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const CLOSE_BTN_RECT: RECT = RECT {
right: (NW - NM - NM / 2) + 8,
bottom: NM + 8,
};
const CLOSE_BTN_RECT_EXTRA: RECT = RECT {
left: CLOSE_BTN_RECT.left - 8,
top: CLOSE_BTN_RECT.top - 8,
right: CLOSE_BTN_RECT.right + 8,
bottom: CLOSE_BTN_RECT.bottom + 8,
};

static ACTIVE_NOTIFICATIONS: Lazy<Mutex<Vec<HWND>>> = Lazy::new(|| Mutex::new(Vec::new()));
static PRIMARY_MONITOR: Lazy<Mutex<MONITORINFOEXW>> =
Expand All @@ -60,6 +66,7 @@ pub struct Notification {
pub summary: String,
pub body: String,
pub timeout: Timeout,
pub silent: bool,
}

impl Default for Notification {
Expand All @@ -72,6 +79,7 @@ impl Default for Notification {
icon_height: 32,
icon_width: 32,
timeout: Timeout::Default,
silent: false,
}
}
}
Expand Down Expand Up @@ -134,6 +142,12 @@ impl Notification {
self
}

/// Set the `silent` field.
pub fn silent(&mut self, silent: bool) -> &mut Notification {
self.silent = silent;
self
}

/// Shows the Notification.
///
/// Requires a win32 event_loop to be running on the thread, otherwise the notification will close immediately.
Expand Down Expand Up @@ -219,9 +233,11 @@ impl Notification {

util::skip_taskbar(hwnd);
ShowWindow(hwnd, SW_SHOW);
// Passing an invalid path to `PlaySoundW` will make windows play default sound.
// https://docs.microsoft.com/en-us/previous-versions/dd743680(v=vs.85)#remarks
PlaySoundW(w!("null"), hinstance, SND_ASYNC);
if !self.silent {
// Passing an invalid path to `PlaySoundW` will make windows play default sound.
// https://docs.microsoft.com/en-us/previous-versions/dd743680(v=vs.85)#remarks
PlaySoundW(w!("null"), hinstance, SND_ASYNC);
}

let timeout = self.timeout;
thread::spawn(move || {
Expand Down Expand Up @@ -422,7 +438,7 @@ pub unsafe extern "system" fn window_proc(
let userdata = userdata as *mut WindowData;

let (x, y) = (GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));
let hit = util::rect_contains(CLOSE_BTN_RECT, x as i32, y as i32);
let hit = util::rect_contains(CLOSE_BTN_RECT_EXTRA, x as i32, y as i32);

SetCursor(LoadCursorW(0, if hit { IDC_HAND } else { IDC_ARROW }));
if hit != (*userdata).mouse_hovering_close_btn {
Expand All @@ -437,7 +453,7 @@ pub unsafe extern "system" fn window_proc(
w32wm::WM_LBUTTONDOWN => {
let (x, y) = (GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));

if util::rect_contains(CLOSE_BTN_RECT, x as i32, y as i32) {
if util::rect_contains(CLOSE_BTN_RECT_EXTRA, x as i32, y as i32) {
close_notification(hwnd)
}

Expand Down

0 comments on commit c3c0136

Please sign in to comment.