1 /*
2 * Copyright (c) 2022-2023 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 "bridge/declarative_frontend/jsview/models/text_model_impl.h"
17
18 #include "bridge/declarative_frontend/jsview/models/view_abstract_model_impl.h"
19 #include "bridge/declarative_frontend/view_stack_processor.h"
20
21 namespace OHOS::Ace::Framework {
Create(const std::string & content)22 void TextModelImpl::Create(const std::string& content)
23 {
24 auto textComponent = AceType::MakeRefPtr<TextComponentV2>(content);
25 ViewStackProcessor::GetInstance()->ClaimElementId(textComponent);
26 ViewStackProcessor::GetInstance()->Push(textComponent);
27
28 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
29 if (focusableComponent) {
30 focusableComponent->SetFocusable(false);
31 focusableComponent->SetFocusNode(true);
32 }
33
34 // Init text style, allowScale is not supported in declarative.
35 auto textStyle = textComponent->GetTextStyle();
36 textStyle.SetAllowScale(false);
37 constexpr Dimension fontSize = 30.0_px;
38 textStyle.SetFontSize(fontSize);
39 textComponent->SetTextStyle(textStyle);
40 }
41
Create(const std::u16string & content)42 void TextModelImpl::Create(const std::u16string& content)
43 {
44 Create(UtfUtils::Str16DebugToStr8(content));
45 }
46
SetFont(const Font & value)47 void TextModelImpl::SetFont(const Font& value) {}
48
SetFontSize(const Dimension & value)49 void TextModelImpl::SetFontSize(const Dimension& value)
50 {
51 auto component = GetComponent();
52 CHECK_NULL_VOID(component);
53
54 auto textStyle = component->GetTextStyle();
55 textStyle.SetFontSize(value);
56 component->SetTextStyle(textStyle);
57 }
58
SetTextColor(const Color & value)59 void TextModelImpl::SetTextColor(const Color& value)
60 {
61 auto component = GetComponent();
62 CHECK_NULL_VOID(component);
63
64 auto textStyle = component->GetTextStyle();
65 textStyle.SetTextColor(value);
66 component->SetTextStyle(textStyle);
67 }
68
SetTextShadow(const std::vector<Shadow> & value)69 void TextModelImpl::SetTextShadow(const std::vector<Shadow>& value) {}
70
SetTextCaretColor(const Color & value)71 void TextModelImpl::SetTextCaretColor(const Color& value) {}
72
SetSelectedBackgroundColor(const Color & value)73 void TextModelImpl::SetSelectedBackgroundColor(const Color& value) {}
74
SetItalicFontStyle(Ace::FontStyle value)75 void TextModelImpl::SetItalicFontStyle(Ace::FontStyle value)
76 {
77 auto component = GetComponent();
78 CHECK_NULL_VOID(component);
79
80 auto textStyle = component->GetTextStyle();
81 textStyle.SetFontStyle(value);
82 component->SetTextStyle(textStyle);
83 }
84
SetFontWeight(Ace::FontWeight value)85 void TextModelImpl::SetFontWeight(Ace::FontWeight value)
86 {
87 auto component = GetComponent();
88 CHECK_NULL_VOID(component);
89
90 auto textStyle = component->GetTextStyle();
91 textStyle.SetFontWeight(value);
92 component->SetTextStyle(textStyle);
93 }
94
SetMinFontScale(const float value)95 void TextModelImpl::SetMinFontScale(const float value) {}
96
SetMaxFontScale(const float value)97 void TextModelImpl::SetMaxFontScale(const float value) {}
98
SetFontFamily(const std::vector<std::string> & value)99 void TextModelImpl::SetFontFamily(const std::vector<std::string>& value)
100 {
101 auto component = GetComponent();
102 CHECK_NULL_VOID(component);
103
104 auto textStyle = component->GetTextStyle();
105 textStyle.SetFontFamilies(value);
106 component->SetTextStyle(textStyle);
107 }
108
SetTextAlign(Ace::TextAlign value)109 void TextModelImpl::SetTextAlign(Ace::TextAlign value)
110 {
111 auto component = GetComponent();
112 CHECK_NULL_VOID(component);
113
114 auto textStyle = component->GetTextStyle();
115 textStyle.SetTextAlign(value);
116 component->SetTextStyle(textStyle);
117 }
118
SetTextOverflow(Ace::TextOverflow value)119 void TextModelImpl::SetTextOverflow(Ace::TextOverflow value)
120 {
121 auto component = GetComponent();
122 CHECK_NULL_VOID(component);
123 auto textStyle = component->GetTextStyle();
124 textStyle.SetTextOverflow(value);
125 component->SetTextStyle(textStyle);
126 }
127
SetMaxLines(uint32_t value)128 void TextModelImpl::SetMaxLines(uint32_t value)
129 {
130 auto component = GetComponent();
131 CHECK_NULL_VOID(component);
132
133 auto textStyle = component->GetTextStyle();
134 textStyle.SetMaxLines(value);
135 component->SetTextStyle(textStyle);
136 }
137
SetTextIndent(const Dimension & value)138 void TextModelImpl::SetTextIndent(const Dimension& value)
139 {
140 auto component = GetComponent();
141 CHECK_NULL_VOID(component);
142
143 auto textStyle = component->GetTextStyle();
144 textStyle.SetTextIndent(value);
145 component->SetTextStyle(textStyle);
146 }
147
SetLineHeight(const Dimension & value)148 void TextModelImpl::SetLineHeight(const Dimension& value)
149 {
150 auto component = GetComponent();
151 CHECK_NULL_VOID(component);
152
153 auto textStyle = component->GetTextStyle();
154 textStyle.SetLineHeight(value);
155 component->SetTextStyle(textStyle);
156 }
157
SetTextDecoration(Ace::TextDecoration value)158 void TextModelImpl::SetTextDecoration(Ace::TextDecoration value)
159 {
160 auto component = GetComponent();
161 CHECK_NULL_VOID(component);
162 auto textStyle = component->GetTextStyle();
163 textStyle.SetTextDecoration(value);
164 component->SetTextStyle(textStyle);
165 }
166
SetTextDecorationColor(const Color & value)167 void TextModelImpl::SetTextDecorationColor(const Color& value)
168 {
169 auto component = GetComponent();
170 CHECK_NULL_VOID(component);
171 auto textStyle = component->GetTextStyle();
172 textStyle.SetTextDecorationColor(value);
173 component->SetTextStyle(textStyle);
174 }
175
SetTextDecorationStyle(TextDecorationStyle value)176 void TextModelImpl::SetTextDecorationStyle(TextDecorationStyle value)
177 {
178 auto component = GetComponent();
179 CHECK_NULL_VOID(component);
180 auto textStyle = component->GetTextStyle();
181 textStyle.SetTextDecorationStyle(value);
182 component->SetTextStyle(textStyle);
183 }
184
SetBaselineOffset(const Dimension & value)185 void TextModelImpl::SetBaselineOffset(const Dimension& value)
186 {
187 auto component = GetComponent();
188 CHECK_NULL_VOID(component);
189
190 auto textStyle = component->GetTextStyle();
191 textStyle.SetBaselineOffset(value);
192 component->SetTextStyle(textStyle);
193 }
194
SetTextCase(Ace::TextCase value)195 void TextModelImpl::SetTextCase(Ace::TextCase value)
196 {
197 auto component = GetComponent();
198 CHECK_NULL_VOID(component);
199
200 auto textStyle = component->GetTextStyle();
201 textStyle.SetTextCase(value);
202 component->SetTextStyle(textStyle);
203 }
204
SetLetterSpacing(const Dimension & value)205 void TextModelImpl::SetLetterSpacing(const Dimension& value)
206 {
207 auto component = GetComponent();
208 CHECK_NULL_VOID(component);
209
210 auto textStyle = component->GetTextStyle();
211 textStyle.SetLetterSpacing(value);
212 component->SetTextStyle(textStyle);
213 }
214
SetLineSpacing(const Dimension & value)215 void TextModelImpl::SetLineSpacing(const Dimension& value) {}
216
SetIsOnlyBetweenLines(bool isOnlyBetweenLines)217 void TextModelImpl::SetIsOnlyBetweenLines(bool isOnlyBetweenLines) {}
218
SetOptimizeTrailingSpace(bool trim)219 void TextModelImpl::SetOptimizeTrailingSpace(bool trim) {}
220
SetGradientShaderStyle(NG::Gradient & gradient)221 void TextModelImpl::SetGradientShaderStyle(NG::Gradient& gradient) {}
222
SetColorShaderStyle(const Color & value)223 void TextModelImpl::SetColorShaderStyle(const Color& value) {}
224
ResetGradientShaderStyle()225 void TextModelImpl::ResetGradientShaderStyle() {}
226
SetAdaptMinFontSize(const Dimension & value)227 void TextModelImpl::SetAdaptMinFontSize(const Dimension& value)
228 {
229 auto component = GetComponent();
230 CHECK_NULL_VOID(component);
231
232 auto textStyle = component->GetTextStyle();
233 textStyle.SetAdaptMinFontSize(value);
234 component->SetTextStyle(textStyle);
235 }
236
SetAdaptMaxFontSize(const Dimension & value)237 void TextModelImpl::SetAdaptMaxFontSize(const Dimension& value)
238 {
239 auto component = GetComponent();
240 CHECK_NULL_VOID(component);
241
242 auto textStyle = component->GetTextStyle();
243 textStyle.SetAdaptMaxFontSize(value);
244 component->SetTextStyle(textStyle);
245 }
246
SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value)247 void TextModelImpl::SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value) {}
248
SetTextDetectEnable(bool value)249 void TextModelImpl::SetTextDetectEnable(bool value) {}
250
SetTextDetectConfig(const TextDetectConfig & textDetectConfig)251 void TextModelImpl::SetTextDetectConfig(const TextDetectConfig& textDetectConfig) {}
252
OnSetWidth()253 void TextModelImpl::OnSetWidth()
254 {
255 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
256 CHECK_NULL_VOID(box);
257 auto component = GetComponent();
258 CHECK_NULL_VOID(component);
259 component->SetMaxWidthLayout(box->GetWidthDimension().IsValid());
260 }
261
OnSetHeight()262 void TextModelImpl::OnSetHeight()
263 {
264 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
265 CHECK_NULL_VOID(box);
266 box->SetBoxClipFlag(true);
267 }
268
GetComponent()269 RefPtr<TextComponentV2> TextModelImpl::GetComponent()
270 {
271 auto* stack = ViewStackProcessor::GetInstance();
272 if (!stack) {
273 return nullptr;
274 }
275 auto component = AceType::DynamicCast<TextComponentV2>(stack->GetMainComponent());
276 return component;
277 }
278
OnSetAlign()279 void TextModelImpl::OnSetAlign()
280 {
281 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
282 CHECK_NULL_VOID(box);
283 auto component = GetComponent();
284 CHECK_NULL_VOID(component);
285 auto alignment = box->GetAlignment();
286 if (NearEqual(alignment.GetHorizontal(), -1.0)) {
287 component->SetAlignment(TextAlign::LEFT);
288 } else if (NearEqual(alignment.GetHorizontal(), 0.0)) {
289 component->SetAlignment(TextAlign::CENTER);
290 } else if (NearEqual(alignment.GetHorizontal(), 1.0)) {
291 component->SetAlignment(TextAlign::RIGHT);
292 }
293 }
294
SetOnClick(std::function<void (BaseEventInfo *)> && click,double distanceThreshold)295 void TextModelImpl::SetOnClick(std::function<void(BaseEventInfo*)>&& click, double distanceThreshold)
296 {
297 auto clickId = EventMarker(std::move(click));
298 auto gesture = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent();
299 if (gesture) {
300 gesture->SetOnClickId(clickId);
301 }
302 auto component = GetComponent();
303 if (component) {
304 component->SetOnClick(clickId);
305 }
306
307 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
308 if (focusableComponent) {
309 focusableComponent->SetOnClickId(clickId);
310 }
311 }
312
SetRemoteMessage(std::function<void ()> && event)313 void TextModelImpl::SetRemoteMessage(std::function<void()>&& event)
314 {
315 auto textComponent = GetComponent();
316 CHECK_NULL_VOID(textComponent);
317 textComponent->SetRemoteMessageEvent(EventMarker(std::move(event)));
318 }
319
SetCopyOption(CopyOptions copyOption)320 void TextModelImpl::SetCopyOption(CopyOptions copyOption)
321 {
322 auto component = GetComponent();
323 CHECK_NULL_VOID(component);
324 component->SetCopyOption(copyOption);
325 }
326
SetOnDragStart(NG::OnDragStartFunc && onDragStart)327 void TextModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
328 {
329 auto component = GetComponent();
330 CHECK_NULL_VOID(component);
331 component->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart)));
332 }
333
SetHalfLeading(bool halfLeading)334 void TextModelImpl::SetHalfLeading(bool halfLeading) {}
335 } // namespace OHOS::Ace::Framework
336