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

This commit is contained in:
Lucas Fernandes Nogueira 2024-03-13 22:02:05 -03:00 committed by GitHub
parent 7213b9e472
commit ea0242db4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 52 additions and 23 deletions

View File

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

View File

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

View File

@ -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.

View File

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

View File

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

File diff suppressed because one or more lines are too long

View File

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

View File

@ -4,9 +4,22 @@
import { Resource, invoke } from './core' 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. */ /** An RGBA Image in row-major order from top to bottom. */
export class Image extends Resource { 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) 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. */ /** Returns the RGBA data for this image, in row-major order from top to bottom. */
async rgba(): Promise<ArrayBuffer | number[]> { async rgba(): Promise<Uint8Array> {
return invoke<ArrayBuffer | number[]>('plugin:image|rgba', { return invoke<number[]>('plugin:image|rgba', {
rid: this.rid rid: this.rid
}) }).then((buffer) => new Uint8Array(buffer))
} }
/** Returns the width of this image. */ /** Returns the size of this image. */
async width() { async size(): Promise<ImageSize> {
return invoke<number>('plugin:image|width', { rid: this.rid }) return invoke<ImageSize>('plugin:image|size', { rid: this.rid })
}
/** Returns the height of this image. */
async height() {
return invoke<number>('plugin:image|height', { rid: this.rid })
} }
} }