1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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 "FrameMetrics.h"
10 #include "nsStyleConsts.h"
11 #include "nsStyleStruct.h"
12 #include "mozilla/WritingModes.h"
13 #include "mozilla/gfx/Types.h"
18 const ScrollableLayerGuid::ViewID
ScrollableLayerGuid::NULL_SCROLL_ID
= 0;
20 void FrameMetrics::RecalculateLayoutViewportOffset() {
21 if (!mIsRootContent
) {
24 KeepLayoutViewportEnclosingVisualViewport(GetVisualViewport(),
25 mScrollableRect
, mLayoutViewport
);
29 void FrameMetrics::KeepLayoutViewportEnclosingVisualViewport(
30 const CSSRect
& aVisualViewport
, const CSSRect
& aScrollableRect
,
31 CSSRect
& aLayoutViewport
) {
32 // If the visual viewport is contained within the layout viewport, we don't
33 // need to make any adjustments, so we can exit early.
35 // Additionally, if the composition bounds changes (due to an orientation
36 // change, window resize, etc.), it may take a few frames for aLayoutViewport
37 // to update and during that time, the visual viewport may be larger than the
38 // layout viewport. In such situations, we take an early exit if the visual
39 // viewport contains the layout viewport.
40 if (aLayoutViewport
.Contains(aVisualViewport
) ||
41 aVisualViewport
.Contains(aLayoutViewport
)) {
45 // If visual viewport size is greater than the layout viewport, move the
46 // layout viewport such that it remains inside the visual viewport. Otherwise,
47 // move the layout viewport such that the visual viewport is contained
48 // inside the layout viewport.
49 if ((aLayoutViewport
.Width() < aVisualViewport
.Width() &&
50 !FuzzyEqualsMultiplicative(aLayoutViewport
.Width(),
51 aVisualViewport
.Width())) ||
52 (aLayoutViewport
.Height() < aVisualViewport
.Height() &&
53 !FuzzyEqualsMultiplicative(aLayoutViewport
.Height(),
54 aVisualViewport
.Height()))) {
55 if (aLayoutViewport
.X() < aVisualViewport
.X()) {
56 // layout viewport moves right
57 aLayoutViewport
.MoveToX(aVisualViewport
.X());
58 } else if (aVisualViewport
.XMost() < aLayoutViewport
.XMost()) {
59 // layout viewport moves left
60 aLayoutViewport
.MoveByX(aVisualViewport
.XMost() -
61 aLayoutViewport
.XMost());
63 if (aLayoutViewport
.Y() < aVisualViewport
.Y()) {
64 // layout viewport moves down
65 aLayoutViewport
.MoveToY(aVisualViewport
.Y());
66 } else if (aVisualViewport
.YMost() < aLayoutViewport
.YMost()) {
67 // layout viewport moves up
68 aLayoutViewport
.MoveByY(aVisualViewport
.YMost() -
69 aLayoutViewport
.YMost());
72 if (aVisualViewport
.X() < aLayoutViewport
.X()) {
73 aLayoutViewport
.MoveToX(aVisualViewport
.X());
74 } else if (aLayoutViewport
.XMost() < aVisualViewport
.XMost()) {
75 aLayoutViewport
.MoveByX(aVisualViewport
.XMost() -
76 aLayoutViewport
.XMost());
78 if (aVisualViewport
.Y() < aLayoutViewport
.Y()) {
79 aLayoutViewport
.MoveToY(aVisualViewport
.Y());
80 } else if (aLayoutViewport
.YMost() < aVisualViewport
.YMost()) {
81 aLayoutViewport
.MoveByY(aVisualViewport
.YMost() -
82 aLayoutViewport
.YMost());
86 // Regardless of any adjustment above, the layout viewport is not allowed
87 // to go outside the scrollable rect.
88 aLayoutViewport
= aLayoutViewport
.MoveInsideAndClamp(aScrollableRect
);
91 ScrollSnapInfo::ScrollSnapInfo()
92 : mScrollSnapStrictnessX(StyleScrollSnapStrictness::None
),
93 mScrollSnapStrictnessY(StyleScrollSnapStrictness::None
) {}
95 bool ScrollSnapInfo::HasScrollSnapping() const {
96 return mScrollSnapStrictnessY
!= StyleScrollSnapStrictness::None
||
97 mScrollSnapStrictnessX
!= StyleScrollSnapStrictness::None
;
100 bool ScrollSnapInfo::HasSnapPositions() const {
101 return (!mSnapPositionX
.IsEmpty() &&
102 mScrollSnapStrictnessX
!= StyleScrollSnapStrictness::None
) ||
103 (!mSnapPositionY
.IsEmpty() &&
104 mScrollSnapStrictnessY
!= StyleScrollSnapStrictness::None
);
107 void ScrollSnapInfo::InitializeScrollSnapStrictness(
108 WritingMode aWritingMode
, const nsStyleDisplay
* aDisplay
) {
109 if (aDisplay
->mScrollSnapType
.strictness
== StyleScrollSnapStrictness::None
) {
113 mScrollSnapStrictnessX
= StyleScrollSnapStrictness::None
;
114 mScrollSnapStrictnessY
= StyleScrollSnapStrictness::None
;
116 switch (aDisplay
->mScrollSnapType
.axis
) {
117 case StyleScrollSnapAxis::X
:
118 mScrollSnapStrictnessX
= aDisplay
->mScrollSnapType
.strictness
;
120 case StyleScrollSnapAxis::Y
:
121 mScrollSnapStrictnessY
= aDisplay
->mScrollSnapType
.strictness
;
123 case StyleScrollSnapAxis::Block
:
124 if (aWritingMode
.IsVertical()) {
125 mScrollSnapStrictnessX
= aDisplay
->mScrollSnapType
.strictness
;
127 mScrollSnapStrictnessY
= aDisplay
->mScrollSnapType
.strictness
;
130 case StyleScrollSnapAxis::Inline
:
131 if (aWritingMode
.IsVertical()) {
132 mScrollSnapStrictnessY
= aDisplay
->mScrollSnapType
.strictness
;
134 mScrollSnapStrictnessX
= aDisplay
->mScrollSnapType
.strictness
;
137 case StyleScrollSnapAxis::Both
:
138 mScrollSnapStrictnessX
= aDisplay
->mScrollSnapType
.strictness
;
139 mScrollSnapStrictnessY
= aDisplay
->mScrollSnapType
.strictness
;
144 static OverscrollBehavior
ToOverscrollBehavior(
145 StyleOverscrollBehavior aBehavior
) {
147 case StyleOverscrollBehavior::Auto
:
148 return OverscrollBehavior::Auto
;
149 case StyleOverscrollBehavior::Contain
:
150 return OverscrollBehavior::Contain
;
151 case StyleOverscrollBehavior::None
:
152 return OverscrollBehavior::None
;
154 MOZ_ASSERT_UNREACHABLE("Invalid overscroll behavior");
155 return OverscrollBehavior::Auto
;
158 OverscrollBehaviorInfo
OverscrollBehaviorInfo::FromStyleConstants(
159 StyleOverscrollBehavior aBehaviorX
, StyleOverscrollBehavior aBehaviorY
) {
160 OverscrollBehaviorInfo result
;
161 result
.mBehaviorX
= ToOverscrollBehavior(aBehaviorX
);
162 result
.mBehaviorY
= ToOverscrollBehavior(aBehaviorY
);
166 void ScrollMetadata::SetBackgroundColor(
167 const gfx::sRGBColor
& aBackgroundColor
) {
168 mBackgroundColor
= gfx::ToDeviceColor(aBackgroundColor
);
171 StaticAutoPtr
<const ScrollMetadata
> ScrollMetadata::sNullMetadata
;
173 } // namespace layers
174 } // namespace mozilla