Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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` commands from the `Image` class, use `size` instead.
lucasfernog marked this conversation as resolved.
Show resolved Hide resolved
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