Skip to content

Commit

Permalink
Merge pull request #2449 from bungoboingo/fix/screenshot
Browse files Browse the repository at this point in the history
Add viewport scale to screenshot data
  • Loading branch information
hecrj committed May 25, 2024
2 parents 663a081 + 647761a commit 8d1e639
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions runtime/src/window/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,36 @@ use std::fmt::{Debug, Formatter};
pub struct Screenshot {
/// The bytes of the [`Screenshot`].
pub bytes: Bytes,
/// The size of the [`Screenshot`].
/// The size of the [`Screenshot`] in physical pixels.
pub size: Size<u32>,
/// The scale factor of the [`Screenshot`]. This can be useful when converting between widget
/// bounds (which are in logical pixels) to crop screenshots.
pub scale_factor: f64,
}

impl Debug for Screenshot {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Screenshot: {{ \n bytes: {}\n size: {:?} }}",
"Screenshot: {{ \n bytes: {}\n scale: {}\n size: {:?} }}",
self.bytes.len(),
self.scale_factor,
self.size
)
}
}

impl Screenshot {
/// Creates a new [`Screenshot`].
pub fn new(bytes: impl Into<Bytes>, size: Size<u32>) -> Self {
pub fn new(
bytes: impl Into<Bytes>,
size: Size<u32>,
scale_factor: f64,
) -> Self {
Self {
bytes: bytes.into(),
size,
scale_factor,
}
}

Expand Down Expand Up @@ -70,6 +79,7 @@ impl Screenshot {
Ok(Self {
bytes: Bytes::from(chopped),
size: Size::new(region.width, region.height),
scale_factor: self.scale_factor,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ pub fn run_command<A, C, E>(
proxy.send(tag(window::Screenshot::new(
bytes,
state.physical_size(),
state.viewport().scale_factor(),
)));
}
},
Expand Down
1 change: 1 addition & 0 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ fn run_command<A, C, E>(
proxy.send(tag(window::Screenshot::new(
bytes,
window.state.physical_size(),
window.state.viewport().scale_factor(),
)));
}
}
Expand Down

0 comments on commit 8d1e639

Please sign in to comment.