Skip to content

Commit bbb4f4a

Browse files
committedJun 14, 2022
Bug 1773813 - Incorporate OS zoom factor in window sizing calculations. r=tnikkel
In bug 1773342 I made OS text scale factor behave like a full zoom factor which applies to all pages (including the browser chrome). That's generally straight forward but it makes some callsites that use unzoomed CSS coordinates misbehave (or behave correctly accidentally actually in some other cases). The main fix here is making nsIBaseWindow::UnscaledDevicePixelsPerCSSPixel() and nsIScreen::GetDefaultCSSScaleFactor() account for OS zoom as necessary. However, I also went through the relevant code and cleaned it up to use typed units and operations when possible. The setup means: * nsIWidget::GetDefaultScale() doesn't account for OS full zoom. * nsIBaseWindow and nsIScreen does. These are the places where this should matter and stuff can get confused, but this works surprisingly well for all callers (except one nsDeviceContext one which we use only for PuppetWidget and we can remove by falling back to 1.0 like all other widgets until the update comes). Differential Revision: https://phabricator.services.mozilla.com/D149033
1 parent f605d9d commit bbb4f4a

20 files changed

+242
-366
lines changed
 

‎docshell/base/nsDocShell.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -4641,20 +4641,14 @@ nsDocShell::Destroy() {
46414641
return NS_OK;
46424642
}
46434643

4644-
NS_IMETHODIMP
4645-
nsDocShell::GetUnscaledDevicePixelsPerCSSPixel(double* aScale) {
4644+
double nsDocShell::GetWidgetCSSToDeviceScale() {
46464645
if (mParentWidget) {
4647-
*aScale = mParentWidget->GetDefaultScale().scale;
4648-
return NS_OK;
4646+
return mParentWidget->GetDefaultScale().scale;
46494647
}
4650-
4651-
nsCOMPtr<nsIBaseWindow> ownerWindow(do_QueryInterface(mTreeOwner));
4652-
if (ownerWindow) {
4653-
return ownerWindow->GetUnscaledDevicePixelsPerCSSPixel(aScale);
4648+
if (nsCOMPtr<nsIBaseWindow> ownerWindow = do_QueryInterface(mTreeOwner)) {
4649+
return ownerWindow->GetWidgetCSSToDeviceScale();
46544650
}
4655-
4656-
*aScale = 1.0;
4657-
return NS_OK;
4651+
return 1.0;
46584652
}
46594653

46604654
NS_IMETHODIMP

‎docshell/base/nsDocShellTreeOwner.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,8 @@ nsDocShellTreeOwner::Destroy() {
552552
return NS_ERROR_NULL_POINTER;
553553
}
554554

555-
NS_IMETHODIMP
556-
nsDocShellTreeOwner::GetUnscaledDevicePixelsPerCSSPixel(double* aScale) {
557-
if (mWebBrowser) {
558-
return mWebBrowser->GetUnscaledDevicePixelsPerCSSPixel(aScale);
559-
}
560-
561-
*aScale = 1.0;
562-
return NS_OK;
555+
double nsDocShellTreeOwner::GetWidgetCSSToDeviceScale() {
556+
return mWebBrowser ? mWebBrowser->GetWidgetCSSToDeviceScale() : 1.0;
563557
}
564558

565559
NS_IMETHODIMP

‎dom/base/nsGlobalWindowInner.cpp

+15-30
Original file line numberDiff line numberDiff line change
@@ -7518,53 +7518,37 @@ void nsGlobalWindowInner::SetReplaceableWindowCoord(
75187518
if (innerWidthSpecified || innerHeightSpecified || outerWidthSpecified ||
75197519
outerHeightSpecified) {
75207520
nsCOMPtr<nsIBaseWindow> treeOwnerAsWin = outer->GetTreeOwnerWindow();
7521-
nsCOMPtr<nsIScreen> screen;
75227521
nsCOMPtr<nsIScreenManager> screenMgr(
75237522
do_GetService("@mozilla.org/gfx/screenmanager;1"));
7524-
int32_t winLeft = 0;
7525-
int32_t winTop = 0;
7526-
int32_t winWidth = 0;
7527-
int32_t winHeight = 0;
7528-
double scale = 1.0;
75297523

75307524
if (treeOwnerAsWin && screenMgr) {
75317525
// Acquire current window size.
7532-
treeOwnerAsWin->GetUnscaledDevicePixelsPerCSSPixel(&scale);
7533-
treeOwnerAsWin->GetPositionAndSize(&winLeft, &winTop, &winWidth,
7534-
&winHeight);
7535-
winLeft = NSToIntRound(winHeight / scale);
7536-
winTop = NSToIntRound(winWidth / scale);
7537-
winWidth = NSToIntRound(winWidth / scale);
7538-
winHeight = NSToIntRound(winHeight / scale);
7526+
//
7527+
// FIXME: This needs to account for full zoom like the outer window code
7528+
// does! Ideally move there?
7529+
auto cssScale = treeOwnerAsWin->UnscaledDevicePixelsPerCSSPixel();
7530+
LayoutDeviceIntRect devWinRect = treeOwnerAsWin->GetPositionAndSize();
7531+
CSSIntRect cssWinRect = RoundedToInt(devWinRect / cssScale);
75397532

75407533
// Acquire content window size.
75417534
CSSSize contentSize;
75427535
outer->GetInnerSize(contentSize);
75437536

7544-
screenMgr->ScreenForRect(winLeft, winTop, winWidth, winHeight,
7545-
getter_AddRefs(screen));
7546-
7537+
nsCOMPtr<nsIScreen> screen = screenMgr->ScreenForRect(RoundedToInt(
7538+
devWinRect / treeOwnerAsWin->DevicePixelsPerDesktopPixel()));
75477539
if (screen) {
75487540
int32_t roundedValue = std::round(value);
75497541
int32_t* targetContentWidth = nullptr;
75507542
int32_t* targetContentHeight = nullptr;
7551-
int32_t screenWidth = 0;
7552-
int32_t screenHeight = 0;
7553-
int32_t chromeWidth = 0;
7554-
int32_t chromeHeight = 0;
75557543
int32_t inputWidth = 0;
75567544
int32_t inputHeight = 0;
75577545
int32_t unused = 0;
75587546

7559-
// Get screen dimensions (in device pixels)
7560-
screen->GetAvailRect(&unused, &unused, &screenWidth, &screenHeight);
7561-
// Convert them to CSS pixels
7562-
screenWidth = NSToIntRound(screenWidth / scale);
7563-
screenHeight = NSToIntRound(screenHeight / scale);
7547+
CSSIntSize availScreenSize =
7548+
RoundedToInt(screen->GetAvailRect().Size() / cssScale);
75647549

75657550
// Calculate the chrome UI size.
7566-
chromeWidth = winWidth - contentSize.width;
7567-
chromeHeight = winHeight - contentSize.height;
7551+
CSSIntSize chromeSize = cssWinRect.Size() - RoundedToInt(contentSize);
75687552

75697553
if (innerWidthSpecified || outerWidthSpecified) {
75707554
inputWidth = value;
@@ -7577,9 +7561,10 @@ void nsGlobalWindowInner::SetReplaceableWindowCoord(
75777561
}
75787562

75797563
nsContentUtils::CalcRoundedWindowSizeForResistingFingerprinting(
7580-
chromeWidth, chromeHeight, screenWidth, screenHeight, inputWidth,
7581-
inputHeight, outerWidthSpecified, outerHeightSpecified,
7582-
targetContentWidth, targetContentHeight);
7564+
chromeSize.width, chromeSize.height, availScreenSize.width,
7565+
availScreenSize.height, inputWidth, inputHeight,
7566+
outerWidthSpecified, outerHeightSpecified, targetContentWidth,
7567+
targetContentHeight);
75837568
value = T(roundedValue);
75847569
}
75857570
}

0 commit comments

Comments
 (0)
Failed to load comments.