Skip to content

Commit

Permalink
refactor(image): expose constructor, unify size getters (#9179)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Mar 14, 2024
1 parent 7213b9e commit ea0242d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/expose-image-constructor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tauri-apps/api": patch:enhance
---

The `Image` constructor is now public (for internal use only).
5 changes: 5 additions & 0 deletions .changes/image-rgba-uint8array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tauri-apps/api": patch:breaking
---

`Image::rgba()` now returns `Promise<Uint8Array>`.
6 changes: 6 additions & 0 deletions .changes/image-size-refactor.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 1 addition & 2 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]),
Expand Down
2 changes: 2 additions & 0 deletions core/tauri/permissions/image/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.|
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions core/tauri/src/image/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
}
32 changes: 20 additions & 12 deletions tooling/api/src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 })
}
}

Expand Down

0 comments on commit ea0242d

Please sign in to comment.