GridTracks.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- Mode: C++; tab-width: 8; 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 "GridTracks.h"
  6. #include "GridDimension.h"
  7. #include "GridTrack.h"
  8. #include "mozilla/dom/GridBinding.h"
  9. #include "nsGridContainerFrame.h"
  10. namespace mozilla {
  11. namespace dom {
  12. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GridTracks, mParent, mTracks)
  13. NS_IMPL_CYCLE_COLLECTING_ADDREF(GridTracks)
  14. NS_IMPL_CYCLE_COLLECTING_RELEASE(GridTracks)
  15. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GridTracks)
  16. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  17. NS_INTERFACE_MAP_ENTRY(nsISupports)
  18. NS_INTERFACE_MAP_END
  19. GridTracks::GridTracks(GridDimension *aParent)
  20. : mParent(aParent)
  21. {
  22. MOZ_ASSERT(aParent,
  23. "Should never be instantiated with a null GridDimension");
  24. }
  25. GridTracks::~GridTracks()
  26. {
  27. }
  28. JSObject*
  29. GridTracks::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  30. {
  31. return GridTracksBinding::Wrap(aCx, this, aGivenProto);
  32. }
  33. uint32_t
  34. GridTracks::Length() const
  35. {
  36. return mTracks.Length();
  37. }
  38. GridTrack*
  39. GridTracks::Item(uint32_t aIndex)
  40. {
  41. return mTracks.SafeElementAt(aIndex);
  42. }
  43. GridTrack*
  44. GridTracks::IndexedGetter(uint32_t aIndex,
  45. bool& aFound)
  46. {
  47. aFound = aIndex < mTracks.Length();
  48. if (!aFound) {
  49. return nullptr;
  50. }
  51. return mTracks[aIndex];
  52. }
  53. void
  54. GridTracks::SetTrackInfo(const ComputedGridTrackInfo* aTrackInfo)
  55. {
  56. // rebuild the tracks based on aTrackInfo
  57. mTracks.Clear();
  58. if (!aTrackInfo) {
  59. return;
  60. }
  61. nscoord lastTrackEdge = 0;
  62. uint32_t repeatIndex = 0;
  63. auto AppendRemovedAutoFits = [this, &aTrackInfo, &lastTrackEdge,
  64. &repeatIndex]()
  65. {
  66. uint32_t numRepeatTracks = aTrackInfo->mRemovedRepeatTracks.Length();
  67. // Add in removed auto-fit tracks
  68. while (repeatIndex < numRepeatTracks &&
  69. aTrackInfo->mRemovedRepeatTracks[repeatIndex]) {
  70. RefPtr<GridTrack> track = new GridTrack(this);
  71. mTracks.AppendElement(track);
  72. track->SetTrackValues(
  73. nsPresContext::AppUnitsToDoubleCSSPixels(lastTrackEdge),
  74. nsPresContext::AppUnitsToDoubleCSSPixels(0),
  75. GridDeclaration::Explicit,
  76. GridTrackState::Removed
  77. );
  78. repeatIndex++;
  79. }
  80. repeatIndex++;
  81. };
  82. for (size_t i = aTrackInfo->mStartFragmentTrack;
  83. i < aTrackInfo->mEndFragmentTrack;
  84. i++) {
  85. if (i >= aTrackInfo->mRepeatFirstTrack) {
  86. // Append removed auto-fit tracks, if appropriate. The
  87. // AppendRemovedAutoFits function exits early once it has been called
  88. // aTrackInfo->mRemovedRepeatTracks.Length() times -- a check we don't
  89. // replicate here or at subsequent calling sites.
  90. AppendRemovedAutoFits();
  91. }
  92. RefPtr<GridTrack> track = new GridTrack(this);
  93. mTracks.AppendElement(track);
  94. track->SetTrackValues(
  95. nsPresContext::AppUnitsToDoubleCSSPixels(aTrackInfo->mPositions[i]),
  96. nsPresContext::AppUnitsToDoubleCSSPixels(aTrackInfo->mSizes[i]),
  97. (
  98. // Implicit if index is before the first explicit track, or after
  99. // the last explicit track.
  100. (i < aTrackInfo->mNumLeadingImplicitTracks) ||
  101. (i >= aTrackInfo->mNumLeadingImplicitTracks +
  102. aTrackInfo->mNumExplicitTracks) ?
  103. GridDeclaration::Implicit :
  104. GridDeclaration::Explicit
  105. ),
  106. GridTrackState(aTrackInfo->mStates[i])
  107. );
  108. lastTrackEdge = aTrackInfo->mPositions[i] + aTrackInfo->mSizes[i];
  109. }
  110. // Append any trailing removed auto-fit tracks.
  111. AppendRemovedAutoFits();
  112. }
  113. } // namespace dom
  114. } // namespace mozilla