Add screenshot helpers to iced_test

This commit is contained in:
Héctor Ramón Jiménez 2025-11-12 00:53:10 +01:00
parent ea614387f4
commit 085c8fae8d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
10 changed files with 183 additions and 78 deletions

View file

@ -1,8 +1,6 @@
//! Load and draw raster graphics.
pub use bytes::Bytes;
use crate::border;
use crate::{Radians, Rectangle, Size};
use crate::{Bytes, Radians, Rectangle, Size};
use rustc_hash::FxHasher;

View file

@ -83,6 +83,7 @@ pub use transformation::Transformation;
pub use vector::Vector;
pub use widget::Widget;
pub use bytes::Bytes;
pub use smol_str::SmolStr;
/// A function that can _never_ be called.

View file

@ -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
}
}