Skip to content

Commit ea0242d

Browse files
authored
refactor(image): expose constructor, unify size getters (#9179)
1 parent 7213b9e commit ea0242d

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": patch:enhance
3+
---
4+
5+
The `Image` constructor is now public (for internal use only).

.changes/image-rgba-uint8array.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": patch:breaking
3+
---
4+
5+
`Image::rgba()` now returns `Promise<Uint8Array>`.

.changes/image-size-refactor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": patch:breaking
3+
"tauri": patch:breaking
4+
---
5+
6+
Removed `width` and `height` methods on the JS `Image` class, use `size` instead.

core/tauri/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
144144
("from_bytes", true),
145145
("from_path", true),
146146
("rgba", true),
147-
("width", true),
148-
("height", true),
147+
("size", true),
149148
],
150149
),
151150
("resources", &[("close", true)]),

core/tauri/permissions/image/autogenerated/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
|`deny-new`|Denies the new command without any pre-configured scope.|
1111
|`allow-rgba`|Enables the rgba command without any pre-configured scope.|
1212
|`deny-rgba`|Denies the rgba command without any pre-configured scope.|
13+
|`allow-size`|Enables the size command without any pre-configured scope.|
14+
|`deny-size`|Denies the size command without any pre-configured scope.|
1315
|`allow-width`|Enables the width command without any pre-configured scope.|
1416
|`deny-width`|Denies the width command without any pre-configured scope.|
1517
|`default`|Default permissions for the plugin.|

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/image/plugin.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use serde::Serialize;
6+
57
use crate::plugin::{Builder, TauriPlugin};
68
use crate::{command, image::Image, AppHandle, Manager, ResourceId, Runtime};
79

@@ -55,25 +57,27 @@ fn rgba<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<Vec<u8>
5557
Ok(image.rgba().to_vec())
5658
}
5759

58-
#[command(root = "crate")]
59-
fn width<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<u32> {
60-
let resources_table = app.resources_table();
61-
let image = resources_table.get::<Image<'_>>(rid)?;
62-
Ok(image.width())
60+
#[derive(Serialize)]
61+
struct Size {
62+
width: u32,
63+
height: u32,
6364
}
6465

6566
#[command(root = "crate")]
66-
fn height<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<u32> {
67+
fn size<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<Size> {
6768
let resources_table = app.resources_table();
6869
let image = resources_table.get::<Image<'_>>(rid)?;
69-
Ok(image.height())
70+
Ok(Size {
71+
width: image.width(),
72+
height: image.height(),
73+
})
7074
}
7175

7276
/// Initializes the plugin.
7377
pub fn init<R: Runtime>() -> TauriPlugin<R> {
7478
Builder::new("image")
7579
.invoke_handler(crate::generate_handler![
76-
new, from_bytes, from_path, rgba, width, height
80+
new, from_bytes, from_path, rgba, size
7781
])
7882
.build()
7983
}

tooling/api/src/image.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@
44

55
import { Resource, invoke } from './core'
66

7+
/// Image dimensions type.
8+
export interface ImageSize {
9+
/// Image width.
10+
width: number
11+
/// Image height.
12+
height: number
13+
}
14+
715
/** An RGBA Image in row-major order from top to bottom. */
816
export class Image extends Resource {
9-
private constructor(rid: number) {
17+
/**
18+
* Creates an Image from a resource ID. For internal use only.
19+
*
20+
* @ignore
21+
*/
22+
constructor(rid: number) {
1023
super(rid)
1124
}
1225

@@ -63,20 +76,15 @@ export class Image extends Resource {
6376
}
6477

6578
/** Returns the RGBA data for this image, in row-major order from top to bottom. */
66-
async rgba(): Promise<ArrayBuffer | number[]> {
67-
return invoke<ArrayBuffer | number[]>('plugin:image|rgba', {
79+
async rgba(): Promise<Uint8Array> {
80+
return invoke<number[]>('plugin:image|rgba', {
6881
rid: this.rid
69-
})
70-
}
71-
72-
/** Returns the width of this image. */
73-
async width() {
74-
return invoke<number>('plugin:image|width', { rid: this.rid })
82+
}).then((buffer) => new Uint8Array(buffer))
7583
}
7684

77-
/** Returns the height of this image. */
78-
async height() {
79-
return invoke<number>('plugin:image|height', { rid: this.rid })
85+
/** Returns the size of this image. */
86+
async size(): Promise<ImageSize> {
87+
return invoke<ImageSize>('plugin:image|size', { rid: this.rid })
8088
}
8189
}
8290

0 commit comments

Comments
 (0)