From 15144220fa207cd909282ea9f91cf794a7762fd8 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Tue, 31 Oct 2023 17:06:14 +0400 Subject: Show notification on screenshot --- src/niri.rs | 15 +++++++++++++-- src/utils.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/niri.rs b/src/niri.rs index 5f5a02e4..440962c2 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -2053,14 +2053,25 @@ impl Niri { let buf: Arc<[u8]> = Arc::from(buf.into_boxed_slice()); let _ = tx.send(buf.clone()); + let mut image_path = None; + if let Some(path) = path { debug!("saving screenshot to {path:?}"); - if let Err(err) = std::fs::write(path, buf) { - warn!("error saving screenshot image: {err:?}"); + + match std::fs::write(&path, buf) { + Ok(()) => image_path = Some(path), + Err(err) => { + warn!("error saving screenshot image: {err:?}"); + } } } else { debug!("not saving screenshot to disk"); } + + #[cfg(feature = "dbus")] + crate::utils::show_screenshot_notification(image_path); + #[cfg(not(feature = "dbus"))] + drop(image_path); }); Ok(()) diff --git a/src/utils.rs b/src/utils.rs index 15917f85..a04b8e9b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -112,3 +112,34 @@ pub fn write_png_rgba8( let mut writer = encoder.write_header()?; writer.write_image_data(pixels) } + +#[cfg(feature = "dbus")] +pub fn show_screenshot_notification(image_path: Option) { + let mut notification = notify_rust::Notification::new(); + notification + .summary("Screenshot captured") + .body("You can paste the image from the clipboard.") + .urgency(notify_rust::Urgency::Normal) + .hint(notify_rust::Hint::Transient(true)); + + // Try to add the screenshot as an image if possible. + if let Some(path) = image_path { + match path.canonicalize() { + Ok(path) => match url::Url::from_file_path(path) { + Ok(url) => { + notification.image_path(url.as_str()); + } + Err(err) => { + warn!("error converting screenshot path to file url: {err:?}"); + } + }, + Err(err) => { + warn!("error canonicalizing screenshot path: {err:?}"); + } + } + } + + if let Err(err) = notification.show() { + warn!("error showing screenshot notification: {err:?}"); + } +} -- cgit