Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / nsViewportInfo.h
blob5401e4bd877e63926f1ae8d3c45ca7ffc80cb706
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsViewportInfo_h___
6 #define nsViewportInfo_h___
8 #include <algorithm>
9 #include <stdint.h>
10 #include "mozilla/Attributes.h"
11 #include "mozilla/StaticPrefs_apz.h"
12 #include "Units.h"
14 namespace mozilla::dom {
15 enum class ViewportFitType : uint8_t {
16 Auto,
17 Contain,
18 Cover,
20 } // namespace mozilla::dom
22 /**
23 * Default values for the nsViewportInfo class.
25 static const mozilla::CSSIntSize kViewportMinSize(200, 40);
26 static const mozilla::CSSIntSize kViewportMaxSize(10000, 10000);
28 inline mozilla::LayoutDeviceToScreenScale ViewportMinScale() {
29 return mozilla::LayoutDeviceToScreenScale(
30 std::max(mozilla::StaticPrefs::apz_min_zoom(), 0.1f));
33 inline mozilla::LayoutDeviceToScreenScale ViewportMaxScale() {
34 return mozilla::LayoutDeviceToScreenScale(
35 std::min(mozilla::StaticPrefs::apz_max_zoom(), 100.0f));
38 /**
39 * Information retrieved from the <meta name="viewport"> tag. See
40 * Document::GetViewportInfo for more information on this functionality.
42 class MOZ_STACK_CLASS nsViewportInfo {
43 public:
44 enum class AutoSizeFlag {
45 AutoSize,
46 FixedSize,
48 enum class AutoScaleFlag {
49 AutoScale,
50 FixedScale,
52 enum class ZoomFlag {
53 AllowZoom,
54 DisallowZoom,
56 enum class ZoomBehaviour {
57 Mobile,
58 Desktop, // disallows zooming out past default zoom
60 nsViewportInfo(const mozilla::ScreenIntSize& aDisplaySize,
61 const mozilla::CSSToScreenScale& aDefaultZoom,
62 ZoomFlag aZoomFlag, ZoomBehaviour aBehaviour,
63 AutoScaleFlag aAutoScaleFlag = AutoScaleFlag::FixedScale)
64 : mDefaultZoom(aDefaultZoom),
65 mViewportFit(mozilla::dom::ViewportFitType::Auto),
66 mDefaultZoomValid(aAutoScaleFlag != AutoScaleFlag::AutoScale),
67 mAutoSize(true),
68 mAllowZoom(aZoomFlag == ZoomFlag::AllowZoom) {
69 mSize = mozilla::ScreenSize(aDisplaySize) / mDefaultZoom;
70 mozilla::CSSToLayoutDeviceScale pixelRatio(1.0f);
71 if (aBehaviour == ZoomBehaviour::Desktop) {
72 mMinZoom = aDefaultZoom;
73 } else {
74 mMinZoom = pixelRatio * ViewportMinScale();
76 mMaxZoom = pixelRatio * ViewportMaxScale();
77 ConstrainViewportValues();
80 nsViewportInfo(const mozilla::CSSToScreenScale& aDefaultZoom,
81 const mozilla::CSSToScreenScale& aMinZoom,
82 const mozilla::CSSToScreenScale& aMaxZoom,
83 const mozilla::CSSSize& aSize, AutoSizeFlag aAutoSizeFlag,
84 AutoScaleFlag aAutoScaleFlag, ZoomFlag aZoomFlag,
85 mozilla::dom::ViewportFitType aViewportFit)
86 : mDefaultZoom(aDefaultZoom),
87 mMinZoom(aMinZoom),
88 mMaxZoom(aMaxZoom),
89 mSize(aSize),
90 mViewportFit(aViewportFit),
91 mDefaultZoomValid(aAutoScaleFlag != AutoScaleFlag::AutoScale),
92 mAutoSize(aAutoSizeFlag == AutoSizeFlag::AutoSize),
93 mAllowZoom(aZoomFlag == ZoomFlag::AllowZoom) {
94 ConstrainViewportValues();
97 bool IsDefaultZoomValid() const { return mDefaultZoomValid; }
98 mozilla::CSSToScreenScale GetDefaultZoom() const { return mDefaultZoom; }
99 mozilla::CSSToScreenScale GetMinZoom() const { return mMinZoom; }
100 mozilla::CSSToScreenScale GetMaxZoom() const { return mMaxZoom; }
102 mozilla::CSSSize GetSize() const { return mSize; }
104 bool IsAutoSizeEnabled() const { return mAutoSize; }
105 bool IsZoomAllowed() const { return mAllowZoom; }
107 mozilla::dom::ViewportFitType GetViewportFit() const { return mViewportFit; }
109 static constexpr float kAuto = -1.0f;
110 static constexpr float kExtendToZoom = -2.0f;
111 static constexpr float kDeviceSize =
112 -3.0f; // for device-width or device-height
114 // MIN/MAX computations where one of the arguments is auto resolve to the
115 // other argument. For instance, MIN(0.25, auto) = 0.25, and
116 // MAX(5, auto) = 5.
117 // https://drafts.csswg.org/css-device-adapt/#constraining-defs
118 static const float& Max(const float& aA, const float& aB);
119 static const float& Min(const float& aA, const float& aB);
121 private:
123 * Constrain the viewport calculations from the
124 * Document::GetViewportInfo() function in order to always return
125 * sane minimum/maximum values.
127 void ConstrainViewportValues();
129 // Default zoom indicates the level at which the display is 'zoomed in'
130 // initially for the user, upon loading of the page.
131 mozilla::CSSToScreenScale mDefaultZoom;
133 // The minimum zoom level permitted by the page.
134 mozilla::CSSToScreenScale mMinZoom;
136 // The maximum zoom level permitted by the page.
137 mozilla::CSSToScreenScale mMaxZoom;
139 // The size of the viewport, specified by the <meta name="viewport"> tag.
140 mozilla::CSSSize mSize;
142 // The value of the viewport-fit.
143 mozilla::dom::ViewportFitType mViewportFit;
145 // If the default zoom was specified and was between the min and max
146 // zoom values.
147 // FIXME: Bug 1504362 - Unify this and mDefaultZoom into
148 // Maybe<CSSToScreenScale>.
149 bool mDefaultZoomValid;
151 // Whether or not we should automatically size the viewport to the device's
152 // width. This is true if the document has been optimized for mobile, and
153 // the width property of a specified <meta name="viewport"> tag is either
154 // not specified, or is set to the special value 'device-width'.
155 bool mAutoSize;
157 // Whether or not the user can zoom in and out on the page. Default is true.
158 bool mAllowZoom;
161 #endif