diff options
| author | Lin Xianyi <iynaix@gmail.com> | 2025-10-19 11:22:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-19 14:22:31 +0300 |
| commit | 23cd5aa78a26ccf57c6a7993313e42a5c39d43d8 (patch) | |
| tree | fff3ad5057850bf916c404cc95437e1058c56a20 /src/niri.rs | |
| parent | 8c8447918f4fd7bc6c86a8622b1db52417fbbbbd (diff) | |
| download | niri-23cd5aa78a26ccf57c6a7993313e42a5c39d43d8.tar.gz niri-23cd5aa78a26ccf57c6a7993313e42a5c39d43d8.tar.bz2 niri-23cd5aa78a26ccf57c6a7993313e42a5c39d43d8.zip | |
Add --path argument for niri msg screenshot* commands (#2126)
* Check for empty screenshot parent before creating
Avoids a warning.
* Add --path argument for niri msg screenshot* commands
* fix
---------
Co-authored-by: Ivan Molodetskikh <yalterz@gmail.com>
Diffstat (limited to 'src/niri.rs')
| -rw-r--r-- | src/niri.rs | 50 |
1 files changed, 32 insertions, 18 deletions
diff --git a/src/niri.rs b/src/niri.rs index 3ced8a90..0a7894b5 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -1835,7 +1835,7 @@ impl State { self.niri.output_management_state.notify_changes(new_config); } - pub fn open_screenshot_ui(&mut self, show_pointer: bool) { + pub fn open_screenshot_ui(&mut self, show_pointer: bool, path: Option<String>) { if self.niri.is_locked() || self.niri.screenshot_ui.is_open() { return; } @@ -1876,7 +1876,7 @@ impl State { self.backend.with_primary_renderer(|renderer| { self.niri .screenshot_ui - .open(renderer, screenshots, default_output, show_pointer) + .open(renderer, screenshots, default_output, show_pointer, path) }); self.niri @@ -1902,14 +1902,15 @@ impl State { } pub fn confirm_screenshot(&mut self, write_to_disk: bool) { - if !self.niri.screenshot_ui.is_open() { + let ScreenshotUi::Open { path, .. } = &mut self.niri.screenshot_ui else { return; - } + }; + let path = path.take(); self.backend.with_primary_renderer(|renderer| { match self.niri.screenshot_ui.capture(renderer) { Ok((size, pixels)) => { - if let Err(err) = self.niri.save_screenshot(size, pixels, write_to_disk) { + if let Err(err) = self.niri.save_screenshot(size, pixels, write_to_disk, path) { warn!("error saving screenshot: {err:?}"); } } @@ -5533,6 +5534,7 @@ impl Niri { output: &Output, write_to_disk: bool, include_pointer: bool, + path: Option<String>, ) -> anyhow::Result<()> { let _span = tracy_client::span!("Niri::screenshot"); @@ -5559,7 +5561,7 @@ impl Niri { elements, )?; - self.save_screenshot(size, pixels, write_to_disk) + self.save_screenshot(size, pixels, write_to_disk, path) .context("error saving screenshot") } @@ -5569,6 +5571,7 @@ impl Niri { output: &Output, mapped: &Mapped, write_to_disk: bool, + path: Option<String>, ) -> anyhow::Result<()> { let _span = tracy_client::span!("Niri::screenshot_window"); @@ -5600,7 +5603,7 @@ impl Niri { elements, )?; - self.save_screenshot(geo.size, pixels, write_to_disk) + self.save_screenshot(geo.size, pixels, write_to_disk, path) .context("error saving screenshot") } @@ -5609,14 +5612,20 @@ impl Niri { size: Size<i32, Physical>, pixels: Vec<u8>, write_to_disk: bool, + path_arg: Option<String>, ) -> anyhow::Result<()> { let path = write_to_disk - .then(|| match make_screenshot_path(&self.config.borrow()) { - Ok(path) => path, - Err(err) => { - warn!("error making screenshot path: {err:?}"); - None - } + .then(|| { + // When given an explicit path, don't try to strftime it or create parents. + path_arg.map(|p| (PathBuf::from(p), false)).or_else(|| { + match make_screenshot_path(&self.config.borrow()) { + Ok(path) => path.map(|p| (p, true)), + Err(err) => { + warn!("error making screenshot path: {err:?}"); + None + } + } + }) }) .flatten(); @@ -5652,13 +5661,18 @@ impl Niri { let mut image_path = None; - if let Some(path) = path { + if let Some((path, create_parent)) = path { debug!("saving screenshot to {path:?}"); - if let Some(parent) = path.parent() { - if let Err(err) = std::fs::create_dir(parent) { - if err.kind() != std::io::ErrorKind::AlreadyExists { - warn!("error creating screenshot directory: {err:?}"); + if create_parent { + if let Some(parent) = path.parent() { + // Relative paths with one component, i.e. "test.png", have Some("") parent. + if !parent.as_os_str().is_empty() { + if let Err(err) = std::fs::create_dir(parent) { + if err.kind() != std::io::ErrorKind::AlreadyExists { + warn!("error creating screenshot directory: {err:?}"); + } + } } } } |
