nsScrollbarFrame.h (5071B)
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 // 8 // nsScrollbarFrame 9 // 10 11 #ifndef nsScrollbarFrame_h__ 12 #define nsScrollbarFrame_h__ 13 14 #include "mozilla/Attributes.h" 15 #include "mozilla/ScrollTypes.h" 16 #include "nsContainerFrame.h" 17 #include "nsIAnonymousContentCreator.h" 18 19 class nsIScrollbarMediator; 20 21 namespace mozilla { 22 class PresShell; 23 namespace dom { 24 class Element; 25 } 26 } // namespace mozilla 27 28 nsIFrame* NS_NewScrollbarFrame(mozilla::PresShell* aPresShell, 29 mozilla::ComputedStyle* aStyle); 30 31 class nsScrollbarFrame final : public nsContainerFrame, 32 public nsIAnonymousContentCreator { 33 using Element = mozilla::dom::Element; 34 using CSSIntCoord = mozilla::CSSIntCoord; 35 36 public: 37 explicit nsScrollbarFrame(ComputedStyle* aStyle, nsPresContext* aPresContext) 38 : nsContainerFrame(aStyle, aPresContext, kClassID) {} 39 40 NS_DECL_QUERYFRAME 41 NS_DECL_FRAMEARENA_HELPERS(nsScrollbarFrame) 42 43 #ifdef DEBUG_FRAME_DUMP 44 nsresult GetFrameName(nsAString& aResult) const override { 45 return MakeFrameName(u"ScrollbarFrame"_ns, aResult); 46 } 47 #endif 48 49 // nsIFrame overrides 50 NS_IMETHOD HandlePress(nsPresContext* aPresContext, 51 mozilla::WidgetGUIEvent* aEvent, 52 nsEventStatus* aEventStatus) override; 53 54 NS_IMETHOD HandleMultiplePress(nsPresContext* aPresContext, 55 mozilla::WidgetGUIEvent* aEvent, 56 nsEventStatus* aEventStatus, 57 bool aControlHeld) override; 58 59 MOZ_CAN_RUN_SCRIPT 60 NS_IMETHOD HandleDrag(nsPresContext* aPresContext, 61 mozilla::WidgetGUIEvent* aEvent, 62 nsEventStatus* aEventStatus) override; 63 64 NS_IMETHOD HandleRelease(nsPresContext* aPresContext, 65 mozilla::WidgetGUIEvent* aEvent, 66 nsEventStatus* aEventStatus) override; 67 68 mozilla::StyleScrollbarWidth ScrollbarWidth() const; 69 nscoord ScrollbarTrackSize() const; 70 nsSize ScrollbarMinSize() const; 71 bool IsHorizontal() const; 72 73 void Destroy(DestroyContext&) override; 74 75 void Init(nsIContent* aContent, nsContainerFrame* aParent, 76 nsIFrame* aPrevInFlow) override; 77 78 void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize, 79 const ReflowInput& aReflowInput, 80 nsReflowStatus& aStatus) override; 81 82 void SetOverrideScrollbarMediator(nsIScrollbarMediator*); 83 nsIScrollbarMediator* GetScrollbarMediator(); 84 void WillBecomeActive(); 85 86 void MoveToNewPosition(); 87 int32_t GetButtonScrollDirection() const { return mButtonScrollDirection; } 88 void SetButtonScrollDirectionAndUnit(int32_t aDirection, 89 mozilla::ScrollUnit aUnit) { 90 mButtonScrollDirection = aDirection; 91 mButtonScrollUnit = aUnit; 92 } 93 94 // nsIAnonymousContentCreator 95 nsresult CreateAnonymousContent(nsTArray<ContentInfo>& aElements) override; 96 void AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements, 97 uint32_t aFilter) override; 98 99 void ElementStateChanged(mozilla::dom::ElementState) override; 100 bool HasBeenHovered() const { return mHasBeenHovered; } 101 102 // If we're an horizontal scrollbar, get the vertical one, or viceversa. 103 nsScrollbarFrame* GetOppositeScrollbar() const; 104 void ActivityChanged(bool aIsNowActive); 105 106 // Sets the current scrollbar position. Returns true if the value changed. 107 bool SetCurPos(CSSIntCoord); 108 CSSIntCoord GetCurPos() const { return mCurPos; } 109 bool SetMaxPos(CSSIntCoord); 110 CSSIntCoord GetMaxPos() const { return mMaxPos; } 111 bool SetPageIncrement(CSSIntCoord); 112 CSSIntCoord GetPageIncrement() const { return mPageIncrement; } 113 114 bool SetEnabled(bool); 115 bool IsEnabled() const; 116 bool IsDisabled() const { return !IsEnabled(); } 117 118 protected: 119 void InvalidateForHoverChange(bool aIsNowHovered); 120 void RequestSliderReflow(); 121 122 // TODO(emilio): These probably shouldn't be CSSIntCoords (could just be 123 // nscoords). 124 CSSIntCoord mCurPos = 0; 125 CSSIntCoord mMaxPos = 0; 126 CSSIntCoord mPageIncrement = 0; 127 128 // Direction and unit that our button scrolled us to. 129 // TODO(emilio): Find a better place to store this? 130 int32_t mButtonScrollDirection = 0; 131 mozilla::ScrollUnit mButtonScrollUnit = mozilla::ScrollUnit::DEVICE_PIXELS; 132 // On macOS, overlay scrollbar hover state should be sticky (remain hovered 133 // while we've been hovered at least once). 134 bool mHasBeenHovered = false; 135 136 private: 137 WeakFrame mOverriddenScrollbarMediator; 138 139 nsCOMPtr<Element> mUpTopButton; 140 nsCOMPtr<Element> mDownTopButton; 141 nsCOMPtr<Element> mSlider; 142 nsCOMPtr<Element> mThumb; 143 nsCOMPtr<Element> mUpBottomButton; 144 nsCOMPtr<Element> mDownBottomButton; 145 }; // class nsScrollbarFrame 146 147 #endif