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)76 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(
77 const std::string& message, const RefPtr<FrameNode>& iconNode)
78 {
79 return ShowLongPressDialog(UtfUtils::Str8ToStr16(message), iconNode);
80 }
81
ShowLongPressDialog(const std::u16string & message,const RefPtr<FrameNode> & iconNode)82 RefPtr<FrameNode> AgingAdapationDialogUtil::ShowLongPressDialog(
83 const std::u16string& message, const RefPtr<FrameNode>& iconNode)
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() ? symbolProperty->UpdateSymbolColorList({ dialogTheme->GetDialogIconColor() })
103 : symbolProperty->UpdateSymbolColorList(symbolColorList);
104 auto fontWeight = srcLayoutProperty->GetFontWeightValue(FontWeight::NORMAL);
105 symbolProperty->UpdateFontWeight(fontWeight);
106 auto renderStrategy = srcLayoutProperty->GetSymbolRenderingStrategyValue(0);
107 symbolProperty->UpdateSymbolRenderingStrategy(renderStrategy);
108 MarginProperty symbolMargin;
109 Dimension dialogHeight;
110 if (message.empty()) {
111 float scale = context->GetFontScale();
112 if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
113 NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
114 dialogHeight = Dimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP);
115 } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
116 dialogHeight = Dimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP);
117 }
118 auto marginSize = (dialogHeight - dialogTheme->GetIdealSize()).ConvertToPx() / 2;
119 symbolMargin.top = CalcLength(Dimension(marginSize, DimensionUnit::PX));
120 symbolMargin.bottom = CalcLength(Dimension(marginSize, DimensionUnit::PX));
121 symbolProperty->UpdateMargin(symbolMargin);
122 symbolNode->MountToParent(columnNode);
123 symbolNode->MarkModifyDone();
124 return CreateCustomDialog(columnNode, themeScopeId);
125 }
126 symbolMargin.top = CalcLength(dialogTheme->GetDialogPropertyTop());
127 symbolMargin.bottom = CalcLength(dialogTheme->GetDialogPropertyBottom());
128 symbolProperty->UpdateMargin(symbolMargin);
129 symbolNode->MountToParent(columnNode);
130 symbolNode->MarkModifyDone();
131 CreateDialogTextNode(columnNode, message, themeScopeId);
132 return CreateCustomDialog(columnNode, themeScopeId);
133 }
134
CreateCustomDialog(const RefPtr<FrameNode> & columnNode,int32_t themeScopeId)135 RefPtr<FrameNode> AgingAdapationDialogUtil::CreateCustomDialog(
136 const RefPtr<FrameNode>& columnNode, int32_t themeScopeId)
137 {
138 CHECK_NULL_RETURN(columnNode, nullptr);
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 DialogProperties dialogProperties;
144 dialogProperties.alignment = DialogAlignment::CENTER;
145 dialogProperties.gridCount = dialogTheme->GetGridCount();
146 dialogProperties.isModal = false;
147 dialogProperties.backgroundColor = Color::TRANSPARENT;
148 dialogProperties.shadow = Shadow::CreateShadow(ShadowStyle::OuterDefaultLG);
149 dialogProperties.borderRadius = BorderRadiusProperty(dialogTheme->GetDialogCornerRadius());
150 CalcSize columnMinSize;
151 float scale = context->GetFontScale();
152 if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
153 NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
154 dialogProperties.width = CalcDimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP);
155 columnMinSize.SetHeight(CalcLength(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP));
156 } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
157 dialogProperties.width = CalcDimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP);
158 columnMinSize.SetHeight(CalcLength(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP));
159 }
160 auto layoutProperty = columnNode->GetLayoutProperty();
161 CHECK_NULL_RETURN(layoutProperty, nullptr);
162 layoutProperty->UpdateCalcMinSize(columnMinSize);
163 layoutProperty->UpdateMeasureType(MeasureType::MATCH_PARENT_CROSS_AXIS);
164 bool isRightToLeft = AceApplicationInfo::GetInstance().IsRightToLeft();
165 auto pipelineContext = PipelineContext::GetCurrentContext();
166 CHECK_NULL_RETURN(pipelineContext, nullptr);
167 auto overlayManager = pipelineContext->GetOverlayManager();
168 CHECK_NULL_RETURN(overlayManager, nullptr);
169 return overlayManager->ShowDialogWithNode(dialogProperties, columnNode, isRightToLeft);
170 }
171
CreateDialogTextNode(const RefPtr<FrameNode> & columnNode,const std::u16string & message,int32_t themeScopeId)172 void AgingAdapationDialogUtil::CreateDialogTextNode(
173 const RefPtr<FrameNode>& columnNode, const std::u16string& message, int32_t themeScopeId)
174 {
175 CHECK_NULL_VOID(columnNode);
176 auto context = PipelineBase::GetCurrentContext();
177 CHECK_NULL_VOID(context);
178 auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>(themeScopeId);
179 CHECK_NULL_VOID(dialogTheme);
180 auto textNode = FrameNode::CreateFrameNode(
181 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
182 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
183 CHECK_NULL_VOID(textLayoutProperty);
184 textLayoutProperty->UpdateContent(message);
185 textLayoutProperty->UpdateFontSize(dialogTheme->GetDialogFontSize());
186 textLayoutProperty->UpdateTextOverflow(TextOverflow::ELLIPSIS);
187 textLayoutProperty->UpdateMaxLines(dialogTheme->GetMaxLines());
188 MarginProperty margin;
189 margin.left = CalcLength(dialogTheme->GetTextPropertyLeft());
190 margin.right = CalcLength(dialogTheme->GetTextPropertyRight());
191 margin.bottom = CalcLength(dialogTheme->GetTextPropertyBottom());
192 if (columnNode->GetFirstChild() == nullptr) {
193 margin.top = CalcLength(dialogTheme->GetTextPropertyBottom());
194 CalcSize textMinSize;
195 Dimension textMinHeight;
196 float scale = context->GetFontScale();
197 if (NearEqual(scale, dialogTheme->GetBigFontSizeScale()) ||
198 NearEqual(scale, dialogTheme->GetLargeFontSizeScale())) {
199 textMinHeight = Dimension(dialogTheme->GetBigDialogWidth(), DimensionUnit::VP) -
200 dialogTheme->GetTextPropertyBottom() - dialogTheme->GetTextPropertyBottom();
201 } else if (NearEqual(scale, dialogTheme->GetMaxFontSizeScale())) {
202 textMinHeight = Dimension(dialogTheme->GetMaxDialogWidth(), DimensionUnit::VP) -
203 dialogTheme->GetTextPropertyBottom() - dialogTheme->GetTextPropertyBottom();
204 }
205 textMinSize.SetHeight(CalcLength(textMinHeight));
206 textLayoutProperty->UpdateCalcMinSize(textMinSize);
207 }
208 textLayoutProperty->UpdateMargin(margin);
209 auto color = dialogTheme->GetDialogFontColor();
210 textLayoutProperty->UpdateTextColor(Color(color.GetValue()));
211 textNode->MountToParent(columnNode);
212 }
213
GetDialogBigFontSizeScale()214 float AgingAdapationDialogUtil::GetDialogBigFontSizeScale()
215 {
216 auto context = PipelineBase::GetCurrentContext();
217 CHECK_NULL_RETURN(context, 0.0);
218 auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>();
219 CHECK_NULL_RETURN(dialogTheme, 0.0);
220 return dialogTheme->GetBigFontSizeScale();
221 }
222
GetDialogLargeFontSizeScale()223 float AgingAdapationDialogUtil::GetDialogLargeFontSizeScale()
224 {
225 auto context = PipelineBase::GetCurrentContextSafely();
226 CHECK_NULL_RETURN(context, 0.0);
227 auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>();
228 CHECK_NULL_RETURN(dialogTheme, 0.0);
229 return dialogTheme->GetLargeFontSizeScale();
230 }
231
GetDialogMaxFontSizeScale()232 float AgingAdapationDialogUtil::GetDialogMaxFontSizeScale()
233 {
234 auto context = PipelineBase::GetCurrentContextSafely();
235 CHECK_NULL_RETURN(context, 0.0);
236 auto dialogTheme = context->GetTheme<AgingAdapationDialogTheme>();
237 CHECK_NULL_RETURN(dialogTheme, 0.0);
238 return dialogTheme->GetMaxFontSizeScale();
239 }
240 } // namespace OHOS::Ace::NG