浏览代码

feat(api): validate window API `size` and `location` arguments (#1846)

* feat(api): validate window API `size` and `location` arguments

* fmt
Lucas Fernandes Nogueira 4 年之前
父节点
当前提交
7616e6cc7b

+ 5 - 0
.changes/window-api-validations.md

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Validate arguments on the window `setLocation`, `setSize`, `setMinSize` and `setMaxSize` API.

文件差异内容过多而无法显示
+ 0 - 0
core/tauri/scripts/bundle.js


文件差异内容过多而无法显示
+ 0 - 0
examples/api/public/build/bundle.js


文件差异内容过多而无法显示
+ 0 - 0
examples/api/public/build/bundle.js.map


+ 46 - 5
tooling/api/src/window.ts

@@ -511,11 +511,21 @@ export class WindowManager {
 
   /**
    * Resizes the window.
+   * @example
+   * ```typescript
+   * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
+   * await appWindow.setSize(new LogicalSize(600, 500))
+   * ```
    *
    * @param size The logical or physical size.
    * @returns A promise indicating the success or failure of the operation.
    */
   async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
+    if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
+      throw new Error(
+        'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
+      )
+    }
     return invokeTauriCommand({
       __tauriModule: 'Window',
       message: {
@@ -532,7 +542,12 @@ export class WindowManager {
   }
 
   /**
-   * Sets the window min size.
+   * Sets the window min size. If the `size` argument is not provided, the min size is unset.
+   * @example
+   * ```typescript
+   * import { appWindow, PhysicalSize } from '@tauri-apps/api/window'
+   * await appWindow.setMinSize(new PhysicalSize(600, 500))
+   * ```
    *
    * @param size The logical or physical size.
    * @returns A promise indicating the success or failure of the operation.
@@ -540,6 +555,11 @@ export class WindowManager {
   async setMinSize(
     size: LogicalSize | PhysicalSize | undefined
   ): Promise<void> {
+    if (size && size.type !== 'Logical' && size.type !== 'Physical') {
+      throw new Error(
+        'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
+      )
+    }
     return invokeTauriCommand({
       __tauriModule: 'Window',
       message: {
@@ -558,7 +578,12 @@ export class WindowManager {
   }
 
   /**
-   * Sets the window max size.
+   * Sets the window max size. If the `size` argument is undefined, the max size is unset.
+   * @example
+   * ```typescript
+   * import { appWindow, LogicalSize } from '@tauri-apps/api/window'
+   * await appWindow.setMaxSize(new LogicalSize(600, 500))
+   * ```
    *
    * @param size The logical or physical size.
    * @returns A promise indicating the success or failure of the operation.
@@ -566,6 +591,11 @@ export class WindowManager {
   async setMaxSize(
     size: LogicalSize | PhysicalSize | undefined
   ): Promise<void> {
+    if (size && size.type !== 'Logical' && size.type !== 'Physical') {
+      throw new Error(
+        'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
+      )
+    }
     return invokeTauriCommand({
       __tauriModule: 'Window',
       message: {
@@ -585,6 +615,11 @@ export class WindowManager {
 
   /**
    * Sets the window position.
+   * @example
+   * ```typescript
+   * import { appWindow, LogicalPosition } from '@tauri-apps/api/window'
+   * await appWindow.setPosition(new LogicalPosition(600, 500))
+   * ```
    *
    * @param position The new position, in logical or physical pixels.
    * @returns A promise indicating the success or failure of the operation.
@@ -592,6 +627,14 @@ export class WindowManager {
   async setPosition(
     position: LogicalPosition | PhysicalPosition
   ): Promise<void> {
+    if (
+      !position ||
+      (position.type !== 'Logical' && position.type !== 'Physical')
+    ) {
+      throw new Error(
+        'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
+      )
+    }
     return invokeTauriCommand({
       __tauriModule: 'Window',
       message: {
@@ -750,6 +793,4 @@ export {
   availableMonitors
 }
 
-export type {
-  Monitor
-}
+export type { Monitor }

部分文件因为文件数量过多而无法显示