Skip to content

Commit

Permalink
fix(api.js): fix Monitor initialization, closes #4672 (#5314)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Sep 30, 2022
1 parent 1377f8e commit 6f41a27
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changes/api-monitor-class-initialization.md
@@ -0,0 +1,5 @@
---
"api": "patch"
---

Initialize `Monitor` instances with the correct classes for `position` and `size` fields instead of plain object.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

23 changes: 17 additions & 6 deletions tooling/api/src/window.ts
Expand Up @@ -2035,6 +2035,17 @@ interface WindowOptions {
theme?: Theme
}

function mapMonitor(m: Monitor | null): Monitor | null {
return m === null
? null
: {
name: m.name,
scaleFactor: m.scaleFactor,
position: new PhysicalPosition(m.position.x, m.position.y),
size: new PhysicalSize(m.size.width, m.size.height)
}
}

/**
* Returns the monitor on which the window currently resides.
* Returns `null` if current monitor can't be detected.
Expand All @@ -2047,7 +2058,7 @@ interface WindowOptions {
* @since 1.0.0
*/
async function currentMonitor(): Promise<Monitor | null> {
return invokeTauriCommand({
return invokeTauriCommand<Monitor | null>({
__tauriModule: 'Window',
message: {
cmd: 'manage',
Expand All @@ -2057,7 +2068,7 @@ async function currentMonitor(): Promise<Monitor | null> {
}
}
}
})
}).then(mapMonitor)
}

/**
Expand All @@ -2072,7 +2083,7 @@ async function currentMonitor(): Promise<Monitor | null> {
* @since 1.0.0
*/
async function primaryMonitor(): Promise<Monitor | null> {
return invokeTauriCommand({
return invokeTauriCommand<Monitor | null>({
__tauriModule: 'Window',
message: {
cmd: 'manage',
Expand All @@ -2082,7 +2093,7 @@ async function primaryMonitor(): Promise<Monitor | null> {
}
}
}
})
}).then(mapMonitor)
}

/**
Expand All @@ -2096,7 +2107,7 @@ async function primaryMonitor(): Promise<Monitor | null> {
* @since 1.0.0
*/
async function availableMonitors(): Promise<Monitor[]> {
return invokeTauriCommand({
return invokeTauriCommand<Monitor[]>({
__tauriModule: 'Window',
message: {
cmd: 'manage',
Expand All @@ -2106,7 +2117,7 @@ async function availableMonitors(): Promise<Monitor[]> {
}
}
}
})
}).then((ms) => ms.map(mapMonitor) as Monitor[])
}

export {
Expand Down

0 comments on commit 6f41a27

Please sign in to comment.