瀏覽代碼

implment `toJSON` instead

amrbashir 10 月之前
父節點
當前提交
c58811a935

+ 5 - 0
.changes/api-dpi-toJSON.md

@@ -0,0 +1,5 @@
+---
+"@tauri-apps/cli": "patch:feat"
+---
+
+Implementd `toJSON` method on `PhysicalSize`, `PhysicalPosition`, `LogicalSize` and `LogicalPosition` to convert it into a valid JSON that can be deserialized correctly on the Rust side into its equivalent struct.

+ 0 - 5
.changes/api-toIpc.md

@@ -1,5 +0,0 @@
----
-"@tauri-apps/cli": "patch:feat"
----
-
-Add `toIpc` method on `PhysicalSize`, `PhysicalPosition`, `LogicalSize` and `LogicalPosition` to convert it into IPC-compatible value that can be deserialized correctly on the Rust side into its equivalent struct.

文件差異過大導致無法顯示
+ 0 - 0
crates/tauri/scripts/bundle.global.js


+ 1 - 3
crates/tauri/scripts/process-ipc-message-fn.js

@@ -17,9 +17,7 @@
   } 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) {

+ 1 - 1
crates/tauri/src/window/mod.rs

@@ -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>,

+ 1 - 1
packages/api/eslint.config.js

@@ -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,

+ 2 - 2
packages/api/package.json

@@ -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",

+ 10 - 12
packages/api/src/dpi.ts

@@ -63,17 +63,15 @@ class LogicalSize {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const size = new LogicalSize(400, 500);
-   * await invoke("do_something_with_size", { size: size.toIpc() })
+   * await invoke("do_something_with_size", { size: size.toJSON() })
    * ```
    *
    * @since 2.0.0
    */
-  toIpc(): { Logical: { width: number; height: number } } {
+  toJSON() {
     return {
-      Logical: {
-        width: this.width,
-        height: this.height
-      }
+      width: this.width,
+      height: this.height
     }
   }
 }
@@ -135,12 +133,12 @@ class PhysicalSize {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const size = new PhysicalSize(400, 500);
-   * await invoke("do_something_with_size", { size: size.toIpc() })
+   * await invoke("do_something_with_size", { size: size.toJSON() })
    * ```
    *
    * @since 2.0.0
    */
-  toIpc(): { Physical: { width: number; height: number } } {
+  toJSON() {
     return {
       Physical: {
         width: this.width,
@@ -211,12 +209,12 @@ class LogicalPosition {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const position = new LogicalPosition(400, 500);
-   * await invoke("do_something_with_position", { size: size.toIpc() })
+   * await invoke("do_something_with_position", { size: size.toJSON() })
    * ```
    *
    * @since 2.0.0
    */
-  toIpc(): { Logical: { x: number; y: number } } {
+  toJSON() {
     return {
       Logical: {
         x: this.x,
@@ -284,12 +282,12 @@ class PhysicalPosition {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const position = new PhysicalPosition(400, 500);
-   * await invoke("do_something_with_position", { size: size.toIpc() })
+   * await invoke("do_something_with_position", { size: size.toJSON() })
    * ```
    *
    * @since 2.0.0
    */
-  toIpc(): { Physical: { x: number; y: number } } {
+  toJSON() {
     return {
       Physical: {
         x: this.x,

+ 1 - 1
packages/api/src/menu/menu.ts

@@ -247,7 +247,7 @@ export class Menu extends MenuItemBase {
       rid: this.rid,
       kind: this.kind,
       window: window?.label ?? null,
-      at: at?.toIpc()
+      at
     })
   }
 

+ 1 - 1
packages/api/src/menu/submenu.ts

@@ -247,7 +247,7 @@ export class Submenu extends MenuItemBase {
       rid: this.rid,
       kind: this.kind,
       window: window?.label ?? null,
-      at: at?.toIpc()
+      at
     })
   }
 

+ 2 - 2
packages/api/src/webview.ts

@@ -413,7 +413,7 @@ class Webview {
   async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
     return invoke('plugin:webview|set_webview_size', {
       label: this.label,
-      value: size.toIpc()
+      value: size
     })
   }
 
@@ -433,7 +433,7 @@ class Webview {
   ): Promise<void> {
     return invoke('plugin:webview|set_webview_position', {
       label: this.label,
-      value: position.toIpc()
+      value: position
     })
   }
 

+ 5 - 5
packages/api/src/window.ts

@@ -1265,7 +1265,7 @@ class Window {
   async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
     return invoke('plugin:window|set_size', {
       label: this.label,
-      value: size.toIpc()
+      value: size
     })
   }
 
@@ -1285,7 +1285,7 @@ class Window {
   ): Promise<void> {
     return invoke('plugin:window|set_min_size', {
       label: this.label,
-      value: size?.toIpc()
+      value: size
     })
   }
 
@@ -1305,7 +1305,7 @@ class Window {
   ): Promise<void> {
     return invoke('plugin:window|set_max_size', {
       label: this.label,
-      value: size?.toIpc()
+      value: size
     })
   }
 
@@ -1354,7 +1354,7 @@ class Window {
   ): Promise<void> {
     return invoke('plugin:window|set_position', {
       label: this.label,
-      value: position.toIpc()
+      value: position
     })
   }
 
@@ -1525,7 +1525,7 @@ class Window {
   ): Promise<void> {
     return invoke('plugin:window|set_cursor_position', {
       label: this.label,
-      value: position.toIpc()
+      value: position
     })
   }
 

部分文件因文件數量過多而無法顯示