Added scale_factor to Screenshot data for use when cropping to widget bounds

This commit is contained in:
Shan 2024-05-24 19:46:18 -07:00
parent 663a081bdd
commit 647761ad56
No known key found for this signature in database
GPG key ID: 5F84D2AA40A9F170
3 changed files with 15 additions and 3 deletions

View file

@ -11,16 +11,20 @@ use std::fmt::{Debug, Formatter};
pub struct Screenshot { pub struct Screenshot {
/// The bytes of the [`Screenshot`]. /// The bytes of the [`Screenshot`].
pub bytes: Bytes, pub bytes: Bytes,
/// The size of the [`Screenshot`]. /// The size of the [`Screenshot`] in physical pixels.
pub size: Size<u32>, 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 { impl Debug for Screenshot {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
"Screenshot: {{ \n bytes: {}\n size: {:?} }}", "Screenshot: {{ \n bytes: {}\n scale: {}\n size: {:?} }}",
self.bytes.len(), self.bytes.len(),
self.scale_factor,
self.size self.size
) )
} }
@ -28,10 +32,15 @@ impl Debug for Screenshot {
impl Screenshot { impl Screenshot {
/// Creates a new [`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 { Self {
bytes: bytes.into(), bytes: bytes.into(),
size, size,
scale_factor,
} }
} }
@ -70,6 +79,7 @@ impl Screenshot {
Ok(Self { Ok(Self {
bytes: Bytes::from(chopped), bytes: Bytes::from(chopped),
size: Size::new(region.width, region.height), size: Size::new(region.width, region.height),
scale_factor: self.scale_factor,
}) })
} }
} }

View file

@ -1066,6 +1066,7 @@ pub fn run_command<A, C, E>(
proxy.send(tag(window::Screenshot::new( proxy.send(tag(window::Screenshot::new(
bytes, bytes,
state.physical_size(), state.physical_size(),
state.viewport().scale_factor(),
))); )));
} }
}, },

View file

@ -1239,6 +1239,7 @@ fn run_command<A, C, E>(
proxy.send(tag(window::Screenshot::new( proxy.send(tag(window::Screenshot::new(
bytes, bytes,
window.state.physical_size(), window.state.physical_size(),
window.state.viewport().scale_factor(),
))); )));
} }
} }