Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 9ac0f98

Browse files
committed
Bug 1493081 - Added nsIWidget::GetDesktopToDeviceScaleByScreen for scale factor lookup by window position; r=mattwoodrow
We've added nsIWidget::GetDesktopToDeviceScaleByScreen which will return scale factor of the newly placed window according to its position on the display. This change is to move implementation to the nsIWidget derived classes. We need that for GTK Wayland, because on the Wayland we cannot determine absolute position of the window, we need to use parent's window scale factor. For other platforms the GetDesktopToDeviceScaleByScreen is implemented in nsBaseWidget. Differential Revision: https://phabricator.services.mozilla.com/D7290 --HG-- extra : moz-landing-system : lando
1 parent b3d5ff5 commit 9ac0f98

6 files changed

Lines changed: 49 additions & 3 deletions

File tree

view/nsView.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ void nsView::DoResetWidgetBounds(bool aMoveOnly,
320320
// Stash a copy of these and use them so we can handle this being deleted (say
321321
// from sync painting/flushing from Show/Move/Resize on the widget).
322322
LayoutDeviceIntRect newBounds;
323-
RefPtr<nsDeviceContext> dx = mViewManager->GetDeviceContext();
324323

325324
nsWindowType type = widget->WindowType();
326325

@@ -360,7 +359,8 @@ void nsView::DoResetWidgetBounds(bool aMoveOnly,
360359
// because of the potential for device-pixel coordinate spaces for mixed
361360
// hidpi/lodpi screens to overlap each other and result in bad placement
362361
// (bug 814434).
363-
DesktopToLayoutDeviceScale scale = dx->GetDesktopToDeviceScale();
362+
363+
DesktopToLayoutDeviceScale scale = widget->GetDesktopToDeviceScaleByScreen();
364364

365365
DesktopRect deskRect = newBounds / scale;
366366
if (changedPos) {

widget/android/moz.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ EXPORTS.mozilla.widget += [
3838

3939
SOURCES += [
4040
'!GeneratedJNIWrappers.cpp',
41+
'EventDispatcher.cpp',
4142
]
4243

4344
UNIFIED_SOURCES += [
@@ -47,7 +48,6 @@ UNIFIED_SOURCES += [
4748
'AndroidContentController.cpp',
4849
'AndroidUiThread.cpp',
4950
'ANRReporter.cpp',
50-
'EventDispatcher.cpp',
5151
'GeckoEditableSupport.cpp',
5252
'GfxInfo.cpp',
5353
'nsAndroidProtocolHandler.cpp',

widget/gtk/nsWindow.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
#if defined(MOZ_WAYLAND)
5959
#include <gdk/gdkwayland.h>
60+
#include "nsView.h"
6061
#endif
6162

6263
#include "nsGkAtoms.h"
@@ -841,6 +842,37 @@ nsWindow::GetDesktopToDeviceScale()
841842
return DesktopToLayoutDeviceScale(1.0);
842843
}
843844

845+
DesktopToLayoutDeviceScale
846+
nsWindow::GetDesktopToDeviceScaleByScreen()
847+
{
848+
#ifdef MOZ_WAYLAND
849+
GdkDisplay* gdkDisplay = gdk_display_get_default();
850+
// In Wayland there's no way to get absolute position of the window and use it to
851+
// determine the screen factor of the monitor on which the window is placed.
852+
// The window is notified of the current scale factor but not at this point,
853+
// so the GdkScaleFactor can return wrong value which can lead to wrong popup
854+
// placement.
855+
// We need to use parent's window scale factor for the new one.
856+
if (GDK_IS_WAYLAND_DISPLAY(gdkDisplay)) {
857+
nsView* view = nsView::GetViewFor(this);
858+
if (view) {
859+
nsView* parentView = view->GetParent();
860+
if (parentView) {
861+
nsIWidget* parentWidget = parentView->GetNearestWidget(nullptr);
862+
if (parentWidget) {
863+
return DesktopToLayoutDeviceScale(parentWidget->RoundsWidgetCoordinatesTo());
864+
} else {
865+
NS_WARNING("Widget has no parent");
866+
}
867+
}
868+
} else {
869+
NS_WARNING("Cannot find widget view");
870+
}
871+
}
872+
#endif
873+
return nsBaseWidget::GetDesktopToDeviceScale();
874+
}
875+
844876
void
845877
nsWindow::SetParent(nsIWidget *aNewParent)
846878
{

widget/gtk/nsWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class nsWindow final : public nsBaseWidget
125125
virtual float GetDPI() override;
126126
virtual double GetDefaultScaleInternal() override;
127127
mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale() override;
128+
mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScaleByScreen() override;
128129
virtual void SetParent(nsIWidget* aNewParent) override;
129130
virtual void SetModal(bool aModal) override;
130131
virtual bool IsVisible() const override;

widget/nsBaseWidget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "nsIWidgetListener.h"
2525
#include "nsPIDOMWindow.h"
2626
#include "nsWeakReference.h"
27+
#include "nsView.h"
28+
#include "nsViewManager.h"
2729
#include <algorithm>
2830

2931
#if defined(XP_WIN)
@@ -238,6 +240,10 @@ class nsBaseWidget : public nsIWidget, public nsSupportsWeakReference
238240
mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale() override {
239241
return mozilla::DesktopToLayoutDeviceScale(1.0);
240242
}
243+
mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScaleByScreen() override {
244+
return (nsView::GetViewFor(this)->GetViewManager()->GetDeviceContext())->GetDesktopToDeviceScale();
245+
}
246+
241247
virtual void ConstrainPosition(bool aAllowSlop,
242248
int32_t *aX,
243249
int32_t *aY) override {}

widget/nsIWidget.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@ class nsIWidget : public nsISupports
577577
*/
578578
virtual mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale() = 0;
579579

580+
/**
581+
* Return the scaling factor between device pixels and the platform-
582+
* dependent "desktop pixels" by looking up the screen by the position
583+
* of the widget.
584+
*/
585+
virtual mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScaleByScreen() = 0;
586+
580587
/**
581588
* Return the default scale factor for the window. This is the
582589
* default number of device pixels per CSS pixel to use. This should

0 commit comments

Comments
 (0)