nsProgressFrame.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsProgressFrame.h"
  6. #include "nsIContent.h"
  7. #include "nsPresContext.h"
  8. #include "nsGkAtoms.h"
  9. #include "nsNameSpaceManager.h"
  10. #include "nsIDocument.h"
  11. #include "nsIPresShell.h"
  12. #include "nsNodeInfoManager.h"
  13. #include "nsContentCreatorFunctions.h"
  14. #include "nsContentUtils.h"
  15. #include "nsFormControlFrame.h"
  16. #include "nsFontMetrics.h"
  17. #include "mozilla/dom/Element.h"
  18. #include "mozilla/dom/HTMLProgressElement.h"
  19. #include "nsContentList.h"
  20. #include "nsCSSPseudoElements.h"
  21. #include "nsStyleSet.h"
  22. #include "mozilla/StyleSetHandle.h"
  23. #include "mozilla/StyleSetHandleInlines.h"
  24. #include "nsThemeConstants.h"
  25. #include <algorithm>
  26. using namespace mozilla;
  27. using namespace mozilla::dom;
  28. nsIFrame*
  29. NS_NewProgressFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  30. {
  31. return new (aPresShell) nsProgressFrame(aContext);
  32. }
  33. NS_IMPL_FRAMEARENA_HELPERS(nsProgressFrame)
  34. nsProgressFrame::nsProgressFrame(nsStyleContext* aContext)
  35. : nsContainerFrame(aContext)
  36. , mBarDiv(nullptr)
  37. {
  38. }
  39. nsProgressFrame::~nsProgressFrame()
  40. {
  41. }
  42. void
  43. nsProgressFrame::DestroyFrom(nsIFrame* aDestructRoot)
  44. {
  45. NS_ASSERTION(!GetPrevContinuation(),
  46. "nsProgressFrame should not have continuations; if it does we "
  47. "need to call RegUnregAccessKey only for the first.");
  48. nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
  49. nsContentUtils::DestroyAnonymousContent(&mBarDiv);
  50. nsContainerFrame::DestroyFrom(aDestructRoot);
  51. }
  52. nsIAtom*
  53. nsProgressFrame::GetType() const
  54. {
  55. return nsGkAtoms::progressFrame;
  56. }
  57. nsresult
  58. nsProgressFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
  59. {
  60. // Create the progress bar div.
  61. nsCOMPtr<nsIDocument> doc = mContent->GetComposedDoc();
  62. mBarDiv = doc->CreateHTMLElement(nsGkAtoms::div);
  63. // Associate ::-moz-progress-bar pseudo-element to the anonymous child.
  64. mBarDiv->SetPseudoElementType(CSSPseudoElementType::mozProgressBar);
  65. if (!aElements.AppendElement(mBarDiv)) {
  66. return NS_ERROR_OUT_OF_MEMORY;
  67. }
  68. return NS_OK;
  69. }
  70. void
  71. nsProgressFrame::AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
  72. uint32_t aFilter)
  73. {
  74. if (mBarDiv) {
  75. aElements.AppendElement(mBarDiv);
  76. }
  77. }
  78. NS_QUERYFRAME_HEAD(nsProgressFrame)
  79. NS_QUERYFRAME_ENTRY(nsProgressFrame)
  80. NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
  81. NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
  82. void
  83. nsProgressFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
  84. const nsDisplayListSet& aLists)
  85. {
  86. BuildDisplayListForInline(aBuilder, aLists);
  87. }
  88. void
  89. nsProgressFrame::Reflow(nsPresContext* aPresContext,
  90. ReflowOutput& aDesiredSize,
  91. const ReflowInput& aReflowInput,
  92. nsReflowStatus& aStatus)
  93. {
  94. MarkInReflow();
  95. DO_GLOBAL_REFLOW_COUNT("nsProgressFrame");
  96. DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
  97. NS_ASSERTION(mBarDiv, "Progress bar div must exist!");
  98. NS_ASSERTION(PrincipalChildList().GetLength() == 1 &&
  99. PrincipalChildList().FirstChild() == mBarDiv->GetPrimaryFrame(),
  100. "unexpected child frames");
  101. NS_ASSERTION(!GetPrevContinuation(),
  102. "nsProgressFrame should not have continuations; if it does we "
  103. "need to call RegUnregAccessKey only for the first.");
  104. if (mState & NS_FRAME_FIRST_REFLOW) {
  105. nsFormControlFrame::RegUnRegAccessKey(this, true);
  106. }
  107. aDesiredSize.SetSize(aReflowInput.GetWritingMode(),
  108. aReflowInput.ComputedSizeWithBorderPadding());
  109. aDesiredSize.SetOverflowAreasToDesiredBounds();
  110. for (auto childFrame : PrincipalChildList()) {
  111. ReflowChildFrame(childFrame, aPresContext, aReflowInput, aStatus);
  112. ConsiderChildOverflow(aDesiredSize.mOverflowAreas, childFrame);
  113. }
  114. FinishAndStoreOverflow(&aDesiredSize);
  115. aStatus = NS_FRAME_COMPLETE;
  116. NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aDesiredSize);
  117. }
  118. void
  119. nsProgressFrame::ReflowChildFrame(nsIFrame* aChild,
  120. nsPresContext* aPresContext,
  121. const ReflowInput& aReflowInput,
  122. nsReflowStatus& aStatus)
  123. {
  124. bool vertical = ResolvedOrientationIsVertical();
  125. WritingMode wm = aChild->GetWritingMode();
  126. LogicalSize availSize = aReflowInput.ComputedSize(wm);
  127. availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
  128. ReflowInput reflowInput(aPresContext, aReflowInput, aChild, availSize);
  129. nscoord size = vertical ? aReflowInput.ComputedHeight()
  130. : aReflowInput.ComputedWidth();
  131. nscoord xoffset = aReflowInput.ComputedPhysicalBorderPadding().left;
  132. nscoord yoffset = aReflowInput.ComputedPhysicalBorderPadding().top;
  133. double position = static_cast<HTMLProgressElement*>(mContent)->Position();
  134. // Force the bar's size to match the current progress.
  135. // When indeterminate, the progress' size will be 100%.
  136. if (position >= 0.0) {
  137. size *= position;
  138. }
  139. if (!vertical && (wm.IsVertical() ? wm.IsVerticalRL() : !wm.IsBidiLTR())) {
  140. xoffset += aReflowInput.ComputedWidth() - size;
  141. }
  142. // The bar size is fixed in these cases:
  143. // - the progress position is determined: the bar size is fixed according
  144. // to it's value.
  145. // - the progress position is indeterminate and the bar appearance should be
  146. // shown as native: the bar size is forced to 100%.
  147. // Otherwise (when the progress is indeterminate and the bar appearance isn't
  148. // native), the bar size isn't fixed and can be set by the author.
  149. if (position != -1 || ShouldUseNativeStyle()) {
  150. if (vertical) {
  151. // We want the bar to begin at the bottom.
  152. yoffset += aReflowInput.ComputedHeight() - size;
  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. xoffset += reflowInput.ComputedPhysicalMargin().left;
  170. yoffset += reflowInput.ComputedPhysicalMargin().top;
  171. ReflowOutput barDesiredSize(aReflowInput);
  172. ReflowChild(aChild, aPresContext, barDesiredSize, reflowInput, xoffset,
  173. yoffset, 0, aStatus);
  174. FinishReflowChild(aChild, aPresContext, barDesiredSize, &reflowInput,
  175. xoffset, yoffset, 0);
  176. }
  177. nsresult
  178. nsProgressFrame::AttributeChanged(int32_t aNameSpaceID,
  179. nsIAtom* aAttribute,
  180. int32_t aModType)
  181. {
  182. NS_ASSERTION(mBarDiv, "Progress bar div must exist!");
  183. if (aNameSpaceID == kNameSpaceID_None &&
  184. (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max)) {
  185. auto shell = PresContext()->PresShell();
  186. for (auto childFrame : PrincipalChildList()) {
  187. shell->FrameNeedsReflow(childFrame, nsIPresShell::eResize,
  188. NS_FRAME_IS_DIRTY);
  189. }
  190. InvalidateFrame();
  191. }
  192. return nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
  193. }
  194. LogicalSize
  195. nsProgressFrame::ComputeAutoSize(nsRenderingContext* aRenderingContext,
  196. WritingMode aWM,
  197. const LogicalSize& aCBSize,
  198. nscoord aAvailableISize,
  199. const LogicalSize& aMargin,
  200. const LogicalSize& aBorder,
  201. const LogicalSize& aPadding,
  202. ComputeSizeFlags aFlags)
  203. {
  204. const WritingMode wm = GetWritingMode();
  205. LogicalSize autoSize(wm);
  206. autoSize.BSize(wm) = autoSize.ISize(wm) =
  207. NSToCoordRound(StyleFont()->mFont.size *
  208. nsLayoutUtils::FontSizeInflationFor(this)); // 1em
  209. if (ResolvedOrientationIsVertical() == wm.IsVertical()) {
  210. autoSize.ISize(wm) *= 10; // 10em
  211. } else {
  212. autoSize.BSize(wm) *= 10; // 10em
  213. }
  214. return autoSize.ConvertTo(aWM, wm);
  215. }
  216. nscoord
  217. nsProgressFrame::GetMinISize(nsRenderingContext *aRenderingContext)
  218. {
  219. RefPtr<nsFontMetrics> fontMet =
  220. nsLayoutUtils::GetFontMetricsForFrame(this, 1.0f);
  221. nscoord minISize = fontMet->Font().size; // 1em
  222. if (ResolvedOrientationIsVertical() == GetWritingMode().IsVertical()) {
  223. // The orientation is inline
  224. minISize *= 10; // 10em
  225. }
  226. return minISize;
  227. }
  228. nscoord
  229. nsProgressFrame::GetPrefISize(nsRenderingContext *aRenderingContext)
  230. {
  231. return GetMinISize(aRenderingContext);
  232. }
  233. bool
  234. nsProgressFrame::ShouldUseNativeStyle() const
  235. {
  236. nsIFrame* barFrame = PrincipalChildList().FirstChild();
  237. // Use the native style if these conditions are satisfied:
  238. // - both frames use the native appearance;
  239. // - neither frame has author specified rules setting the border or the
  240. // background.
  241. return StyleDisplay()->mAppearance == NS_THEME_PROGRESSBAR &&
  242. !PresContext()->HasAuthorSpecifiedRules(this,
  243. NS_AUTHOR_SPECIFIED_BORDER | NS_AUTHOR_SPECIFIED_BACKGROUND) &&
  244. barFrame &&
  245. barFrame->StyleDisplay()->mAppearance == NS_THEME_PROGRESSCHUNK &&
  246. !PresContext()->HasAuthorSpecifiedRules(barFrame,
  247. NS_AUTHOR_SPECIFIED_BORDER | NS_AUTHOR_SPECIFIED_BACKGROUND);
  248. }
  249. Element*
  250. nsProgressFrame::GetPseudoElement(CSSPseudoElementType aType)
  251. {
  252. if (aType == CSSPseudoElementType::mozProgressBar) {
  253. return mBarDiv;
  254. }
  255. return nsContainerFrame::GetPseudoElement(aType);
  256. }