Event-stream event for when a screenshot is taken (#2565)
Browse files at this commit- feat
- event-stream event for when a screenshot is taken (#2565)
5 changed files +48 -4
- niri-ipc/src/lib.rs +8 -0
- src/ipc/client.rs +9 -0
- src/ipc/server.rs +11 -0
- src/niri.rs +19 -3
- src/utils/mod.rs +1 -1
niri-ipc/src/lib.rsmodified+8 -0
| @@ -1483,6 +1483,14 @@ pub enum Event { | |||
| 1483 | /// For example, the config file couldn't be parsed. | 1483 | /// For example, the config file couldn't be parsed. |
| 1484 | failed: bool, | 1484 | failed: bool, |
| 1485 | }, | 1485 | }, |
| 1486 | + /// A screenshot was captured. | ||
| 1487 | + ScreenshotCaptured { | ||
| 1488 | + /// The file path where the screenshot was saved, if it was written to disk. | ||
| 1489 | + /// | ||
| 1490 | + /// If `None`, the screenshot was either only copied to the clipboard, or the path couldn't | ||
| 1491 | + /// be converted to a `String` (e.g. contained invalid UTF-8 bytes). | ||
| 1492 | + path: Option<String>, | ||
| 1493 | + }, | ||
| 1486 | } | 1494 | } |
| 1487 | 1495 | ||
| 1488 | impl FromStr for WorkspaceReferenceArg { | 1496 | impl FromStr for WorkspaceReferenceArg { |
src/ipc/client.rsmodified+9 -0
| @@ -481,6 +481,15 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 481 | }; | 481 | }; |
| 482 | println!("Config loaded {status}"); | 482 | println!("Config loaded {status}"); |
| 483 | } | 483 | } |
| 484 | + Event::ScreenshotCaptured { path } => { | ||
| 485 | + let mut parts = vec![]; | ||
| 486 | + parts.push("copied to clipboard".to_string()); | ||
| 487 | + if let Some(path) = &path { | ||
| 488 | + parts.push(format!("saved to {path}")); | ||
| 489 | + } | ||
| 490 | + let description = parts.join(" and "); | ||
| 491 | + println!("Screenshot captured: {description}"); | ||
| 492 | + } | ||
| 484 | } | 493 | } |
| 485 | } | 494 | } |
| 486 | } | 495 | } |
src/ipc/server.rsmodified+11 -0
| @@ -792,4 +792,15 @@ impl State { | |||
| 792 | state.apply(event.clone()); | 792 | state.apply(event.clone()); |
| 793 | server.send_event(event); | 793 | server.send_event(event); |
| 794 | } | 794 | } |
| 795 | + | ||
| 796 | + pub fn ipc_screenshot_taken(&mut self, path: Option<String>) { | ||
| 797 | + let Some(server) = &self.niri.ipc_server else { | ||
| 798 | + return; | ||
| 799 | + }; | ||
| 800 | + let mut state = server.event_stream_state.borrow_mut(); | ||
| 801 | + | ||
| 802 | + let event = Event::ScreenshotCaptured { path }; | ||
| 803 | + state.apply(event.clone()); | ||
| 804 | + server.send_event(event); | ||
| 805 | + } | ||
| 795 | } | 806 | } |
src/niri.rsmodified+19 -3
| @@ -5643,6 +5643,17 @@ impl Niri { | |||
| 5643 | }) | 5643 | }) |
| 5644 | .unwrap(); | 5644 | .unwrap(); |
| 5645 | 5645 | ||
| 5646 | + // Prepare to send screenshot completion event back to main thread. | ||
| 5647 | + let (event_tx, event_rx) = calloop::channel::sync_channel::<Option<String>>(1); | ||
| 5648 | + self.event_loop | ||
| 5649 | + .insert_source(event_rx, move |event, _, state| match event { | ||
| 5650 | + calloop::channel::Event::Msg(path) => { | ||
| 5651 | + state.ipc_screenshot_taken(path); | ||
| 5652 | + } | ||
| 5653 | + calloop::channel::Event::Closed => (), | ||
| 5654 | + }) | ||
| 5655 | + .unwrap(); | ||
| 5656 | + | ||
| 5646 | // Encode and save the image in a thread as it's slow. | 5657 | // Encode and save the image in a thread as it's slow. |
| 5647 | thread::spawn(move || { | 5658 | thread::spawn(move || { |
| 5648 | let mut buf = vec![]; | 5659 | let mut buf = vec![]; |
| @@ -5685,11 +5696,16 @@ impl Niri { | |||
| 5685 | } | 5696 | } |
| 5686 | 5697 | ||
| 5687 | #[cfg(feature = "dbus")] | 5698 | #[cfg(feature = "dbus")] |
| 5688 | - if let Err(err) = crate::utils::show_screenshot_notification(image_path) { | 5699 | + if let Err(err) = crate::utils::show_screenshot_notification(image_path.as_deref()) { |
| 5689 | warn!("error showing screenshot notification: {err:?}"); | 5700 | warn!("error showing screenshot notification: {err:?}"); |
| 5690 | } | 5701 | } |
| 5691 | - #[cfg(not(feature = "dbus"))] | 5702 | + |
| 5692 | - drop(image_path); | 5703 | + // Send screenshot completion event. |
| 5704 | + let path_string = image_path | ||
| 5705 | + .as_ref() | ||
| 5706 | + .and_then(|p| p.to_str()) | ||
| 5707 | + .map(|s| s.to_owned()); | ||
| 5708 | + let _ = event_tx.send(path_string); | ||
| 5693 | }); | 5709 | }); |
| 5694 | 5710 | ||
| 5695 | Ok(()) | 5711 | Ok(()) |
src/utils/mod.rsmodified+1 -1
| @@ -461,7 +461,7 @@ pub fn baba_is_float_offset(now: Duration, view_height: f64) -> f64 { | |||
| 461 | } | 461 | } |
| 462 | 462 | ||
| 463 | #[cfg(feature = "dbus")] | 463 | #[cfg(feature = "dbus")] |
| 464 | -pub fn show_screenshot_notification(image_path: Option<PathBuf>) -> anyhow::Result<()> { | 464 | +pub fn show_screenshot_notification(image_path: Option<&Path>) -> anyhow::Result<()> { |
| 465 | use std::collections::HashMap; | 465 | use std::collections::HashMap; |
| 466 | 466 | ||
| 467 | use zbus::zvariant; | 467 | use zbus::zvariant; |