// Copyright 2019-2024 Tauri Programme within The Commons Conservancy // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT use serde::Serialize; use crate::plugin::{Builder, TauriPlugin}; use crate::{command, image::Image, AppHandle, Manager, ResourceId, Runtime}; #[command(root = "crate")] fn new( app: AppHandle, rgba: Vec, width: u32, height: u32, ) -> crate::Result { let image = Image::new_owned(rgba, width, height); let mut resources_table = app.resources_table(); let rid = resources_table.add(image); Ok(rid) } #[cfg(any(feature = "image-ico", feature = "image-png"))] #[command(root = "crate")] fn from_bytes(app: AppHandle, bytes: Vec) -> crate::Result { let image = Image::from_bytes(&bytes)?.to_owned(); let mut resources_table = app.resources_table(); let rid = resources_table.add(image); Ok(rid) } #[cfg(not(any(feature = "image-ico", feature = "image-png")))] #[command(root = "crate")] fn from_bytes() -> std::result::Result<(), &'static str> { Err("from_bytes is only supported if the `image-ico` or `image-png` Cargo features are enabled") } #[cfg(any(feature = "image-ico", feature = "image-png"))] #[command(root = "crate")] fn from_path(app: AppHandle, path: std::path::PathBuf) -> crate::Result { let image = Image::from_path(path)?.to_owned(); let mut resources_table = app.resources_table(); let rid = resources_table.add(image); Ok(rid) } #[cfg(not(any(feature = "image-ico", feature = "image-png")))] #[command(root = "crate")] fn from_path() -> std::result::Result<(), &'static str> { Err("from_path is only supported if the `image-ico` or `image-png` Cargo features are enabled") } #[command(root = "crate")] fn rgba(app: AppHandle, rid: ResourceId) -> crate::Result> { let resources_table = app.resources_table(); let image = resources_table.get::>(rid)?; Ok(image.rgba().to_vec()) } #[derive(Serialize)] struct Size { width: u32, height: u32, } #[command(root = "crate")] fn size(app: AppHandle, rid: ResourceId) -> crate::Result { let resources_table = app.resources_table(); let image = resources_table.get::>(rid)?; Ok(Size { width: image.width(), height: image.height(), }) } /// Initializes the plugin. pub fn init() -> TauriPlugin { Builder::new("image") .invoke_handler(crate::generate_handler![ new, from_bytes, from_path, rgba, size ]) .build() }