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