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