1 /*
2  * Copyright (c) 2022 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 #include "core/components_ng/manager/select_overlay/select_overlay_proxy.h"
17 
18 #include <cstdint>
19 
20 #include "base/memory/referenced.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/base/ui_node.h"
23 #include "core/components_ng/pattern/select_overlay/select_overlay_pattern.h"
24 #include "core/pipeline_ng/pipeline_context.h"
25 
26 namespace OHOS::Ace::NG {
27 namespace {
GetSelectOverlayPattern(int32_t selectOverlayId)28 RefPtr<SelectOverlayPattern> GetSelectOverlayPattern(int32_t selectOverlayId)
29 {
30     auto pipeline = PipelineContext::GetCurrentContext();
31     CHECK_NULL_RETURN(pipeline, nullptr);
32     auto manager = pipeline->GetSelectOverlayManager();
33     CHECK_NULL_RETURN(manager, nullptr);
34     auto node = manager->GetSelectOverlayNode(selectOverlayId);
35     CHECK_NULL_RETURN(node, nullptr);
36     return node->GetPattern<SelectOverlayPattern>();
37 }
38 } // namespace
39 
UpdateFirstSelectHandleInfo(const SelectHandleInfo & info) const40 void SelectOverlayProxy::UpdateFirstSelectHandleInfo(const SelectHandleInfo& info) const
41 {
42     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
43     CHECK_NULL_VOID(pattern);
44     pattern->UpdateFirstSelectHandleInfo(info);
45 }
46 
UpdateSecondSelectHandleInfo(const SelectHandleInfo & info) const47 void SelectOverlayProxy::UpdateSecondSelectHandleInfo(const SelectHandleInfo& info) const
48 {
49     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
50     CHECK_NULL_VOID(pattern);
51     pattern->UpdateSecondSelectHandleInfo(info);
52 }
53 
UpdateFirstAndSecondHandleInfo(const SelectHandleInfo & firstInfo,const SelectHandleInfo & secondInfo) const54 void SelectOverlayProxy::UpdateFirstAndSecondHandleInfo(
55     const SelectHandleInfo& firstInfo, const SelectHandleInfo& secondInfo) const
56 {
57     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
58     CHECK_NULL_VOID(pattern);
59     pattern->UpdateFirstAndSecondHandleInfo(firstInfo, secondInfo);
60 }
61 
UpdateSelectMenuInfo(const SelectMenuInfo & info) const62 void SelectOverlayProxy::UpdateSelectMenuInfo(const SelectMenuInfo& info) const
63 {
64     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
65     CHECK_NULL_VOID(pattern);
66     pattern->UpdateSelectMenuInfo(info);
67 }
68 
UpdateSelectMenuInfo(std::function<void (SelectMenuInfo & menuInfo)> updateAction) const69 void SelectOverlayProxy::UpdateSelectMenuInfo(std::function<void(SelectMenuInfo& menuInfo)> updateAction) const
70 {
71     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
72     CHECK_NULL_VOID(pattern);
73     pattern->UpdateSelectMenuInfo(updateAction);
74 }
75 
UpdateShowArea(const RectF & area) const76 void SelectOverlayProxy::UpdateShowArea(const RectF& area) const
77 {
78     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
79     CHECK_NULL_VOID(pattern);
80     pattern->UpdateShowArea(area);
81 }
82 
SetSelectRegionVisible(bool isSelectRegionVisible)83 void SelectOverlayProxy::SetSelectRegionVisible(bool isSelectRegionVisible)
84 {
85     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
86     CHECK_NULL_VOID(pattern);
87     pattern->SetSelectRegionVisible(isSelectRegionVisible);
88 }
89 
SetHandleReverse(bool reverse)90 void SelectOverlayProxy::SetHandleReverse(bool reverse)
91 {
92     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
93     CHECK_NULL_VOID(pattern);
94     pattern->SetHandleReverse(reverse);
95 }
96 
IsClosed() const97 bool SelectOverlayProxy::IsClosed() const
98 {
99     auto pipeline = PipelineContext::GetCurrentContext();
100     CHECK_NULL_RETURN(pipeline, true);
101     auto manager = pipeline->GetSelectOverlayManager();
102     CHECK_NULL_RETURN(manager, true);
103     return !manager->HasSelectOverlay(selectOverlayId_);
104 }
105 
Close(bool animation) const106 void SelectOverlayProxy::Close(bool animation) const
107 {
108     auto pipeline = PipelineContext::GetCurrentContext();
109     CHECK_NULL_VOID(pipeline);
110     auto manager = pipeline->GetSelectOverlayManager();
111     CHECK_NULL_VOID(manager);
112     manager->DestroySelectOverlay(selectOverlayId_, animation);
113 }
114 
SetSelectInfo(const std::string & selectInfo) const115 void SelectOverlayProxy::SetSelectInfo(const std::string& selectInfo) const
116 {
117     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
118     CHECK_NULL_VOID(pattern);
119     pattern->SetSelectInfo(selectInfo);
120 }
121 
ShowOrHiddenMenu(bool isHidden,bool noAnimation)122 void SelectOverlayProxy::ShowOrHiddenMenu(bool isHidden, bool noAnimation)
123 {
124     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
125     CHECK_NULL_VOID(pattern);
126     pattern->ShowOrHiddenMenu(isHidden, noAnimation);
127 }
128 
DisableMenu(bool isDisabled)129 void SelectOverlayProxy::DisableMenu(bool isDisabled)
130 {
131     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
132     CHECK_NULL_VOID(pattern);
133     pattern->DisableMenu(isDisabled);
134 }
135 
IsMenuShow()136 bool SelectOverlayProxy::IsMenuShow()
137 {
138     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
139     CHECK_NULL_RETURN(pattern, false);
140     return pattern->IsMenuShow();
141 }
142 
IsSingleHandleMenuShow()143 bool SelectOverlayProxy::IsSingleHandleMenuShow()
144 {
145     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
146     CHECK_NULL_RETURN(pattern, false);
147     return pattern->IsSingleHandleMenuShow();
148 }
149 
IsHandleShow()150 bool SelectOverlayProxy::IsHandleShow()
151 {
152     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
153     CHECK_NULL_RETURN(pattern, false);
154     return pattern->IsHandleShow();
155 }
156 
IsSingleHandle()157 bool SelectOverlayProxy::IsSingleHandle()
158 {
159     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
160     CHECK_NULL_RETURN(pattern, false);
161     return pattern->IsSingleHandle();
162 }
163 
IsHandleReverse()164 bool SelectOverlayProxy::IsHandleReverse()
165 {
166     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
167     CHECK_NULL_RETURN(pattern, false);
168     auto info = pattern->GetSelectOverlayInfo();
169     return info && info->handleReverse;
170 }
171 
GetSelectOverlayMangerInfo()172 SelectOverlayInfo SelectOverlayProxy::GetSelectOverlayMangerInfo()
173 {
174     auto pipeline = PipelineContext::GetCurrentContext();
175     CHECK_NULL_RETURN(pipeline, SelectOverlayInfo());
176     auto manager = pipeline->GetSelectOverlayManager();
177     CHECK_NULL_RETURN(manager, SelectOverlayInfo());
178     return manager->GetSelectOverlayInfo();
179 }
180 
UpdateSelectArea(const RectF & selectArea)181 void SelectOverlayProxy::UpdateSelectArea(const RectF& selectArea)
182 {
183     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
184     CHECK_NULL_VOID(pattern);
185     pattern->UpdateSelectArea(selectArea);
186     auto host = pattern->GetHost();
187     CHECK_NULL_VOID(host);
188     host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
189 }
190 
SetIsNewAvoid(bool isNewAvoid)191 void SelectOverlayProxy::SetIsNewAvoid(bool isNewAvoid)
192 {
193     auto pattern = GetSelectOverlayPattern(selectOverlayId_);
194     CHECK_NULL_VOID(pattern);
195     pattern->SetIsNewAvoid(isNewAvoid);
196 }
197 } // namespace OHOS::Ace::NG
198