Skip to content

Commit 9638dd2

Browse files
committed
Remove sleep before positioning window
For whatever reason this breaks window positioning on mac Issue #283
1 parent 825d606 commit 9638dd2

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

src/index.tsx

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,42 @@ function setupWebEvents(config: Config) {
9898
});
9999
}
100100

101+
async function restoreWindow(config: Config) {
102+
const positionWindow = async function () {
103+
const pos = config.values.app.window.position;
104+
if (pos?.length === 2 && pos?.[0] > -32000 && pos?.[1] > -32000) {
105+
await appWindow.setPosition(pos);
106+
} else {
107+
await appWindow.center();
108+
}
109+
};
110+
111+
const sizeCheck = async function () {
112+
const sz = await appWindow.getSize();
113+
const delta = [sz.width - size[0], sz.height - size[1]];
114+
if (delta[0] != 0 || delta[1] != 0) {
115+
console.log("Window size delta", delta);
116+
await appWindow.setSize([size[0] - delta[0], size[1] - delta[1]]);
117+
}
118+
};
119+
120+
const sleep20ms = async function () {
121+
await new Promise<void>((r) => setTimeout(r, 20));
122+
};
123+
124+
const size = config.values.app.window.size;
125+
126+
if (size.length === 2 && size[0] > 100 && size[1] > 100) {
127+
await appWindow.setSize(size);
128+
129+
// Run the size verification a bit later to allow the window system to apply the size
130+
// (on some platforms the immediate getSize() after setSize() returns the old value).
131+
void sleep20ms().then(sizeCheck);
132+
}
133+
134+
void positionWindow();
135+
}
136+
101137
function Loader() {
102138
const config = useContext(ConfigContext);
103139

@@ -139,42 +175,7 @@ async function run(config: Config) {
139175
void invoke("create_tray");
140176
}
141177

142-
const positionWindow = async function () {
143-
const pos = config.values.app.window.position;
144-
if (pos?.length === 2 && pos?.[0] > -32000 && pos?.[1] > -32000) {
145-
await appWindow.setPosition(pos);
146-
} else {
147-
await appWindow.center();
148-
}
149-
};
150-
151-
const sizeCheck = async function () {
152-
const sz = await appWindow.getSize();
153-
const delta = [sz.width - size[0], sz.height - size[1]];
154-
if (delta[0] != 0 || delta[1] != 0) {
155-
console.log("Window size delta", delta);
156-
await appWindow.setSize([size[0] - delta[0], size[1] - delta[1]]);
157-
}
158-
};
159-
160-
const sleep20ms = async function () {
161-
await new Promise<void>((r) => setTimeout(r, 20));
162-
};
163-
164-
const size = config.values.app.window.size;
165-
166-
if (size.length === 2 && size[0] > 100 && size[1] > 100) {
167-
await appWindow.setSize(size);
168-
169-
// Run the size verification a bit later to allow the window system to apply the size
170-
// (on some platforms the immediate getSize() after setSize() returns the old value).
171-
void sleep20ms()
172-
.then(sizeCheck)
173-
.then(sleep20ms)
174-
.then(positionWindow);
175-
} else {
176-
void positionWindow();
177-
}
178+
await restoreWindow(config);
178179
} else {
179180
setupWebEvents(config);
180181
}

0 commit comments

Comments
 (0)