Add screenshot-window show-pointer=true
Browse files at this commit9f8eadc5bc04994f395832e7135693f87aa899a9Parent: a4963075 changed files +99 -7
- docs/wiki/Configuration:-Key-Bindings.md +11 -0
- niri-config/src/binds.rs +6 -1
- niri-ipc/src/lib.rs +7 -0
- src/input/mod.rs +4 -1
- src/niri.rs +71 -5
docs/wiki/Configuration:-Key-Bindings.mdmodified+11 -0
| @@ -382,6 +382,17 @@ binds { | |||
| 382 | } | 382 | } |
| 383 | ``` | 383 | ``` |
| 384 | 384 | ||
| 385 | +<sup>Since: next release</sup> You can show the mouse pointer on window screenshots with the `show-pointer=true` property. | ||
| 386 | +The pointer will be included only if the window is currently receiving pointer input (usually this means the pointer is on top of the window). | ||
| 387 | + | ||
| 388 | +```kdl | ||
| 389 | +binds { | ||
| 390 | + // The pointer will be visible on the screenshot | ||
| 391 | + // if it's on top of the window. | ||
| 392 | + Alt+Print { screenshot-window show-pointer=true; } | ||
| 393 | +} | ||
| 394 | +``` | ||
| 395 | + | ||
| 385 | #### `toggle-keyboard-shortcuts-inhibit` | 396 | #### `toggle-keyboard-shortcuts-inhibit` |
| 386 | 397 | ||
| 387 | <sup>Since: 25.02</sup> | 398 | <sup>Since: 25.02</sup> |
niri-config/src/binds.rsmodified+6 -1
| @@ -132,6 +132,7 @@ pub enum Action { | |||
| 132 | ), | 132 | ), |
| 133 | ScreenshotWindow( | 133 | ScreenshotWindow( |
| 134 | #[knuffel(property(name = "write-to-disk"), default = true)] bool, | 134 | #[knuffel(property(name = "write-to-disk"), default = true)] bool, |
| 135 | + #[knuffel(property(name = "show-pointer"), default = false)] bool, | ||
| 135 | // Path; not settable from knuffel | 136 | // Path; not settable from knuffel |
| 136 | Option<String>, | 137 | Option<String>, |
| 137 | ), | 138 | ), |
| @@ -139,6 +140,7 @@ pub enum Action { | |||
| 139 | ScreenshotWindowById { | 140 | ScreenshotWindowById { |
| 140 | id: u64, | 141 | id: u64, |
| 141 | write_to_disk: bool, | 142 | write_to_disk: bool, |
| 143 | + show_pointer: bool, | ||
| 142 | path: Option<String>, | 144 | path: Option<String>, |
| 143 | }, | 145 | }, |
| 144 | ToggleKeyboardShortcutsInhibit, | 146 | ToggleKeyboardShortcutsInhibit, |
| @@ -407,15 +409,18 @@ impl From<niri_ipc::Action> for Action { | |||
| 407 | niri_ipc::Action::ScreenshotWindow { | 409 | niri_ipc::Action::ScreenshotWindow { |
| 408 | id: None, | 410 | id: None, |
| 409 | write_to_disk, | 411 | write_to_disk, |
| 412 | + show_pointer, | ||
| 410 | path, | 413 | path, |
| 411 | - } => Self::ScreenshotWindow(write_to_disk, path), | 414 | + } => Self::ScreenshotWindow(write_to_disk, show_pointer, path), |
| 412 | niri_ipc::Action::ScreenshotWindow { | 415 | niri_ipc::Action::ScreenshotWindow { |
| 413 | id: Some(id), | 416 | id: Some(id), |
| 414 | write_to_disk, | 417 | write_to_disk, |
| 418 | + show_pointer, | ||
| 415 | path, | 419 | path, |
| 416 | } => Self::ScreenshotWindowById { | 420 | } => Self::ScreenshotWindowById { |
| 417 | id, | 421 | id, |
| 418 | write_to_disk, | 422 | write_to_disk, |
| 423 | + show_pointer, | ||
| 419 | path, | 424 | path, |
| 420 | }, | 425 | }, |
| 421 | niri_ipc::Action::ToggleKeyboardShortcutsInhibit {} => { | 426 | niri_ipc::Action::ToggleKeyboardShortcutsInhibit {} => { |
niri-ipc/src/lib.rsmodified+7 -0
| @@ -264,6 +264,13 @@ pub enum Action { | |||
| 264 | #[cfg_attr(feature = "clap", arg(short = 'd', long, action = clap::ArgAction::Set, default_value_t = true))] | 264 | #[cfg_attr(feature = "clap", arg(short = 'd', long, action = clap::ArgAction::Set, default_value_t = true))] |
| 265 | write_to_disk: bool, | 265 | write_to_disk: bool, |
| 266 | 266 | ||
| 267 | + /// Whether to include the mouse pointer in the screenshot. | ||
| 268 | + /// | ||
| 269 | + /// The pointer will be included only if the window is currently receiving pointer input | ||
| 270 | + /// (usually this means the pointer is on top of the window). | ||
| 271 | + #[cfg_attr(feature = "clap", arg(short = 'p', long, action = clap::ArgAction::Set, default_value_t = false))] | ||
| 272 | + show_pointer: bool, | ||
| 273 | + | ||
| 267 | /// Path to save the screenshot to. | 274 | /// Path to save the screenshot to. |
| 268 | /// | 275 | /// |
| 269 | /// The path must be absolute, otherwise an error is returned. | 276 | /// The path must be absolute, otherwise an error is returned. |
src/input/mod.rsmodified+4 -1
| @@ -743,7 +743,7 @@ impl State { | |||
| 743 | self.open_screenshot_ui(show_cursor, path); | 743 | self.open_screenshot_ui(show_cursor, path); |
| 744 | self.niri.cancel_mru(); | 744 | self.niri.cancel_mru(); |
| 745 | } | 745 | } |
| 746 | - Action::ScreenshotWindow(write_to_disk, path) => { | 746 | + Action::ScreenshotWindow(write_to_disk, show_pointer, path) => { |
| 747 | let focus = self.niri.layout.focus_with_output(); | 747 | let focus = self.niri.layout.focus_with_output(); |
| 748 | if let Some((mapped, output)) = focus { | 748 | if let Some((mapped, output)) = focus { |
| 749 | self.backend.with_primary_renderer(|renderer| { | 749 | self.backend.with_primary_renderer(|renderer| { |
| @@ -752,6 +752,7 @@ impl State { | |||
| 752 | output, | 752 | output, |
| 753 | mapped, | 753 | mapped, |
| 754 | write_to_disk, | 754 | write_to_disk, |
| 755 | + show_pointer, | ||
| 755 | path, | 756 | path, |
| 756 | ) { | 757 | ) { |
| 757 | warn!("error taking screenshot: {err:?}"); | 758 | warn!("error taking screenshot: {err:?}"); |
| @@ -762,6 +763,7 @@ impl State { | |||
| 762 | Action::ScreenshotWindowById { | 763 | Action::ScreenshotWindowById { |
| 763 | id, | 764 | id, |
| 764 | write_to_disk, | 765 | write_to_disk, |
| 766 | + show_pointer, | ||
| 765 | path, | 767 | path, |
| 766 | } => { | 768 | } => { |
| 767 | let mut windows = self.niri.layout.windows(); | 769 | let mut windows = self.niri.layout.windows(); |
| @@ -774,6 +776,7 @@ impl State { | |||
| 774 | output, | 776 | output, |
| 775 | mapped, | 777 | mapped, |
| 776 | write_to_disk, | 778 | write_to_disk, |
| 779 | + show_pointer, | ||
| 777 | path, | 780 | path, |
| 778 | ) { | 781 | ) { |
| 779 | warn!("error taking screenshot: {err:?}"); | 782 | warn!("error taking screenshot: {err:?}"); |
src/niri.rsmodified+71 -5
| @@ -139,7 +139,9 @@ use crate::layer::mapped::LayerSurfaceRenderElement; | |||
| 139 | use crate::layer::MappedLayer; | 139 | use crate::layer::MappedLayer; |
| 140 | use crate::layout::tile::TileRenderElement; | 140 | use crate::layout::tile::TileRenderElement; |
| 141 | use crate::layout::workspace::{Workspace, WorkspaceId}; | 141 | use crate::layout::workspace::{Workspace, WorkspaceId}; |
| 142 | -use crate::layout::{HitType, Layout, LayoutElement as _, MonitorRenderElement}; | 142 | +use crate::layout::{ |
| 143 | + HitType, Layout, LayoutElement as _, LayoutElementRenderElement, MonitorRenderElement, | ||
| 144 | +}; | ||
| 143 | use crate::niri_render_elements; | 145 | use crate::niri_render_elements; |
| 144 | use crate::protocols::ext_workspace::{self, ExtWorkspaceManagerState}; | 146 | use crate::protocols::ext_workspace::{self, ExtWorkspaceManagerState}; |
| 145 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; | 147 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; |
| @@ -5665,6 +5667,7 @@ impl Niri { | |||
| 5665 | output: &Output, | 5667 | output: &Output, |
| 5666 | mapped: &Mapped, | 5668 | mapped: &Mapped, |
| 5667 | write_to_disk: bool, | 5669 | write_to_disk: bool, |
| 5670 | + show_pointer: bool, | ||
| 5668 | path: Option<String>, | 5671 | path: Option<String>, |
| 5669 | ) -> anyhow::Result<()> { | 5672 | ) -> anyhow::Result<()> { |
| 5670 | let _span = tracy_client::span!("Niri::screenshot_window"); | 5673 | let _span = tracy_client::span!("Niri::screenshot_window"); |
| @@ -5676,17 +5679,73 @@ impl Niri { | |||
| 5676 | } else { | 5679 | } else { |
| 5677 | mapped.rules().opacity.unwrap_or(1.).clamp(0., 1.) | 5680 | mapped.rules().opacity.unwrap_or(1.).clamp(0., 1.) |
| 5678 | }; | 5681 | }; |
| 5679 | - // FIXME: pointer. | 5682 | + |
| 5680 | - let mut elements = Vec::new(); | 5683 | + let mut elements: Vec<WindowScreenshotRenderElement<GlesRenderer>> = Vec::new(); |
| 5684 | + | ||
| 5685 | + // Add pointer if requested and it's over this window. | ||
| 5686 | + if show_pointer { | ||
| 5687 | + if let Some((w, HitType::Input { win_pos })) = &self.pointer_contents.window { | ||
| 5688 | + if w == &mapped.window { | ||
| 5689 | + // Grabs can modify the pointer focus, making it different from | ||
| 5690 | + // pointer_contents. Notably, gestures like Mod+MMB will remove the pointer | ||
| 5691 | + // focus, and ClickGrab will keep pointer focus on the clicked window even | ||
| 5692 | + // while it's moving over a different window. | ||
| 5693 | + // | ||
| 5694 | + // So, double-check that current_focus() (after grabs) also matches the pointer | ||
| 5695 | + // contents. | ||
| 5696 | + let pointer = self.seat.get_pointer().unwrap(); | ||
| 5697 | + | ||
| 5698 | + // The DnD grab is a bit special because it has its own focus (data device) | ||
| 5699 | + // while the pointer focus is cleared. That focus is not currently exposed from | ||
| 5700 | + // Smithay, and showing DnD icons on window screenshots seems useful, so let's | ||
| 5701 | + // just allow it during DnD grabs. | ||
| 5702 | + let is_dnd_grab = pointer | ||
| 5703 | + .with_grab(|_, grab| State::is_dnd_grab(grab.as_any())) | ||
| 5704 | + .unwrap_or(false); | ||
| 5705 | + | ||
| 5706 | + let current_focus_matches = is_dnd_grab | ||
| 5707 | + || pointer | ||
| 5708 | + .current_focus() | ||
| 5709 | + .map(|focused| self.find_root_shell_surface(&focused)) | ||
| 5710 | + .is_some_and(|focused| mapped.is_wl_surface(&focused)); | ||
| 5711 | + if current_focus_matches { | ||
| 5712 | + // win_pos is the window buffer position in output-local logical coords. | ||
| 5713 | + let win_pos = win_pos.to_physical_precise_round(scale); | ||
| 5714 | + | ||
| 5715 | + // We don't check for pointer visibility because it can only be Visible or | ||
| 5716 | + // Hidden, and never Disabled (then it wouldn't have focus). Even when the | ||
| 5717 | + // pointer is Hidden, we want to render it, since the user explicitly | ||
| 5718 | + // requested show_pointer = true, and otherwise there's no easy way to | ||
| 5719 | + // screenshot a window with pointer with hide-when-typing because pressing | ||
| 5720 | + // the screenshot bind will hide the pointer. | ||
| 5721 | + self.render_pointer(renderer, output, &mut |elem| { | ||
| 5722 | + // Pointer elements are at output-local physical coords. | ||
| 5723 | + // Relocate by -win_pos to make them window-relative. | ||
| 5724 | + let elem = RelocateRenderElement::from_element( | ||
| 5725 | + elem, | ||
| 5726 | + win_pos.upscale(-1), | ||
| 5727 | + Relocate::Relative, | ||
| 5728 | + ); | ||
| 5729 | + elements.push(elem.into()); | ||
| 5730 | + }); | ||
| 5731 | + } | ||
| 5732 | + } | ||
| 5733 | + } | ||
| 5734 | + } | ||
| 5735 | + let pointer_count = elements.len(); | ||
| 5736 | + | ||
| 5681 | mapped.render( | 5737 | mapped.render( |
| 5682 | renderer, | 5738 | renderer, |
| 5683 | mapped.window.geometry().loc.to_f64(), | 5739 | mapped.window.geometry().loc.to_f64(), |
| 5684 | scale, | 5740 | scale, |
| 5685 | alpha, | 5741 | alpha, |
| 5686 | RenderTarget::ScreenCapture, | 5742 | RenderTarget::ScreenCapture, |
| 5687 | - &mut |elem| elements.push(elem), | 5743 | + &mut |elem| elements.push(elem.into()), |
| 5688 | ); | 5744 | ); |
| 5689 | - let geo = encompassing_geo(scale, elements.iter()); | 5745 | + |
| 5746 | + // The pointer is not included in encompassing_geo because we don't want it to expand the | ||
| 5747 | + // screenshot size. | ||
| 5748 | + let geo = encompassing_geo(scale, elements.iter().skip(pointer_count)); | ||
| 5690 | let elements = elements.iter().rev().map(|elem| { | 5749 | let elements = elements.iter().rev().map(|elem| { |
| 5691 | RelocateRenderElement::from_element(elem, geo.loc.upscale(-1), Relocate::Relative) | 5750 | RelocateRenderElement::from_element(elem, geo.loc.upscale(-1), Relocate::Relative) |
| 5692 | }); | 5751 | }); |
| @@ -6557,6 +6616,13 @@ niri_render_elements! { | |||
| 6557 | } | 6616 | } |
| 6558 | } | 6617 | } |
| 6559 | 6618 | ||
| 6619 | +niri_render_elements! { | ||
| 6620 | + WindowScreenshotRenderElement<R> => { | ||
| 6621 | + Layout = LayoutElementRenderElement<R>, | ||
| 6622 | + Pointer = RelocateRenderElement<PointerRenderElements<R>>, | ||
| 6623 | + } | ||
| 6624 | +} | ||
| 6625 | + | ||
| 6560 | niri_render_elements! { | 6626 | niri_render_elements! { |
| 6561 | OutputRenderElements<R> => { | 6627 | OutputRenderElements<R> => { |
| 6562 | Monitor = MonitorRenderElement<R>, | 6628 | Monitor = MonitorRenderElement<R>, |