Skip to content

Commit 2dde656

Browse files
committed
Fixed issue where coordiantes were incorrect after resize event.
1 parent 20bdf53 commit 2dde656

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/services/browser/BrowserSession.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,11 +828,34 @@ export class BrowserSession {
828828

829829
/**
830830
* Returns the last known viewport size (if any)
831+
*
832+
* Prefer the live page viewport when available so we stay accurate after:
833+
* - browser_action resize
834+
* - manual window resizes (especially with remote browsers)
835+
*
836+
* Falls back to the configured default viewport when no prior information exists.
831837
*/
832838
getViewportSize(): { width?: number; height?: number } {
833-
return {
834-
width: this.lastViewportWidth,
835-
height: this.lastViewportHeight,
839+
// If we have an active page, ask Puppeteer for the current viewport.
840+
// This keeps us in sync with any resizes that happen outside of our own
841+
// browser_action lifecycle (e.g. user dragging the window).
842+
if (this.page) {
843+
const vp = this.page.viewport()
844+
if (vp?.width) this.lastViewportWidth = vp.width
845+
if (vp?.height) this.lastViewportHeight = vp.height
846+
}
847+
848+
// If we've ever observed a viewport, use that.
849+
if (this.lastViewportWidth && this.lastViewportHeight) {
850+
return {
851+
width: this.lastViewportWidth,
852+
height: this.lastViewportHeight,
853+
}
836854
}
855+
856+
// Otherwise fall back to the configured default so the tool can still
857+
// operate before the first screenshot-based action has run.
858+
const { width, height } = this.getViewport()
859+
return { width, height }
837860
}
838861
}

src/services/browser/__tests__/BrowserSession.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,61 @@ describe("cursor visualization", () => {
394394
// Verify no cursor position in result
395395
expect(result.currentMousePosition).toBeUndefined()
396396
})
397+
398+
describe("getViewportSize", () => {
399+
it("falls back to configured viewport when no page or last viewport is available", () => {
400+
const localCtx: any = {
401+
globalState: {
402+
get: vi.fn((key: string) => {
403+
if (key === "browserViewportSize") return "1024x768"
404+
return undefined
405+
}),
406+
update: vi.fn(),
407+
},
408+
globalStorageUri: { fsPath: "/mock/global/storage/path" },
409+
extensionUri: { fsPath: "/mock/extension/path" },
410+
}
411+
412+
const session = new BrowserSession(localCtx)
413+
const vp = (session as any).getViewportSize()
414+
expect(vp).toEqual({ width: 1024, height: 768 })
415+
})
416+
417+
it("returns live page viewport when available and updates lastViewport cache", () => {
418+
const localCtx: any = {
419+
globalState: {
420+
get: vi.fn(),
421+
update: vi.fn(),
422+
},
423+
globalStorageUri: { fsPath: "/mock/global/storage/path" },
424+
extensionUri: { fsPath: "/mock/extension/path" },
425+
}
426+
const session = new BrowserSession(localCtx)
427+
;(session as any).page = {
428+
viewport: vi.fn().mockReturnValue({ width: 1111, height: 555 }),
429+
}
430+
431+
const vp = (session as any).getViewportSize()
432+
expect(vp).toEqual({ width: 1111, height: 555 })
433+
expect((session as any).lastViewportWidth).toBe(1111)
434+
expect((session as any).lastViewportHeight).toBe(555)
435+
})
436+
437+
it("returns cached last viewport when page no longer exists", () => {
438+
const localCtx: any = {
439+
globalState: {
440+
get: vi.fn(),
441+
update: vi.fn(),
442+
},
443+
globalStorageUri: { fsPath: "/mock/global/storage/path" },
444+
extensionUri: { fsPath: "/mock/extension/path" },
445+
}
446+
const session = new BrowserSession(localCtx)
447+
;(session as any).lastViewportWidth = 800
448+
;(session as any).lastViewportHeight = 600
449+
450+
const vp = (session as any).getViewportSize()
451+
expect(vp).toEqual({ width: 800, height: 600 })
452+
})
453+
})
397454
})

0 commit comments

Comments
 (0)