1 /*
2  * Copyright (c) 2020-2021 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 GRAPHIC_LITE_FOCUS_MANAGER_H
17 #define GRAPHIC_LITE_FOCUS_MANAGER_H
18 
19 #include "graphic_config.h"
20 #if ENABLE_FOCUS_MANAGER
21 #include "components/ui_view_group.h"
22 namespace OHOS {
23 /**
24  * @brief Enumerates focus directions.
25  *
26  * @since 5.0
27  * @version 3.0
28  */
29 enum : uint8_t {
30     FOCUS_DIRECTION_RIGHT,
31     FOCUS_DIRECTION_LEFT,
32     FOCUS_DIRECTION_UP,
33     FOCUS_DIRECTION_DOWN,
34 };
35 
36 class FocusManager {
37 public:
38     /**
39      * @brief Get the FocusManager's singleton.
40      *
41      * @return FocusManager's singleton.
42      * @since 5.0
43      * @version 3.0
44      */
45     static FocusManager* GetInstance();
46 
47     /**
48      * @brief Clear the focus.
49      *
50      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
51      * @since 5.0
52      * @version 3.0
53      */
54     bool ClearFocus();
55 
56     /**
57      * @brief Get the focused view.
58      *
59      * @return the focused view.
60      * @since 5.0
61      * @version 3.0
62      */
GetFocusedView()63     UIView* GetFocusedView()
64     {
65         return focusView_;
66     }
67 
68     /**
69      * @brief Request the focus.
70      *
71      * @param view the focus.
72      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
73      * @since 5.0
74      * @version 3.0
75      */
76     bool RequestFocus(UIView* view);
77 
78     /**
79      * @brief Request focus by direction.
80      *
81      * @param direction focus direction.
82      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
83      * @since 5.0
84      * @version 3.0
85      */
86     bool RequestFocusByDirection(uint8_t direction);
87 
88 private:
FocusManager()89     FocusManager() : focusView_(nullptr), lastFocusView_(nullptr) {}
~FocusManager()90     ~FocusManager() {}
91 
92     bool GetNextFocus(UIView* focusedView, UIView*& candidate, uint8_t direction);
93     bool GetNextFocus(UIView* focusedView, UIView*& candidate, UIView* view, uint8_t direction);
94     bool GetNextFocus(UIView* focusedView, UIView*& candidate, UIViewGroup* viewGroup, uint8_t direction);
95     bool IsAtSameCol(const Rect& rect1, const Rect& rect2);
96     bool IsAtSameRow(const Rect& rect1, const Rect& rect2);
97     bool CompareCandidates(UIView* focusedView, UIView*& candidate, UIView* current, uint8_t direction);
98     bool CompareCandidatesByUp(UIView* focusedView, UIView*& candidate, UIView* current);
99     bool CompareCandidatesByDown(UIView* focusedView, UIView*& candidate, UIView* current);
100     bool CompareCandidatesByLeft(UIView* focusedView, UIView*& candidate, UIView* current);
101     bool CompareCandidatesByRight(UIView* focusedView, UIView*& candidate, UIView* current);
102     bool CompareCandidatesDistance(const Rect& focused, const Rect& candidate, const Rect& current);
103 
104     UIView* focusView_;
105     UIView* lastFocusView_;
106 };
107 } // namespace OHOS
108 #endif
109 #endif // GRAPHIC_LITE_FOCUS_MANAGER_H
110