• 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 #include "base/utils/utf_helper.h"
16 #include "core/common/agingadapation/aging_adapation_dialog_util.h"
17 
18 #include "core/common/agingadapation/aging_adapation_dialog_theme.h"
19 #include "core/components_ng/pattern/text/text_pattern.h"
20 namespace OHOS::Ace::NG {
ShowLongPressDialog(const std::string & message,ImageSourceInfo & imageSourceInfo,int32_t themeScopeId)21 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(
22     const std::string& message, ImageSourceInfo& imageSourceInfo, int32_t themeScopeId)
23 {
24     return ShowLongPressDialog(UtfUtils::Str8ToStr16(message), imageSourceInfo, themeScopeId);
25 }
26 
ShowLongPressDialog(const std::u16string & message,ImageSourceInfo & imageSourceInfo,int32_t themeScopeId)27 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(
28     const std::u16string& message, ImageSourceInfo& imageSourceInfo, int32_t themeScopeId)
29 {
30     RefPtr<FrameNode> columnNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG,
31         ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
32 
33     if (imageSourceInfo.IsValid()) {
34         auto context = PipelineBase::GetCurrentContext();
35         CHECK_NULL_RETURN(context, nullptr);
36         auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>(themeScopeId);
37         CHECK_NULL_RETURN(dialogTheme, nullptr);
38         auto color = dialogTheme->GetDialogIconColor();
39         imageSourceInfo.SetFillColor(Color(color.GetValue()));
40         auto imageNode = FrameNode::CreateFrameNode(
41             V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ImagePattern>());
42         auto imageLayoutProperty = imageNode->GetLayoutProperty<ImageLayoutProperty>();
43         CHECK_NULL_RETURN(imageLayoutProperty, nullptr);
44         imageLayoutProperty->UpdateUserDefinedIdealSize(
45             CalcSize(CalcLength(dialogTheme->GetIdealSize()), CalcLength(dialogTheme->GetIdealSize())));
46         imageLayoutProperty->UpdateImageFit(ImageFit::FILL);
47         imageLayoutProperty->UpdateImageSourceInfo(imageSourceInfo);
48         MarginProperty imageMargin;
49         Dimension dialogHeight;
50         if (message.empty()) {
51             float scale = context->GetFontScale();
52             if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
53                 NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
54                 dialogHeight = Dimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP);
55             } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
56                 dialogHeight = Dimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP);
57             }
58             auto marginSize = (dialogHeight - dialogTheme->GetIdealSize()).ConvertToPx() / 2;
59             imageMargin.top = CalcLength(Dimension(marginSize, DimensionUnit::PX));
60             imageMargin.bottom = CalcLength(Dimension(marginSize, DimensionUnit::PX));
61             imageLayoutProperty->UpdateMargin(imageMargin);
62             imageNode->MountToParent(columnNode);
63             imageNode->MarkModifyDone();
64             return CreateCustomDialog(columnNode, themeScopeId);
65         }
66         imageMargin.top = CalcLength(dialogTheme->GetDialogPropertyTop());
67         imageMargin.bottom = CalcLength(dialogTheme->GetDialogPropertyBottom());
68         imageLayoutProperty->UpdateMargin(imageMargin);
69         imageNode->MountToParent(columnNode);
70         imageNode->MarkModifyDone();
71     }
72     CreateDialogTextNode(columnNode, message, themeScopeId);
73     return CreateCustomDialog(columnNode, themeScopeId);
74 }
75 
ShowLongPressDialog(const std::string & message,const RefPtr<FrameNode> & iconNode,bool isWithDialogTheme)76 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(
77     const std::string& message, const RefPtr<FrameNode>& iconNode, bool isWithDialogTheme)
78 {
79     return ShowLongPressDialog(UtfUtils::Str8ToStr16(message), iconNode, isWithDialogTheme);
80 }
81 
ShowLongPressDialog(const std::u16string & message,const RefPtr<FrameNode> & iconNode,bool isWithDialogTheme)82 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(
83     const std::u16string& message, const RefPtr<FrameNode>& iconNode, bool isWithDialogTheme)
84 {
85     CHECK_NULL_RETURN(iconNode, nullptr);
86     int32_t themeScopeId = iconNode->GetThemeScopeId();
87     auto context = PipelineBase::GetCurrentContext();
88     CHECK_NULL_RETURN(context, nullptr);
89     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>(themeScopeId);
90     CHECK_NULL_RETURN(dialogTheme, nullptr);
91     auto srcLayoutProperty = iconNode->GetLayoutProperty<TextLayoutProperty>();
92     CHECK_NULL_RETURN(srcLayoutProperty, nullptr);
93     RefPtr<FrameNode> columnNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG,
94         ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
95     auto symbolNode = FrameNode::GetOrCreateFrameNode(V2::SYMBOL_ETS_TAG,
96         ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<TextPattern>(); });
97     auto symbolProperty = symbolNode->GetLayoutProperty<TextLayoutProperty>();
98     CHECK_NULL_RETURN(symbolProperty, nullptr);
99     symbolProperty->UpdateFontSize(dialogTheme->GetIdealSize());
100     symbolProperty->UpdateSymbolSourceInfo(srcLayoutProperty->GetSymbolSourceInfoValue());
101     auto symbolColorList = srcLayoutProperty->GetSymbolColorListValue({});
102     (symbolColorList.empty() || isWithDialogTheme)
103         ? symbolProperty->UpdateSymbolColorList({ dialogTheme->GetDialogIconColor() })
104         : symbolProperty->UpdateSymbolColorList(symbolColorList);
105     auto fontWeight = srcLayoutProperty->GetFontWeightValue(FontWeight::NORMAL);
106     symbolProperty->UpdateFontWeight(fontWeight);
107     auto renderStrategy = srcLayoutProperty->GetSymbolRenderingStrategyValue(0);
108     symbolProperty->UpdateSymbolRenderingStrategy(renderStrategy);
109     MarginProperty symbolMargin;
110     Dimension dialogHeight;
111     if (message.empty()) {
112         float scale = context->GetFontScale();
113         if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
114             NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
115             dialogHeight = Dimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP);
116         } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
117             dialogHeight = Dimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP);
118         }
119         auto marginSize = (dialogHeight - dialogTheme->GetIdealSize()).ConvertToPx() / 2;
120         symbolMargin.top = CalcLength(Dimension(marginSize, DimensionUnit::PX));
121         symbolMargin.bottom = CalcLength(Dimension(marginSize, DimensionUnit::PX));
122         symbolProperty->UpdateMargin(symbolMargin);
123         symbolNode->MountToParent(columnNode);
124         symbolNode->MarkModifyDone();
125         return CreateCustomDialog(columnNode, themeScopeId);
126     }
127     symbolMargin.top = CalcLength(dialogTheme->GetDialogPropertyTop());
128     symbolMargin.bottom = CalcLength(dialogTheme->GetDialogPropertyBottom());
129     symbolProperty->UpdateMargin(symbolMargin);
130     symbolNode->MountToParent(columnNode);
131     symbolNode->MarkModifyDone();
132     CreateDialogTextNode(columnNode, message, themeScopeId);
133     return CreateCustomDialog(columnNode, themeScopeId);
134 }
135 
ShowLongPressDialog(const std::u16string & message)136 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(const std::u16string& message)
137 {
138     int32_t themeScopeId = 0;
139     auto context = PipelineBase::GetCurrentContext();
140     CHECK_NULL_RETURN(context, nullptr);
141     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>(themeScopeId);
142     CHECK_NULL_RETURN(dialogTheme, nullptr);
143     RefPtr<FrameNode> columnNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG,
144         ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
145     Dimension dialogHeight;
146     if (message.empty()) {
147         float scale = context->GetFontScale();
148         if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
149             NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
150             dialogHeight = Dimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP);
151         } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
152             dialogHeight = Dimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP);
153         }
154         return CreateCustomDialog(columnNode, themeScopeId);
155     }
156     CreateDialogTextNode(columnNode, message, themeScopeId);
157     return CreateCustomDialog(columnNode, themeScopeId);
158 }
159 
CreateCustomDialog(const RefPtr<FrameNode> & columnNode,int32_t themeScopeId)160 RefPtr<FrameNode> AgingAdapationDialogUtil::CreateCustomDialog(
161     const RefPtr<FrameNode>& columnNode, int32_t themeScopeId)
162 {
163     CHECK_NULL_RETURN(columnNode, nullptr);
164     auto context = PipelineBase::GetCurrentContext();
165     CHECK_NULL_RETURN(context, nullptr);
166     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>(themeScopeId);
167     CHECK_NULL_RETURN(dialogTheme, nullptr);
168     DialogProperties dialogProperties;
169     dialogProperties.alignment = DialogAlignment::CENTER;
170     dialogProperties.gridCount = dialogTheme->GetGridCount();
171     dialogProperties.isModal = false;
172     dialogProperties.backgroundColor = Color::TRANSPARENT;
173     dialogProperties.shadow = Shadow::CreateShadow(ShadowStyle::OuterDefaultLG);
174     dialogProperties.borderRadius = BorderRadiusProperty(dialogTheme->GetDialogCornerRadius());
175     CalcSize columnMinSize;
176     float scale = context->GetFontScale();
177     if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
178         NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
179         dialogProperties.width = CalcDimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP);
180         columnMinSize.SetHeight(CalcLength(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP));
181     } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
182         dialogProperties.width = CalcDimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP);
183         columnMinSize.SetHeight(CalcLength(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP));
184     }
185     auto layoutProperty = columnNode->GetLayoutProperty();
186     CHECK_NULL_RETURN(layoutProperty, nullptr);
187     layoutProperty->UpdateCalcMinSize(columnMinSize);
188     layoutProperty->UpdateMeasureType(MeasureType::MATCH_PARENT_CROSS_AXIS);
189     bool isRightToLeft = AceApplicationInfo::GetInstance().IsRightToLeft();
190     auto pipelineContext = PipelineContext::GetCurrentContext();
191     CHECK_NULL_RETURN(pipelineContext, nullptr);
192     auto overlayManager = pipelineContext->GetOverlayManager();
193     CHECK_NULL_RETURN(overlayManager, nullptr);
194     return overlayManager->ShowDialogWithNode(dialogProperties, columnNode, isRightToLeft);
195 }
196 
CreateDialogTextNode(const RefPtr<FrameNode> & columnNode,const std::u16string & message,int32_t themeScopeId)197 void AgingAdapationDialogUtil::CreateDialogTextNode(
198     const RefPtr<FrameNode>& columnNode, const std::u16string& message, int32_t themeScopeId)
199 {
200     CHECK_NULL_VOID(columnNode);
201     auto context = PipelineBase::GetCurrentContext();
202     CHECK_NULL_VOID(context);
203     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>(themeScopeId);
204     CHECK_NULL_VOID(dialogTheme);
205     auto textNode = FrameNode::CreateFrameNode(
206         V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
207     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
208     CHECK_NULL_VOID(textLayoutProperty);
209     textLayoutProperty->UpdateContent(message);
210     textLayoutProperty->UpdateFontSize(dialogTheme->GetDialogFontSize());
211     textLayoutProperty->UpdateTextOverflow(TextOverflow::ELLIPSIS);
212     textLayoutProperty->UpdateMaxLines(dialogTheme->GetMaxLines());
213     MarginProperty margin;
214     margin.left = CalcLength(dialogTheme->GetTextPropertyLeft());
215     margin.right = CalcLength(dialogTheme->GetTextPropertyRight());
216     margin.bottom = CalcLength(dialogTheme->GetTextPropertyBottom());
217     if (columnNode->GetFirstChild() == nullptr) {
218         margin.top = CalcLength(dialogTheme->GetTextPropertyBottom());
219         CalcSize textMinSize;
220         Dimension textMinHeight;
221         float scale = context->GetFontScale();
222         if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
223             NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
224             textMinHeight = Dimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP) -
225                             dialogTheme->GetTextPropertyBottom() - dialogTheme->GetTextPropertyBottom();
226         } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
227             textMinHeight = Dimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP) -
228                             dialogTheme->GetTextPropertyBottom() - dialogTheme->GetTextPropertyBottom();
229         }
230         textMinSize.SetHeight(CalcLength(textMinHeight));
231         textLayoutProperty->UpdateCalcMinSize(textMinSize);
232     }
233     textLayoutProperty->UpdateMargin(margin);
234     auto color = dialogTheme->GetDialogFontColor();
235     textLayoutProperty->UpdateTextColor(Color(color.GetValue()));
236     textNode->MountToParent(columnNode);
237 }
238 
GetDialogBigFontSizeScale()239 float AgingAdapationDialogUtil::GetDialogBigFontSizeScale()
240 {
241     auto context = PipelineBase::GetCurrentContext();
242     CHECK_NULL_RETURN(context, 0.0);
243     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>();
244     CHECK_NULL_RETURN(dialogTheme, 0.0);
245     return dialogTheme->GetBigFontSizeScale();
246 }
247 
GetDialogLargeFontSizeScale()248 float AgingAdapationDialogUtil::GetDialogLargeFontSizeScale()
249 {
250     auto context = PipelineBase::GetCurrentContextSafely();
251     CHECK_NULL_RETURN(context, 0.0);
252     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>();
253     CHECK_NULL_RETURN(dialogTheme, 0.0);
254     return dialogTheme->GetLargeFontSizeScale();
255 }
256 
GetDialogMaxFontSizeScale()257 float AgingAdapationDialogUtil::GetDialogMaxFontSizeScale()
258 {
259     auto context = PipelineBase::GetCurrentContextSafely();
260     CHECK_NULL_RETURN(context, 0.0);
261     auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>();
262     CHECK_NULL_RETURN(dialogTheme, 0.0);
263     return dialogTheme->GetMaxFontSizeScale();
264 }
265 } // namespace OHOS::Ace::NG