add write-to-disk argument to screenshot actions
Browse files at this commit0df7a085de6fb0ebfe7af640e60fb496e52656aaParent: 6ae51f25 changed files +70 -29
- niri-config/src/lib.rs +20 -7
- niri-ipc/src/lib.rs +12 -1
- src/input/mod.rs +15 -8
- src/niri.rs +14 -9
- src/ui/screenshot_ui.rs +9 -4
niri-config/src/lib.rsmodified+20 -7
| @@ -1185,16 +1185,21 @@ pub enum Action { | |||
| 1185 | Spawn(#[knuffel(arguments)] Vec<String>), | 1185 | Spawn(#[knuffel(arguments)] Vec<String>), |
| 1186 | DoScreenTransition(#[knuffel(property(name = "delay-ms"))] Option<u16>), | 1186 | DoScreenTransition(#[knuffel(property(name = "delay-ms"))] Option<u16>), |
| 1187 | #[knuffel(skip)] | 1187 | #[knuffel(skip)] |
| 1188 | - ConfirmScreenshot, | 1188 | + ConfirmScreenshot { |
| 1189 | + write_to_disk: bool, | ||
| 1190 | + }, | ||
| 1189 | #[knuffel(skip)] | 1191 | #[knuffel(skip)] |
| 1190 | CancelScreenshot, | 1192 | CancelScreenshot, |
| 1191 | #[knuffel(skip)] | 1193 | #[knuffel(skip)] |
| 1192 | ScreenshotTogglePointer, | 1194 | ScreenshotTogglePointer, |
| 1193 | Screenshot, | 1195 | Screenshot, |
| 1194 | - ScreenshotScreen, | 1196 | + ScreenshotScreen(#[knuffel(property(name = "write-to-disk"), default = true)] bool), |
| 1195 | - ScreenshotWindow, | 1197 | + ScreenshotWindow(#[knuffel(property(name = "write-to-disk"), default = true)] bool), |
| 1196 | #[knuffel(skip)] | 1198 | #[knuffel(skip)] |
| 1197 | - ScreenshotWindowById(u64), | 1199 | + ScreenshotWindowById { |
| 1200 | + id: u64, | ||
| 1201 | + write_to_disk: bool, | ||
| 1202 | + }, | ||
| 1198 | CloseWindow, | 1203 | CloseWindow, |
| 1199 | #[knuffel(skip)] | 1204 | #[knuffel(skip)] |
| 1200 | CloseWindowById(u64), | 1205 | CloseWindowById(u64), |
| @@ -1351,9 +1356,17 @@ impl From<niri_ipc::Action> for Action { | |||
| 1351 | niri_ipc::Action::Spawn { command } => Self::Spawn(command), | 1356 | niri_ipc::Action::Spawn { command } => Self::Spawn(command), |
| 1352 | niri_ipc::Action::DoScreenTransition { delay_ms } => Self::DoScreenTransition(delay_ms), | 1357 | niri_ipc::Action::DoScreenTransition { delay_ms } => Self::DoScreenTransition(delay_ms), |
| 1353 | niri_ipc::Action::Screenshot {} => Self::Screenshot, | 1358 | niri_ipc::Action::Screenshot {} => Self::Screenshot, |
| 1354 | - niri_ipc::Action::ScreenshotScreen {} => Self::ScreenshotScreen, | 1359 | + niri_ipc::Action::ScreenshotScreen { write_to_disk } => { |
| 1355 | - niri_ipc::Action::ScreenshotWindow { id: None } => Self::ScreenshotWindow, | 1360 | + Self::ScreenshotScreen(write_to_disk) |
| 1356 | - niri_ipc::Action::ScreenshotWindow { id: Some(id) } => Self::ScreenshotWindowById(id), | 1361 | + } |
| 1362 | + niri_ipc::Action::ScreenshotWindow { | ||
| 1363 | + id: None, | ||
| 1364 | + write_to_disk, | ||
| 1365 | + } => Self::ScreenshotWindow(write_to_disk), | ||
| 1366 | + niri_ipc::Action::ScreenshotWindow { | ||
| 1367 | + id: Some(id), | ||
| 1368 | + write_to_disk, | ||
| 1369 | + } => Self::ScreenshotWindowById { id, write_to_disk }, | ||
| 1357 | niri_ipc::Action::CloseWindow { id: None } => Self::CloseWindow, | 1370 | niri_ipc::Action::CloseWindow { id: None } => Self::CloseWindow, |
| 1358 | niri_ipc::Action::CloseWindow { id: Some(id) } => Self::CloseWindowById(id), | 1371 | niri_ipc::Action::CloseWindow { id: Some(id) } => Self::CloseWindowById(id), |
| 1359 | niri_ipc::Action::FullscreenWindow { id: None } => Self::FullscreenWindow, | 1372 | niri_ipc::Action::FullscreenWindow { id: None } => Self::FullscreenWindow, |
niri-ipc/src/lib.rsmodified+12 -1
| @@ -167,7 +167,13 @@ pub enum Action { | |||
| 167 | /// Open the screenshot UI. | 167 | /// Open the screenshot UI. |
| 168 | Screenshot {}, | 168 | Screenshot {}, |
| 169 | /// Screenshot the focused screen. | 169 | /// Screenshot the focused screen. |
| 170 | - ScreenshotScreen {}, | 170 | + ScreenshotScreen { |
| 171 | + /// Write the screenshot to disk in addition to putting it in your clipboard. | ||
| 172 | + /// | ||
| 173 | + /// The screenshot is saved according to the `screenshot-path` config setting. | ||
| 174 | + #[cfg_attr(feature = "clap", arg(short = 'd', long, action = clap::ArgAction::Set, default_value_t = true))] | ||
| 175 | + write_to_disk: bool, | ||
| 176 | + }, | ||
| 171 | /// Screenshot a window. | 177 | /// Screenshot a window. |
| 172 | #[cfg_attr(feature = "clap", clap(about = "Screenshot the focused window"))] | 178 | #[cfg_attr(feature = "clap", clap(about = "Screenshot the focused window"))] |
| 173 | ScreenshotWindow { | 179 | ScreenshotWindow { |
| @@ -176,6 +182,11 @@ pub enum Action { | |||
| 176 | /// If `None`, uses the focused window. | 182 | /// If `None`, uses the focused window. |
| 177 | #[cfg_attr(feature = "clap", arg(long))] | 183 | #[cfg_attr(feature = "clap", arg(long))] |
| 178 | id: Option<u64>, | 184 | id: Option<u64>, |
| 185 | + /// Write the screenshot to disk in addition to putting it in your clipboard. | ||
| 186 | + /// | ||
| 187 | + /// The screenshot is saved according to the `screenshot-path` config setting. | ||
| 188 | + #[cfg_attr(feature = "clap", arg(short = 'd', long, action = clap::ArgAction::Set, default_value_t = true))] | ||
| 189 | + write_to_disk: bool, | ||
| 179 | }, | 190 | }, |
| 180 | /// Close a window. | 191 | /// Close a window. |
| 181 | #[cfg_attr(feature = "clap", clap(about = "Close the focused window"))] | 192 | #[cfg_attr(feature = "clap", clap(about = "Close the focused window"))] |
src/input/mod.rsmodified+15 -8
| @@ -529,17 +529,17 @@ impl State { | |||
| 529 | self.niri.do_screen_transition(renderer, delay_ms); | 529 | self.niri.do_screen_transition(renderer, delay_ms); |
| 530 | }); | 530 | }); |
| 531 | } | 531 | } |
| 532 | - Action::ScreenshotScreen => { | 532 | + Action::ScreenshotScreen(write_to_disk) => { |
| 533 | let active = self.niri.layout.active_output().cloned(); | 533 | let active = self.niri.layout.active_output().cloned(); |
| 534 | if let Some(active) = active { | 534 | if let Some(active) = active { |
| 535 | self.backend.with_primary_renderer(|renderer| { | 535 | self.backend.with_primary_renderer(|renderer| { |
| 536 | - if let Err(err) = self.niri.screenshot(renderer, &active) { | 536 | + if let Err(err) = self.niri.screenshot(renderer, &active, write_to_disk) { |
| 537 | warn!("error taking screenshot: {err:?}"); | 537 | warn!("error taking screenshot: {err:?}"); |
| 538 | } | 538 | } |
| 539 | }); | 539 | }); |
| 540 | } | 540 | } |
| 541 | } | 541 | } |
| 542 | - Action::ConfirmScreenshot => { | 542 | + Action::ConfirmScreenshot { write_to_disk } => { |
| 543 | if !self.niri.screenshot_ui.is_open() { | 543 | if !self.niri.screenshot_ui.is_open() { |
| 544 | return; | 544 | return; |
| 545 | } | 545 | } |
| @@ -547,7 +547,8 @@ impl State { | |||
| 547 | self.backend.with_primary_renderer(|renderer| { | 547 | self.backend.with_primary_renderer(|renderer| { |
| 548 | match self.niri.screenshot_ui.capture(renderer) { | 548 | match self.niri.screenshot_ui.capture(renderer) { |
| 549 | Ok((size, pixels)) => { | 549 | Ok((size, pixels)) => { |
| 550 | - if let Err(err) = self.niri.save_screenshot(size, pixels) { | 550 | + if let Err(err) = self.niri.save_screenshot(size, pixels, write_to_disk) |
| 551 | + { | ||
| 551 | warn!("error saving screenshot: {err:?}"); | 552 | warn!("error saving screenshot: {err:?}"); |
| 552 | } | 553 | } |
| 553 | } | 554 | } |
| @@ -581,23 +582,29 @@ impl State { | |||
| 581 | Action::Screenshot => { | 582 | Action::Screenshot => { |
| 582 | self.open_screenshot_ui(); | 583 | self.open_screenshot_ui(); |
| 583 | } | 584 | } |
| 584 | - Action::ScreenshotWindow => { | 585 | + Action::ScreenshotWindow(write_to_disk) => { |
| 585 | let focus = self.niri.layout.focus_with_output(); | 586 | let focus = self.niri.layout.focus_with_output(); |
| 586 | if let Some((mapped, output)) = focus { | 587 | if let Some((mapped, output)) = focus { |
| 587 | self.backend.with_primary_renderer(|renderer| { | 588 | self.backend.with_primary_renderer(|renderer| { |
| 588 | - if let Err(err) = self.niri.screenshot_window(renderer, output, mapped) { | 589 | + if let Err(err) = |
| 590 | + self.niri | ||
| 591 | + .screenshot_window(renderer, output, mapped, write_to_disk) | ||
| 592 | + { | ||
| 589 | warn!("error taking screenshot: {err:?}"); | 593 | warn!("error taking screenshot: {err:?}"); |
| 590 | } | 594 | } |
| 591 | }); | 595 | }); |
| 592 | } | 596 | } |
| 593 | } | 597 | } |
| 594 | - Action::ScreenshotWindowById(id) => { | 598 | + Action::ScreenshotWindowById { id, write_to_disk } => { |
| 595 | let mut windows = self.niri.layout.windows(); | 599 | let mut windows = self.niri.layout.windows(); |
| 596 | let window = windows.find(|(_, m)| m.id().get() == id); | 600 | let window = windows.find(|(_, m)| m.id().get() == id); |
| 597 | if let Some((Some(monitor), mapped)) = window { | 601 | if let Some((Some(monitor), mapped)) = window { |
| 598 | let output = monitor.output(); | 602 | let output = monitor.output(); |
| 599 | self.backend.with_primary_renderer(|renderer| { | 603 | self.backend.with_primary_renderer(|renderer| { |
| 600 | - if let Err(err) = self.niri.screenshot_window(renderer, output, mapped) { | 604 | + if let Err(err) = |
| 605 | + self.niri | ||
| 606 | + .screenshot_window(renderer, output, mapped, write_to_disk) | ||
| 607 | + { | ||
| 601 | warn!("error taking screenshot: {err:?}"); | 608 | warn!("error taking screenshot: {err:?}"); |
| 602 | } | 609 | } |
| 603 | }); | 610 | }); |
src/niri.rsmodified+14 -9
| @@ -4519,6 +4519,7 @@ impl Niri { | |||
| 4519 | &mut self, | 4519 | &mut self, |
| 4520 | renderer: &mut GlesRenderer, | 4520 | renderer: &mut GlesRenderer, |
| 4521 | output: &Output, | 4521 | output: &Output, |
| 4522 | + write_to_disk: bool, | ||
| 4522 | ) -> anyhow::Result<()> { | 4523 | ) -> anyhow::Result<()> { |
| 4523 | let _span = tracy_client::span!("Niri::screenshot"); | 4524 | let _span = tracy_client::span!("Niri::screenshot"); |
| 4524 | 4525 | ||
| @@ -4541,7 +4542,7 @@ impl Niri { | |||
| 4541 | elements, | 4542 | elements, |
| 4542 | )?; | 4543 | )?; |
| 4543 | 4544 | ||
| 4544 | - self.save_screenshot(size, pixels) | 4545 | + self.save_screenshot(size, pixels, write_to_disk) |
| 4545 | .context("error saving screenshot") | 4546 | .context("error saving screenshot") |
| 4546 | } | 4547 | } |
| 4547 | 4548 | ||
| @@ -4550,6 +4551,7 @@ impl Niri { | |||
| 4550 | renderer: &mut GlesRenderer, | 4551 | renderer: &mut GlesRenderer, |
| 4551 | output: &Output, | 4552 | output: &Output, |
| 4552 | mapped: &Mapped, | 4553 | mapped: &Mapped, |
| 4554 | + write_to_disk: bool, | ||
| 4553 | ) -> anyhow::Result<()> { | 4555 | ) -> anyhow::Result<()> { |
| 4554 | let _span = tracy_client::span!("Niri::screenshot_window"); | 4556 | let _span = tracy_client::span!("Niri::screenshot_window"); |
| 4555 | 4557 | ||
| @@ -4585,7 +4587,7 @@ impl Niri { | |||
| 4585 | elements, | 4587 | elements, |
| 4586 | )?; | 4588 | )?; |
| 4587 | 4589 | ||
| 4588 | - self.save_screenshot(geo.size, pixels) | 4590 | + self.save_screenshot(geo.size, pixels, write_to_disk) |
| 4589 | .context("error saving screenshot") | 4591 | .context("error saving screenshot") |
| 4590 | } | 4592 | } |
| 4591 | 4593 | ||
| @@ -4593,14 +4595,17 @@ impl Niri { | |||
| 4593 | &self, | 4595 | &self, |
| 4594 | size: Size<i32, Physical>, | 4596 | size: Size<i32, Physical>, |
| 4595 | pixels: Vec<u8>, | 4597 | pixels: Vec<u8>, |
| 4598 | + write_to_disk: bool, | ||
| 4596 | ) -> anyhow::Result<()> { | 4599 | ) -> anyhow::Result<()> { |
| 4597 | - let path = match make_screenshot_path(&self.config.borrow()) { | 4600 | + let path = write_to_disk |
| 4598 | - Ok(path) => path, | 4601 | + .then(|| match make_screenshot_path(&self.config.borrow()) { |
| 4599 | - Err(err) => { | 4602 | + Ok(path) => path, |
| 4600 | - warn!("error making screenshot path: {err:?}"); | 4603 | + Err(err) => { |
| 4601 | - None | 4604 | + warn!("error making screenshot path: {err:?}"); |
| 4602 | - } | 4605 | + None |
| 4603 | - }; | 4606 | + } |
| 4607 | + }) | ||
| 4608 | + .flatten(); | ||
| 4604 | 4609 | ||
| 4605 | // Prepare to set the encoded image as our clipboard selection. This must be done from the | 4610 | // Prepare to set the encoded image as our clipboard selection. This must be done from the |
| 4606 | // main thread. | 4611 | // main thread. |
src/ui/screenshot_ui.rsmodified+9 -4
| @@ -629,10 +629,15 @@ fn action(raw: Keysym, mods: ModifiersState) -> Option<Action> { | |||
| 629 | return None; | 629 | return None; |
| 630 | } | 630 | } |
| 631 | 631 | ||
| 632 | - if (mods.ctrl && raw == Keysym::c) | 632 | + if !mods.ctrl && (raw == Keysym::space || raw == Keysym::Return) { |
| 633 | - || (!mods.ctrl && (raw == Keysym::space || raw == Keysym::Return)) | 633 | + return Some(Action::ConfirmScreenshot { |
| 634 | - { | 634 | + write_to_disk: true, |
| 635 | - return Some(Action::ConfirmScreenshot); | 635 | + }); |
| 636 | + } | ||
| 637 | + if mods.ctrl && raw == Keysym::c { | ||
| 638 | + return Some(Action::ConfirmScreenshot { | ||
| 639 | + write_to_disk: false, | ||
| 640 | + }); | ||
| 636 | } | 641 | } |
| 637 | 642 | ||
| 638 | if !mods.ctrl && raw == Keysym::p { | 643 | if !mods.ctrl && raw == Keysym::p { |