Skip to content

Commit cab7d3c

Browse files
committed
fix resize logic
1 parent c566873 commit cab7d3c

File tree

4 files changed

+12
-48
lines changed

4 files changed

+12
-48
lines changed

examples/simple-server/src/ui-raw.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,9 @@ window.addEventListener("load", async () => {
131131
});
132132

133133
new ResizeObserver(() => {
134-
const { body, documentElement: html } = document;
135-
136-
const bodyStyle = getComputedStyle(body);
137-
const htmlStyle = getComputedStyle(html);
138-
139-
const width = body.scrollWidth;
140-
const height =
141-
body.scrollHeight +
142-
(parseFloat(bodyStyle.borderTop) || 0) +
143-
(parseFloat(bodyStyle.borderBottom) || 0) +
144-
(parseFloat(htmlStyle.borderTop) || 0) +
145-
(parseFloat(htmlStyle.borderBottom) || 0);
146-
134+
const rect = (document.body.parentElement ?? document.body).getBoundingClientRect();
135+
const width = Math.ceil(rect.width);
136+
const height = Math.ceil(rect.height);
147137
app.sendNotification<McpUiSizeChangeNotification>({
148138
method: "ui/notifications/size-change",
149139
params: { width, height },

examples/simple-server/src/ui-vanilla.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ window.addEventListener("load", async () => {
4848
appendText(`Host context changed: ${JSON.stringify(params)}`);
4949
};
5050

51-
document.body.addEventListener("resize", () => {
52-
app.sendSizeChange({
53-
width: document.body.scrollWidth,
54-
height: document.body.scrollHeight,
55-
});
56-
});
57-
5851
root.appendChild(
5952
Object.assign(document.createElement("button"), {
6053
textContent: "Get Weather (Tool)",

src/app-bridge.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,6 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
183183
};
184184
}
185185

186-
sendSizeChange(params: McpUiSizeChangeNotification["params"]) {
187-
return this.notification(<McpUiSizeChangeNotification>{
188-
method: "ui/notifications/size-change",
189-
params,
190-
});
191-
}
192-
193186
sendToolInput(params: McpUiToolInputNotification["params"]) {
194187
return this.notification(<McpUiToolInputNotification>{
195188
method: "ui/notifications/tool-input",

src/app.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,18 @@ export class App extends Protocol<Request, Notification, Result> {
166166
}
167167

168168
setupSizeChangeNotifications() {
169+
let scheduled = false;
169170
const sendBodySizeChange = () => {
170-
let rafId: number | null = null;
171-
172-
// Debounce using requestAnimationFrame to avoid duplicate messages
173-
// when both documentElement and body fire resize events
174-
if (rafId !== null) {
175-
cancelAnimationFrame(rafId);
171+
if (scheduled) {
172+
return;
176173
}
177-
rafId = requestAnimationFrame(() => {
178-
const { body, documentElement: html } = document;
179-
180-
const bodyStyle = getComputedStyle(body);
181-
const htmlStyle = getComputedStyle(html);
182-
183-
const width = body.scrollWidth;
184-
const height =
185-
body.scrollHeight +
186-
(parseFloat(bodyStyle.borderTop) || 0) +
187-
(parseFloat(bodyStyle.borderBottom) || 0) +
188-
(parseFloat(htmlStyle.borderTop) || 0) +
189-
(parseFloat(htmlStyle.borderBottom) || 0);
190-
174+
scheduled = true;
175+
requestAnimationFrame(() => {
176+
scheduled = false;
177+
const rect = (document.body.parentElement ?? document.body).getBoundingClientRect();
178+
const width = Math.ceil(rect.width);
179+
const height = Math.ceil(rect.height);
191180
this.sendSizeChange({ width, height });
192-
rafId = null;
193181
});
194182
};
195183

0 commit comments

Comments
 (0)