• 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/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "core/components_ng/manager/focus/focus_view.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class PipelineContext;
29 
30 using FocusViewMap = std::unordered_map<int32_t, std::pair<WeakPtr<FocusView>, std::list<WeakPtr<FocusView>>>>;
31 using RequestFocusCallback = std::function<void(NG::RequestFocusResult result)>;
32 using FocusHubScopeMap = std::unordered_map<std::string, std::pair<WeakPtr<FocusHub>, std::list<WeakPtr<FocusHub>>>>;
33 using FocusChangeCallback = std::function<void(const WeakPtr<FocusHub>& last, const RefPtr<FocusHub>& current)>;
34 
35 class FocusManager : public virtual AceType {
36     DECLARE_ACE_TYPE(FocusManager, AceType);
37 
38 public:
39     class FocusGuard {
40     public:
41         explicit FocusGuard(const RefPtr<FocusHub>& focushub, SwitchingStartReason reason);
42         FocusGuard() = delete;
43         ~FocusGuard();
44         void CreateFocusGuard(const RefPtr<FocusHub>& focushub, const RefPtr<FocusManager>& focusManager,
45             SwitchingStartReason reason);
46     private:
47         RefPtr<FocusManager> focusMng_;
48     };
49     friend FocusGuard;
50 
51     explicit FocusManager(const RefPtr<PipelineContext>& pipeline);
52     ~FocusManager() override = default;
53 
54     void FocusViewShow(const RefPtr<FocusView>& focusView, bool isTriggerByStep = false);
55     void FocusViewHide(const RefPtr<FocusView>& focusView);
56     void FocusViewClose(const RefPtr<FocusView>& focusView);
57 
58     void FlushFocusView();
59 
60     void DumpFocusManager();
61 
GetLastFocusView()62     WeakPtr<FocusView> GetLastFocusView() const
63     {
64         return lastFocusView_;
65     }
66 
GetWeakFocusViewList()67     const std::list<WeakPtr<FocusView>>& GetWeakFocusViewList() const
68     {
69         return focusViewStack_;
70     }
71 
SetRequestFocusCallback(const RequestFocusCallback & callback)72     void SetRequestFocusCallback(const RequestFocusCallback& callback)
73     {
74         requestCallback_ = std::move(callback);
75     }
76 
ResetRequestFocusCallback()77     void ResetRequestFocusCallback()
78     {
79         requestCallback_ = nullptr;
80     }
81 
TriggerRequestFocusCallback(NG::RequestFocusResult result)82     void TriggerRequestFocusCallback(NG::RequestFocusResult result)
83     {
84         if (requestCallback_) {
85             requestCallback_(result);
86             requestCallback_ = nullptr;
87         }
88     }
89 
SetLastFocusStateNode(const RefPtr<FocusHub> & node)90     void SetLastFocusStateNode(const RefPtr<FocusHub>& node)
91     {
92         lastFocusStateNode_ = AceType::WeakClaim(AceType::RawPtr(node));
93         isNeedTriggerScroll_ = true;
94     }
GetLastFocusStateNode()95     RefPtr<FocusHub> GetLastFocusStateNode() const
96     {
97         return lastFocusStateNode_.Upgrade();
98     }
99 
SetNeedTriggerScroll(bool isNeedTriggerScroll)100     void SetNeedTriggerScroll(bool isNeedTriggerScroll)
101     {
102         isNeedTriggerScroll_ = isNeedTriggerScroll;
103     }
GetNeedTriggerScroll()104     bool GetNeedTriggerScroll() const
105     {
106         return isNeedTriggerScroll_;
107     }
108 
109     void PaintFocusState();
110 
111     bool AddFocusScope(const std::string& focusScopeId, const RefPtr<FocusHub>& scopeFocusHub);
112     void RemoveFocusScope(const std::string& focusScopeId);
113     void AddScopePriorityNode(const std::string& focusScopeId, const RefPtr<FocusHub>& priorFocusHub, bool pushFront);
114     void RemoveScopePriorityNode(const std::string& focusScopeId, const RefPtr<FocusHub>& priorFocusHub);
115     std::optional<std::list<WeakPtr<FocusHub>>*> GetFocusScopePriorityList(const std::string& focusScopeId);
116 
117     void UpdateCurrentFocus(const RefPtr<FocusHub>& current, SwitchingUpdateReason reason);
118     RefPtr<FocusHub> GetCurrentFocus();
119     int32_t AddFocusListener(FocusChangeCallback&& callback);
120     void RemoveFocusListener(int32_t id);
121     void FocusSwitchingStart(const RefPtr<FocusHub>& focusHub, SwitchingStartReason reason);
122     void FocusSwitchingEnd(SwitchingEndReason reason = SwitchingEndReason::FOCUS_GUARD_DESTROY);
123     void WindowFocusMoveStart();
124     void WindowFocusMoveEnd();
125     void WindowFocus(bool isFocus);
126 
127     static RefPtr<FocusManager> GetFocusManager(RefPtr<FrameNode>& node);
128 
129 private:
130     void GetFocusViewMap(FocusViewMap& focusViewMap);
131     void ReportFocusSwitching();
132 
133     std::list<WeakPtr<FocusView>> focusViewStack_;
134     WeakPtr<FocusView> lastFocusView_;
135     const WeakPtr<PipelineContext> pipeline_;
136 
137     RequestFocusCallback requestCallback_;
138 
139     WeakPtr<FocusHub> lastFocusStateNode_;
140     WeakPtr<FocusHub> currentFocus_;
141     bool isNeedTriggerScroll_ = false;
142     FocusHubScopeMap focusHubScopeMap_;
143 
144     std::map<int32_t, FocusChangeCallback> listeners_;
145     int32_t nextListenerHdl_ = -1;
146     std::optional<bool> isSwitchingFocus_;
147     bool isSwitchingWindow_ = false;
148     RefPtr<FocusHub> switchingFocus_;
149 
150     std::optional<SwitchingStartReason> startReason_;
151     std::optional<SwitchingEndReason> endReason_;
152     std::optional<SwitchingUpdateReason> updateReason_;
153 
154     ACE_DISALLOW_COPY_AND_MOVE(FocusManager);
155 };
156 } // namespace OHOS::Ace::NG
157 
158 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_FOCUS_FOCUS_MANAGER_H
159