Skip to content

Commit 9167826

Browse files
authored
feat(api): add logical monitor bounds to the Monitor object (#15520)
* feat(api): add logical monitor bounds to the Monitor object * docs(api): document physical pixel units on Monitor fields Replaces the logicalPosition/logicalSize/logicalWorkArea fields with JSDoc on Monitor.size, Monitor.position and Monitor.workArea stating the values are in physical pixels, including examples converting to the logical pixels expected by window creation options. * fix monitor position example null check
1 parent 4222dd1 commit 9167826

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

.changes/monitor-logical-bounds.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": "patch:enhance"
3+
---
4+
5+
Document that `Monitor.size`, `Monitor.position` and `Monitor.workArea` are in physical pixels, with examples showing how to convert them to the logical pixels expected by window creation options via `toLogical(monitor.scaleFactor)`.

packages/api/src/window.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,46 @@ import { Image, transformImage } from './image'
4747
export interface Monitor {
4848
/** Human-readable name of the monitor */
4949
name: string | null
50-
/** The monitor's resolution. */
50+
/**
51+
* The monitor's resolution in physical pixels.
52+
*
53+
* Use {@linkcode Monitor.scaleFactor} to convert to logical pixels:
54+
* ```typescript
55+
* const logicalSize = monitor.size.toLogical(monitor.scaleFactor);
56+
* ```
57+
*/
5158
size: PhysicalSize
52-
/** the Top-left corner position of the monitor relative to the larger full screen area. */
59+
/**
60+
* the Top-left corner position of the monitor relative to the larger full screen area, in physical pixels.
61+
*
62+
* Note that window creation options such as `x`, `y`, `width` and `height` expect
63+
* logical pixels, so convert with {@linkcode Monitor.scaleFactor} first:
64+
* ```typescript
65+
* import { currentMonitor } from '@tauri-apps/api/window';
66+
* import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
67+
*
68+
* const monitor = await currentMonitor();
69+
* if (monitor) {
70+
* const position = monitor.position.toLogical(monitor.scaleFactor);
71+
* const webview = new WebviewWindow('my-label', { x: position.x, y: position.y });
72+
* }
73+
* ```
74+
*/
5375
position: PhysicalPosition
54-
/** The monitor's work area. */
76+
/**
77+
* The monitor's work area (the monitor area excluding taskbars and docks) in physical pixels.
78+
*
79+
* Use {@linkcode Monitor.scaleFactor} to convert to logical pixels as shown in
80+
* {@linkcode Monitor.position}.
81+
*/
5582
workArea: {
5683
position: PhysicalPosition
5784
size: PhysicalSize
5885
}
59-
/** The scale factor that can be used to map physical pixels to logical pixels. */
86+
/**
87+
* The scale factor that can be used to map physical pixels to logical pixels,
88+
* e.g. `monitor.position.toLogical(monitor.scaleFactor)`.
89+
*/
6090
scaleFactor: number
6191
}
6292

0 commit comments

Comments
 (0)