• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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(
56         FocusStep step, const WeakPtr<FocusHub>& currentFocusNode, bool isMainSkip = false);
57     bool GetCurrentFocusInfo(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
58     WeakPtr<FocusHub> HandleFocusSteps(
59         FocusStep step, const WeakPtr<FocusHub>& currentFocusNode, std::pair<FocusStep, FocusStep> focusSteps);
60 
ResetFocusIndex()61     void ResetFocusIndex()
62     {
63         focusIndex_.reset();
64     }
65 
SetFocusIndex(int32_t index)66     void SetFocusIndex(int32_t index)
67     {
68         focusIndex_ = index;
69     }
70 
GetFocusIndex()71     std::optional<int32_t> GetFocusIndex() const
72     {
73         return focusIndex_;
74     }
75 
76 private:
77     void ScrollToFocusNode(const WeakPtr<FocusHub>& focusNode);
78     void FireFocus();
79     std::pair<int32_t, int32_t> GetNextIndexByStep(
80         int32_t curMainIndex, int32_t curCrossIndex, int32_t curMainSpan, int32_t curCrossSpan, FocusStep step);
81     WeakPtr<FocusHub> SearchBigItemFocusableChildInCross(
82         int32_t tarMainIndex, int32_t tarCrossIndex, FocusStep step = FocusStep::NONE, bool isMainSkip = false);
83     bool CheckIsCrossDirectionFocus(FocusStep step);
84     FocusStep HandleDirectionStep(FocusStep step);
85     bool CheckStepDirection(FocusStep step, bool isNext);
86     WeakPtr<FocusHub> SearchFocusableChildInCross(int32_t tarMainIndex, int32_t tarCrossIndex, int32_t maxCrossCount,
87         int32_t curMainIndex = -1, int32_t curCrossIndex = -1);
88     WeakPtr<FocusHub> SearchIrregularFocusableChild(int32_t tarMainIndex, int32_t tarCrossIndex);
89     std::unordered_set<int32_t> GetFocusableChildCrossIndexesAt(int32_t tarMainIndex);
90     std::pair<bool, bool> IsFirstOrLastFocusableChild(int32_t curMainIndex, int32_t curCrossIndex);
91     std::pair<FocusStep, FocusStep> GetFocusSteps(int32_t curMainIndex, int32_t curCrossIndex, FocusStep step);
92     FocusWrapMode GetFocusWrapMode();
93     int32_t GetIndexByFocusHub(const WeakPtr<FocusHub>& focusNode);
94     void ResetAllDirectionsStep();
95     int32_t CalcIntersectAreaInTargetDirectionShadow(GridItemIndexInfo itemIndexInfo, bool isFindInMainAxis);
96     double GetNearestDistanceFromChildToCurFocusItemInMainAxis(int32_t targetIndex, GridItemIndexInfo itemIndexInfo);
97     double GetNearestDistanceFromChildToCurFocusItemInCrossAxis(int32_t targetIndex, GridItemIndexInfo itemIndexInfo);
98     bool IsInViewport(int32_t index, bool needCheckCache) const;
99 
100     std::optional<int32_t> focusIndex_;
101     GridItemIndexInfo curFocusIndexInfo_;
102 
103     GridPattern& grid_;
104     const GridLayoutInfo& info_;
105 
106     bool isTab_ = false;
107     bool isLeftStep_ = false;
108     bool isRightStep_ = false;
109     bool isUpStep_ = false;
110     bool isDownStep_ = false;
111     bool isLeftEndStep_ = false;
112     bool isRightEndStep_ = false;
113     bool needTriggerFocus_ = false;
114     bool triggerFocus_ = false;
115 
116     ACE_DISALLOW_COPY_AND_MOVE(GridFocus);
117 };
118 } // namespace OHOS::Ace::NG
119 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_PATTERN_H
120