1 /* 2 * Copyright (c) 2024 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_FOCUS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_FOCUS_H 18 19 #include <unordered_set> 20 21 #include "base/utils/noncopyable.h" 22 #include "core/components_ng/event/focus_hub.h" 23 #include "core/components_ng/pattern/grid/grid_layout_info.h" 24 25 namespace OHOS::Ace::NG { 26 struct GridItemIndexInfo { 27 int32_t mainIndex = -1; 28 int32_t crossIndex = -1; 29 int32_t mainSpan = -1; 30 int32_t crossSpan = -1; 31 int32_t mainStart = -1; 32 int32_t mainEnd = -1; 33 int32_t crossStart = -1; 34 int32_t crossEnd = -1; 35 }; 36 37 class GridPattern; 38 39 // helper class to handle focus-related tasks for Grid 40 class GridFocus { 41 public: GridFocus(GridPattern & grid,const GridLayoutInfo & info)42 GridFocus(GridPattern& grid, const GridLayoutInfo& info) : grid_(grid), info_(info) {} 43 ~GridFocus() = default; 44 45 int32_t GetFocusNodeIndex(const RefPtr<FocusHub>& focusNode); 46 47 void ProcessFocusEvent(const KeyEvent& event, bool indexChanged); 48 49 bool ScrollToLastFocusIndex(KeyCode keyCode); 50 WeakPtr<FocusHub> GetChildFocusNodeByIndex(int32_t tarMainIndex, int32_t tarCrossIndex, int32_t tarIndex = -1); 51 52 void HandleFocusEvent(const WeakPtr<FocusHub>& child); 53 54 WeakPtr<FocusHub> GetNextFocusSimplified(FocusStep step, const RefPtr<FocusHub>& current); // focus algo rewritten 55 WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode); 56 ResetFocusIndex()57 void ResetFocusIndex() 58 { 59 focusIndex_.reset(); 60 } 61 SetFocusIndex(int32_t index)62 void SetFocusIndex(int32_t index) 63 { 64 focusIndex_ = index; 65 } 66 67 private: 68 void ScrollToFocusNode(const WeakPtr<FocusHub>& focusNode); 69 void FireFocus(); 70 std::pair<int32_t, int32_t> GetNextIndexByStep( 71 int32_t curMainIndex, int32_t curCrossIndex, int32_t curMainSpan, int32_t curCrossSpan, FocusStep step); 72 WeakPtr<FocusHub> SearchFocusableChildInCross(int32_t tarMainIndex, int32_t tarCrossIndex, int32_t maxCrossCount, 73 int32_t curMainIndex = -1, int32_t curCrossIndex = -1); 74 WeakPtr<FocusHub> SearchIrregularFocusableChild(int32_t tarMainIndex, int32_t tarCrossIndex); 75 std::unordered_set<int32_t> GetFocusableChildCrossIndexesAt(int32_t tarMainIndex); 76 std::pair<bool, bool> IsFirstOrLastFocusableChild(int32_t curMainIndex, int32_t curCrossIndex); 77 std::pair<FocusStep, FocusStep> GetFocusSteps(int32_t curMainIndex, int32_t curCrossIndex, FocusStep step); 78 int32_t GetIndexByFocusHub(const WeakPtr<FocusHub>& focusNode); 79 void ResetAllDirectionsStep(); 80 int32_t CalcIntersectAreaInTargetDirectionShadow(GridItemIndexInfo itemIndexInfo, bool isFindInMainAxis); 81 double GetNearestDistanceFromChildToCurFocusItemInMainAxis(int32_t targetIndex, GridItemIndexInfo itemIndexInfo); 82 double GetNearestDistanceFromChildToCurFocusItemInCrossAxis(int32_t targetIndex, GridItemIndexInfo itemIndexInfo); 83 bool IsInViewport(int32_t index, bool needCheckCache) const; 84 85 std::optional<int32_t> focusIndex_; 86 GridItemIndexInfo curFocusIndexInfo_; 87 88 GridPattern& grid_; 89 const GridLayoutInfo& info_; 90 91 bool isLeftStep_ = false; 92 bool isRightStep_ = false; 93 bool isUpStep_ = false; 94 bool isDownStep_ = false; 95 bool isLeftEndStep_ = false; 96 bool isRightEndStep_ = false; 97 bool needTriggerFocus_ = false; 98 bool triggerFocus_ = false; 99 100 ACE_DISALLOW_COPY_AND_MOVE(GridFocus); 101 }; 102 } // namespace OHOS::Ace::NG 103 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_PATTERN_H 104