zng::image

Struct IMAGES

source
pub struct IMAGES;
Expand description

Image loading, cache and render service.

If the app is running without a VIEW_PROCESS all images are dummy, see load_in_headless for details.

Implementations§

source§

impl IMAGES

source

pub fn render<N, R>( &self, mask: Option<ImageMaskMode>, render: N, ) -> ReadOnlyVar<Img, ArcVar<Img>>
where N: FnOnce() -> R + Send + Sync + 'static, R: ImageRenderWindowRoot,

Render the window generated by render to an image.

The window is created as a headless surface and rendered to the returned image. You can use the IMAGE_RENDER.retain var create an image that updates with new frames, or by default only render once.

The closure runs in the WINDOW context of the headless window.

source

pub fn render_node<U, N>( &self, render_mode: RenderMode, scale_factor: impl Into<Factor>, mask: Option<ImageMaskMode>, render: N, ) -> ReadOnlyVar<Img, ArcVar<Img>>
where U: UiNode, N: FnOnce() -> U + Send + Sync + 'static,

Render an UiNode to an image.

This method is a shortcut to render a node without needing to declare the headless window, note that a headless window is still used, the node does not have the same context as the calling widget.

source§

impl IMAGES

source

pub fn load_in_headless(&self) -> ArcVar<bool>

If should still download/read image bytes in headless/renderless mode.

When an app is in headless mode without renderer no VIEW_PROCESS is available, so images cannot be decoded, in this case all images are the dummy image and no attempt to download/read the image files is made. You can enable loading in headless tests to detect IO errors, in this case if there is an error acquiring the image file the image will be a dummy with error.

source

pub fn limits(&self) -> ArcVar<ImageLimits>

Default loading and decoding limits for each image.

source

pub fn dummy(&self, error: Option<Txt>) -> ReadOnlyVar<Img, ArcVar<Img>>

Returns a dummy image that reports it is loaded or an error.

source

pub fn read(&self, path: impl Into<PathBuf>) -> ReadOnlyVar<Img, ArcVar<Img>>

Cache or load an image file from a file system path.

source

pub fn download( &self, uri: impl TryUri, accept: Option<Txt>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Get a cached uri or download it.

Optionally define the HTTP ACCEPT header, if not set all image formats supported by the view-process backend are accepted.

source

pub fn from_static( &self, data: &'static [u8], format: impl Into<ImageDataFormat>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Get a cached image from &'static [u8] data.

The data can be any of the formats described in ImageDataFormat.

The image key is a ImageHash of the image data.

§Examples

Get an image from a PNG file embedded in the app executable using include_bytes!.

let image_var = IMAGES.from_static(include_bytes!("ico.png"), "png");
source

pub fn from_data( &self, data: Arc<Vec<u8>>, format: impl Into<ImageDataFormat>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Get a cached image from shared data.

The image key is a ImageHash of the image data. The data reference is held only until the image is decoded.

The data can be any of the formats described in ImageDataFormat.

source

pub fn cache( &self, source: impl Into<ImageSource>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Get a cached image or add it to the cache.

source

pub fn retry( &self, source: impl Into<ImageSource>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Get a cached image or add it to the cache or retry if the cached image is an error.

source

pub fn reload( &self, source: impl Into<ImageSource>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Load an image, if it was already cached update the cached image with the reloaded data.

source

pub fn image( &self, source: impl Into<ImageSource>, cache_mode: impl Into<ImageCacheMode>, limits: Option<ImageLimits>, downscale: Option<ImageDownscale>, mask: Option<ImageMaskMode>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Get or load an image.

If limits is None the IMAGES.limits is used.

source

pub fn image_task<F>( &self, source: impl IntoFuture<IntoFuture = F>, cache_mode: impl Into<ImageCacheMode>, cache_key: Option<ImageHash>, limits: Option<ImageLimits>, downscale: Option<ImageDownscale>, mask: Option<ImageMaskMode>, ) -> ReadOnlyVar<Img, ArcVar<Img>>
where F: Future<Output = ImageSource> + Send + 'static,

Await for an image source, then get or load the image.

If limits is None the IMAGES.limits is used.

This method returns immediately with a loading ImageVar, when source is ready it is used to get the actual ImageVar and binds it to the returned image.

Note that the cache_mode always applies to the inner image, and only to the return image if cache_key is set.

source

pub fn register( &self, key: ImageHash, image: ViewImage, ) -> Result<ReadOnlyVar<Img, ArcVar<Img>>, (ViewImage, ReadOnlyVar<Img, ArcVar<Img>>)>

Associate the image with the key in the cache.

Returns Ok(ImageVar) with the new image var that tracks image, or Err(ViewImage, ImageVar) that returns the image and a clone of the var already associated with the key.

source

pub fn clean(&self, key: ImageHash) -> bool

Remove the image from the cache, if it is only held by the cache.

You can use ImageSource::hash128_read and ImageSource::hash128_download to get the key for files or downloads.

Returns true if the image was removed.

source

pub fn purge(&self, key: &ImageHash) -> bool

Remove the image from the cache, even if it is still referenced outside of the cache.

You can use ImageSource::hash128_read and ImageSource::hash128_download to get the key for files or downloads.

Returns true if the image was cached.

source

pub fn cache_key(&self, image: &Img) -> Option<ImageHash>

Gets the cache key of an image.

source

pub fn is_cached(&self, image: &Img) -> bool

If the image is cached.

source

pub fn detach( &self, image: ReadOnlyVar<Img, ArcVar<Img>>, ) -> ReadOnlyVar<Img, ArcVar<Img>>

Returns an image that is not cached.

If the image is the only reference returns it and removes it from the cache. If there are other references a new ImageVar is generated from a clone of the image.

source

pub fn clean_all(&self)

Clear cached images that are not referenced outside of the cache.

source

pub fn purge_all(&self)

Clear all cached images, including images that are still referenced outside of the cache.

Image memory only drops when all strong references are removed, so if an image is referenced outside of the cache it will merely be disconnected from the cache by this method.

source

pub fn install_proxy(&self, proxy: Box<dyn ImageCacheProxy>)

Add a cache proxy.

Proxies can intercept cache requests and map to a different request or return an image directly.

source

pub fn available_decoders(&self) -> Vec<Txt>

Image format decoders implemented by the current view-process.

source

pub fn available_encoders(&self) -> Vec<Txt>

Image format encoders implemented by the current view-process.

Auto Trait Implementations§

§

impl Freeze for IMAGES

§

impl RefUnwindSafe for IMAGES

§

impl Send for IMAGES

§

impl Sync for IMAGES

§

impl Unpin for IMAGES

§

impl UnwindSafe for IMAGES

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T

source§

impl<T> StateValue for T
where T: Any + Send + Sync,