forked from mozilla-firefox/firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollContainerFrame.cpp
8074 lines (7166 loc) · 314 KB
/
ScrollContainerFrame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* rendering object to wrap rendering objects that should be scrollable */
#include "mozilla/ScrollContainerFrame.h"
#include "ScrollPositionUpdate.h"
#include "mozilla/layers/LayersTypes.h"
#include "nsIXULRuntime.h"
#include "DisplayItemClip.h"
#include "nsCOMPtr.h"
#include "nsIDocumentViewer.h"
#include "nsPresContext.h"
#include "nsView.h"
#include "nsViewportInfo.h"
#include "nsContainerFrame.h"
#include "nsGkAtoms.h"
#include "nsNameSpaceManager.h"
#include "mozilla/intl/BidiEmbeddingLevel.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/gfx/gfxVars.h"
#include "nsFontMetrics.h"
#include "nsFlexContainerFrame.h"
#include "mozilla/dom/NodeInfo.h"
#include "nsScrollbarFrame.h"
#include "nsINode.h"
#include "nsIScrollbarMediator.h"
#include "nsILayoutHistoryState.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsStyleTransformMatrix.h"
#include "mozilla/PresState.h"
#include "nsContentUtils.h"
#include "nsDisplayList.h"
#include "nsHTMLDocument.h"
#include "nsLayoutUtils.h"
#include "nsBidiPresUtils.h"
#include "nsBidiUtils.h"
#include "nsDocShell.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/DisplayPortUtils.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/Preferences.h"
#include "mozilla/PresShell.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/ScrollbarPreferences.h"
#include "mozilla/ScrollingMetrics.h"
#include "mozilla/StaticPrefs_bidi.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/StaticPrefs_toolkit.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/SVGOuterSVGFrame.h"
#include "mozilla/ViewportUtils.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/HTMLMarqueeElement.h"
#include "mozilla/dom/ScrollTimeline.h"
#include "mozilla/dom/BrowserChild.h"
#include <stdint.h>
#include "mozilla/MathAlgorithms.h"
#include "nsSubDocumentFrame.h"
#include "mozilla/Attributes.h"
#include "ScrollbarActivity.h"
#include "nsRefreshDriver.h"
#include "nsStyleConsts.h"
#include "nsIScrollPositionListener.h"
#include "StickyScrollContainer.h"
#include "nsIFrameInlines.h"
#include "gfxPlatform.h"
#include "mozilla/StaticPrefs_apz.h"
#include "mozilla/StaticPrefs_general.h"
#include "mozilla/StaticPrefs_layers.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPrefs_mousewheel.h"
#include "mozilla/ToString.h"
#include "ScrollAnimationPhysics.h"
#include "ScrollAnimationBezierPhysics.h"
#include "ScrollAnimationMSDPhysics.h"
#include "ScrollSnap.h"
#include "UnitTransforms.h"
#include "nsSliderFrame.h"
#include "ViewportFrame.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/layers/APZCCallbackHelper.h"
#include "mozilla/layers/APZPublicUtils.h"
#include "mozilla/layers/AxisPhysicsModel.h"
#include "mozilla/layers/AxisPhysicsMSDModel.h"
#include "mozilla/layers/ScrollingInteractionContext.h"
#include "mozilla/layers/ScrollLinkedEffectDetector.h"
#include "mozilla/Unused.h"
#include "MobileViewportManager.h"
#include "TextOverflow.h"
#include "VisualViewport.h"
#include "WindowRenderer.h"
#include <algorithm>
#include <cstdlib> // for std::abs(int/long)
#include <cmath> // for std::abs(float/double)
#include <tuple> // for std::tie
static mozilla::LazyLogModule sApzPaintSkipLog("apz.paintskip");
#define PAINT_SKIP_LOG(...) \
MOZ_LOG(sApzPaintSkipLog, LogLevel::Debug, (__VA_ARGS__))
static mozilla::LazyLogModule sScrollRestoreLog("scrollrestore");
#define SCROLLRESTORE_LOG(...) \
MOZ_LOG(sScrollRestoreLog, LogLevel::Debug, (__VA_ARGS__))
static mozilla::LazyLogModule sRootScrollbarsLog("rootscrollbars");
#define ROOT_SCROLLBAR_LOG(...) \
if (mIsRoot) { \
MOZ_LOG(sRootScrollbarsLog, LogLevel::Debug, (__VA_ARGS__)); \
}
static mozilla::LazyLogModule sDisplayportLog("apz.displayport");
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::gfx;
using namespace mozilla::layers;
using namespace mozilla::layout;
using nsStyleTransformMatrix::TransformReferenceBox;
static ScrollDirections GetOverflowChange(const nsRect& aCurScrolledRect,
const nsRect& aPrevScrolledRect) {
ScrollDirections result;
if (aPrevScrolledRect.x != aCurScrolledRect.x ||
aPrevScrolledRect.width != aCurScrolledRect.width) {
result += ScrollDirection::eHorizontal;
}
if (aPrevScrolledRect.y != aCurScrolledRect.y ||
aPrevScrolledRect.height != aCurScrolledRect.height) {
result += ScrollDirection::eVertical;
}
return result;
}
/**
* This class handles the dispatching of scroll events to content.
*
* Scroll events are posted to the refresh driver via
* nsRefreshDriver::PostScrollEvent(), and they are fired during a refresh
* driver tick, after running requestAnimationFrame callbacks but before
* the style flush. This allows rAF callbacks to perform scrolling and have
* that scrolling be reflected on the same refresh driver tick, while at
* the same time allowing scroll event listeners to make style changes and
* have those style changes be reflected on the same refresh driver tick.
*
* ScrollEvents cannot be refresh observers, because none of the existing
* categories of refresh observers (FlushType::Style, FlushType::Layout,
* and FlushType::Display) are run at the desired time in a refresh driver
* tick. They behave similarly to refresh observers in that their presence
* causes the refresh driver to tick.
*
* ScrollEvents are one-shot runnables; the refresh driver drops them after
* running them.
*/
class ScrollContainerFrame::ScrollEvent : public Runnable {
public:
NS_DECL_NSIRUNNABLE
explicit ScrollEvent(ScrollContainerFrame* aHelper, bool aDelayed);
void Revoke() { mHelper = nullptr; }
private:
ScrollContainerFrame* mHelper;
};
class ScrollContainerFrame::ScrollEndEvent : public Runnable {
public:
NS_DECL_NSIRUNNABLE
explicit ScrollEndEvent(ScrollContainerFrame* aHelper, bool aDelayed);
void Revoke() { mHelper = nullptr; }
private:
ScrollContainerFrame* mHelper;
};
class ScrollContainerFrame::AsyncScrollPortEvent : public Runnable {
public:
NS_DECL_NSIRUNNABLE
explicit AsyncScrollPortEvent(ScrollContainerFrame* helper)
: Runnable("ScrollContainerFrame::AsyncScrollPortEvent"),
mHelper(helper) {}
void Revoke() { mHelper = nullptr; }
private:
ScrollContainerFrame* mHelper;
};
class ScrollContainerFrame::ScrolledAreaEvent : public Runnable {
public:
NS_DECL_NSIRUNNABLE
explicit ScrolledAreaEvent(ScrollContainerFrame* helper)
: Runnable("ScrollContainerFrame::ScrolledAreaEvent"), mHelper(helper) {}
void Revoke() { mHelper = nullptr; }
private:
ScrollContainerFrame* mHelper;
};
class ScrollFrameActivityTracker final
: public nsExpirationTracker<ScrollContainerFrame, 4> {
public:
// Wait for 3-4s between scrolls before we remove our layers.
// That's 4 generations of 1s each.
enum { TIMEOUT_MS = 1000 };
explicit ScrollFrameActivityTracker(nsIEventTarget* aEventTarget)
: nsExpirationTracker<ScrollContainerFrame, 4>(
TIMEOUT_MS, "ScrollFrameActivityTracker", aEventTarget) {}
~ScrollFrameActivityTracker() { AgeAllGenerations(); }
virtual void NotifyExpired(ScrollContainerFrame* aObject) override {
RemoveObject(aObject);
aObject->MarkNotRecentlyScrolled();
}
};
static StaticAutoPtr<ScrollFrameActivityTracker> gScrollFrameActivityTracker;
ScrollContainerFrame* NS_NewScrollContainerFrame(mozilla::PresShell* aPresShell,
ComputedStyle* aStyle,
bool aIsRoot) {
return new (aPresShell)
ScrollContainerFrame(aStyle, aPresShell->GetPresContext(), aIsRoot);
}
NS_IMPL_FRAMEARENA_HELPERS(ScrollContainerFrame)
ScrollContainerFrame::ScrollContainerFrame(ComputedStyle* aStyle,
nsPresContext* aPresContext,
nsIFrame::ClassID aID, bool aIsRoot)
: nsContainerFrame(aStyle, aPresContext, aID),
mHScrollbarBox(nullptr),
mVScrollbarBox(nullptr),
mScrolledFrame(nullptr),
mScrollCornerBox(nullptr),
mResizerBox(nullptr),
mReferenceFrameDuringPainting(nullptr),
mAsyncScroll(nullptr),
mAsyncSmoothMSDScroll(nullptr),
mLastScrollOrigin(ScrollOrigin::None),
mDestination(0, 0),
mRestorePos(-1, -1),
mLastPos(-1, -1),
mApzScrollPos(0, 0),
mLastUpdateFramesPos(-1, -1),
mScrollParentID(mozilla::layers::ScrollableLayerGuid::NULL_SCROLL_ID),
mAnchor(this),
mCurrentAPZScrollAnimationType(APZScrollAnimationType::No),
mIsFirstScrollableFrameSequenceNumber(Nothing()),
mInScrollingGesture(InScrollingGesture::No),
mAllowScrollOriginDowngrade(false),
mHadDisplayPortAtLastFrameUpdate(false),
mHasVerticalScrollbar(false),
mHasHorizontalScrollbar(false),
mOnlyNeedVScrollbarToScrollVVInsideLV(false),
mOnlyNeedHScrollbarToScrollVVInsideLV(false),
mFrameIsUpdatingScrollbar(false),
mDidHistoryRestore(false),
mIsRoot(aIsRoot),
mSuppressScrollbarUpdate(false),
mSkippedScrollbarLayout(false),
mHadNonInitialReflow(false),
mFirstReflow(true),
mHorizontalOverflow(false),
mVerticalOverflow(false),
mPostedReflowCallback(false),
mMayHaveDirtyFixedChildren(false),
mUpdateScrollbarAttributes(false),
mHasBeenScrolledRecently(false),
mWillBuildScrollableLayer(false),
mIsParentToActiveScrollFrames(false),
mHasBeenScrolled(false),
mIgnoreMomentumScroll(false),
mTransformingByAPZ(false),
mScrollableByAPZ(false),
mZoomableByAPZ(false),
mHasOutOfFlowContentInsideFilter(false),
mSuppressScrollbarRepaints(false),
mIsUsingMinimumScaleSize(false),
mMinimumScaleSizeChanged(false),
mProcessingScrollEvent(false),
mApzAnimationRequested(false),
mApzAnimationTriggeredByScriptRequested(false),
mReclampVVOffsetInReflowFinished(false),
mMayScheduleScrollAnimations(false),
#ifdef MOZ_WIDGET_ANDROID
mHasVerticalOverflowForDynamicToolbar(false),
#endif
mVelocityQueue(PresContext()) {
AppendScrollUpdate(ScrollPositionUpdate::NewScrollframe(nsPoint()));
if (UsesOverlayScrollbars()) {
mScrollbarActivity = new ScrollbarActivity(this);
}
if (mIsRoot) {
mZoomableByAPZ = PresShell()->GetZoomableByAPZ();
}
}
ScrollContainerFrame::~ScrollContainerFrame() = default;
void ScrollContainerFrame::ScrollbarActivityStarted() const {
if (mScrollbarActivity) {
mScrollbarActivity->ActivityStarted();
}
}
void ScrollContainerFrame::ScrollbarActivityStopped() const {
if (mScrollbarActivity) {
mScrollbarActivity->ActivityStopped();
}
}
void ScrollContainerFrame::Destroy(DestroyContext& aContext) {
DestroyAbsoluteFrames(aContext);
if (mIsRoot) {
PresShell()->ResetVisualViewportOffset();
}
mAnchor.Destroy();
if (mScrollbarActivity) {
mScrollbarActivity->Destroy();
mScrollbarActivity = nullptr;
}
// Unbind the content created in CreateAnonymousContent later...
aContext.AddAnonymousContent(mHScrollbarContent.forget());
aContext.AddAnonymousContent(mVScrollbarContent.forget());
aContext.AddAnonymousContent(mScrollCornerContent.forget());
aContext.AddAnonymousContent(mResizerContent.forget());
if (mPostedReflowCallback) {
PresShell()->CancelReflowCallback(this);
mPostedReflowCallback = false;
}
if (mDisplayPortExpiryTimer) {
mDisplayPortExpiryTimer->Cancel();
mDisplayPortExpiryTimer = nullptr;
}
if (mActivityExpirationState.IsTracked()) {
gScrollFrameActivityTracker->RemoveObject(this);
}
if (gScrollFrameActivityTracker && gScrollFrameActivityTracker->IsEmpty()) {
gScrollFrameActivityTracker = nullptr;
}
if (mScrollActivityTimer) {
mScrollActivityTimer->Cancel();
mScrollActivityTimer = nullptr;
}
RemoveObservers();
if (mScrollEvent) {
mScrollEvent->Revoke();
}
if (mScrollEndEvent) {
mScrollEndEvent->Revoke();
}
nsContainerFrame::Destroy(aContext);
}
void ScrollContainerFrame::SetInitialChildList(ChildListID aListID,
nsFrameList&& aChildList) {
nsContainerFrame::SetInitialChildList(aListID, std::move(aChildList));
ReloadChildFrames();
}
void ScrollContainerFrame::AppendFrames(ChildListID aListID,
nsFrameList&& aFrameList) {
NS_ASSERTION(aListID == FrameChildListID::Principal,
"Only main list supported");
mFrames.AppendFrames(nullptr, std::move(aFrameList));
ReloadChildFrames();
}
void ScrollContainerFrame::InsertFrames(
ChildListID aListID, nsIFrame* aPrevFrame,
const nsLineList::iterator* aPrevFrameLine, nsFrameList&& aFrameList) {
NS_ASSERTION(aListID == FrameChildListID::Principal,
"Only main list supported");
NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
"inserting after sibling frame with different parent");
mFrames.InsertFrames(nullptr, aPrevFrame, std::move(aFrameList));
ReloadChildFrames();
}
void ScrollContainerFrame::RemoveFrame(DestroyContext& aContext,
ChildListID aListID,
nsIFrame* aOldFrame) {
NS_ASSERTION(aListID == FrameChildListID::Principal,
"Only main list supported");
mFrames.DestroyFrame(aContext, aOldFrame);
ReloadChildFrames();
}
/**
HTML scrolling implementation
All other things being equal, we prefer layouts with fewer scrollbars showing.
*/
namespace mozilla {
enum class ShowScrollbar : uint8_t {
Auto,
Always,
// Never is a misnomer. We can still get a scrollbar if we need to scroll the
// visual viewport inside the layout viewport. Thus this enum is best thought
// of as value used by layout, which does not know about the visual viewport.
// The visual viewport does not affect any layout sizes, so this is sound.
Never,
};
static ShowScrollbar ShouldShowScrollbar(StyleOverflow aOverflow) {
switch (aOverflow) {
case StyleOverflow::Scroll:
return ShowScrollbar::Always;
case StyleOverflow::Hidden:
return ShowScrollbar::Never;
default:
case StyleOverflow::Auto:
return ShowScrollbar::Auto;
}
}
struct MOZ_STACK_CLASS ScrollReflowInput {
// === Filled in by the constructor. Members in this section shouldn't change
// their values after the constructor. ===
const ReflowInput& mReflowInput;
ShowScrollbar mHScrollbar;
// If the horizontal scrollbar is allowed (even if mHScrollbar ==
// ShowScrollbar::Never) provided that it is for scrolling the visual viewport
// inside the layout viewport only.
bool mHScrollbarAllowedForScrollingVVInsideLV = true;
ShowScrollbar mVScrollbar;
// If the vertical scrollbar is allowed (even if mVScrollbar ==
// ShowScrollbar::Never) provided that it is for scrolling the visual viewport
// inside the layout viewport only.
bool mVScrollbarAllowedForScrollingVVInsideLV = true;
nsMargin mComputedBorder;
// === Filled in by ReflowScrolledFrame ===
OverflowAreas mContentsOverflowAreas;
// The scrollbar gutter sizes used in the most recent reflow of
// mScrolledFrame. The writing-mode is the same as the scroll
// container.
LogicalMargin mScrollbarGutterFromLastReflow;
// True if the most recent reflow of mScrolledFrame is with the
// horizontal scrollbar.
bool mReflowedContentsWithHScrollbar = false;
// True if the most recent reflow of mScrolledFrame is with the
// vertical scrollbar.
bool mReflowedContentsWithVScrollbar = false;
// === Filled in when TryLayout succeeds ===
// The size of the inside-border area
nsSize mInsideBorderSize;
// Whether we decided to show the horizontal scrollbar in the most recent
// TryLayout.
bool mShowHScrollbar = false;
// Whether we decided to show the vertical scrollbar in the most recent
// TryLayout.
bool mShowVScrollbar = false;
// If mShow(H|V)Scrollbar is true then
// mOnlyNeed(V|H)ScrollbarToScrollVVInsideLV indicates if the only reason we
// need that scrollbar is to scroll the visual viewport inside the layout
// viewport. These scrollbars are special in that even if they are layout
// scrollbars they do not take up any layout space.
bool mOnlyNeedHScrollbarToScrollVVInsideLV = false;
bool mOnlyNeedVScrollbarToScrollVVInsideLV = false;
ScrollReflowInput(ScrollContainerFrame* aFrame,
const ReflowInput& aReflowInput);
nscoord VScrollbarMinHeight() const { return mVScrollbarPrefSize.height; }
nscoord VScrollbarPrefWidth() const { return mVScrollbarPrefSize.width; }
nscoord HScrollbarMinWidth() const { return mHScrollbarPrefSize.width; }
nscoord HScrollbarPrefHeight() const { return mHScrollbarPrefSize.height; }
// Returns the sizes occupied by the scrollbar gutters. If aShowVScroll or
// aShowHScroll is true, the sizes occupied by the scrollbars are also
// included.
nsMargin ScrollbarGutter(bool aShowVScrollbar, bool aShowHScrollbar,
bool aScrollbarOnRight) const {
if (mOverlayScrollbars) {
return mScrollbarGutter;
}
nsMargin gutter = mScrollbarGutter;
if (aShowVScrollbar && gutter.right == 0 && gutter.left == 0) {
const nscoord w = VScrollbarPrefWidth();
if (aScrollbarOnRight) {
gutter.right = w;
} else {
gutter.left = w;
}
}
if (aShowHScrollbar && gutter.bottom == 0) {
// The horizontal scrollbar is always at the bottom side.
gutter.bottom = HScrollbarPrefHeight();
}
return gutter;
}
bool OverlayScrollbars() const { return mOverlayScrollbars; }
private:
// Filled in by the constructor. Put variables here to keep them unchanged
// after initializing them in the constructor.
nsSize mVScrollbarPrefSize;
nsSize mHScrollbarPrefSize;
bool mOverlayScrollbars = false;
// The scrollbar gutter sizes resolved from the scrollbar-gutter and
// scrollbar-width property.
nsMargin mScrollbarGutter;
};
ScrollReflowInput::ScrollReflowInput(ScrollContainerFrame* aFrame,
const ReflowInput& aReflowInput)
: mReflowInput(aReflowInput),
mComputedBorder(aReflowInput.ComputedPhysicalBorderPadding() -
aReflowInput.ComputedPhysicalPadding()),
mScrollbarGutterFromLastReflow(aFrame->GetWritingMode()) {
ScrollStyles styles = aFrame->GetScrollStyles();
mHScrollbar = ShouldShowScrollbar(styles.mHorizontal);
mVScrollbar = ShouldShowScrollbar(styles.mVertical);
mOverlayScrollbars = aFrame->UsesOverlayScrollbars();
if (nsScrollbarFrame* scrollbar = aFrame->GetScrollbarBox(false)) {
scrollbar->SetScrollbarMediatorContent(mReflowInput.mFrame->GetContent());
mHScrollbarPrefSize = scrollbar->ScrollbarMinSize();
// A zero minimum size is a bug with non-overlay scrollbars. That means
// we'll always try to place the scrollbar, even if it will ultimately not
// fit, see bug 1809630. XUL collapsing is the exception because the
// front-end uses it.
MOZ_ASSERT(mHScrollbarPrefSize.width && mHScrollbarPrefSize.height,
"Shouldn't have a zero horizontal scrollbar-size");
} else {
mHScrollbar = ShowScrollbar::Never;
mHScrollbarAllowedForScrollingVVInsideLV = false;
}
if (nsScrollbarFrame* scrollbar = aFrame->GetScrollbarBox(true)) {
scrollbar->SetScrollbarMediatorContent(mReflowInput.mFrame->GetContent());
mVScrollbarPrefSize = scrollbar->ScrollbarMinSize();
// See above.
MOZ_ASSERT(mVScrollbarPrefSize.width && mVScrollbarPrefSize.height,
"Shouldn't have a zero vertical scrollbar-size");
} else {
mVScrollbar = ShowScrollbar::Never;
mVScrollbarAllowedForScrollingVVInsideLV = false;
}
const auto* scrollbarStyle =
nsLayoutUtils::StyleForScrollbar(mReflowInput.mFrame);
// Hide the scrollbar when the scrollbar-width is set to none.
//
// Note: In some cases this is unnecessary, because scrollbar-width:none
// makes us suppress scrollbars in CreateAnonymousContent. But if this frame
// initially had a non-'none' scrollbar-width and dynamically changed to
// 'none', then we'll need to handle it here.
const auto scrollbarWidth = scrollbarStyle->StyleUIReset()->ScrollbarWidth();
if (scrollbarWidth == StyleScrollbarWidth::None) {
mHScrollbar = ShowScrollbar::Never;
mHScrollbarAllowedForScrollingVVInsideLV = false;
mVScrollbar = ShowScrollbar::Never;
mVScrollbarAllowedForScrollingVVInsideLV = false;
}
mScrollbarGutter = aFrame->ComputeStableScrollbarGutter(
scrollbarWidth, scrollbarStyle->StyleDisplay()->mScrollbarGutter);
}
} // namespace mozilla
static nsSize ComputeInsideBorderSize(const ScrollReflowInput& aState,
const nsSize& aDesiredInsideBorderSize) {
// aDesiredInsideBorderSize is the frame size; i.e., it includes
// borders and padding (but the scrolled child doesn't have
// borders). The scrolled child has the same padding as us.
const WritingMode wm = aState.mReflowInput.GetWritingMode();
const LogicalSize desiredInsideBorderSize(wm, aDesiredInsideBorderSize);
LogicalSize contentSize = aState.mReflowInput.ComputedSize();
const LogicalMargin padding = aState.mReflowInput.ComputedLogicalPadding(wm);
if (contentSize.ISize(wm) == NS_UNCONSTRAINEDSIZE) {
contentSize.ISize(wm) =
desiredInsideBorderSize.ISize(wm) - padding.IStartEnd(wm);
}
if (contentSize.BSize(wm) == NS_UNCONSTRAINEDSIZE) {
contentSize.BSize(wm) =
desiredInsideBorderSize.BSize(wm) - padding.BStartEnd(wm);
}
contentSize.ISize(wm) =
aState.mReflowInput.ApplyMinMaxISize(contentSize.ISize(wm));
contentSize.BSize(wm) =
aState.mReflowInput.ApplyMinMaxBSize(contentSize.BSize(wm));
return (contentSize + padding.Size(wm)).GetPhysicalSize(wm);
}
/**
* Assuming that we know the metrics for our wrapped frame and
* whether the horizontal and/or vertical scrollbars are present,
* compute the resulting layout and return true if the layout is
* consistent. If the layout is consistent then we fill in the
* computed fields of the ScrollReflowInput.
*
* The layout is consistent when both scrollbars are showing if and only
* if they should be showing. A horizontal scrollbar should be showing if all
* following conditions are met:
* 1) the style is not HIDDEN
* 2) our inside-border height is at least the scrollbar height (i.e., the
* scrollbar fits vertically)
* 3) the style is SCROLL, or the kid's overflow-area XMost is
* greater than the scrollport width
*
* @param aForce if true, then we just assume the layout is consistent.
*/
bool ScrollContainerFrame::TryLayout(ScrollReflowInput& aState,
ReflowOutput* aKidMetrics,
bool aAssumeHScroll, bool aAssumeVScroll,
bool aForce) {
if ((aState.mVScrollbar == ShowScrollbar::Never && aAssumeVScroll) ||
(aState.mHScrollbar == ShowScrollbar::Never && aAssumeHScroll)) {
NS_ASSERTION(!aForce, "Shouldn't be forcing a hidden scrollbar to show!");
return false;
}
const auto wm = GetWritingMode();
const nsMargin scrollbarGutter = aState.ScrollbarGutter(
aAssumeVScroll, aAssumeHScroll, IsScrollbarOnRight());
const LogicalMargin logicalScrollbarGutter(wm, scrollbarGutter);
const bool inlineEndsGutterChanged =
aState.mScrollbarGutterFromLastReflow.IStartEnd(wm) !=
logicalScrollbarGutter.IStartEnd(wm);
const bool blockEndsGutterChanged =
aState.mScrollbarGutterFromLastReflow.BStartEnd(wm) !=
logicalScrollbarGutter.BStartEnd(wm);
const bool shouldReflowScrolledFrame =
inlineEndsGutterChanged ||
(blockEndsGutterChanged && ScrolledContentDependsOnBSize(aState));
if (shouldReflowScrolledFrame) {
if (blockEndsGutterChanged) {
nsLayoutUtils::MarkIntrinsicISizesDirtyIfDependentOnBSize(mScrolledFrame);
}
aKidMetrics->mOverflowAreas.Clear();
ROOT_SCROLLBAR_LOG(
"TryLayout reflowing scrolled frame with scrollbars h=%d, v=%d\n",
aAssumeHScroll, aAssumeVScroll);
ReflowScrolledFrame(aState, aAssumeHScroll, aAssumeVScroll, aKidMetrics);
}
const nsSize scrollbarGutterSize(scrollbarGutter.LeftRight(),
scrollbarGutter.TopBottom());
// First, compute our inside-border size and scrollport size
nsSize kidSize = GetContainSizeAxes().ContainSize(
aKidMetrics->PhysicalSize(), *aState.mReflowInput.mFrame);
const nsSize desiredInsideBorderSize = kidSize + scrollbarGutterSize;
aState.mInsideBorderSize =
ComputeInsideBorderSize(aState, desiredInsideBorderSize);
nsSize layoutSize =
mIsUsingMinimumScaleSize ? mMinimumScaleSize : aState.mInsideBorderSize;
const nsSize scrollPortSize =
Max(nsSize(0, 0), layoutSize - scrollbarGutterSize);
if (mIsUsingMinimumScaleSize) {
mICBSize =
Max(nsSize(0, 0), aState.mInsideBorderSize - scrollbarGutterSize);
}
nsSize visualViewportSize = scrollPortSize;
ROOT_SCROLLBAR_LOG("TryLayout with VV %s\n",
ToString(visualViewportSize).c_str());
mozilla::PresShell* presShell = PresShell();
// Note: we check for a non-null MobileViepwortManager here, but ideally we
// should be able to drop that clause as well. It's just that in some cases
// with extension popups the composition size comes back as stale, because
// the content viewer is only resized after the popup contents are reflowed.
// That case also happens to have no APZ and no MVM, so we use that as a
// way to detect the scenario. Bug 1648669 tracks removing this clause.
if (mIsRoot && presShell->GetMobileViewportManager()) {
visualViewportSize = nsLayoutUtils::CalculateCompositionSizeForFrame(
this, false, &layoutSize);
visualViewportSize =
Max(nsSize(0, 0), visualViewportSize - scrollbarGutterSize);
float resolution = presShell->GetResolution();
visualViewportSize.width /= resolution;
visualViewportSize.height /= resolution;
ROOT_SCROLLBAR_LOG("TryLayout now with VV %s\n",
ToString(visualViewportSize).c_str());
}
nsRect overflowRect = aState.mContentsOverflowAreas.ScrollableOverflow();
// If the content height expanded by the minimum-scale will be taller than
// the scrollable overflow area, we need to expand the area here to tell
// properly whether we need to render the overlay vertical scrollbar.
// NOTE: This expanded size should NOT be used for non-overley scrollbars
// cases since putting the vertical non-overlay scrollbar will make the
// content width narrow a little bit, which in turn the minimum scale value
// becomes a bit bigger than before, then the vertical scrollbar is no longer
// needed, which means the content width becomes the original width, then the
// minimum-scale is changed to the original one, and so forth.
if (UsesOverlayScrollbars() && mIsUsingMinimumScaleSize &&
mMinimumScaleSize.height > overflowRect.YMost()) {
overflowRect.height += mMinimumScaleSize.height - overflowRect.YMost();
}
nsRect scrolledRect =
GetUnsnappedScrolledRectInternal(overflowRect, scrollPortSize);
ROOT_SCROLLBAR_LOG(
"TryLayout scrolledRect:%s overflowRect:%s scrollportSize:%s\n",
ToString(scrolledRect).c_str(), ToString(overflowRect).c_str(),
ToString(scrollPortSize).c_str());
nscoord oneDevPixel = PresContext()->DevPixelsToAppUnits(1);
bool showHScrollbar = aAssumeHScroll;
bool showVScrollbar = aAssumeVScroll;
if (!aForce) {
nsSize sizeToCompare = visualViewportSize;
if (gfxPlatform::UseDesktopZoomingScrollbars()) {
sizeToCompare = scrollPortSize;
}
// No need to compute showHScrollbar if we got ShowScrollbar::Never.
if (aState.mHScrollbar != ShowScrollbar::Never) {
showHScrollbar =
aState.mHScrollbar == ShowScrollbar::Always ||
scrolledRect.XMost() >= sizeToCompare.width + oneDevPixel ||
scrolledRect.x <= -oneDevPixel;
// TODO(emilio): This should probably check this scrollbar's minimum size
// in both axes, for consistency?
if (aState.mHScrollbar == ShowScrollbar::Auto &&
scrollPortSize.width < aState.HScrollbarMinWidth()) {
showHScrollbar = false;
}
ROOT_SCROLLBAR_LOG("TryLayout wants H Scrollbar: %d =? %d\n",
showHScrollbar, aAssumeHScroll);
}
// No need to compute showVScrollbar if we got ShowScrollbar::Never.
if (aState.mVScrollbar != ShowScrollbar::Never) {
showVScrollbar =
aState.mVScrollbar == ShowScrollbar::Always ||
scrolledRect.YMost() >= sizeToCompare.height + oneDevPixel ||
scrolledRect.y <= -oneDevPixel;
// TODO(emilio): This should probably check this scrollbar's minimum size
// in both axes, for consistency?
if (aState.mVScrollbar == ShowScrollbar::Auto &&
scrollPortSize.height < aState.VScrollbarMinHeight()) {
showVScrollbar = false;
}
ROOT_SCROLLBAR_LOG("TryLayout wants V Scrollbar: %d =? %d\n",
showVScrollbar, aAssumeVScroll);
}
if (showHScrollbar != aAssumeHScroll || showVScrollbar != aAssumeVScroll) {
const nsMargin wantedScrollbarGutter = aState.ScrollbarGutter(
showVScrollbar, showHScrollbar, IsScrollbarOnRight());
// We report an inconsistent layout only when the desired visibility of
// the scrollbars can change the size of the scrollbar gutters.
if (scrollbarGutter != wantedScrollbarGutter) {
return false;
}
}
}
// If we reach here, the layout is consistent. Record the desired visibility
// of the scrollbars.
aState.mShowHScrollbar = showHScrollbar;
aState.mShowVScrollbar = showVScrollbar;
const nsPoint scrollPortOrigin(
aState.mComputedBorder.left + scrollbarGutter.left,
aState.mComputedBorder.top + scrollbarGutter.top);
SetScrollPort(nsRect(scrollPortOrigin, scrollPortSize));
if (mIsRoot && gfxPlatform::UseDesktopZoomingScrollbars()) {
bool vvChanged = true;
const bool overlay = aState.OverlayScrollbars();
// This loop can run at most twice since we can only add a scrollbar once.
// At this point we've already decided that this layout is consistent so we
// will return true. Scrollbars added here never take up layout space even
// if they are layout scrollbars so any changes made here will not make us
// return false.
while (vvChanged) {
vvChanged = false;
if (!aState.mShowHScrollbar &&
aState.mHScrollbarAllowedForScrollingVVInsideLV) {
if (ScrollPort().width >= visualViewportSize.width + oneDevPixel &&
(overlay ||
visualViewportSize.width >= aState.HScrollbarMinWidth())) {
vvChanged = true;
if (!overlay) {
visualViewportSize.height -= aState.HScrollbarPrefHeight();
}
aState.mShowHScrollbar = true;
aState.mOnlyNeedHScrollbarToScrollVVInsideLV = true;
ROOT_SCROLLBAR_LOG("TryLayout added H scrollbar for VV, VV now %s\n",
ToString(visualViewportSize).c_str());
}
}
if (!aState.mShowVScrollbar &&
aState.mVScrollbarAllowedForScrollingVVInsideLV) {
if (ScrollPort().height >= visualViewportSize.height + oneDevPixel &&
(overlay ||
visualViewportSize.height >= aState.VScrollbarMinHeight())) {
vvChanged = true;
if (!overlay) {
visualViewportSize.width -= aState.VScrollbarPrefWidth();
}
aState.mShowVScrollbar = true;
aState.mOnlyNeedVScrollbarToScrollVVInsideLV = true;
ROOT_SCROLLBAR_LOG("TryLayout added V scrollbar for VV, VV now %s\n",
ToString(visualViewportSize).c_str());
}
}
}
}
return true;
}
bool ScrollContainerFrame::ScrolledContentDependsOnBSize(
const ScrollReflowInput& aState) const {
return mScrolledFrame->HasAnyStateBits(
NS_FRAME_CONTAINS_RELATIVE_BSIZE |
NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE) ||
aState.mReflowInput.ComputedBSize() != NS_UNCONSTRAINEDSIZE ||
aState.mReflowInput.ComputedMinBSize() > 0 ||
aState.mReflowInput.ComputedMaxBSize() != NS_UNCONSTRAINEDSIZE;
}
void ScrollContainerFrame::ReflowScrolledFrame(ScrollReflowInput& aState,
bool aAssumeHScroll,
bool aAssumeVScroll,
ReflowOutput* aMetrics) {
const WritingMode wm = GetWritingMode();
// these could be NS_UNCONSTRAINEDSIZE ... std::min arithmetic should
// be OK
LogicalMargin padding = aState.mReflowInput.ComputedLogicalPadding(wm);
nscoord availISize =
aState.mReflowInput.ComputedISize() + padding.IStartEnd(wm);
nscoord computedBSize = aState.mReflowInput.ComputedBSize();
nscoord computedMinBSize = aState.mReflowInput.ComputedMinBSize();
nscoord computedMaxBSize = aState.mReflowInput.ComputedMaxBSize();
if (!ShouldPropagateComputedBSizeToScrolledContent()) {
computedBSize = NS_UNCONSTRAINEDSIZE;
computedMinBSize = 0;
computedMaxBSize = NS_UNCONSTRAINEDSIZE;
}
const LogicalMargin scrollbarGutter(
wm, aState.ScrollbarGutter(aAssumeVScroll, aAssumeHScroll,
IsScrollbarOnRight()));
if (const nscoord inlineEndsGutter = scrollbarGutter.IStartEnd(wm);
inlineEndsGutter > 0) {
availISize = std::max(0, availISize - inlineEndsGutter);
}
if (const nscoord blockEndsGutter = scrollbarGutter.BStartEnd(wm);
blockEndsGutter > 0) {
if (computedBSize != NS_UNCONSTRAINEDSIZE) {
computedBSize = std::max(0, computedBSize - blockEndsGutter);
}
computedMinBSize = std::max(0, computedMinBSize - blockEndsGutter);
if (computedMaxBSize != NS_UNCONSTRAINEDSIZE) {
computedMaxBSize = std::max(0, computedMaxBSize - blockEndsGutter);
}
}
nsPresContext* presContext = PresContext();
// Pass InitFlags::CallerWillInit so we can pass in the correct padding.
ReflowInput kidReflowInput(presContext, aState.mReflowInput, mScrolledFrame,
LogicalSize(wm, availISize, NS_UNCONSTRAINEDSIZE),
Nothing(), ReflowInput::InitFlag::CallerWillInit);
const WritingMode kidWM = kidReflowInput.GetWritingMode();
kidReflowInput.Init(presContext, Nothing(), Nothing(),
Some(padding.ConvertTo(kidWM, wm)));
kidReflowInput.mFlags.mAssumingHScrollbar = aAssumeHScroll;
kidReflowInput.mFlags.mAssumingVScrollbar = aAssumeVScroll;
kidReflowInput.mFlags.mTreatBSizeAsIndefinite =
aState.mReflowInput.mFlags.mTreatBSizeAsIndefinite;
kidReflowInput.SetComputedBSize(computedBSize);
kidReflowInput.SetComputedMinBSize(computedMinBSize);
kidReflowInput.SetComputedMaxBSize(computedMaxBSize);
if (aState.mReflowInput.IsBResizeForWM(kidWM)) {
kidReflowInput.SetBResize(true);
}
if (aState.mReflowInput.IsBResizeForPercentagesForWM(kidWM)) {
kidReflowInput.SetBResizeForPercentages(true);
}
// Temporarily set mHasHorizontalScrollbar/mHasVerticalScrollbar to
// reflect our assumptions while we reflow the child.
bool didHaveHorizontalScrollbar = mHasHorizontalScrollbar;
bool didHaveVerticalScrollbar = mHasVerticalScrollbar;
mHasHorizontalScrollbar = aAssumeHScroll;
mHasVerticalScrollbar = aAssumeVScroll;
nsReflowStatus status;
// No need to pass a true container-size to ReflowChild or
// FinishReflowChild, because it's only used there when positioning
// the frame (i.e. if ReflowChildFlags::NoMoveFrame isn't set)
const nsSize dummyContainerSize;
ReflowChild(mScrolledFrame, presContext, *aMetrics, kidReflowInput, wm,
LogicalPoint(wm), dummyContainerSize,
ReflowChildFlags::NoMoveFrame, status);
mHasHorizontalScrollbar = didHaveHorizontalScrollbar;
mHasVerticalScrollbar = didHaveVerticalScrollbar;
// Don't resize or position the view (if any) because we're going to resize
// it to the correct size anyway in PlaceScrollArea. Allowing it to
// resize here would size it to the natural height of the frame,
// which will usually be different from the scrollport height;
// invalidating the difference will cause unnecessary repainting.
FinishReflowChild(
mScrolledFrame, presContext, *aMetrics, &kidReflowInput, wm,
LogicalPoint(wm), dummyContainerSize,
ReflowChildFlags::NoMoveFrame | ReflowChildFlags::NoSizeView);
if (mScrolledFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
// Propagate NS_FRAME_CONTAINS_RELATIVE_BSIZE from our inner scrolled frame
// to ourselves so that our containing block is aware of it.
//
// Note: If the scrolled frame has any child whose block-size depends on the
// containing block's block-size, the NS_FRAME_CONTAINS_RELATIVE_BSIZE bit
// is set on the scrolled frame when initializing the child's ReflowInput in
// ReflowInput::InitResizeFlags(). Therefore, we propagate the bit here
// after we reflowed the scrolled frame.
AddStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
}
// XXX Some frames (e.g. nsFrameFrame, nsTextFrame) don't
// bother setting their mOverflowArea. This is wrong because every frame
// should always set mOverflowArea. In fact nsFrameFrame doesn't
// support the 'outline' property because of this. Rather than fix the
// world right now, just fix up the overflow area if necessary. Note that we
// don't check HasOverflowRect() because it could be set even though the
// overflow area doesn't include the frame bounds.
aMetrics->UnionOverflowAreasWithDesiredBounds();
aState.mContentsOverflowAreas = aMetrics->mOverflowAreas;
aState.mScrollbarGutterFromLastReflow = scrollbarGutter;
aState.mReflowedContentsWithHScrollbar = aAssumeHScroll;
aState.mReflowedContentsWithVScrollbar = aAssumeVScroll;
}
bool ScrollContainerFrame::GuessHScrollbarNeeded(
const ScrollReflowInput& aState) {
if (aState.mHScrollbar != ShowScrollbar::Auto) {
// no guessing required
return aState.mHScrollbar == ShowScrollbar::Always;
}
// We only care about scrollbars that might take up space when trying to guess
// if we need a scrollbar, so we ignore scrollbars only created to scroll the
// visual viewport inside the layout viewport because they take up no layout
// space.
return mHasHorizontalScrollbar && !mOnlyNeedHScrollbarToScrollVVInsideLV;
}
bool ScrollContainerFrame::GuessVScrollbarNeeded(
const ScrollReflowInput& aState) {
if (aState.mVScrollbar != ShowScrollbar::Auto) {
// no guessing required
return aState.mVScrollbar == ShowScrollbar::Always;
}
// If we've had at least one non-initial reflow, then just assume
// the state of the vertical scrollbar will be what we determined
// last time.
if (mHadNonInitialReflow) {
// We only care about scrollbars that might take up space when trying to
// guess if we need a scrollbar, so we ignore scrollbars only created to
// scroll the visual viewport inside the layout viewport because they take
// up no layout space.
return mHasVerticalScrollbar && !mOnlyNeedVScrollbarToScrollVVInsideLV;
}
// If this is the initial reflow, guess false because usually
// we have very little content by then.
if (InInitialReflow()) {
return false;
}
if (mIsRoot) {
nsIFrame* f = mScrolledFrame->PrincipalChildList().FirstChild();
if (f && f->IsSVGOuterSVGFrame() &&
static_cast<SVGOuterSVGFrame*>(f)->VerticalScrollbarNotNeeded()) {
// Common SVG case - avoid a bad guess.
return false;