• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_MANAGER_AVOID_INFO_AVOID_INFO_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_AVOID_INFO_AVOID_INFO_MANAGER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <set>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/geometry/ng/rect_t.h"
25 #include "base/want/want_wrap.h"
26 #include "core/components_ng/base/frame_node.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 struct ContainerModalAvoidInfo {
31     bool needAvoid = false;
32     int32_t titleHeight = 0;
33     RectF controlBottonsRect;
34 
35     std::string ToString() const;
36 };
37 
38 class IAvoidInfoListener : public virtual AceType {
39     DECLARE_ACE_TYPE(IAvoidInfoListener, AceType);
40 public:
41     IAvoidInfoListener() = default;
42     virtual ~IAvoidInfoListener() = default;
43 
44     virtual void OnAvoidInfoChange(const ContainerModalAvoidInfo& info) = 0;
45 };
46 
47 using ParseAvoidInfoCallback = std::function<bool(const AAFwk::Want& want, ContainerModalAvoidInfo& info)>;
48 using BuildAvoidInfoCallback = std::function<void(const ContainerModalAvoidInfo& info, AAFwk::Want& want)>;
49 using UECAvoidInfoConsumer = std::function<int32_t(const AAFwk::Want& data)>;
50 using RegisterUECAvoidInfoConsumerCallback = std::function<void(UECAvoidInfoConsumer&& consumer)>;
51 using RequestAvoidInfoCallback = std::function<void()>;
52 
53 class AvoidInfoManager : public virtual AceType {
54     DECLARE_ACE_TYPE(AvoidInfoManager, AceType);
55 public:
56     AvoidInfoManager() = default;
57     ~AvoidInfoManager() = default;
58 
SetPipelineContext(const WeakPtr<PipelineContext> & pipeline)59     void SetPipelineContext(const WeakPtr<PipelineContext>& pipeline)
60     {
61         pipeline_ = pipeline;
62     }
SetInstanceId(int32_t id)63     void SetInstanceId(int32_t id)
64     {
65         instanceId_ = id;
66     }
SetAvoidInfo(const ContainerModalAvoidInfo & info)67     void SetAvoidInfo(const ContainerModalAvoidInfo& info)
68     {
69         avoidInfo_ = info;
70     }
SetAvoidInfoForUEC(const ContainerModalAvoidInfo & info)71     void SetAvoidInfoForUEC(const ContainerModalAvoidInfo& info)
72     {
73         avoidInfoForUEC_ = info;
74     }
75 
SetParseAvoidInfoCallback(ParseAvoidInfoCallback && callback)76     void SetParseAvoidInfoCallback(ParseAvoidInfoCallback&& callback)
77     {
78         parseCallback_ = std::move(callback);
79     }
ParseAvoidInfo(const AAFwk::Want & want,ContainerModalAvoidInfo & info)80     bool ParseAvoidInfo(const AAFwk::Want& want, ContainerModalAvoidInfo& info)
81     {
82         CHECK_NULL_RETURN(parseCallback_, false);
83         return parseCallback_(want, info);
84     }
SetBuildAvoidInfoCallback(BuildAvoidInfoCallback && callback)85     void SetBuildAvoidInfoCallback(BuildAvoidInfoCallback&& callback)
86     {
87         buildCallback_ = std::move(callback);
88     }
BuildAvoidInfo(const ContainerModalAvoidInfo & info,AAFwk::Want & want)89     void BuildAvoidInfo(const ContainerModalAvoidInfo& info, AAFwk::Want& want)
90     {
91         if (buildCallback_) {
92             buildCallback_(info, want);
93         }
94     }
SetRegisterUECAvoidInfoConsumerCallback(RegisterUECAvoidInfoConsumerCallback && callback)95     void SetRegisterUECAvoidInfoConsumerCallback(RegisterUECAvoidInfoConsumerCallback&& callback)
96     {
97         registerUECConsumerCallback_ = std::move(callback);
98     }
SetRequestAvoidInfoCallback(RequestAvoidInfoCallback && callback)99     void SetRequestAvoidInfoCallback(RequestAvoidInfoCallback&& callback)
100     {
101         requestAvoidInfoCallback_ = std::move(callback);
102     }
103 
104     void OnAvoidInfoChange(const ContainerModalAvoidInfo& info);
105     void AddAvoidInfoListener(WeakPtr<IAvoidInfoListener> listener);
106     void RemoveAvoidInfoListener(WeakPtr<IAvoidInfoListener> listener);
107 
108     int32_t GetContainerModalTitleHeight();
109     bool GetContainerModalButtonsRect(RectF& containerModal, RectF& buttonsRect);
110     bool NeedAvoidContainerModal();
111 
112     static void GetContainerModalAvoidInfoForUEC(const RefPtr<FrameNode>& uecNode, ContainerModalAvoidInfo& info);
113     static bool CheckIfNeedNotifyAvoidInfoChange(
114         const ContainerModalAvoidInfo& prevInfo, const ContainerModalAvoidInfo& curInfo);
115 
116 private:
117     void RegisterListenerIfNeeded();
118     void UnregisterListenerIfNeeded();
119     void GetContainerModalAvoidInfo(ContainerModalAvoidInfo& info);
120     void OnContainerModalInfoChange();
121     int32_t OnUECAvoidInfoReceived(const AAFwk::Want& data);
122 
123     int32_t instanceId_ = -1;
124     WeakPtr<PipelineContext> pipeline_;
125     ParseAvoidInfoCallback parseCallback_;
126     BuildAvoidInfoCallback buildCallback_;
127     RegisterUECAvoidInfoConsumerCallback registerUECConsumerCallback_;
128     RequestAvoidInfoCallback requestAvoidInfoCallback_;
129     bool hasRegisterListener_ = false;
130     int32_t containerModalListenerId_ = 0;
131     std::set<WeakPtr<IAvoidInfoListener>> listeners_;
132     ContainerModalAvoidInfo avoidInfo_;
133     ContainerModalAvoidInfo avoidInfoForUEC_;
134 };
135 } // namespace OHOS::Ace::NG
136 
137 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_AVOID_INFO_AVOID_INFO_MANAGER_H
138