Prechádzať zdrojové kódy

refactor(image): expose constructor, unify size getters (#9179)

Lucas Fernandes Nogueira 1 rok pred
rodič
commit
ea0242db4a

+ 5 - 0
.changes/expose-image-constructor.md

@@ -0,0 +1,5 @@
+---
+"@tauri-apps/api": patch:enhance
+---
+
+The `Image` constructor is now public (for internal use only).

+ 5 - 0
.changes/image-rgba-uint8array.md

@@ -0,0 +1,5 @@
+---
+"@tauri-apps/api": patch:breaking
+---
+
+`Image::rgba()` now returns `Promise<Uint8Array>`.

+ 6 - 0
.changes/image-size-refactor.md

@@ -0,0 +1,6 @@
+---
+"@tauri-apps/api": patch:breaking
+"tauri": patch:breaking
+---
+
+Removed `width` and `height` methods on the JS `Image` class, use `size` instead.

+ 1 - 2
core/tauri/build.rs

@@ -144,8 +144,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
       ("from_bytes", true),
       ("from_path", true),
       ("rgba", true),
-      ("width", true),
-      ("height", true),
+      ("size", true),
     ],
   ),
   ("resources", &[("close", true)]),

+ 2 - 0
core/tauri/permissions/image/autogenerated/reference.md

@@ -10,6 +10,8 @@
 |`deny-new`|Denies the new command without any pre-configured scope.|
 |`allow-rgba`|Enables the rgba command without any pre-configured scope.|
 |`deny-rgba`|Denies the rgba command without any pre-configured scope.|
+|`allow-size`|Enables the size command without any pre-configured scope.|
+|`deny-size`|Denies the size command without any pre-configured scope.|
 |`allow-width`|Enables the width command without any pre-configured scope.|
 |`deny-width`|Denies the width command without any pre-configured scope.|
 |`default`|Default permissions for the plugin.|

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
core/tauri/scripts/bundle.global.js


+ 12 - 8
core/tauri/src/image/plugin.rs

@@ -2,6 +2,8 @@
 // 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};
 
@@ -55,25 +57,27 @@ fn rgba<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<Vec<u8>
   Ok(image.rgba().to_vec())
 }
 
-#[command(root = "crate")]
-fn width<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<u32> {
-  let resources_table = app.resources_table();
-  let image = resources_table.get::<Image<'_>>(rid)?;
-  Ok(image.width())
+#[derive(Serialize)]
+struct Size {
+  width: u32,
+  height: u32,
 }
 
 #[command(root = "crate")]
-fn height<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<u32> {
+fn size<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<Size> {
   let resources_table = app.resources_table();
   let image = resources_table.get::<Image<'_>>(rid)?;
-  Ok(image.height())
+  Ok(Size {
+    width: image.width(),
+    height: image.height(),
+  })
 }
 
 /// Initializes the plugin.
 pub fn init<R: Runtime>() -> TauriPlugin<R> {
   Builder::new("image")
     .invoke_handler(crate::generate_handler![
-      new, from_bytes, from_path, rgba, width, height
+      new, from_bytes, from_path, rgba, size
     ])
     .build()
 }

+ 20 - 12
tooling/api/src/image.ts

@@ -4,9 +4,22 @@
 
 import { Resource, invoke } from './core'
 
+/// Image dimensions type.
+export interface ImageSize {
+  /// Image width.
+  width: number
+  /// Image height.
+  height: number
+}
+
 /** An RGBA Image in row-major order from top to bottom. */
 export class Image extends Resource {
-  private constructor(rid: number) {
+  /**
+   * Creates an Image from a resource ID. For internal use only.
+   *
+   * @ignore
+   */
+  constructor(rid: number) {
     super(rid)
   }
 
@@ -63,20 +76,15 @@ export class Image extends Resource {
   }
 
   /** Returns the RGBA data for this image, in row-major order from top to bottom.  */
-  async rgba(): Promise<ArrayBuffer | number[]> {
-    return invoke<ArrayBuffer | number[]>('plugin:image|rgba', {
+  async rgba(): Promise<Uint8Array> {
+    return invoke<number[]>('plugin:image|rgba', {
       rid: this.rid
-    })
-  }
-
-  /** Returns the width of this image.  */
-  async width() {
-    return invoke<number>('plugin:image|width', { rid: this.rid })
+    }).then((buffer) => new Uint8Array(buffer))
   }
 
-  /** Returns the height of this image. */
-  async height() {
-    return invoke<number>('plugin:image|height', { rid: this.rid })
+  /** Returns the size of this image.  */
+  async size(): Promise<ImageSize> {
+    return invoke<ImageSize>('plugin:image|size', { rid: this.rid })
   }
 }
 

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov