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

Merged
merged 26 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bee00b4
feat(api): add `toIpc` method on dpi types, add more constructors
amrbashir Oct 2, 2024
93177b7
Update api-toIpc.md
amrbashir Oct 2, 2024
db2f2d3
Update api-toIpc.md
amrbashir Oct 2, 2024
c58811a
implment `toJSON` instead
amrbashir Oct 2, 2024
e795c8c
bring back toIPC to fix isolation
amrbashir Oct 2, 2024
f241ce3
bundle
amrbashir Oct 2, 2024
9f2d412
fix isolation
amrbashir Oct 2, 2024
27117b9
Merge branch 'dev' into feat/api/dpi-toIpc
amrbashir Oct 3, 2024
d88aa47
review suggestions
amrbashir Oct 3, 2024
ff11a44
Update .changes/api-dpi-toJSON-toIPC.md
amrbashir Oct 3, 2024
dcd4089
Update .changes/api-dpi-toJSON-toIPC.md
amrbashir Oct 3, 2024
aaf301f
Update packages/api/src/window.ts
amrbashir Oct 3, 2024
4ed949d
fmt
amrbashir Oct 3, 2024
ef145ab
Update packages/api/src/window.ts
amrbashir Oct 3, 2024
aed5d67
Update crates/tauri/scripts/process-ipc-message-fn.js
amrbashir Oct 3, 2024
77fce45
use `ToIPCSymbol` to define the special function
amrbashir Oct 3, 2024
efb1728
rename
amrbashir Oct 3, 2024
eb877b0
Merge branch 'dev' into feat/api/dpi-toIpc
lucasfernog Oct 17, 2024
6e79d48
rename ToIPCKey to SERIALIZE_TO_IPC_FN
lucasfernog Oct 17, 2024
744f4bc
use toIPC mechanism for channels, fix bug checking `null`
lucasfernog Oct 17, 2024
4ed185f
Add `Size` and `Position` dpi types
amrbashir Oct 18, 2024
b01da7f
fix menu and submenu popup
amrbashir Oct 18, 2024
258fe72
Merge branch 'dev' into feat/api/dpi-toIpc
amrbashir Oct 21, 2024
6ffa222
revert channel change
lucasfernog Nov 5, 2024
890723b
typo
lucasfernog Nov 5, 2024
bc08c82
Merge branch 'dev' into feat/api/dpi-toIpc
lucasfernog Nov 5, 2024
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.

39 changes: 39 additions & 0 deletions crates/tauri/scripts/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,45 @@
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 processToIPCKey(data) {
// if this value changes, make sure to update it in:
// 1. process-ipc-message-fn.js
// 2. core.ts
const ToIPCKey = '__TAURI_TO_IPC_KEY__'

if (
typeof data === 'object' &&
'constructor' in data &&
data.constructor === Array
) {
return data.map((v) => processToIPCKey(v))
}

if (typeof data === 'object' && ToIPCKey in data) {
return data[ToIPCKey]()
}

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

return data
}

data.payload = processToIPCKey(data.payload)

isolation.frame.contentWindow.postMessage(
data,
'*' /* todo: set this to the secure origin */
Expand Down
13 changes: 9 additions & 4 deletions crates/tauri/scripts/process-ipc-message-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@
}
} else {
const data = JSON.stringify(message, (_k, val) => {
// if this value changes, make sure to update it in:
// 1. ipc.js
// 2. core.ts
const ToIPCKey = '__TAURI_TO_IPC_KEY__'

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" && ToIPCKey in val) {
return val[ToIPCKey]()
} 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
50 changes: 50 additions & 0 deletions packages/api/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,56 @@ export class Resource {
}
}

/**
* A key to be used to implement a special function
* on your types that define how your type should be serialized
* when passing across the IPC.
* @example
* Given a type in Rust that looks like this
* ```rs
* #[derive(serde::Serialize, serde::Deserialize)
* enum UserId {
* String(String),
* Number(u32),
* }
* ```
* `UserId::String("id")` would be serialized into `{ String: "id" }`
* and so we need to pass the same structure back to Rust
* ```ts
* import { ToIPCKey } from "@tauri-apps/api/core"
*
* class UserIdString {
* id
* constructor(id) {
* this.id = id
* }
*
* [ToIPCKey]() {
* return { String: this.id }
* }
* }
*
* class UserIdNumber {
* id
* constructor(id) {
* this.id = id
* }
*
* [ToIPCKey]() {
* return { Number: this.id }
* }
* }
*
*
* type UserId = UserIdString | UserIdNumber
* ```
*
*/
// if this value changes, make sure to update it in:
// 1. ipc.js
// 2. process-ipc-message-fn.js
export const ToIPCKey = '__TAURI_TO_IPC_KEY__'

function isTauri(): boolean {
return 'isTauri' in window && !!window.isTauri
}
Expand Down
Loading