Add screenshot helpers to iced_test
This commit is contained in:
parent
ea614387f4
commit
085c8fae8d
10 changed files with 183 additions and 78 deletions
|
|
@ -1,7 +1,6 @@
|
|||
//! Take screenshots of a window.
|
||||
use crate::{Rectangle, Size};
|
||||
use crate::{Bytes, Rectangle, Size};
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
/// Data of a screenshot, captured with `window::screenshot()`.
|
||||
|
|
@ -9,8 +8,8 @@ use std::fmt::{Debug, Formatter};
|
|||
/// The `bytes` of this screenshot will always be ordered as `RGBA` in the `sRGB` color space.
|
||||
#[derive(Clone)]
|
||||
pub struct Screenshot {
|
||||
/// The bytes of the [`Screenshot`].
|
||||
pub bytes: Bytes,
|
||||
/// The RGBA bytes of the [`Screenshot`].
|
||||
pub rgba: Bytes,
|
||||
/// 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
|
||||
|
|
@ -23,7 +22,7 @@ impl Debug for Screenshot {
|
|||
write!(
|
||||
f,
|
||||
"Screenshot: {{ \n bytes: {}\n scale: {}\n size: {:?} }}",
|
||||
self.bytes.len(),
|
||||
self.rgba.len(),
|
||||
self.scale_factor,
|
||||
self.size
|
||||
)
|
||||
|
|
@ -33,12 +32,12 @@ impl Debug for Screenshot {
|
|||
impl Screenshot {
|
||||
/// Creates a new [`Screenshot`].
|
||||
pub fn new(
|
||||
bytes: impl Into<Bytes>,
|
||||
rgba: impl Into<Bytes>,
|
||||
size: Size<u32>,
|
||||
scale_factor: f32,
|
||||
) -> Self {
|
||||
Self {
|
||||
bytes: bytes.into(),
|
||||
rgba: rgba.into(),
|
||||
size,
|
||||
scale_factor,
|
||||
}
|
||||
|
|
@ -65,7 +64,7 @@ impl Screenshot {
|
|||
let column_range = region.x as usize * PIXEL_SIZE
|
||||
..(region.x + region.width) as usize * PIXEL_SIZE;
|
||||
|
||||
let chopped = self.bytes.chunks(bytes_per_row).enumerate().fold(
|
||||
let chopped = self.rgba.chunks(bytes_per_row).enumerate().fold(
|
||||
vec![],
|
||||
|mut acc, (row, bytes)| {
|
||||
if row_range.contains(&row) {
|
||||
|
|
@ -77,7 +76,7 @@ impl Screenshot {
|
|||
);
|
||||
|
||||
Ok(Self {
|
||||
bytes: Bytes::from(chopped),
|
||||
rgba: Bytes::from(chopped),
|
||||
size: Size::new(region.width, region.height),
|
||||
scale_factor: self.scale_factor,
|
||||
})
|
||||
|
|
@ -86,13 +85,13 @@ impl Screenshot {
|
|||
|
||||
impl AsRef<[u8]> for Screenshot {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.bytes
|
||||
&self.rgba
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Screenshot> for Bytes {
|
||||
fn from(screenshot: Screenshot) -> Self {
|
||||
screenshot.bytes
|
||||
screenshot.rgba
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue