• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MANAGER_FOCUS_FOCUS_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_FOCUS_FOCUS_MANAGER_H
18 
19 #include <list>
20 #include <optional>
21 
22 #include "base/error/error_code.h"
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "core/components_ng/manager/focus/focus_view.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class PipelineContext;
30 
31 using FocusViewMap = std::unordered_map<int32_t, std::pair<WeakPtr<FocusView>, std::list<WeakPtr<FocusView>>>>;
32 using RequestFocusCallback = std::function<void(NG::RequestFocusResult result)>;
33 using FocusHubScopeMap = std::unordered_map<std::string, std::pair<WeakPtr<FocusHub>, std::list<WeakPtr<FocusHub>>>>;
34 using FocusChangeCallback = std::function<void(const WeakPtr<FocusHub>& last,
35     const RefPtr<FocusHub>& current, FocusReason focusReason)>;
36 
37 enum class FocusViewStackState : int32_t {
38     IDLE = 0,
39     SHOW = 1,
40     CLOSE = 2,
41 };
42 
43 enum class FocusActiveReason : int32_t {
44     POINTER_EVENT = 0,
45     KEYBOARD_EVENT = 1,
46     USE_API = 2,
47 };
48 
49 enum class KeyProcessingMode : int32_t {
50     FOCUS_NAVIGATION = 0,
51     ANCESTOR_EVENT = 1,
52 };
53 
54 class FocusManager : public virtual AceType {
55     DECLARE_ACE_TYPE(FocusManager, AceType);
56 
57 public:
58     class FocusGuard {
59     public:
60         explicit FocusGuard(const RefPtr<FocusHub>& focushub, SwitchingStartReason reason);
61         FocusGuard() = delete;
62         ~FocusGuard();
63         void CreateFocusGuard(const RefPtr<FocusHub>& focushub, const RefPtr<FocusManager>& focusManager,
64             SwitchingStartReason reason);
65     private:
66         RefPtr<FocusManager> focusMng_;
67     };
68     friend FocusGuard;
69 
70     explicit FocusManager(const RefPtr<PipelineContext>& pipeline);
71     ~FocusManager() override = default;
72 
73     void FocusViewShow(const RefPtr<FocusView>& focusView, bool isTriggerByStep = false);
74     void FocusViewHide(const RefPtr<FocusView>& focusView);
75     void FocusViewClose(const RefPtr<FocusView>& focusView, bool isDetachFromTree = false);
76 
77     void FlushFocusView();
78 
79     void DumpFocusManager();
80 
GetLastFocusView()81     WeakPtr<FocusView> GetLastFocusView() const
82     {
83         return lastFocusView_;
84     }
85 
GetWeakFocusViewList()86     const std::list<WeakPtr<FocusView>>& GetWeakFocusViewList() const
87     {
88         return focusViewStack_;
89     }
90 
SetRequestFocusCallback(const RequestFocusCallback & callback)91     void SetRequestFocusCallback(const RequestFocusCallback& callback)
92     {
93         requestCallback_ = std::move(callback);
94     }
95 
ResetRequestFocusCallback()96     void ResetRequestFocusCallback()
97     {
98         requestCallback_ = nullptr;
99     }
100 
TriggerRequestFocusCallback(NG::RequestFocusResult result)101     void TriggerRequestFocusCallback(NG::RequestFocusResult result)
102     {
103         if (requestCallback_) {
104             requestCallback_(result);
105             requestCallback_ = nullptr;
106         }
107     }
108 
SetRequestFocusResult(const int32_t requestFocusResult)109     void SetRequestFocusResult(const int32_t requestFocusResult)
110     {
111         requestFocusResult_ = requestFocusResult;
112     }
113 
GetRequestFocusResult()114     int32_t GetRequestFocusResult()
115     {
116         return requestFocusResult_;
117     }
118 
ResetRequestFocusResult()119     void ResetRequestFocusResult()
120     {
121         requestFocusResult_ = ERROR_CODE_NO_ERROR;
122     }
123 
SetLastFocusStateNode(const RefPtr<FocusHub> & node)124     void SetLastFocusStateNode(const RefPtr<FocusHub>& node)
125     {
126         lastFocusStateNode_ = AceType::WeakClaim(AceType::RawPtr(node));
127         isNeedTriggerScroll_ = true;
128     }
GetLastFocusStateNode()129     RefPtr<FocusHub> GetLastFocusStateNode() const
130     {
131         return lastFocusStateNode_.Upgrade();
132     }
133 
SetNeedTriggerScroll(bool isNeedTriggerScroll)134     void SetNeedTriggerScroll(bool isNeedTriggerScroll)
135     {
136         isNeedTriggerScroll_ = isNeedTriggerScroll;
137     }
GetNeedTriggerScroll()138     bool GetNeedTriggerScroll() const
139     {
140         return isNeedTriggerScroll_;
141     }
142 
SetIsAutoFocusTransfer(bool isAutoFocusTransfer)143     void SetIsAutoFocusTransfer(bool isAutoFocusTransfer)
144     {
145         isAutoFocusTransfer_ = isAutoFocusTransfer;
146     }
147 
IsAutoFocusTransfer()148     bool IsAutoFocusTransfer() const
149     {
150         return isAutoFocusTransfer_;
151     }
152 
153     bool RearrangeViewStack();
154 
SetFocusViewStackState(FocusViewStackState focusViewStackState)155     void SetFocusViewStackState(FocusViewStackState focusViewStackState)
156     {
157         focusViewStackState_ = focusViewStackState;
158     }
159 
SetKeyProcessingMode(KeyProcessingMode keyProcessingMode)160     void SetKeyProcessingMode(KeyProcessingMode keyProcessingMode)
161     {
162         keyProcessingMode_ = keyProcessingMode;
163     }
164 
GetKeyProcessingMode()165     KeyProcessingMode GetKeyProcessingMode() const
166     {
167         return keyProcessingMode_;
168     }
169 
170     bool SetFocusViewRootScope(const RefPtr<FocusView>& focusView);
171 
172     void PaintFocusState();
173 
174     bool AddFocusScope(const std::string& focusScopeId, const RefPtr<FocusHub>& scopeFocusHub);
175     void RemoveFocusScope(const std::string& focusScopeId);
176     void AddScopePriorityNode(const std::string& focusScopeId, const RefPtr<FocusHub>& priorFocusHub, bool pushFront);
177     void RemoveScopePriorityNode(const std::string& focusScopeId, const RefPtr<FocusHub>& priorFocusHub);
178     std::optional<std::list<WeakPtr<FocusHub>>*> GetFocusScopePriorityList(const std::string& focusScopeId);
179 
180     void UpdateCurrentFocus(const RefPtr<FocusHub>& current, SwitchingUpdateReason reason);
181     RefPtr<FocusHub> GetCurrentFocus();
UpdateSwitchingEndReason(SwitchingEndReason reason)182     void UpdateSwitchingEndReason(SwitchingEndReason reason)
183     {
184         if (isSwitchingFocus_.value_or(false)) {
185             endReason_ = reason;
186         }
187     }
188     int32_t AddFocusListener(FocusChangeCallback&& callback);
189     void RemoveFocusListener(int32_t id);
190     void FocusSwitchingStart(const RefPtr<FocusHub>& focusHub, SwitchingStartReason reason);
191     void FocusSwitchingEnd(SwitchingEndReason reason = SwitchingEndReason::FOCUS_GUARD_DESTROY);
192     void WindowFocusMoveStart();
193     void WindowFocusMoveEnd();
194     void WindowFocus(bool isFocus);
GetCurrentFocusEvent()195     std::optional<FocusEvent> GetCurrentFocusEvent()
196     {
197         return currentFocusEvent_;
198     }
199 
SetCurrentFocusEvent(const FocusEvent & event)200     void SetCurrentFocusEvent(const FocusEvent& event)
201     {
202         currentFocusEvent_.reset();
203         currentFocusEvent_.emplace(event);
204     }
205 
ResetCurrentFocusEvent()206     void ResetCurrentFocusEvent()
207     {
208         currentFocusEvent_.reset();
209     }
210 
211     static RefPtr<FocusManager> GetFocusManager(RefPtr<FrameNode>& node);
212 
213 private:
214     void GetFocusViewMap(FocusViewMap& focusViewMap);
215     void ReportFocusSwitching(FocusReason focusReason);
216 
217     std::list<WeakPtr<FocusView>> focusViewStack_;
218     WeakPtr<FocusView> lastFocusView_;
219     const WeakPtr<PipelineContext> pipeline_;
220 
221     RequestFocusCallback requestCallback_;
222 
223     WeakPtr<FocusHub> lastFocusStateNode_;
224     WeakPtr<FocusHub> currentFocus_;
225     bool isNeedTriggerScroll_ = false;
226     FocusHubScopeMap focusHubScopeMap_;
227 
228     std::map<int32_t, FocusChangeCallback> listeners_;
229     int32_t nextListenerHdl_ = -1;
230     std::optional<bool> isSwitchingFocus_;
231     bool isSwitchingWindow_ = false;
232     RefPtr<FocusHub> switchingFocus_;
233 
234     std::optional<SwitchingStartReason> startReason_;
235     std::optional<SwitchingEndReason> endReason_;
236     std::optional<SwitchingUpdateReason> updateReason_;
237 
238     bool isAutoFocusTransfer_ = true;
239     FocusViewStackState focusViewStackState_ = FocusViewStackState::IDLE;
240     KeyProcessingMode keyProcessingMode_ = KeyProcessingMode::FOCUS_NAVIGATION;
241     int32_t requestFocusResult_ = ERROR_CODE_NO_ERROR;
242 
243     std::optional<FocusEvent> currentFocusEvent_;
244     ACE_DISALLOW_COPY_AND_MOVE(FocusManager);
245 };
246 } // namespace OHOS::Ace::NG
247 
248 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_FOCUS_FOCUS_MANAGER_H
249