浏览代码

bring back toIPC to fix isolation

amrbashir 10 月之前
父节点
当前提交
e795c8c118

+ 8 - 0
.changes/api-dpi-toJSON-toIPC.md

@@ -0,0 +1,8 @@
+---
+"@tauri-apps/cli": "patch:feat"
+---
+
+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.

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

@@ -1,5 +0,0 @@
----
-"@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.

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

@@ -23,11 +23,13 @@
       } 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){
+        return val.toIPC()
       } else {
         return val
       }

+ 81 - 31
packages/api/src/dpi.ts

@@ -63,17 +63,35 @@ class LogicalSize {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const size = new LogicalSize(400, 500);
-   * await invoke("do_something_with_size", { size: size.toJSON() })
+   * await invoke("do_something_with_size", { size: size.toIPC() })
    * ```
    *
    * @since 2.0.0
    */
-  toJSON() {
+  toIPC() {
     return {
       width: this.width,
       height: this.height
     }
   }
+
+  /**
+   * Converts this size into JSON value, that can be deserialized
+   * correctly on the Rust side using `tauri::LogicalSize` struct.
+   * @example
+   * ```typescript
+   * import { LogicalSize } from '@tauri-apps/api/dpi';
+   * import { invoke } from '@tauri-apps/api/core';
+   *
+   * const size = new LogicalSize(400, 500);
+   * await invoke("do_something_with_size", { size: size.toJSON() })
+   * ```
+   *
+   * @since 2.0.0
+   */
+  toJSON() {
+    return this.toIPC()
+  }
 }
 
 /**
@@ -133,19 +151,35 @@ class PhysicalSize {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const size = new PhysicalSize(400, 500);
-   * await invoke("do_something_with_size", { size: size.toJSON() })
+   * await invoke("do_something_with_size", { size: size.toIPC() })
    * ```
    *
    * @since 2.0.0
    */
-  toJSON() {
+  toIPC() {
     return {
-      Physical: {
-        width: this.width,
-        height: this.height
-      }
+      width: this.width,
+      height: this.height
     }
   }
+
+  /**
+   * Converts this size into JSON value, that can be deserialized
+   * correctly on the Rust side using `tauri::PhysicalSize` struct.
+   * @example
+   * ```typescript
+   * import { PhysicalSize } from '@tauri-apps/api/dpi';
+   * import { invoke } from '@tauri-apps/api/core';
+   *
+   * const size = new PhysicalSize(400, 500);
+   * await invoke("do_something_with_size", { size: size.toJSON() })
+   * ```
+   *
+   * @since 2.0.0
+   */
+  toJSON() {
+    return this.toIPC()
+  }
 }
 
 /**
@@ -209,19 +243,35 @@ class LogicalPosition {
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const position = new LogicalPosition(400, 500);
-   * await invoke("do_something_with_position", { size: size.toJSON() })
+   * await invoke("do_something_with_position", { position: position.toIPC() })
    * ```
    *
    * @since 2.0.0
    */
-  toJSON() {
+  toIPC() {
     return {
-      Logical: {
-        x: this.x,
-        y: this.y
-      }
+      x: this.x,
+      y: this.y
     }
   }
+
+  /**
+   * Converts this position into JSON value, that can be deserialized
+   * correctly on the Rust side using `tauri::LogicalPosition` struct.
+   * @example
+   * ```typescript
+   * import { LogicalPosition } from '@tauri-apps/api/dpi';
+   * import { invoke } from '@tauri-apps/api/core';
+   *
+   * const position = new LogicalPosition(400, 500);
+   * await invoke("do_something_with_position", { position: position.toJSON() })
+   * ```
+   *
+   * @since 2.0.0
+   */
+  toJSON() {
+    return this.toIPC()
+  }
 }
 
 /**
@@ -258,42 +308,42 @@ class PhysicalPosition {
   }
 
   /**
-   * Converts the physical position to a logical one.
+   * Converts this position into IPC-compatible value, so it can be
+   * deserialized correctly on the Rust side using `tauri::PhysicalPosition` struct.
    * @example
    * ```typescript
-   * import { getCurrentWindow } from '@tauri-apps/api/window';
+   * import { PhysicalPosition } from '@tauri-apps/api/dpi';
+   * import { invoke } from '@tauri-apps/api/core';
    *
-   * const appWindow = getCurrentWindow();
-   * const factor = await appWindow.scaleFactor();
-   * const position = await appWindow.innerPosition(); // PhysicalPosition
-   * const logical = position.toLogical(factor);
+   * const position = new PhysicalPosition(400, 500);
+   * await invoke("do_something_with_position", { position: position.toIPC() })
    * ```
+   *
+   * @since 2.0.0
    */
-  toLogical(scaleFactor: number): LogicalPosition {
-    return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
+  toIPC() {
+    return {
+      x: this.x,
+      y: this.y
+    }
   }
 
   /**
-   * Converts this position into IPC-compatible value, so it can be
-   * deserialized correctly on the Rust side using `tauri::PhysicalPosition` struct.
+   * Converts this position into JSON value, that can be deserialized
+   * correctly on the Rust side using `tauri::PhysicalPosition` struct.
    * @example
    * ```typescript
    * import { PhysicalPosition } from '@tauri-apps/api/dpi';
    * import { invoke } from '@tauri-apps/api/core';
    *
    * const position = new PhysicalPosition(400, 500);
-   * await invoke("do_something_with_position", { size: size.toJSON() })
+   * await invoke("do_something_with_position", { position: position.toJSON() })
    * ```
    *
    * @since 2.0.0
    */
   toJSON() {
-    return {
-      Physical: {
-        x: this.x,
-        y: this.y
-      }
-    }
+    return this.toIPC()
   }
 }