Skip to content

Commit

Permalink
Utilize bytes::Bytes iced_runtime::window::Screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajix committed Apr 8, 2024
1 parent c270e9e commit e8166dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ debug = []
multi-window = []

[dependencies]
bytes.workspace = true
iced_core.workspace = true
iced_futures.workspace = true
iced_futures.features = ["thread-pool"]
Expand Down
16 changes: 11 additions & 5 deletions runtime/src/window/screenshot.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! Take screenshots of a window.
use crate::core::{Rectangle, Size};

use bytes::Bytes;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;

/// Data of a screenshot, captured with `window::screenshot()`.
///
/// 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: Arc<Vec<u8>>,
pub bytes: Bytes,
/// The size of the [`Screenshot`].
pub size: Size<u32>,
}
Expand All @@ -28,9 +28,9 @@ impl Debug for Screenshot {

impl Screenshot {
/// Creates a new [`Screenshot`].
pub fn new(bytes: Vec<u8>, size: Size<u32>) -> Self {
pub fn new(bytes: impl Into<Bytes>, size: Size<u32>) -> Self {
Self {
bytes: Arc::new(bytes),
bytes: bytes.into(),
size,
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Screenshot {
);

Ok(Self {
bytes: Arc::new(chopped),
bytes: Bytes::from(chopped),
size: Size::new(region.width, region.height),
})
}
Expand All @@ -80,6 +80,12 @@ impl AsRef<[u8]> for Screenshot {
}
}

impl Into<Bytes> for Screenshot {
fn into(self) -> Bytes {
self.bytes
}
}

#[derive(Debug, thiserror::Error)]
/// Errors that can occur when cropping a [`Screenshot`].
pub enum CropError {
Expand Down

0 comments on commit e8166dd

Please sign in to comment.