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/. */
6 
7 #include "nsProgressFrame.h"
8 
9 #include "mozilla/PresShell.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/HTMLProgressElement.h"
13 #include "nsIContent.h"
14 #include "nsPresContext.h"
15 #include "nsGkAtoms.h"
16 #include "nsNameSpaceManager.h"
17 #include "nsNodeInfoManager.h"
18 #include "nsContentCreatorFunctions.h"
19 #include "nsFontMetrics.h"
20 #include "nsCSSPseudoElements.h"
21 #include "nsStyleConsts.h"
22 #include <algorithm>
23 
24 using namespace mozilla;
25 using namespace mozilla::dom;
26 
NS_NewProgressFrame(PresShell * aPresShell,ComputedStyle * aStyle)27 nsIFrame* NS_NewProgressFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
28   return new (aPresShell) nsProgressFrame(aStyle, aPresShell->GetPresContext());
29 }
30 
NS_IMPL_FRAMEARENA_HELPERS(nsProgressFrame)31 NS_IMPL_FRAMEARENA_HELPERS(nsProgressFrame)
32 
33 nsProgressFrame::nsProgressFrame(ComputedStyle* aStyle,
34                                  nsPresContext* aPresContext)
35     : nsContainerFrame(aStyle, aPresContext, kClassID), mBarDiv(nullptr) {}
36 
37 nsProgressFrame::~nsProgressFrame() = default;
38 
DestroyFrom(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)39 void nsProgressFrame::DestroyFrom(nsIFrame* aDestructRoot,
40                                   PostDestroyData& aPostDestroyData) {
41   NS_ASSERTION(!GetPrevContinuation(),
42                "nsProgressFrame should not have continuations; if it does we "
43                "need to call RegUnregAccessKey only for the first.");
44   aPostDestroyData.AddAnonymousContent(mBarDiv.forget());
45   nsContainerFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
46 }
47 
CreateAnonymousContent(nsTArray<ContentInfo> & aElements)48 nsresult nsProgressFrame::CreateAnonymousContent(
49     nsTArray<ContentInfo>& aElements) {
50   // Create the progress bar div.
51   nsCOMPtr<Document> doc = mContent->GetComposedDoc();
52   mBarDiv = doc->CreateHTMLElement(nsGkAtoms::div);
53 
54   // Associate ::-moz-progress-bar pseudo-element to the anonymous child.
55   mBarDiv->SetPseudoElementType(PseudoStyleType::mozProgressBar);
56 
57   // XXX(Bug 1631371) Check if this should use a fallible operation as it
58   // pretended earlier, or change the return type to void.
59   aElements.AppendElement(mBarDiv);
60 
61   return NS_OK;
62 }
63 
AppendAnonymousContentTo(nsTArray<nsIContent * > & aElements,uint32_t aFilter)64 void nsProgressFrame::AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
65                                                uint32_t aFilter) {
66   if (mBarDiv) {
67     aElements.AppendElement(mBarDiv);
68   }
69 }
70 
71 NS_QUERYFRAME_HEAD(nsProgressFrame)
NS_QUERYFRAME_ENTRY(nsProgressFrame)72   NS_QUERYFRAME_ENTRY(nsProgressFrame)
73   NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
74 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
75 
76 void nsProgressFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
77                                        const nsDisplayListSet& aLists) {
78   BuildDisplayListForInline(aBuilder, aLists);
79 }
80 
Reflow(nsPresContext * aPresContext,ReflowOutput & aDesiredSize,const ReflowInput & aReflowInput,nsReflowStatus & aStatus)81 void nsProgressFrame::Reflow(nsPresContext* aPresContext,
82                              ReflowOutput& aDesiredSize,
83                              const ReflowInput& aReflowInput,
84                              nsReflowStatus& aStatus) {
85   MarkInReflow();
86   DO_GLOBAL_REFLOW_COUNT("nsProgressFrame");
87   DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
88   MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
89 
90   NS_ASSERTION(mBarDiv, "Progress bar div must exist!");
91   NS_ASSERTION(
92       PrincipalChildList().GetLength() == 1 &&
93           PrincipalChildList().FirstChild() == mBarDiv->GetPrimaryFrame(),
94       "unexpected child frames");
95   NS_ASSERTION(!GetPrevContinuation(),
96                "nsProgressFrame should not have continuations; if it does we "
97                "need to call RegUnregAccessKey only for the first.");
98 
99   const auto wm = aReflowInput.GetWritingMode();
100   aDesiredSize.SetSize(wm, aReflowInput.ComputedSizeWithBorderPadding(wm));
101   aDesiredSize.SetOverflowAreasToDesiredBounds();
102 
103   for (auto childFrame : PrincipalChildList()) {
104     ReflowChildFrame(childFrame, aPresContext, aReflowInput, aStatus);
105     ConsiderChildOverflow(aDesiredSize.mOverflowAreas, childFrame);
106   }
107 
108   FinishAndStoreOverflow(&aDesiredSize);
109 
110   aStatus.Reset();  // This type of frame can't be split.
111 
112   NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aDesiredSize);
113 }
114 
ReflowChildFrame(nsIFrame * aChild,nsPresContext * aPresContext,const ReflowInput & aReflowInput,nsReflowStatus & aStatus)115 void nsProgressFrame::ReflowChildFrame(nsIFrame* aChild,
116                                        nsPresContext* aPresContext,
117                                        const ReflowInput& aReflowInput,
118                                        nsReflowStatus& aStatus) {
119   bool vertical = ResolvedOrientationIsVertical();
120   WritingMode wm = aChild->GetWritingMode();
121   LogicalSize availSize = aReflowInput.ComputedSize(wm);
122   availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
123   ReflowInput reflowInput(aPresContext, aReflowInput, aChild, availSize);
124   nscoord size =
125       vertical ? aReflowInput.ComputedHeight() : aReflowInput.ComputedWidth();
126   nscoord xoffset = aReflowInput.ComputedPhysicalBorderPadding().left;
127   nscoord yoffset = aReflowInput.ComputedPhysicalBorderPadding().top;
128 
129   double position = static_cast<HTMLProgressElement*>(GetContent())->Position();
130 
131   // Force the bar's size to match the current progress.
132   // When indeterminate, the progress' size will be 100%.
133   if (position >= 0.0) {
134     size *= position;
135   }
136 
137   if (!vertical && wm.IsPhysicalRTL()) {
138     xoffset += aReflowInput.ComputedWidth() - size;
139   }
140 
141   // The bar size is fixed in these cases:
142   // - the progress position is determined: the bar size is fixed according
143   //   to it's value.
144   // - the progress position is indeterminate and the bar appearance should be
145   //   shown as native: the bar size is forced to 100%.
146   // Otherwise (when the progress is indeterminate and the bar appearance isn't
147   // native), the bar size isn't fixed and can be set by the author.
148   if (position != -1 || ShouldUseNativeStyle()) {
149     if (vertical) {
150       // We want the bar to begin at the bottom.
151       yoffset += aReflowInput.ComputedHeight() - size;
152 
153       size -= reflowInput.ComputedPhysicalMargin().TopBottom() +
154               reflowInput.ComputedPhysicalBorderPadding().TopBottom();
155       size = std::max(size, 0);
156       reflowInput.SetComputedHeight(size);
157     } else {
158       size -= reflowInput.ComputedPhysicalMargin().LeftRight() +
159               reflowInput.ComputedPhysicalBorderPadding().LeftRight();
160       size = std::max(size, 0);
161       reflowInput.SetComputedWidth(size);
162     }
163   } else if (vertical) {
164     // For vertical progress bars, we need to position the bar specificly when
165     // the width isn't constrained (position == -1 and !ShouldUseNativeStyle())
166     // because aReflowInput.ComputedHeight() - size == 0.
167     yoffset += aReflowInput.ComputedHeight() - reflowInput.ComputedHeight();
168   }
169 
170   xoffset += reflowInput.ComputedPhysicalMargin().left;
171   yoffset += reflowInput.ComputedPhysicalMargin().top;
172 
173   ReflowOutput barDesiredSize(aReflowInput);
174   ReflowChild(aChild, aPresContext, barDesiredSize, reflowInput, xoffset,
175               yoffset, ReflowChildFlags::Default, aStatus);
176   FinishReflowChild(aChild, aPresContext, barDesiredSize, &reflowInput, xoffset,
177                     yoffset, ReflowChildFlags::Default);
178 }
179 
AttributeChanged(int32_t aNameSpaceID,nsAtom * aAttribute,int32_t aModType)180 nsresult nsProgressFrame::AttributeChanged(int32_t aNameSpaceID,
181                                            nsAtom* aAttribute,
182                                            int32_t aModType) {
183   NS_ASSERTION(mBarDiv, "Progress bar div must exist!");
184 
185   if (aNameSpaceID == kNameSpaceID_None &&
186       (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max)) {
187     auto presShell = PresShell();
188     for (auto childFrame : PrincipalChildList()) {
189       presShell->FrameNeedsReflow(childFrame, IntrinsicDirty::Resize,
190                                   NS_FRAME_IS_DIRTY);
191     }
192     InvalidateFrame();
193   }
194 
195   return nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
196 }
197 
ComputeAutoSize(gfxContext * aRenderingContext,WritingMode aWM,const LogicalSize & aCBSize,nscoord aAvailableISize,const LogicalSize & aMargin,const LogicalSize & aBorderPadding,const StyleSizeOverrides & aSizeOverrides,ComputeSizeFlags aFlags)198 LogicalSize nsProgressFrame::ComputeAutoSize(
199     gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
200     nscoord aAvailableISize, const LogicalSize& aMargin,
201     const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
202     ComputeSizeFlags aFlags) {
203   const WritingMode wm = GetWritingMode();
204   LogicalSize autoSize(wm);
205   autoSize.BSize(wm) = autoSize.ISize(wm) =
206       StyleFont()
207           ->mFont.size.ScaledBy(nsLayoutUtils::FontSizeInflationFor(this))
208           .ToAppUnits();  // 1em
209 
210   if (ResolvedOrientationIsVertical() == wm.IsVertical()) {
211     autoSize.ISize(wm) *= 10;  // 10em
212   } else {
213     autoSize.BSize(wm) *= 10;  // 10em
214   }
215 
216   return autoSize.ConvertTo(aWM, wm);
217 }
218 
GetMinISize(gfxContext * aRenderingContext)219 nscoord nsProgressFrame::GetMinISize(gfxContext* aRenderingContext) {
220   RefPtr<nsFontMetrics> fontMet =
221       nsLayoutUtils::GetFontMetricsForFrame(this, 1.0f);
222 
223   nscoord minISize = fontMet->Font().size.ToAppUnits();  // 1em
224 
225   if (ResolvedOrientationIsVertical() == GetWritingMode().IsVertical()) {
226     // The orientation is inline
227     minISize *= 10;  // 10em
228   }
229 
230   return minISize;
231 }
232 
GetPrefISize(gfxContext * aRenderingContext)233 nscoord nsProgressFrame::GetPrefISize(gfxContext* aRenderingContext) {
234   return GetMinISize(aRenderingContext);
235 }
236 
ShouldUseNativeStyle() const237 bool nsProgressFrame::ShouldUseNativeStyle() const {
238   nsIFrame* barFrame = PrincipalChildList().FirstChild();
239 
240   // Use the native style if these conditions are satisfied:
241   // - both frames use the native appearance;
242   // - neither frame has author specified rules setting the border or the
243   //   background.
244   return StyleDisplay()->EffectiveAppearance() ==
245              StyleAppearance::ProgressBar &&
246          !PresContext()->HasAuthorSpecifiedRules(
247              this, NS_AUTHOR_SPECIFIED_BORDER_OR_BACKGROUND) &&
248          barFrame &&
249          barFrame->StyleDisplay()->EffectiveAppearance() ==
250              StyleAppearance::Progresschunk &&
251          !PresContext()->HasAuthorSpecifiedRules(
252              barFrame, NS_AUTHOR_SPECIFIED_BORDER_OR_BACKGROUND);
253 }
254