1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_PATTERN_H 18 19 #include "core/components_ng/pattern/grid/grid_accessibility_property.h" 20 #include "core/components_ng/pattern/grid/grid_event_hub.h" 21 #include "core/components_ng/pattern/grid/grid_layout_info.h" 22 #include "core/components_ng/pattern/grid/grid_layout_property.h" 23 #include "core/components_ng/pattern/grid/grid_paint_method.h" 24 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h" 25 26 namespace OHOS::Ace::NG { 27 struct GridItemIndexInfo { 28 int32_t mainIndex = -1; 29 int32_t crossIndex = -1; 30 int32_t mainSpan = -1; 31 int32_t crossSpan = -1; 32 int32_t mainStart = -1; 33 int32_t mainEnd = -1; 34 int32_t crossStart = -1; 35 int32_t crossEnd = -1; 36 }; 37 38 class ACE_EXPORT GridPattern : public ScrollablePattern { 39 DECLARE_ACE_TYPE(GridPattern, ScrollablePattern); 40 41 public: 42 GridPattern() = default; 43 CreateLayoutProperty()44 RefPtr<LayoutProperty> CreateLayoutProperty() override 45 { 46 return MakeRefPtr<GridLayoutProperty>(); 47 } 48 49 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override; 50 51 void BeforeCreateLayoutWrapper() override; 52 53 RefPtr<NodePaintMethod> CreateNodePaintMethod() override; 54 CreateAccessibilityProperty()55 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 56 { 57 return MakeRefPtr<GridAccessibilityProperty>(); 58 } 59 IsScrollable()60 bool IsScrollable() const override 61 { 62 return isConfigScrollable_; 63 } 64 65 DisplayMode GetDefaultScrollBarDisplayMode() const override; 66 SetMultiSelectable(bool multiSelectable)67 void SetMultiSelectable(bool multiSelectable) 68 { 69 multiSelectable_ = multiSelectable; 70 } 71 MultiSelectable()72 bool MultiSelectable() const 73 { 74 return multiSelectable_; 75 } 76 SetSupportAnimation(bool supportAnimation)77 void SetSupportAnimation(bool supportAnimation) 78 { 79 supportAnimation_ = supportAnimation; 80 } 81 SupportAnimation()82 bool SupportAnimation() const 83 { 84 return supportAnimation_; 85 } 86 GetFocusPattern()87 FocusPattern GetFocusPattern() const override 88 { 89 return { FocusType::SCOPE, true }; 90 } 91 GetScopeFocusAlgorithm()92 ScopeFocusAlgorithm GetScopeFocusAlgorithm() override 93 { 94 auto property = GetLayoutProperty<GridLayoutProperty>(); 95 if (!property) { 96 return ScopeFocusAlgorithm(); 97 } 98 return ScopeFocusAlgorithm(property->IsVertical(), true, ScopeType::OTHERS, 99 [wp = WeakClaim(this)]( 100 FocusStep step, const WeakPtr<FocusHub>& currFocusNode, WeakPtr<FocusHub>& nextFocusNode) { 101 auto grid = wp.Upgrade(); 102 if (grid) { 103 nextFocusNode = grid->GetNextFocusNode(step, currFocusNode); 104 } 105 }); 106 } 107 108 int32_t GetFocusNodeIndex(const RefPtr<FocusHub>& focusNode) override; 109 110 void ScrollToFocusNodeIndex(int32_t index) override; 111 112 bool ScrollToNode(const RefPtr<FrameNode>& focusFrameNode) override; 113 CreateEventHub()114 RefPtr<EventHub> CreateEventHub() override 115 { 116 return MakeRefPtr<GridEventHub>(); 117 } 118 UsResRegion()119 bool UsResRegion() override 120 { 121 return false; 122 } 123 GetGridLayoutInfo()124 GridLayoutInfo GetGridLayoutInfo() const 125 { 126 return gridLayoutInfo_; 127 } 128 ResetGridLayoutInfo()129 void ResetGridLayoutInfo() 130 { 131 gridLayoutInfo_.lineHeightMap_.clear(); 132 gridLayoutInfo_.gridMatrix_.clear(); 133 gridLayoutInfo_.endIndex_ = gridLayoutInfo_.startIndex_ - 1; 134 gridLayoutInfo_.endMainLineIndex_ = 0; 135 gridLayoutInfo_.ResetPositionFlags(); 136 gridLayoutInfo_.irregularItemsPosition_.clear(); 137 } 138 ResetPositionFlags()139 void ResetPositionFlags() 140 { 141 gridLayoutInfo_.ResetPositionFlags(); 142 } 143 144 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override; 145 146 bool UpdateCurrentOffset(float offset, int32_t source) override; 147 IsAtTop()148 bool IsAtTop() const override 149 { 150 return gridLayoutInfo_.reachStart_; 151 } 152 IsAtBottom()153 bool IsAtBottom() const override 154 { 155 return gridLayoutInfo_.offsetEnd_; 156 } 157 158 OverScrollOffset GetOverScrollOffset(double delta) const override; 159 160 bool OutBoundaryCallback() override; 161 162 void ScrollPage(bool reverse); 163 164 bool UpdateStartIndex(int32_t index); 165 166 bool UpdateStartIndex(int32_t index, ScrollAlign align); 167 GetTotalOffset()168 float GetTotalOffset() const override 169 { 170 return EstimateHeight(); 171 } 172 173 float GetTotalHeight() const override; 174 175 void OnAnimateStop() override; 176 177 void AnimateTo( 178 float position, float duration, const RefPtr<Curve>& curve, bool smooth, bool canOverScroll = false) override; 179 void ScrollTo(float position) override; 180 181 void ScrollBy(float offset); 182 GetDefaultScrollAlign()183 ScrollAlign GetDefaultScrollAlign() const override 184 { 185 return ScrollAlign::AUTO; 186 } 187 188 void ScrollToIndex(int32_t index, bool smooth = false, ScrollAlign align = ScrollAlign::AUTO) override; 189 void AnimateToTarget(ScrollAlign align, RefPtr<LayoutAlgorithmWrapper>& layoutAlgorithmWrapper); 190 bool AnimateToTargetImp(ScrollAlign align, RefPtr<LayoutAlgorithmWrapper>& layoutAlgorithmWrapper); 191 int32_t GetOriginalIndex() const; 192 int32_t GetCrossCount() const; 193 int32_t GetChildrenCount() const; 194 void MoveItems(int32_t itemIndex, int32_t insertIndex); 195 void ClearDragState(); 196 float EstimateHeight() const; 197 float GetAverageHeight() const; 198 199 void DumpAdvanceInfo() override; 200 201 std::string ProvideRestoreInfo() override; 202 void OnRestoreInfo(const std::string& restoreInfo) override; 203 Rect GetItemRect(int32_t index) const override; 204 IsNeedInitClickEventRecorder()205 bool IsNeedInitClickEventRecorder() const override 206 { 207 return true; 208 } 209 GetPredictLayoutParam()210 std::optional<GridPredictLayoutParam> GetPredictLayoutParam() const 211 { 212 return predictLayoutParam_; 213 } 214 SetPredictLayoutParam(std::optional<GridPredictLayoutParam> param)215 void SetPredictLayoutParam(std::optional<GridPredictLayoutParam> param) 216 { 217 predictLayoutParam_ = param; 218 } 219 220 private: 221 float GetEndOffset(); 222 float GetMainGap() const; 223 float GetAllDelta(); 224 void CheckRestartSpring(); 225 void CheckScrollable(); 226 bool IsOutOfBoundary(bool useCurrentDelta = true) override; 227 void SetEdgeEffectCallback(const RefPtr<ScrollEdgeEffect>& scrollEffect) override; 228 SizeF GetContentSize() const; 229 void OnModifyDone() override; 230 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 231 WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode); 232 std::pair<int32_t, int32_t> GetNextIndexByStep( 233 int32_t curMainIndex, int32_t curCrossIndex, int32_t curMainSpan, int32_t curCrossSpan, FocusStep step); 234 WeakPtr<FocusHub> SearchFocusableChildInCross(int32_t tarMainIndex, int32_t tarCrossIndex, int32_t maxCrossCount, 235 int32_t curMainIndex = -1, int32_t curCrossIndex = -1); 236 WeakPtr<FocusHub> SearchIrregularFocusableChild(int32_t tarMainIndex, int32_t tarCrossIndex); 237 WeakPtr<FocusHub> GetChildFocusNodeByIndex(int32_t tarMainIndex, int32_t tarCrossIndex, int32_t tarIndex = -1); 238 std::unordered_set<int32_t> GetFocusableChildCrossIndexesAt(int32_t tarMainIndex); 239 void ScrollToFocusNode(const WeakPtr<FocusHub>& focusNode); 240 void FlushFocusOnScroll(const GridLayoutInfo& gridLayoutInfo); 241 std::pair<bool, bool> IsFirstOrLastFocusableChild(int32_t curMainIndex, int32_t curCrossIndex); 242 std::pair<FocusStep, FocusStep> GetFocusSteps(int32_t curMainIndex, int32_t curCrossIndex, FocusStep step); 243 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 244 bool OnKeyEvent(const KeyEvent& event); 245 bool HandleDirectionKey(KeyCode code); 246 247 void ClearMultiSelect() override; 248 bool IsItemSelected(const GestureEvent& info) override; 249 void MultiSelectWithoutKeyboard(const RectF& selectedZone) override; 250 void UpdateScrollBarOffset() override; 251 void UpdateRectOfDraggedInItem(int32_t insertIndex); 252 void SetAccessibilityAction(); 253 254 void ProcessEvent(bool indexChanged, float finalOffset); 255 void MarkDirtyNodeSelf(); 256 void OnScrollEndCallback() override; 257 258 void FireOnScrollStart() override; 259 void InitScrollableEvent(); 260 261 int32_t CalcIntersectAreaInTargetDirectionShadow(GridItemIndexInfo itemIndexInfo, bool isFindInMainAxis); 262 double GetNearestDistanceFromChildToCurFocusItemInMainAxis(int32_t targetIndex, GridItemIndexInfo itemIndexInfo); 263 double GetNearestDistanceFromChildToCurFocusItemInCrossAxis(int32_t targetIndex, GridItemIndexInfo itemIndexInfo); 264 void ResetAllDirectionsStep(); 265 266 float prevHeight_ = 0; 267 float currentHeight_ = 0; 268 269 bool supportAnimation_ = false; 270 bool isConfigScrollable_ = false; 271 272 bool scrollable_ = true; 273 274 float endHeight_ = 0.0f; 275 bool isLeftStep_ = false; 276 bool isRightStep_ = false; 277 bool isUpStep_ = false; 278 bool isDownStep_ = false; 279 bool isLeftEndStep_ = false; 280 bool isRightEndStep_ = false; 281 282 ScrollAlign scrollAlign_ = ScrollAlign::AUTO; 283 std::optional<int32_t> targetIndex_; 284 std::pair<std::optional<float>, std::optional<float>> scrollbarInfo_; 285 GridItemIndexInfo curFocusIndexInfo_; 286 bool isSmoothScrolling_ = false; 287 GridLayoutInfo scrollGridLayoutInfo_; 288 GridLayoutInfo gridLayoutInfo_; 289 std::optional<GridPredictLayoutParam> predictLayoutParam_; 290 ACE_DISALLOW_COPY_AND_MOVE(GridPattern); 291 }; 292 293 } // namespace OHOS::Ace::NG 294 295 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_PATTERN_H 296