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___
9 #include "mozilla/Attributes.h"
14 enum class ViewportFitType
: uint8_t {
20 } // namespace mozilla
23 * Default values for the nsViewportInfo class.
25 static const mozilla::LayoutDeviceToScreenScale
kViewportMinScale(0.25f
);
26 static const mozilla::LayoutDeviceToScreenScale
kViewportMaxScale(10.0f
);
27 static const mozilla::CSSIntSize
kViewportMinSize(200, 40);
28 static const mozilla::CSSIntSize
kViewportMaxSize(10000, 10000);
31 * Information retrieved from the <meta name="viewport"> tag. See
32 * Document::GetViewportInfo for more information on this functionality.
34 class MOZ_STACK_CLASS nsViewportInfo
{
36 enum class AutoSizeFlag
{
40 enum class AutoScaleFlag
{
48 enum class ZoomBehaviour
{
50 Desktop
, // disallows zooming out past default zoom
52 nsViewportInfo(const mozilla::ScreenIntSize
& aDisplaySize
,
53 const mozilla::CSSToScreenScale
& aDefaultZoom
,
54 ZoomFlag aZoomFlag
, ZoomBehaviour aBehaviour
)
55 : mDefaultZoom(aDefaultZoom
),
56 mViewportFit(mozilla::dom::ViewportFitType::Auto
),
57 mDefaultZoomValid(true),
59 mAllowZoom(aZoomFlag
== ZoomFlag::AllowZoom
) {
60 mSize
= mozilla::ScreenSize(aDisplaySize
) / mDefaultZoom
;
61 mozilla::CSSToLayoutDeviceScale
pixelRatio(1.0f
);
62 if (aBehaviour
== ZoomBehaviour::Desktop
) {
63 mMinZoom
= aDefaultZoom
;
65 mMinZoom
= pixelRatio
* kViewportMinScale
;
67 mMaxZoom
= pixelRatio
* kViewportMaxScale
;
68 ConstrainViewportValues();
71 nsViewportInfo(const mozilla::CSSToScreenScale
& aDefaultZoom
,
72 const mozilla::CSSToScreenScale
& aMinZoom
,
73 const mozilla::CSSToScreenScale
& aMaxZoom
,
74 const mozilla::CSSSize
& aSize
, AutoSizeFlag aAutoSizeFlag
,
75 AutoScaleFlag aAutoScaleFlag
, ZoomFlag aZoomFlag
,
76 mozilla::dom::ViewportFitType aViewportFit
)
77 : mDefaultZoom(aDefaultZoom
),
81 mViewportFit(aViewportFit
),
82 mDefaultZoomValid(aAutoScaleFlag
!= AutoScaleFlag::AutoScale
),
83 mAutoSize(aAutoSizeFlag
== AutoSizeFlag::AutoSize
),
84 mAllowZoom(aZoomFlag
== ZoomFlag::AllowZoom
) {
85 ConstrainViewportValues();
88 bool IsDefaultZoomValid() const { return mDefaultZoomValid
; }
89 mozilla::CSSToScreenScale
GetDefaultZoom() const { return mDefaultZoom
; }
90 mozilla::CSSToScreenScale
GetMinZoom() const { return mMinZoom
; }
91 mozilla::CSSToScreenScale
GetMaxZoom() const { return mMaxZoom
; }
93 mozilla::CSSSize
GetSize() const { return mSize
; }
95 bool IsAutoSizeEnabled() const { return mAutoSize
; }
96 bool IsZoomAllowed() const { return mAllowZoom
; }
98 mozilla::dom::ViewportFitType
GetViewportFit() const { return mViewportFit
; }
103 DeviceSize
= -3, // for device-width or device-height
105 // MIN/MAX computations where one of the arguments is auto resolve to the
106 // other argument. For instance, MIN(0.25, auto) = 0.25, and
108 // https://drafts.csswg.org/css-device-adapt/#constraining-defs
109 static const float& Max(const float& aA
, const float& aB
);
110 static const float& Min(const float& aA
, const float& aB
);
114 * Constrain the viewport calculations from the
115 * Document::GetViewportInfo() function in order to always return
116 * sane minimum/maximum values.
118 void ConstrainViewportValues();
120 // Default zoom indicates the level at which the display is 'zoomed in'
121 // initially for the user, upon loading of the page.
122 mozilla::CSSToScreenScale mDefaultZoom
;
124 // The minimum zoom level permitted by the page.
125 mozilla::CSSToScreenScale mMinZoom
;
127 // The maximum zoom level permitted by the page.
128 mozilla::CSSToScreenScale mMaxZoom
;
130 // The size of the viewport, specified by the <meta name="viewport"> tag.
131 mozilla::CSSSize mSize
;
133 // The value of the viewport-fit.
134 mozilla::dom::ViewportFitType mViewportFit
;
136 // If the default zoom was specified and was between the min and max
138 // FIXME: Bug 1504362 - Unify this and mDefaultZoom into
139 // Maybe<CSSToScreenScale>.
140 bool mDefaultZoomValid
;
142 // Whether or not we should automatically size the viewport to the device's
143 // width. This is true if the document has been optimized for mobile, and
144 // the width property of a specified <meta name="viewport"> tag is either
145 // not specified, or is set to the special value 'device-width'.
148 // Whether or not the user can zoom in and out on the page. Default is true.