Bug 1792034 [wpt PR 36019] - Make location.search always expect UTF-8, a=testonly
[gecko.git] / widget / Screen.cpp
blob4962d1c9bac7151608f3f03cd98defa5fedc957d
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Screen.h"
9 #include "mozilla/dom/DOMTypes.h"
10 #include "mozilla/Hal.h"
11 #include "mozilla/LookAndFeel.h"
12 #include "mozilla/StaticPrefs_layout.h"
14 namespace mozilla::widget {
16 NS_IMPL_ISUPPORTS(Screen, nsIScreen)
18 static hal::ScreenOrientation EffectiveOrientation(
19 hal::ScreenOrientation aOrientation, const LayoutDeviceIntRect& aRect) {
20 if (aOrientation == hal::ScreenOrientation::None) {
21 return aRect.Width() >= aRect.Height()
22 ? hal::ScreenOrientation::LandscapePrimary
23 : hal::ScreenOrientation::PortraitPrimary;
25 return aOrientation;
28 Screen::Screen(LayoutDeviceIntRect aRect, LayoutDeviceIntRect aAvailRect,
29 uint32_t aPixelDepth, uint32_t aColorDepth,
30 uint32_t aRefreshRate, DesktopToLayoutDeviceScale aContentsScale,
31 CSSToLayoutDeviceScale aDefaultCssScale, float aDPI,
32 IsPseudoDisplay aIsPseudoDisplay,
33 hal::ScreenOrientation aOrientation,
34 OrientationAngle aOrientationAngle)
35 : mRect(aRect),
36 mAvailRect(aAvailRect),
37 mRectDisplayPix(RoundedToInt(aRect / aContentsScale)),
38 mAvailRectDisplayPix(RoundedToInt(aAvailRect / aContentsScale)),
39 mPixelDepth(aPixelDepth),
40 mColorDepth(aColorDepth),
41 mRefreshRate(aRefreshRate),
42 mContentsScale(aContentsScale),
43 mDefaultCssScale(aDefaultCssScale),
44 mDPI(aDPI),
45 mScreenOrientation(EffectiveOrientation(aOrientation, aRect)),
46 mOrientationAngle(aOrientationAngle),
47 mIsPseudoDisplay(aIsPseudoDisplay == IsPseudoDisplay::Yes) {}
49 Screen::Screen(const dom::ScreenDetails& aScreen)
50 : mRect(aScreen.rect()),
51 mAvailRect(aScreen.availRect()),
52 mRectDisplayPix(aScreen.rectDisplayPix()),
53 mAvailRectDisplayPix(aScreen.availRectDisplayPix()),
54 mPixelDepth(aScreen.pixelDepth()),
55 mColorDepth(aScreen.colorDepth()),
56 mRefreshRate(aScreen.refreshRate()),
57 mContentsScale(aScreen.contentsScaleFactor()),
58 mDefaultCssScale(aScreen.defaultCSSScaleFactor()),
59 mDPI(aScreen.dpi()),
60 mScreenOrientation(aScreen.orientation()),
61 mOrientationAngle(aScreen.orientationAngle()),
62 mIsPseudoDisplay(aScreen.isPseudoDisplay()) {}
64 Screen::Screen(const Screen& aOther)
65 : mRect(aOther.mRect),
66 mAvailRect(aOther.mAvailRect),
67 mRectDisplayPix(aOther.mRectDisplayPix),
68 mAvailRectDisplayPix(aOther.mAvailRectDisplayPix),
69 mPixelDepth(aOther.mPixelDepth),
70 mColorDepth(aOther.mColorDepth),
71 mRefreshRate(aOther.mRefreshRate),
72 mContentsScale(aOther.mContentsScale),
73 mDefaultCssScale(aOther.mDefaultCssScale),
74 mDPI(aOther.mDPI),
75 mScreenOrientation(aOther.mScreenOrientation),
76 mOrientationAngle(aOther.mOrientationAngle),
77 mIsPseudoDisplay(aOther.mIsPseudoDisplay) {}
79 dom::ScreenDetails Screen::ToScreenDetails() const {
80 return dom::ScreenDetails(
81 mRect, mRectDisplayPix, mAvailRect, mAvailRectDisplayPix, mPixelDepth,
82 mColorDepth, mRefreshRate, mContentsScale, mDefaultCssScale, mDPI,
83 mScreenOrientation, mOrientationAngle, mIsPseudoDisplay);
86 NS_IMETHODIMP
87 Screen::GetRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
88 int32_t* aOutHeight) {
89 mRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
90 return NS_OK;
93 NS_IMETHODIMP
94 Screen::GetRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
95 int32_t* aOutWidth, int32_t* aOutHeight) {
96 mRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
97 return NS_OK;
100 NS_IMETHODIMP
101 Screen::GetAvailRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
102 int32_t* aOutHeight) {
103 mAvailRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
104 return NS_OK;
107 NS_IMETHODIMP
108 Screen::GetAvailRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
109 int32_t* aOutWidth, int32_t* aOutHeight) {
110 mAvailRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
111 return NS_OK;
114 NS_IMETHODIMP
115 Screen::GetPixelDepth(int32_t* aPixelDepth) {
116 *aPixelDepth = mPixelDepth;
117 return NS_OK;
120 NS_IMETHODIMP
121 Screen::GetColorDepth(int32_t* aColorDepth) {
122 *aColorDepth = mColorDepth;
123 return NS_OK;
126 NS_IMETHODIMP
127 Screen::GetContentsScaleFactor(double* aOutScale) {
128 *aOutScale = mContentsScale.scale;
129 return NS_OK;
132 NS_IMETHODIMP
133 Screen::GetDefaultCSSScaleFactor(double* aOutScale) {
134 double scale = StaticPrefs::layout_css_devPixelsPerPx();
135 if (scale > 0.0) {
136 *aOutScale = scale;
137 } else {
138 *aOutScale = mDefaultCssScale.scale;
140 *aOutScale *= LookAndFeel::SystemZoomSettings().mFullZoom;
141 return NS_OK;
144 NS_IMETHODIMP
145 Screen::GetDpi(float* aDPI) {
146 *aDPI = mDPI;
147 return NS_OK;
150 NS_IMETHODIMP
151 Screen::GetRefreshRate(int32_t* aRefreshRate) {
152 *aRefreshRate = mRefreshRate;
153 return NS_OK;
156 NS_IMETHODIMP
157 Screen::GetIsPseudoDisplay(bool* aIsPseudoDisplay) {
158 *aIsPseudoDisplay = mIsPseudoDisplay;
159 return NS_OK;
162 } // namespace mozilla::widget