plugin.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use serde::Serialize;
  5. use crate::plugin::{Builder, TauriPlugin};
  6. use crate::{command, image::Image, AppHandle, Manager, ResourceId, Runtime};
  7. #[command(root = "crate")]
  8. fn new<R: Runtime>(
  9. app: AppHandle<R>,
  10. rgba: Vec<u8>,
  11. width: u32,
  12. height: u32,
  13. ) -> crate::Result<ResourceId> {
  14. let image = Image::new_owned(rgba, width, height);
  15. let mut resources_table = app.resources_table();
  16. let rid = resources_table.add(image);
  17. Ok(rid)
  18. }
  19. #[cfg(any(feature = "image-ico", feature = "image-png"))]
  20. #[command(root = "crate")]
  21. fn from_bytes<R: Runtime>(app: AppHandle<R>, bytes: Vec<u8>) -> crate::Result<ResourceId> {
  22. let image = Image::from_bytes(&bytes)?.to_owned();
  23. let mut resources_table = app.resources_table();
  24. let rid = resources_table.add(image);
  25. Ok(rid)
  26. }
  27. #[cfg(not(any(feature = "image-ico", feature = "image-png")))]
  28. #[command(root = "crate")]
  29. fn from_bytes() -> std::result::Result<(), &'static str> {
  30. Err("from_bytes is only supported if the `image-ico` or `image-png` Cargo features are enabled")
  31. }
  32. #[cfg(any(feature = "image-ico", feature = "image-png"))]
  33. #[command(root = "crate")]
  34. fn from_path<R: Runtime>(app: AppHandle<R>, path: std::path::PathBuf) -> crate::Result<ResourceId> {
  35. let image = Image::from_path(path)?.to_owned();
  36. let mut resources_table = app.resources_table();
  37. let rid = resources_table.add(image);
  38. Ok(rid)
  39. }
  40. #[cfg(not(any(feature = "image-ico", feature = "image-png")))]
  41. #[command(root = "crate")]
  42. fn from_path() -> std::result::Result<(), &'static str> {
  43. Err("from_path is only supported if the `image-ico` or `image-png` Cargo features are enabled")
  44. }
  45. #[command(root = "crate")]
  46. fn rgba<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<Vec<u8>> {
  47. let resources_table = app.resources_table();
  48. let image = resources_table.get::<Image<'_>>(rid)?;
  49. Ok(image.rgba().to_vec())
  50. }
  51. #[derive(Serialize)]
  52. struct Size {
  53. width: u32,
  54. height: u32,
  55. }
  56. #[command(root = "crate")]
  57. fn size<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<Size> {
  58. let resources_table = app.resources_table();
  59. let image = resources_table.get::<Image<'_>>(rid)?;
  60. Ok(Size {
  61. width: image.width(),
  62. height: image.height(),
  63. })
  64. }
  65. /// Initializes the plugin.
  66. pub fn init<R: Runtime>() -> TauriPlugin<R> {
  67. Builder::new("image")
  68. .invoke_handler(crate::generate_handler![
  69. new, from_bytes, from_path, rgba, size
  70. ])
  71. .build()
  72. }