Skip to content

Commit

Permalink
Bug 1809430 - Clean up ClientToWindowRect. r=cmartin,mstange
Browse files Browse the repository at this point in the history
Make it return a margin from client area to window area, and add an
explicit function to get the size difference.

No behavior change.

Depends on D166428

Differential Revision: https://phabricator.services.mozilla.com/D166431
  • Loading branch information
emilio committed Jan 26, 2023
1 parent 3049c0c commit 95520ca
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 49 deletions.
23 changes: 13 additions & 10 deletions layout/generic/nsContainerFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,16 +872,19 @@ void nsContainerFrame::SetSizeConstraints(nsPresContext* aPresContext,
// The sizes are in inner window sizes, so convert them into outer window
// sizes. Use a size of (200, 200) as only the difference between the inner
// and outer size is needed.
LayoutDeviceIntSize windowSize =
aWidget->ClientToWindowSize(LayoutDeviceIntSize(200, 200));
if (constraints.mMinSize.width)
constraints.mMinSize.width += windowSize.width - 200;
if (constraints.mMinSize.height)
constraints.mMinSize.height += windowSize.height - 200;
if (constraints.mMaxSize.width != NS_MAXSIZE)
constraints.mMaxSize.width += windowSize.width - 200;
if (constraints.mMaxSize.height != NS_MAXSIZE)
constraints.mMaxSize.height += windowSize.height - 200;
const LayoutDeviceIntSize sizeDiff = aWidget->ClientToWindowSizeDifference();
if (constraints.mMinSize.width) {
constraints.mMinSize.width += sizeDiff.width;
}
if (constraints.mMinSize.height) {
constraints.mMinSize.height += sizeDiff.height;
}
if (constraints.mMaxSize.width != NS_MAXSIZE) {
constraints.mMaxSize.width += sizeDiff.width;
}
if (constraints.mMaxSize.height != NS_MAXSIZE) {
constraints.mMaxSize.height += sizeDiff.height;
}

aWidget->SetSizeConstraints(constraints);
}
Expand Down
2 changes: 1 addition & 1 deletion widget/cocoa/nsCocoaWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class nsCocoaWindow final : public nsBaseWidget, public nsPIWidgetCocoa {
virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
virtual LayoutDeviceIntPoint GetClientOffset() override;
virtual LayoutDeviceIntSize ClientToWindowSize(const LayoutDeviceIntSize& aClientSize) override;
virtual LayoutDeviceIntMargin ClientToWindowMargin() override;

virtual void* GetNativeData(uint32_t aDataType) override;

Expand Down
17 changes: 9 additions & 8 deletions widget/cocoa/nsCocoaWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2226,22 +2226,23 @@ static nsSizeMode GetWindowSizeMode(NSWindow* aWindow, bool aFullScreen) {
NS_OBJC_END_TRY_BLOCK_RETURN(LayoutDeviceIntPoint(0, 0));
}

LayoutDeviceIntSize nsCocoaWindow::ClientToWindowSize(const LayoutDeviceIntSize& aClientSize) {
LayoutDeviceIntMargin nsCocoaWindow::ClientToWindowMargin() {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

if (!mWindow || mWindow.drawsContentsIntoWindowFrame || mWindowType == eWindowType_popup) {
if (!mWindow || mWindow.drawsContentsIntoWindowFrame || mWindowType == WindowType::Popup) {
return {};
}

NSRect clientNSRect = mWindow.contentLayoutRect;
NSRect frameNSRect = [mWindow frameRectForChildViewRect:clientNSRect];

CGFloat backingScale = BackingScaleFactor();
LayoutDeviceIntRect r(0, 0, aClientSize.width, aClientSize.height);
NSRect rect = nsCocoaUtils::DevPixelsToCocoaPoints(r, backingScale);
const auto clientRect = nsCocoaUtils::CocoaRectToGeckoRectDevPix(clientNSRect, backingScale);
const auto frameRect = nsCocoaUtils::CocoaRectToGeckoRectDevPix(frameNSRect, backingScale);

NSRect maybeInflatedRect = [mWindow frameRectForChildViewRect:rect];
r = nsCocoaUtils::CocoaRectToGeckoRectDevPix(maybeInflatedRect, backingScale);
return r.Size();
return frameRect - clientRect;

NS_OBJC_END_TRY_BLOCK_RETURN(LayoutDeviceIntSize(0, 0));
NS_OBJC_END_TRY_BLOCK_RETURN({});
}

nsMenuBarX* nsCocoaWindow::GetMenuBar() { return mMenuBar; }
Expand Down
9 changes: 9 additions & 0 deletions widget/nsBaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,15 @@ nsIntSize nsIWidget::CustomCursorSize(const Cursor& aCursor) {
return {width, height};
}

LayoutDeviceIntSize nsIWidget::ClientToWindowSizeDifference() {
auto margin = ClientToWindowMargin();
MOZ_ASSERT(margin.top >= 0, "Window should be bigger than client area");
MOZ_ASSERT(margin.left >= 0, "Window should be bigger than client area");
MOZ_ASSERT(margin.right >= 0, "Window should be bigger than client area");
MOZ_ASSERT(margin.bottom >= 0, "Window should be bigger than client area");
return {margin.LeftRight(), margin.TopBottom()};
}

RefPtr<mozilla::VsyncDispatcher> nsIWidget::GetVsyncDispatcher() {
return nullptr;
}
Expand Down
5 changes: 0 additions & 5 deletions widget/nsBaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,6 @@ class nsBaseWidget : public nsIWidget, public nsSupportsWeakReference {

PopupLevel GetPopupLevel() { return mPopupLevel; }

LayoutDeviceIntSize ClientToWindowSize(
const LayoutDeviceIntSize& aClientSize) override {
return aClientSize;
}

// return true if this is a popup widget with a native titlebar
bool IsPopupWithTitleBar() const {
return (mWindowType == WindowType::Popup &&
Expand Down
13 changes: 8 additions & 5 deletions widget/nsIWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -1320,12 +1320,15 @@ class nsIWidget : public nsISupports {
}

/**
* Given the specified client size, return the corresponding window size,
* which includes the area for the borders and titlebar. This method
* should work even when the window is not yet visible.
* Returns the margins that are applied to go from client sizes to window
* sizes (which includes window borders and titlebar).
* This method should work even when the window is not yet visible.
*/
virtual LayoutDeviceIntSize ClientToWindowSize(
const LayoutDeviceIntSize& aClientSize) = 0;
virtual LayoutDeviceIntMargin ClientToWindowMargin() {
return {};
}

LayoutDeviceIntSize ClientToWindowSizeDifference();

/**
* Dispatches an event to the widget
Expand Down
34 changes: 19 additions & 15 deletions widget/windows/nsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3872,27 +3872,31 @@ LayoutDeviceIntPoint nsWindow::WidgetToScreenOffset() {
return LayoutDeviceIntPoint(point.x, point.y);
}

LayoutDeviceIntSize nsWindow::ClientToWindowSize(
const LayoutDeviceIntSize& aClientSize) {
LayoutDeviceIntMargin nsWindow::ClientToWindowMargin() {
if (mWindowType == WindowType::Popup && !IsPopupWithTitleBar()) {
return aClientSize;
return {};
}

// Just use (200, 200) as the position
const LayoutDeviceIntPoint point(200, 200);
if (mCustomNonClient) {
auto winRect = LayoutDeviceIntRect(point, aClientSize);
winRect.Inflate(NonClientSizeMargin(NormalWindowNonClientOffset()));
return winRect.Size();
return NonClientSizeMargin(NormalWindowNonClientOffset());
}

RECT r;
r.left = point.x;
r.top = point.y;
r.right = point.x + aClientSize.width;
r.bottom = point.y + aClientSize.height;
::AdjustWindowRectEx(&r, WindowStyle(), false, WindowExStyle());
return LayoutDeviceIntSize(r.right - r.left, r.bottom - r.top);
// Just use a dummy 200x200 at (200, 200) client rect as the rect.
RECT clientRect;
clientRect.left = 200;
clientRect.top = 200;
clientRect.right = 400;
clientRect.bottom = 400;

auto ToRect = [](const RECT& aRect) -> LayoutDeviceIntRect {
return {aRect.left, aRect.top, aRect.right - aRect.left,
aRect.bottom - aRect.top};
};

RECT windowRect = clientRect;
::AdjustWindowRectEx(&windowRect, WindowStyle(), false, WindowExStyle());

return ToRect(windowRect) - ToRect(clientRect);
}

/**************************************************************
Expand Down
3 changes: 1 addition & 2 deletions widget/windows/nsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ class nsWindow final : public nsBaseWidget {
nsresult SetTitle(const nsAString& aTitle) override;
void SetIcon(const nsAString& aIconSpec) override;
LayoutDeviceIntPoint WidgetToScreenOffset() override;
LayoutDeviceIntSize ClientToWindowSize(
const LayoutDeviceIntSize& aClientSize) override;
LayoutDeviceIntMargin ClientToWindowMargin() override;
nsresult DispatchEvent(mozilla::WidgetGUIEvent* aEvent,
nsEventStatus& aStatus) override;
void EnableDragDrop(bool aEnable) override;
Expand Down
4 changes: 1 addition & 3 deletions xpfe/appshell/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,7 @@ static LayoutDeviceIntSize GetOuterToInnerSizeDifference(nsIWidget* aWindow) {
if (!aWindow) {
return LayoutDeviceIntSize();
}
LayoutDeviceIntSize baseSize(200, 200);
LayoutDeviceIntSize windowSize = aWindow->ClientToWindowSize(baseSize);
return windowSize - baseSize;
return aWindow->ClientToWindowSizeDifference();
}

static CSSIntSize GetOuterToInnerSizeDifferenceInCSSPixels(
Expand Down

0 comments on commit 95520ca

Please sign in to comment.