Skip to content

Commit 7616e6c

Browse files
authored
feat(api): validate window API size and location arguments (#1846)
* feat(api): validate window API `size` and `location` arguments * fmt
1 parent c1f8e11 commit 7616e6c

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

.changes/window-api-validations.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Validate arguments on the window `setLocation`, `setSize`, `setMinSize` and `setMaxSize` API.

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/public/build/bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/public/build/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/window.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,21 @@ export class WindowManager {
511511

512512
/**
513513
* Resizes the window.
514+
* @example
515+
* ```typescript
516+
* import { appWindow, LogicalSize } from '@tauri-apps/api/window'
517+
* await appWindow.setSize(new LogicalSize(600, 500))
518+
* ```
514519
*
515520
* @param size The logical or physical size.
516521
* @returns A promise indicating the success or failure of the operation.
517522
*/
518523
async setSize(size: LogicalSize | PhysicalSize): Promise<void> {
524+
if (!size || (size.type !== 'Logical' && size.type !== 'Physical')) {
525+
throw new Error(
526+
'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
527+
)
528+
}
519529
return invokeTauriCommand({
520530
__tauriModule: 'Window',
521531
message: {
@@ -532,14 +542,24 @@ export class WindowManager {
532542
}
533543

534544
/**
535-
* Sets the window min size.
545+
* Sets the window min size. If the `size` argument is not provided, the min size is unset.
546+
* @example
547+
* ```typescript
548+
* import { appWindow, PhysicalSize } from '@tauri-apps/api/window'
549+
* await appWindow.setMinSize(new PhysicalSize(600, 500))
550+
* ```
536551
*
537552
* @param size The logical or physical size.
538553
* @returns A promise indicating the success or failure of the operation.
539554
*/
540555
async setMinSize(
541556
size: LogicalSize | PhysicalSize | undefined
542557
): Promise<void> {
558+
if (size && size.type !== 'Logical' && size.type !== 'Physical') {
559+
throw new Error(
560+
'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
561+
)
562+
}
543563
return invokeTauriCommand({
544564
__tauriModule: 'Window',
545565
message: {
@@ -558,14 +578,24 @@ export class WindowManager {
558578
}
559579

560580
/**
561-
* Sets the window max size.
581+
* Sets the window max size. If the `size` argument is undefined, the max size is unset.
582+
* @example
583+
* ```typescript
584+
* import { appWindow, LogicalSize } from '@tauri-apps/api/window'
585+
* await appWindow.setMaxSize(new LogicalSize(600, 500))
586+
* ```
562587
*
563588
* @param size The logical or physical size.
564589
* @returns A promise indicating the success or failure of the operation.
565590
*/
566591
async setMaxSize(
567592
size: LogicalSize | PhysicalSize | undefined
568593
): Promise<void> {
594+
if (size && size.type !== 'Logical' && size.type !== 'Physical') {
595+
throw new Error(
596+
'the `size` argument must be either a LogicalSize or a PhysicalSize instance'
597+
)
598+
}
569599
return invokeTauriCommand({
570600
__tauriModule: 'Window',
571601
message: {
@@ -585,13 +615,26 @@ export class WindowManager {
585615

586616
/**
587617
* Sets the window position.
618+
* @example
619+
* ```typescript
620+
* import { appWindow, LogicalPosition } from '@tauri-apps/api/window'
621+
* await appWindow.setPosition(new LogicalPosition(600, 500))
622+
* ```
588623
*
589624
* @param position The new position, in logical or physical pixels.
590625
* @returns A promise indicating the success or failure of the operation.
591626
*/
592627
async setPosition(
593628
position: LogicalPosition | PhysicalPosition
594629
): Promise<void> {
630+
if (
631+
!position ||
632+
(position.type !== 'Logical' && position.type !== 'Physical')
633+
) {
634+
throw new Error(
635+
'the `position` argument must be either a LogicalPosition or a PhysicalPosition instance'
636+
)
637+
}
595638
return invokeTauriCommand({
596639
__tauriModule: 'Window',
597640
message: {
@@ -750,6 +793,4 @@ export {
750793
availableMonitors
751794
}
752795

753-
export type {
754-
Monitor
755-
}
796+
export type { Monitor }

0 commit comments

Comments
 (0)