mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-25 11:43:06 +03:00
refactor(image): expose constructor, unify size getters (#9179)
This commit is contained in:
parent
7213b9e472
commit
ea0242db4a
5
.changes/expose-image-constructor.md
Normal file
5
.changes/expose-image-constructor.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@tauri-apps/api": patch:enhance
|
||||
---
|
||||
|
||||
The `Image` constructor is now public (for internal use only).
|
5
.changes/image-rgba-uint8array.md
Normal file
5
.changes/image-rgba-uint8array.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@tauri-apps/api": patch:breaking
|
||||
---
|
||||
|
||||
`Image::rgba()` now returns `Promise<Uint8Array>`.
|
6
.changes/image-size-refactor.md
Normal file
6
.changes/image-size-refactor.md
Normal 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.
|
@ -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)]),
|
||||
|
@ -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.|
|
||||
|
File diff suppressed because one or more lines are too long
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
}).then((buffer) => new Uint8Array(buffer))
|
||||
}
|
||||
|
||||
/** Returns the width of this image. */
|
||||
async width() {
|
||||
return invoke<number>('plugin:image|width', { rid: this.rid })
|
||||
}
|
||||
|
||||
/** 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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user