Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compensate for title bar height when setting bounds on BrowserView #34713

Merged
merged 1 commit into from Aug 29, 2022

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Jun 23, 2022

Description of Change

Closes #32880.

Fixes an issue where part of the BrowserView could be cut off when calling setBounds on some windows. This was happening because we were not taking the titlebar height into account when calling setBounds on a framed window, which meant that the y-value for the bounds would end up negative. For example, the following snippet:

main.js
const {app, BrowserView, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 900,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })
  mainWindow.removeMenu();
  const view = new BrowserView()
  mainWindow.setBrowserView(view)
  view.webContents.loadURL('https:// google.com')
  view.webContents.openDevTools({ mode: "bottom" });
  view.setBounds({
    x : 0,
    y : 100,
    width: 1200,
    height : 800
  });
}

app.whenReady().then(() => {
  createWindow()
  
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

Would result in the y value being calculated to -28, which is the height of the titlebar. Fix the problem by correcting for this extra height when relevant.

Visual Comparison:

Before Screen Shot 2022-06-23 at 10 50 26 AM
After Screen Shot 2022-06-23 at 10 50 13 AM
Frameless Window Screen Shot 2022-06-23 at 10 52 32 AM Comparison

Checklist

Release Notes

Notes: Fixed an issue where part of the BrowserView could be cut off when calling setBounds on some windows.

@codebytere codebytere added semver/patch backwards-compatible bug fixes target/18-x-y labels Jun 23, 2022
@electron-cation electron-cation bot added new-pr 🌱 PR opened in the last 24 hours and removed new-pr 🌱 PR opened in the last 24 hours labels Jun 23, 2022
@codebytere codebytere requested review from a team as code owners July 20, 2022 09:29
@codebytere codebytere force-pushed the fix-cut-off-devtools branch 3 times, most recently from 03a0d03 to 16f8d96 Compare August 25, 2022 17:06
@codebytere
Copy link
Member Author

Failure:

not ok 1331 shell module shell.openExternal() opens an external link
  Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\actions-runner\_work\electron\electron\src\electron\spec\api-shell-spec.ts)
      at listOnTimeout (node:internal/timers:559:17)
      at processTimers (node:internal/timers:502:7)

is woa-only and not possible affected by this macOS-only change. merging.

@codebytere codebytere merged commit 75f9573 into main Aug 29, 2022
@codebytere codebytere deleted the fix-cut-off-devtools branch August 29, 2022 15:53
@release-clerk
Copy link

release-clerk bot commented Aug 29, 2022

Release Notes Persisted

Fixed an issue where part of the BrowserView could be cut off when calling setBounds on some windows.

@trop
Copy link
Contributor

trop bot commented Aug 29, 2022

I have automatically backported this PR to "18-x-y", please check out #35493

@trop
Copy link
Contributor

trop bot commented Aug 29, 2022

I have automatically backported this PR to "19-x-y", please check out #35495

@codebytere
Copy link
Member Author

/trop run backport-to 12-x-y

@trop
Copy link
Contributor

trop bot commented Aug 30, 2022

The backport process for this PR has been manually initiated - sending your PR to 12-x-y!

@trop
Copy link
Contributor

trop bot commented Aug 30, 2022

I have automatically backported this PR to "12-x-y", please check out #35501

@codebytere
Copy link
Member Author

/trop run backport-to 21-x-y

@trop
Copy link
Contributor

trop bot commented Aug 30, 2022

The backport process for this PR has been manually initiated - sending your PR to 21-x-y!

@trop
Copy link
Contributor

trop bot commented Aug 30, 2022

I have automatically backported this PR to "21-x-y", please check out #35502

@linonetwo
Copy link

linonetwo commented Sep 5, 2022

So we need to manually add 28 to y when using framed window? And only do this in mac?

@codebytere
Copy link
Member Author

codebytere commented Sep 5, 2022

@linonetwo no - you should not need to change your existing calculations, unless you for some reason want your BrowserViews cut off inescapably.

@linonetwo
Copy link

linonetwo commented Sep 6, 2022

Then this is strange, after upgrade from to 18.2.0 to 20.1.1 , I found browserView move up 28px, so I have to minus 28 from y to compensate this:

https://github.com/tiddly-gittly/TidGi-Desktop/blob/22f79c13ed1a498bd8767eecfe571a4e8b5e187d/src/services/libs/getViewBounds.ts#L16-L34

But never mind, I found this workaround works. You can wait for other user's report to check if this is really a bug in electron side, or just my fault that I didn't realized yet.

@trop trop bot added merged/21-x-y PR was merged to the "21-x-y" branch. and removed in-flight/21-x-y labels Sep 22, 2022
@soundar-m
Copy link

I'm also seeing the same issue.

issue might be due to the title bar height being added to the y coordinate

 auto new_height =
      superview_height - bounds.y() - bounds.height() + titlebar_height;
  view.frame =
      NSMakeRect(bounds.x(), new_height, bounds.width(), bounds.height());

instead of reducing it from the view's height

  auto new_height =
      bounds.height() - titlebar_height;
  auto new_y =
      superview_height - bounds.y() - bounds.height();
  view.frame =
      NSMakeRect(bounds.x(), new_y, bounds.width(), new_height);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged/21-x-y PR was merged to the "21-x-y" branch. semver/patch backwards-compatible bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Devtools opened at bottom for browserView webcontents is not fully visible
4 participants