Implement From<&Handle> for image::Handle

This commit is contained in:
Héctor Ramón Jiménez 2024-08-03 16:20:12 +02:00
parent 169667ef1b
commit e84070acef
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 36 additions and 20 deletions

View file

@ -101,6 +101,12 @@ where
} }
} }
impl From<&Handle> for Handle {
fn from(value: &Handle) -> Self {
value.clone()
}
}
impl std::fmt::Debug for Handle { impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {

View file

@ -20,7 +20,7 @@ fn main() -> iced::Result {
#[derive(Default)] #[derive(Default)]
struct Example { struct Example {
screenshot: Option<Screenshot>, screenshot: Option<(Screenshot, image::Handle)>,
saved_png_path: Option<Result<String, PngError>>, saved_png_path: Option<Result<String, PngError>>,
png_saving: bool, png_saving: bool,
crop_error: Option<screenshot::CropError>, crop_error: Option<screenshot::CropError>,
@ -52,10 +52,17 @@ impl Example {
.map(Message::Screenshotted); .map(Message::Screenshotted);
} }
Message::Screenshotted(screenshot) => { Message::Screenshotted(screenshot) => {
self.screenshot = Some(screenshot); self.screenshot = Some((
screenshot.clone(),
image::Handle::from_rgba(
screenshot.size.width,
screenshot.size.height,
screenshot.bytes,
),
));
} }
Message::Png => { Message::Png => {
if let Some(screenshot) = &self.screenshot { if let Some((screenshot, _handle)) = &self.screenshot {
self.png_saving = true; self.png_saving = true;
return Task::perform( return Task::perform(
@ -81,7 +88,7 @@ impl Example {
self.height_input_value = new_value; self.height_input_value = new_value;
} }
Message::Crop => { Message::Crop => {
if let Some(screenshot) = &self.screenshot { if let Some((screenshot, _handle)) = &self.screenshot {
let cropped = screenshot.crop(Rectangle::<u32> { let cropped = screenshot.crop(Rectangle::<u32> {
x: self.x_input_value.unwrap_or(0), x: self.x_input_value.unwrap_or(0),
y: self.y_input_value.unwrap_or(0), y: self.y_input_value.unwrap_or(0),
@ -91,7 +98,14 @@ impl Example {
match cropped { match cropped {
Ok(screenshot) => { Ok(screenshot) => {
self.screenshot = Some(screenshot); self.screenshot = Some((
screenshot.clone(),
image::Handle::from_rgba(
screenshot.size.width,
screenshot.size.height,
screenshot.bytes,
),
));
self.crop_error = None; self.crop_error = None;
} }
Err(crop_error) => { Err(crop_error) => {
@ -106,20 +120,16 @@ impl Example {
} }
fn view(&self) -> Element<'_, Message> { fn view(&self) -> Element<'_, Message> {
let image: Element<Message> = if let Some(screenshot) = &self.screenshot let image: Element<Message> =
{ if let Some((_screenshot, handle)) = &self.screenshot {
image(image::Handle::from_rgba( image(handle)
screenshot.size.width, .content_fit(ContentFit::Contain)
screenshot.size.height, .width(Fill)
screenshot.clone(), .height(Fill)
)) .into()
.content_fit(ContentFit::Contain) } else {
.width(Fill) text("Press the button to take a screenshot!").into()
.height(Fill) };
.into()
} else {
text("Press the button to take a screenshot!").into()
};
let image = container(image) let image = container(image)
.center_y(FillPortion(2)) .center_y(FillPortion(2))

View file

@ -43,7 +43,7 @@ pub struct Image<Handle> {
impl<Handle> Image<Handle> { impl<Handle> Image<Handle> {
/// Creates a new [`Image`] with the given path. /// Creates a new [`Image`] with the given path.
pub fn new<T: Into<Handle>>(handle: T) -> Self { pub fn new(handle: impl Into<Handle>) -> Self {
Image { Image {
handle: handle.into(), handle: handle.into(),
width: Length::Shrink, width: Length::Shrink,