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>
This commit is contained in:
Lin Xianyi
2025-10-19 11:22:31 +00:00
committed by GitHub
parent 8c8447918f
commit 23cd5aa78a
8 changed files with 178 additions and 58 deletions
+32 -18
View File
@@ -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:?}");
}
}
}
}
}