diff options
| author | Kainoa Kanter <kainoa@t1c.dev> | 2025-10-25 23:33:49 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-26 06:33:49 +0000 |
| commit | e6f3c538da0c646bda43fcde7ef7dc3b771e0c8b (patch) | |
| tree | 935a519695fecc6998790bb628c85c1d6db52262 | |
| parent | 4310c20c320d040f3df7a93de4064e452a1876ae (diff) | |
| download | niri-e6f3c538da0c646bda43fcde7ef7dc3b771e0c8b.tar.gz niri-e6f3c538da0c646bda43fcde7ef7dc3b771e0c8b.tar.bz2 niri-e6f3c538da0c646bda43fcde7ef7dc3b771e0c8b.zip | |
feat: event-stream event for when a screenshot is taken (#2565)
* feat: event-stream event for when a screenshot is taken
* ScreenshotTaken --> ScreenshotCaptured
* review comments
* fix: screenshot completion event path serializatation
* fixes
---------
Co-authored-by: Ivan Molodetskikh <yalterz@gmail.com>
| -rw-r--r-- | niri-ipc/src/lib.rs | 8 | ||||
| -rw-r--r-- | src/ipc/client.rs | 9 | ||||
| -rw-r--r-- | src/ipc/server.rs | 11 | ||||
| -rw-r--r-- | src/niri.rs | 22 | ||||
| -rw-r--r-- | src/utils/mod.rs | 2 |
5 files changed, 48 insertions, 4 deletions
diff --git a/niri-ipc/src/lib.rs b/niri-ipc/src/lib.rs index 05c3a2ec..afe9a6d2 100644 --- a/niri-ipc/src/lib.rs +++ b/niri-ipc/src/lib.rs @@ -1483,6 +1483,14 @@ pub enum Event { /// For example, the config file couldn't be parsed. failed: bool, }, + /// A screenshot was captured. + ScreenshotCaptured { + /// The file path where the screenshot was saved, if it was written to disk. + /// + /// If `None`, the screenshot was either only copied to the clipboard, or the path couldn't + /// be converted to a `String` (e.g. contained invalid UTF-8 bytes). + path: Option<String>, + }, } impl FromStr for WorkspaceReferenceArg { diff --git a/src/ipc/client.rs b/src/ipc/client.rs index 4c356355..3c7b27a9 100644 --- a/src/ipc/client.rs +++ b/src/ipc/client.rs @@ -481,6 +481,15 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { }; println!("Config loaded {status}"); } + Event::ScreenshotCaptured { path } => { + let mut parts = vec![]; + parts.push("copied to clipboard".to_string()); + if let Some(path) = &path { + parts.push(format!("saved to {path}")); + } + let description = parts.join(" and "); + println!("Screenshot captured: {description}"); + } } } } diff --git a/src/ipc/server.rs b/src/ipc/server.rs index 354023b7..7fdc81f5 100644 --- a/src/ipc/server.rs +++ b/src/ipc/server.rs @@ -792,4 +792,15 @@ impl State { state.apply(event.clone()); server.send_event(event); } + + pub fn ipc_screenshot_taken(&mut self, path: Option<String>) { + let Some(server) = &self.niri.ipc_server else { + return; + }; + let mut state = server.event_stream_state.borrow_mut(); + + let event = Event::ScreenshotCaptured { path }; + state.apply(event.clone()); + server.send_event(event); + } } diff --git a/src/niri.rs b/src/niri.rs index e341e964..5bc69c2e 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -5643,6 +5643,17 @@ impl Niri { }) .unwrap(); + // Prepare to send screenshot completion event back to main thread. + let (event_tx, event_rx) = calloop::channel::sync_channel::<Option<String>>(1); + self.event_loop + .insert_source(event_rx, move |event, _, state| match event { + calloop::channel::Event::Msg(path) => { + state.ipc_screenshot_taken(path); + } + calloop::channel::Event::Closed => (), + }) + .unwrap(); + // Encode and save the image in a thread as it's slow. thread::spawn(move || { let mut buf = vec![]; @@ -5685,11 +5696,16 @@ impl Niri { } #[cfg(feature = "dbus")] - if let Err(err) = crate::utils::show_screenshot_notification(image_path) { + if let Err(err) = crate::utils::show_screenshot_notification(image_path.as_deref()) { warn!("error showing screenshot notification: {err:?}"); } - #[cfg(not(feature = "dbus"))] - drop(image_path); + + // Send screenshot completion event. + let path_string = image_path + .as_ref() + .and_then(|p| p.to_str()) + .map(|s| s.to_owned()); + let _ = event_tx.send(path_string); }); Ok(()) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 53537098..4a7b4b6f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -461,7 +461,7 @@ pub fn baba_is_float_offset(now: Duration, view_height: f64) -> f64 { } #[cfg(feature = "dbus")] -pub fn show_screenshot_notification(image_path: Option<PathBuf>) -> anyhow::Result<()> { +pub fn show_screenshot_notification(image_path: Option<&Path>) -> anyhow::Result<()> { use std::collections::HashMap; use zbus::zvariant; |
