Wrap Screenshot::bytes in an Arc and implement AsRef<[u8]>
This commit is contained in:
parent
7adfaa88a6
commit
5324928044
2 changed files with 16 additions and 5 deletions
|
|
@ -133,7 +133,7 @@ impl Application for Example {
|
||||||
image(image::Handle::from_pixels(
|
image(image::Handle::from_pixels(
|
||||||
screenshot.size.width,
|
screenshot.size.width,
|
||||||
screenshot.size.height,
|
screenshot.size.height,
|
||||||
screenshot.bytes.clone(),
|
screenshot.clone(),
|
||||||
))
|
))
|
||||||
.content_fit(ContentFit::ScaleDown)
|
.content_fit(ContentFit::ScaleDown)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use iced_core::{Rectangle, Size};
|
use crate::core::{Rectangle, Size};
|
||||||
|
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// Data of a screenshot, captured with `window::screenshot()`.
|
/// Data of a screenshot, captured with `window::screenshot()`.
|
||||||
///
|
///
|
||||||
|
|
@ -7,7 +9,7 @@ use std::fmt::{Debug, Formatter};
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Screenshot {
|
pub struct Screenshot {
|
||||||
/// The bytes of the [`Screenshot`].
|
/// The bytes of the [`Screenshot`].
|
||||||
pub bytes: Vec<u8>,
|
pub bytes: Arc<Vec<u8>>,
|
||||||
/// The size of the [`Screenshot`].
|
/// The size of the [`Screenshot`].
|
||||||
pub size: Size<u32>,
|
pub size: Size<u32>,
|
||||||
}
|
}
|
||||||
|
|
@ -26,7 +28,10 @@ impl Debug for Screenshot {
|
||||||
impl Screenshot {
|
impl Screenshot {
|
||||||
/// Creates a new [`Screenshot`].
|
/// Creates a new [`Screenshot`].
|
||||||
pub fn new(bytes: Vec<u8>, size: Size<u32>) -> Self {
|
pub fn new(bytes: Vec<u8>, size: Size<u32>) -> Self {
|
||||||
Self { bytes, size }
|
Self {
|
||||||
|
bytes: Arc::new(bytes),
|
||||||
|
size,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Crops a [`Screenshot`] to the provided `region`. This will always be relative to the
|
/// Crops a [`Screenshot`] to the provided `region`. This will always be relative to the
|
||||||
|
|
@ -62,12 +67,18 @@ impl Screenshot {
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
bytes: chopped,
|
bytes: Arc::new(chopped),
|
||||||
size: Size::new(region.width, region.height),
|
size: Size::new(region.width, region.height),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<[u8]> for Screenshot {
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
&self.bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
/// Errors that can occur when cropping a [`Screenshot`].
|
/// Errors that can occur when cropping a [`Screenshot`].
|
||||||
pub enum CropError {
|
pub enum CropError {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue