Skip to content

Commit 9c4624c

Browse files
committed
consistent viewport
1 parent 5e6a8ec commit 9c4624c

1 file changed

Lines changed: 45 additions & 16 deletions

File tree

src/main/session.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,32 @@ export class SessionManager {
2525
this.mainWindow = window;
2626
}
2727

28+
// Store current viewport dimensions for applying to new tabs
29+
private currentViewportWidth: number = 1440;
30+
private currentViewportHeight: number = 900;
31+
2832
async updateViewport(width: number, height: number): Promise<void> {
33+
// Store dimensions for new tabs
34+
this.currentViewportWidth = width;
35+
this.currentViewportHeight = height;
36+
2937
const activePage = this.getActivePage();
3038
if (!activePage) {
3139
console.log("[Viewport] No active page to update viewport");
3240
return;
3341
}
3442

43+
await this.applyViewportToPage(activePage, width, height);
44+
}
45+
46+
private async applyViewportToPage(page: Page, width: number, height: number): Promise<void> {
3547
try {
36-
// Get or create CDP session for the active page
37-
let cdp = this.cdpSession;
38-
if (!cdp) {
39-
cdp = await activePage.context().newCDPSession(activePage);
40-
this.cdpSession = cdp;
41-
}
48+
// Always create a new CDP session for the specific page
49+
const cdp = await page.context().newCDPSession(page);
4250

4351
const deviceScaleFactor = process.platform === "darwin" ? 2 : 1;
4452

45-
console.log(`[Viewport] Updating viewport to ${width}x${height} (scale: ${deviceScaleFactor})`);
53+
console.log(`[Viewport] Applying viewport ${width}x${height} (scale: ${deviceScaleFactor}) to page: ${page.url()}`);
4654

4755
await cdp.send("Emulation.setDeviceMetricsOverride", {
4856
width,
@@ -51,11 +59,18 @@ export class SessionManager {
5159
mobile: false,
5260
});
5361

54-
console.log("[Viewport] Viewport updated successfully");
62+
console.log("[Viewport] Viewport applied successfully");
5563
} catch (error) {
56-
console.error("[Viewport] Failed to update viewport:", error);
57-
// Try to recreate CDP session if it failed
58-
this.cdpSession = null;
64+
console.error("[Viewport] Failed to apply viewport:", error);
65+
}
66+
}
67+
68+
async applyViewportToAllPages(): Promise<void> {
69+
if (!this.context) return;
70+
71+
const pages = this.context.pages();
72+
for (const page of pages) {
73+
await this.applyViewportToPage(page, this.currentViewportWidth, this.currentViewportHeight);
5974
}
6075
}
6176

@@ -66,21 +81,27 @@ export class SessionManager {
6681
const windowBounds = this.mainWindow?.getContentBounds();
6782
const chromeUIHeight = 76; // 36 (tab bar) + 40 (nav bar)
6883

69-
// Use a larger default viewport for better desktop experience
70-
const defaultWidth = 1728;
71-
const defaultHeight = 1440;
84+
// Use actual window size, with reasonable defaults
85+
const defaultWidth = 1440;
86+
const defaultHeight = 900;
7287

7388
const contentWidth = windowBounds?.width || defaultWidth;
7489
const contentHeight = windowBounds
75-
? Math.max(600, windowBounds.height - chromeUIHeight)
90+
? Math.max(400, windowBounds.height - chromeUIHeight)
7691
: defaultHeight - chromeUIHeight;
7792

93+
// Store for later use with new tabs
94+
this.currentViewportWidth = contentWidth;
95+
this.currentViewportHeight = contentHeight;
96+
97+
// For session creation, use larger viewport to avoid issues - we'll override with CDP later
7898
const viewportWidth = Math.max(2560, contentWidth);
7999
const viewportHeight = Math.max(1440, contentHeight);
80100

81101
console.log("Window content bounds:", windowBounds);
82102
console.log(`Chrome UI height: ${chromeUIHeight}px`);
83-
console.log(`Creating session with viewport: ${viewportWidth}x${viewportHeight}`);
103+
console.log(`Actual viewport to use: ${contentWidth}x${contentHeight}`);
104+
console.log(`Creating session with initial viewport: ${viewportWidth}x${viewportHeight} (will override with CDP)`);
84105

85106
// Create a new Browserbase session with viewport matching our window
86107
// Use deviceScaleFactor of 2 for Retina displays
@@ -159,6 +180,10 @@ export class SessionManager {
159180
const defaultUrl = process.env.BROWSERBASE_DEFAULT_URL || "https://www.google.com";
160181
await this.navigateTo(defaultUrl);
161182

183+
// Apply viewport override via CDP to match actual window size
184+
console.log(`[Viewport] Applying initial viewport: ${this.currentViewportWidth}x${this.currentViewportHeight}`);
185+
await this.applyViewportToAllPages();
186+
162187
} catch (error) {
163188
console.error("Failed to connect to browser:", error);
164189
throw error;
@@ -351,6 +376,10 @@ export class SessionManager {
351376
const pages = this.context.pages();
352377
this.activeTabIndex = pages.indexOf(newPage);
353378

379+
// Apply viewport to the new tab
380+
console.log(`[Viewport] Applying viewport to new tab: ${this.currentViewportWidth}x${this.currentViewportHeight}`);
381+
await this.applyViewportToPage(newPage, this.currentViewportWidth, this.currentViewportHeight);
382+
354383
// Navigate to Google like the initial tab
355384
const defaultUrl = process.env.BROWSERBASE_DEFAULT_URL || "https://www.google.com";
356385
await newPage.goto(defaultUrl, { waitUntil: "domcontentloaded", timeout: 60000 });

0 commit comments

Comments
 (0)