Skip to content

Commit cc8b146

Browse files
Fix(api): Window size and position returning wrong class (fix: #2599) (#2621)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 4dc5a9f commit cc8b146

File tree

6 files changed

+132
-13
lines changed

6 files changed

+132
-13
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"api": patch
3+
---
4+
5+
`WindowManager` methods `innerPosition` `outerPosition` now correctly return instance of `PhysicalPosition`.
6+
`WindowManager` methods `innerSize` `outerSize` now correctly return instance of `PhysicalSize`.

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: 16 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.

examples/api/src/components/Window.svelte

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { appWindow, WebviewWindow, LogicalSize, LogicalPosition, UserAttentionType, getCurrent } from "@tauri-apps/api/window";
2+
import { appWindow, WebviewWindow, LogicalSize, LogicalPosition, UserAttentionType, getCurrent, PhysicalSize, PhysicalPosition } from "@tauri-apps/api/window";
33
import { open as openDialog } from "@tauri-apps/api/dialog";
44
import { open } from "@tauri-apps/api/shell";
55
@@ -26,6 +26,13 @@
2626
let maxHeight = null;
2727
let x = 100;
2828
let y = 100;
29+
let scaleFactor = 1;
30+
let innerPosition = new PhysicalPosition(x, y);
31+
let outerPosition = new PhysicalPosition(x, y);
32+
let innerSize = new PhysicalSize(width, height);
33+
let outerSize = new PhysicalSize(width, height);
34+
let resizeEventUnlisten;
35+
let moveEventUnlisten;
2936
3037
let windowTitle = "Awesome Tauri Example!";
3138
@@ -62,6 +69,39 @@
6269
})
6370
}
6471
72+
function handleWindowResize() {
73+
windowMap[selectedWindow].innerSize().then(response => {
74+
innerSize = response
75+
width = innerSize.width
76+
height = innerSize.height
77+
});
78+
windowMap[selectedWindow].outerSize().then(response => {
79+
outerSize = response
80+
});
81+
}
82+
83+
function handleWindowMove() {
84+
windowMap[selectedWindow].innerPosition().then(response => {
85+
innerPosition = response
86+
});
87+
windowMap[selectedWindow].outerPosition().then(response => {
88+
outerPosition = response
89+
x = outerPosition.x
90+
y = outerPosition.y
91+
});
92+
}
93+
94+
function addWindowEventListeners(window) {
95+
if (resizeEventUnlisten) {
96+
resizeEventUnlisten();
97+
}
98+
if(moveEventUnlisten) {
99+
moveEventUnlisten();
100+
}
101+
moveEventUnlisten = window.listen('tauri://move', handleWindowMove);
102+
resizeEventUnlisten = window.listen('tauri://resize', handleWindowResize);
103+
}
104+
65105
async function requestUserAttention_() {
66106
await windowMap[selectedWindow].minimize();
67107
await windowMap[selectedWindow].requestUserAttention(UserAttentionType.Critical);
@@ -79,6 +119,8 @@
79119
$: minWidth && minHeight ? windowMap[selectedWindow].setMinSize(new LogicalSize(minWidth, minHeight)) : windowMap[selectedWindow].setMinSize(null);
80120
$: maxWidth && maxHeight ? windowMap[selectedWindow].setMaxSize(new LogicalSize(maxWidth, maxHeight)) : windowMap[selectedWindow].setMaxSize(null);
81121
$: windowMap[selectedWindow].setPosition(new LogicalPosition(x, y));
122+
$: windowMap[selectedWindow].scaleFactor().then(factor => scaleFactor = factor);
123+
$: addWindowEventListeners(windowMap[selectedWindow]);
82124
</script>
83125

84126
<div class="flex col">
@@ -171,6 +213,56 @@
171213
</div>
172214
</div>
173215
</div>
216+
<div>
217+
<div class="flex">
218+
<div class="grow window-property">
219+
<div>Inner Size</div>
220+
<span>Width: {innerSize.width}</span>
221+
<span>Height: {innerSize.height}</span>
222+
</div>
223+
<div class="grow window-property">
224+
<div>Outer Size</div>
225+
<span>Width: {outerSize.width}</span>
226+
<span>Height: {outerSize.height}</span>
227+
</div>
228+
</div>
229+
<div class="flex">
230+
<div class="grow window-property">
231+
<div>Inner Logical Size</div>
232+
<span>Width: {innerSize.toLogical(scaleFactor).width}</span>
233+
<span>Height: {innerSize.toLogical(scaleFactor).height}</span>
234+
</div>
235+
<div class="grow window-property">
236+
<div>Outer Logical Size</div>
237+
<span>Width: {outerSize.toLogical(scaleFactor).width}</span>
238+
<span>Height: {outerSize.toLogical(scaleFactor).height}</span>
239+
</div>
240+
</div>
241+
<div class="flex">
242+
<div class="grow window-property">
243+
<div>Inner Position</div>
244+
<span>x: {innerPosition.x}</span>
245+
<span>y: {innerPosition.y}</span>
246+
</div>
247+
<div class="grow window-property">
248+
<div>Outer Position</div>
249+
<span>x: {outerPosition.x}</span>
250+
<span>y: {outerPosition.y}</span>
251+
</div>
252+
</div>
253+
<div class="flex">
254+
<div class="grow window-property">
255+
<div>Inner Logical Position</div>
256+
<span>x: {innerPosition.toLogical(scaleFactor).x}</span>
257+
<span>y: {innerPosition.toLogical(scaleFactor).y}</span>
258+
</div>
259+
<div class="grow window-property">
260+
<div>Outer Logical Position</div>
261+
<span>x: {outerPosition.toLogical(scaleFactor).x}</span>
262+
<span>y: {outerPosition.toLogical(scaleFactor).y}</span>
263+
</div>
264+
</div>
265+
</div>
174266
<form style="margin-top: 24px" on:submit|preventDefault={setTitle_}>
175267
<input id="title" bind:value={windowTitle} />
176268
<button class="button" type="submit">Set title</button>
@@ -194,4 +286,11 @@
194286
.window-controls input {
195287
width: 50px;
196288
}
289+
290+
.window-property {
291+
margin-top: 12px;
292+
}
293+
.window-property span {
294+
font-size: 0.8rem;
295+
}
197296
</style>

tooling/api/src/window.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class WindowManager extends WebviewWindowHandle {
330330

331331
/** The position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop. */
332332
async innerPosition(): Promise<PhysicalPosition> {
333-
return invokeTauriCommand({
333+
return invokeTauriCommand<{ x: number; y: number }>({
334334
__tauriModule: 'Window',
335335
message: {
336336
cmd: 'manage',
@@ -341,12 +341,12 @@ class WindowManager extends WebviewWindowHandle {
341341
}
342342
}
343343
}
344-
})
344+
}).then(({ x, y }) => new PhysicalPosition(x, y))
345345
}
346346

347347
/** The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop. */
348348
async outerPosition(): Promise<PhysicalPosition> {
349-
return invokeTauriCommand({
349+
return invokeTauriCommand<{ x: number; y: number }>({
350350
__tauriModule: 'Window',
351351
message: {
352352
cmd: 'manage',
@@ -357,15 +357,15 @@ class WindowManager extends WebviewWindowHandle {
357357
}
358358
}
359359
}
360-
})
360+
}).then(({ x, y }) => new PhysicalPosition(x, y))
361361
}
362362

363363
/**
364364
* The physical size of the window's client area.
365365
* The client area is the content of the window, excluding the title bar and borders.
366366
*/
367367
async innerSize(): Promise<PhysicalSize> {
368-
return invokeTauriCommand({
368+
return invokeTauriCommand<{ width: number; height: number }>({
369369
__tauriModule: 'Window',
370370
message: {
371371
cmd: 'manage',
@@ -376,15 +376,15 @@ class WindowManager extends WebviewWindowHandle {
376376
}
377377
}
378378
}
379-
})
379+
}).then(({ width, height }) => new PhysicalSize(width, height))
380380
}
381381

382382
/**
383383
* The physical size of the entire window.
384384
* These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
385385
*/
386386
async outerSize(): Promise<PhysicalSize> {
387-
return invokeTauriCommand({
387+
return invokeTauriCommand<{ width: number; height: number }>({
388388
__tauriModule: 'Window',
389389
message: {
390390
cmd: 'manage',
@@ -395,7 +395,7 @@ class WindowManager extends WebviewWindowHandle {
395395
}
396396
}
397397
}
398-
})
398+
}).then(({ width, height }) => new PhysicalSize(width, height))
399399
}
400400

401401
/** Gets the window's current fullscreen state. */

0 commit comments

Comments
 (0)