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

feat(api): add SERIALIZE_TO_IPC_FN const and implement it for dpi types, add more constructors #11191

Open
wants to merge 23 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changes/api-dpi-toJSON-toIPC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@tauri-apps/api": "minor:enhance"
---

Improved support for `dpi` module types to allow these types to be used without manual conversions with `invoke`:

- Added `toIPC` method on `PhysicalSize`, `PhysicalPosition`, `LogicalSize` and `LogicalPosition` to convert it into a valid IPC-compatible value that can be deserialized correctly on the Rust side into its equivalent struct.
- Implemented `toJSON` method on `PhysicalSize`, `PhysicalPosition`, `LogicalSize` and `LogicalPosition` that calls the new `toIPC` method, so `JSON.stringify` would serialize these types correctly.
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions crates/tauri/scripts/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@
return
}

// `postMessage` uses `structuredClone` to serialize the data before sending it
// unlike `JSON.stringify`, we can't extend a type with a method similar to `toJSON`
// so that `structuredClone` would use, so until https://github.com/whatwg/html/issues/7428
// we manually call `toIPC`
function recursiveCallToIPC(data) {
if (
typeof data === 'object' &&
'constructor' in data &&
data.constructor === Array
) {
return data.map((v) => recursiveCallToIPC(v))
}

if (typeof data === 'object' && 'toIPC' in data) {
return data.toIPC()
}

if (
typeof data === 'object' &&
'constructor' in data &&
data.constructor === Object
) {
const acc = {}
Object.entries(data).forEach(([k, v]) => {
acc[k] = recursiveCallToIPC(v)
})
return acc
}

return data
}

data.payload = recursiveCallToIPC(data.payload)

isolation.frame.contentWindow.postMessage(
data,
'*' /* todo: set this to the secure origin */
Expand Down
8 changes: 4 additions & 4 deletions crates/tauri/scripts/process-ipc-message-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
} else {
const data = JSON.stringify(message, (_k, val) => {
if (val instanceof Map) {
let o = {}
val.forEach((v, k) => (o[k] = v))
return o
return Object.fromEntries(val.entries())
} else if (val instanceof Uint8Array) {
return Array.from(val)
} else if (val instanceof ArrayBuffer) {
return Array.from(new Uint8Array(val))
} else if (
val instanceof Object &&
typeof val === "object" &&
'__TAURI_CHANNEL_MARKER__' in val &&
typeof val.id === 'number'
) {
return `__CHANNEL__:${val.id}`
} else if (typeof val === "object" && 'toIPC' in val){
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
return val.toIPC()
} else {
return val
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ tauri::Builder::default()
docsrs,
doc(cfg(any(target_os = "macos", target_os = "linux", windows)))
)]
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct ProgressBarState {
/// The progress bar status.
pub status: Option<ProgressBarStatus>,
Expand Down
2 changes: 1 addition & 1 deletion packages/api/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import prettierConfig from 'eslint-config-prettier'
import securityPlugin from 'eslint-plugin-security'
import tseslint from 'typescript-eslint'

/** @type {import('eslint').Linter.FlatConfig[]} */
/** @type {import('eslint').Linter.Config} */
export default [
eslint.configs.recommended,
prettierConfig,
Expand Down
4 changes: 2 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"npm-pack": "pnpm build && cd ./dist && npm pack",
"npm-publish": "pnpm build && cd ./dist && pnpm publish --access public --loglevel silly --tag next --no-git-checks",
"ts:check": "tsc --noEmit",
"eslint:check": "eslint src/**.ts",
"eslint:fix": "eslint src/**.ts --fix"
"eslint:check": "eslint src/**/*.ts",
"eslint:fix": "eslint src/**/*.ts --fix"
},
"devDependencies": {
"@eslint/js": "^9.4.0",
Expand Down
Loading