Bug 1775797 - Make resizers use frame-relative coordinates. r=TYLin

We no longer use the same code to resize windows and so (bug 1694061),
so keeping screen-relative coordinates is not really needed.

Instead, keep the size of the resized element around and do the math in
frame-relative coordinates, which is better behavior in presence of CSS
transforms.

Differential Revision: https://phabricator.services.mozilla.com/D150058
This commit is contained in:
Emilio Cobos Álvarez 2022-06-28 08:33:06 +00:00
parent babf6c7e30
commit 575ef5e7bd
4 changed files with 107 additions and 84 deletions

View File

@ -15,6 +15,7 @@
#include "nsContentUtils.h"
#include "nsICSSDeclaration.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include "nsStyledElement.h"
@ -127,22 +128,15 @@ void XULResizerElement::PostHandleEventInternal(
break;
}
// cache the content rectangle for the frame to resize
// GetScreenRectInAppUnits returns the border box rectangle, so
// adjust to get the desired content rectangle.
nsRect rect = frame->GetScreenRectInAppUnits();
if (frame->StylePosition()->mBoxSizing == StyleBoxSizing::Content) {
rect.Deflate(frame->GetUsedBorderAndPadding());
}
mMouseDownRect = LayoutDeviceIntRect::FromAppUnitsToNearest(
rect, frame->PresContext()->AppUnitsPerDevPixel());
mMouseDownSize =
frame->StylePosition()->mBoxSizing == StyleBoxSizing::Content
? frame->GetContentRect().Size()
: frame->GetRect().Size();
// remember current mouse coordinates
LayoutDeviceIntPoint refPoint;
if (!GetEventPoint(guiEvent, refPoint)) {
if (!GetEventPoint(guiEvent, mMouseDownPoint)) {
break;
}
mMouseDownPoint = refPoint + guiEvent->mWidget->WidgetToScreenOffset();
mTrackingMouseMove = true;
PresShell::SetCapturingContent(this, CaptureFlags::IgnoreAllowedState);
doDefault = false;
@ -171,41 +165,52 @@ void XULResizerElement::PostHandleEventInternal(
if (!GetEventPoint(guiEvent, refPoint)) {
break;
}
LayoutDeviceIntPoint screenPoint =
refPoint + guiEvent->mWidget->WidgetToScreenOffset();
LayoutDeviceIntPoint mouseMove(screenPoint - mMouseDownPoint);
const nsPoint oldPos = nsLayoutUtils::GetEventCoordinatesRelativeTo(
guiEvent->mWidget, mMouseDownPoint, RelativeTo{frame});
const nsPoint newPos = nsLayoutUtils::GetEventCoordinatesRelativeTo(
guiEvent->mWidget, refPoint, RelativeTo{frame});
nsPoint mouseMove(newPos - oldPos);
// Determine which direction to resize by checking the dir attribute.
// For windows and menus, ensure that it can be resized in that
// direction.
Direction direction = GetDirection();
LayoutDeviceIntRect rect = mMouseDownRect;
nsSize newSize = mMouseDownSize;
// Check if there are any size constraints on this window.
AdjustDimensions(&rect.x, &rect.width, mouseMove.x,
direction.mHorizontal);
AdjustDimensions(&rect.y, &rect.height, mouseMove.y,
direction.mVertical);
newSize.width += direction.mHorizontal * mouseMove.x;
newSize.height += direction.mVertical * mouseMove.y;
if (newSize.width < AppUnitsPerCSSPixel() && mouseMove.x != 0) {
newSize.width = AppUnitsPerCSSPixel();
}
if (newSize.height < AppUnitsPerCSSPixel() && mouseMove.y != 0) {
newSize.height = AppUnitsPerCSSPixel();
}
// convert the rectangle into css pixels. When changing the size in a
// direction, don't allow the new size to be less that the resizer's
// size. This ensures that content isn't resized too small as to make
// the resizer invisible.
nsRect appUnitsRect = ToAppUnits(
rect.ToUnknownRect(), frame->PresContext()->AppUnitsPerDevPixel());
if (auto* resizerFrame = GetPrimaryFrame()) {
nsRect frameRect = resizerFrame->GetRect();
if (appUnitsRect.width < frameRect.width && mouseMove.x)
appUnitsRect.width = frameRect.width;
if (appUnitsRect.height < frameRect.height && mouseMove.y)
appUnitsRect.height = frameRect.height;
nsRect resizerRect = resizerFrame->GetRect();
if (newSize.width < resizerRect.width && mouseMove.x != 0) {
newSize.width = resizerRect.width;
}
if (newSize.height < resizerRect.height && mouseMove.y != 0) {
newSize.height = resizerRect.height;
}
}
nsIntRect cssRect = appUnitsRect.ToInsidePixels(AppUnitsPerCSSPixel());
auto cssSize = CSSIntSize::FromAppUnitsRounded(newSize);
SizeInfo sizeInfo, originalSizeInfo;
sizeInfo.width.AppendInt(cssRect.width);
sizeInfo.height.AppendInt(cssRect.height);
sizeInfo.width.AppendInt(cssSize.width);
sizeInfo.height.AppendInt(cssSize.height);
ResizeContent(contentToResize, direction, sizeInfo, &originalSizeInfo);
MaybePersistOriginalSize(contentToResize, originalSizeInfo);
@ -261,55 +266,37 @@ nsIContent* XULResizerElement::GetContentToResize() const {
return parent ? parent->FindFirstNonChromeOnlyAccessContent() : nullptr;
}
/* static */
void XULResizerElement::AdjustDimensions(int32_t* aPos, int32_t* aSize,
int32_t aMovement,
int8_t aResizerDirection) {
int32_t oldSize = *aSize;
*aSize += aResizerDirection * aMovement;
// use one as a minimum size or the element could disappear
if (*aSize < 1) {
*aSize = 1;
}
// For left and top resizers, the window must be moved left by the same
// amount that the window was resized.
if (aResizerDirection == -1) {
*aPos += oldSize - *aSize;
}
}
/* static */
void XULResizerElement::ResizeContent(nsIContent* aContent,
const Direction& aDirection,
const SizeInfo& aSizeInfo,
SizeInfo* aOriginalSizeInfo) {
if (RefPtr<nsStyledElement> inlineStyleContent =
nsStyledElement::FromNode(aContent)) {
nsICSSDeclaration* decl = inlineStyleContent->Style();
RefPtr inlineStyleContent = nsStyledElement::FromNode(aContent);
if (!inlineStyleContent) {
return;
}
nsCOMPtr<nsICSSDeclaration> decl = inlineStyleContent->Style();
if (aOriginalSizeInfo) {
decl->GetPropertyValue("width"_ns, aOriginalSizeInfo->width);
decl->GetPropertyValue("height"_ns, aOriginalSizeInfo->height);
}
if (aOriginalSizeInfo) {
decl->GetPropertyValue("width"_ns, aOriginalSizeInfo->width);
decl->GetPropertyValue("height"_ns, aOriginalSizeInfo->height);
// only set the property if the element could have changed in that
// direction
if (aDirection.mHorizontal) {
nsAutoCString widthstr(aSizeInfo.width);
if (!widthstr.IsEmpty() && !StringEndsWith(widthstr, "px"_ns)) {
widthstr.AppendLiteral("px");
}
decl->SetProperty("width"_ns, widthstr, ""_ns, IgnoreErrors());
}
// only set the property if the element could have changed in that
// direction
if (aDirection.mHorizontal) {
nsAutoCString widthstr(aSizeInfo.width);
if (!widthstr.IsEmpty() &&
!Substring(widthstr, widthstr.Length() - 2, 2).EqualsLiteral("px"))
widthstr.AppendLiteral("px");
decl->SetProperty("width"_ns, widthstr, ""_ns, IgnoreErrors());
}
if (aDirection.mVertical) {
nsAutoCString heightstr(aSizeInfo.height);
if (!heightstr.IsEmpty() &&
!Substring(heightstr, heightstr.Length() - 2, 2).EqualsLiteral("px"))
heightstr.AppendLiteral("px");
decl->SetProperty("height"_ns, heightstr, ""_ns, IgnoreErrors());
if (aDirection.mVertical) {
nsAutoCString heightstr(aSizeInfo.height);
if (!heightstr.IsEmpty() && !StringEndsWith(heightstr, "px"_ns)) {
heightstr.AppendLiteral("px");
}
decl->SetProperty("height"_ns, heightstr, ""_ns, IgnoreErrors());
}
}

View File

@ -38,19 +38,6 @@ class XULResizerElement final : public nsXULElement {
nsIContent* GetContentToResize() const;
/**
* Adjust the window position and size in a direction according to the mouse
* movement and the resizer direction. The minimum and maximum size is used
* to constrain the size.
*
* @param aPos left or top position
* @param aSize width or height
* @param aMovement the amount the mouse was moved
* @param aResizerDirection resizer direction returned by GetDirection
*/
static void AdjustDimensions(int32_t* aPos, int32_t* aSize, int32_t aMovement,
int8_t aResizerDirection);
struct SizeInfo {
nsCString width, height;
};
@ -63,7 +50,7 @@ class XULResizerElement final : public nsXULElement {
const SizeInfo& aSizeInfo);
static void RestoreOriginalSize(nsIContent* aContent);
LayoutDeviceIntRect mMouseDownRect;
nsSize mMouseDownSize;
LayoutDeviceIntPoint mMouseDownPoint;
bool mTrackingMouseMove = false;
};

View File

@ -341,6 +341,11 @@ struct CSSPixel {
aRect.ToNearestPixels(AppUnitsPerCSSPixel()));
}
static CSSIntRect FromAppUnitsToInside(const nsRect& aRect) {
return CSSIntRect::FromUnknownRect(
aRect.ToInsidePixels(AppUnitsPerCSSPixel()));
}
// Conversions to app units
static nscoord ToAppUnits(CSSCoord aCoord) {

View File

@ -0,0 +1,44 @@
<!doctype html>
<meta charset=utf-8>
<title>Resizer should account for transforms to decide resize direction</title>
<link rel=author href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez">
<link rel=author href="https://mozilla.org" title="Mozilla">
<link rel=help href="https://bugzilla.mozilla.org/show_bug.cgi?id=1775797">
<style>
#resizeme {
position: absolute;
top: 200px;
left: 200px;
width: 100px;
height: 100px;
overflow: hidden;
resize: both;
background-color: green;
transform-origin: 0 0;
transform: rotate(90deg);
}
</style>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<div id="resizeme"></div>
<script>
promise_test(async function test() {
let element = document.getElementById("resizeme");
let rect = element.getBoundingClientRect();
// Due to the rotation, the resizer should be at the bottom left.
await new test_driver.Actions()
.pointerMove(rect.left + 1, rect.bottom - 1)
.pointerDown()
.pointerMove(rect.left + 1, rect.bottom + 50)
.pointerUp()
.send();
// We should've made the element wider due to the rotation.
assert_greater_than(parseInt(getComputedStyle(element).width, 10), 100, "Element should be wider");
assert_equals(parseInt(getComputedStyle(element).height, 10), 100, "Element should have the same height");
});
</script>