From f4beccf1d559c82eba274ba19bc8a005fbb064f1 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Mon, 30 Sep 2024 16:27:13 +0200 Subject: [PATCH 1/2] Improve image API. --- src/views/img.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/views/img.rs b/src/views/img.rs index aa5b81aa..f305547c 100644 --- a/src/views/img.rs +++ b/src/views/img.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::rc::Rc; use floem_reactive::create_effect; @@ -90,9 +91,21 @@ pub struct Img { } pub fn img(image: impl Fn() -> Vec + 'static) -> Img { + img_from_bytes(image) +} + +pub fn img_from_bytes(image: impl Fn() -> Vec + 'static) -> Img { img_dynamic(move || image::load_from_memory(&image()).ok().map(Rc::new)) } +pub fn img_from_image(image: impl Fn() -> DynamicImage + 'static) -> Img { + img_dynamic(move || Some(Rc::new(image()))) +} + +pub fn img_from_path(image: impl Fn() -> PathBuf + 'static) -> Img { + img_dynamic(move || image::open(&image()).ok().map(Rc::new)) +} + pub(crate) fn img_dynamic(image: impl Fn() -> Option> + 'static) -> Img { let id = ViewId::new(); create_effect(move |_| { From cc9335895f4fed9a291e6ef6fd24543296bbb9c1 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Mon, 30 Sep 2024 16:27:46 +0200 Subject: [PATCH 2/2] Deprecate the old `img` method, use `img_from_*` instead. --- src/views/img.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/views/img.rs b/src/views/img.rs index f305547c..da595bce 100644 --- a/src/views/img.rs +++ b/src/views/img.rs @@ -90,6 +90,7 @@ pub struct Img { content_node: Option, } +#[deprecated] pub fn img(image: impl Fn() -> Vec + 'static) -> Img { img_from_bytes(image) }