diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2023-10-31 17:06:14 +0400 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2023-10-31 17:06:14 +0400 |
| commit | 15144220fa207cd909282ea9f91cf794a7762fd8 (patch) | |
| tree | 83c2d999aae024c998bfacbb595c4aef3c55a847 /src | |
| parent | b0af1129c91a21e29da15f9c5ea6a6dd32c1d85d (diff) | |
| download | niri-15144220fa207cd909282ea9f91cf794a7762fd8.tar.gz niri-15144220fa207cd909282ea9f91cf794a7762fd8.tar.bz2 niri-15144220fa207cd909282ea9f91cf794a7762fd8.zip | |
Show notification on screenshot
Diffstat (limited to 'src')
| -rw-r--r-- | src/niri.rs | 15 | ||||
| -rw-r--r-- | src/utils.rs | 31 |
2 files changed, 44 insertions, 2 deletions
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<PathBuf>) { + 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:?}"); + } +} |
