Skip to content

Commit

Permalink
Add screenshot-window action
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Oct 10, 2023
1 parent cb73bcf commit c8105ae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions resources/default-config.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ binds {
Mod+Equal { set-column-width "+10%"; }

Print { screenshot; }
Alt+Print { screenshot-window; }

Mod+Shift+E { quit; }
Mod+Shift+P { power-off-monitors; }

Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ pub enum Action {
ToggleDebugTint,
Spawn(#[knuffel(arguments)] Vec<String>),
Screenshot,
ScreenshotWindow,
CloseWindow,
FullscreenWindow,
FocusColumnLeft,
Expand Down
12 changes: 12 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ impl State {
}
}
}
Action::ScreenshotWindow => {
let active = self.niri.layout.active_window();
if let Some((window, output)) = active {
if let Some(renderer) = self.backend.renderer() {
if let Err(err) =
self.niri.screenshot_window(renderer, &output, &window)
{
warn!("error taking screenshot: {err:?}");
}
}
}
}
Action::CloseWindow => {
if let Some(window) = self.niri.layout.focus() {
window.toplevel().send_close();
Expand Down
24 changes: 24 additions & 0 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,30 @@ impl<W: LayoutElement> Layout<W> {
Some(&mon.workspaces[mon.active_workspace_idx])
}

pub fn active_window(&self) -> Option<(W, Output)> {
let MonitorSet::Normal {
monitors,
active_monitor_idx,
..
} = &self.monitor_set
else {
return None;
};

let mon = &monitors[*active_monitor_idx];
let ws = &mon.workspaces[mon.active_workspace_idx];

if ws.columns.is_empty() {
return None;
}

let col = &ws.columns[ws.active_column_idx];
Some((
col.windows[col.active_window_idx].clone(),
mon.output.clone(),
))
}

pub fn workspace_for_output(&self, output: &Output) -> Option<&Workspace<W>> {
let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
return None;
Expand Down
34 changes: 34 additions & 0 deletions src/niri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,40 @@ impl Niri {
let elements = self.render(renderer, output, true);
let pixels = render_to_vec(renderer, size, scale, &elements)?;

self.save_screenshot(size, pixels)
.context("error saving screenshot")
}

pub fn screenshot_window(
&mut self,
renderer: &mut GlesRenderer,
output: &Output,
window: &Window,
) -> anyhow::Result<()> {
let _span = tracy_client::span!("Niri::screenshot_window");

let scale = Scale::from(output.current_scale().fractional_scale());
let bbox = window.bbox_with_popups();
let size = bbox.size.to_physical_precise_ceil(scale);
let buf_pos = Point::from((0, 0)) - bbox.loc;
// FIXME: pointer.
let elements = window.render_elements::<WaylandSurfaceRenderElement<GlesRenderer>>(
renderer,
buf_pos.to_physical_precise_ceil(scale),
scale,
1.,
);
let pixels = render_to_vec(renderer, size, scale, &elements)?;

self.save_screenshot(size, pixels)
.context("error saving screenshot")
}

fn save_screenshot(
&mut self,
size: Size<i32, Physical>,
pixels: Vec<u8>,
) -> anyhow::Result<()> {
let path = make_screenshot_path().context("error making screenshot path")?;
debug!("saving screenshot to {path:?}");

Expand Down

0 comments on commit c8105ae

Please sign in to comment.