• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Shadow & value)71 void TextModelImpl::SetTextShadow(const 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 
SetBaselineOffset(const Dimension & value)170 void TextModelImpl::SetBaselineOffset(const Dimension& value)
171 {
172     auto component = GetComponent();
173     CHECK_NULL_VOID(component);
174 
175     auto textStyle = component->GetTextStyle();
176     textStyle.SetBaselineOffset(value);
177     component->SetTextStyle(textStyle);
178 }
179 
SetTextCase(Ace::TextCase value)180 void TextModelImpl::SetTextCase(Ace::TextCase value)
181 {
182     auto component = GetComponent();
183     CHECK_NULL_VOID(component);
184 
185     auto textStyle = component->GetTextStyle();
186     textStyle.SetTextCase(value);
187     component->SetTextStyle(textStyle);
188 }
189 
SetLetterSpacing(const Dimension & value)190 void TextModelImpl::SetLetterSpacing(const Dimension& value)
191 {
192     auto component = GetComponent();
193     CHECK_NULL_VOID(component);
194 
195     auto textStyle = component->GetTextStyle();
196     textStyle.SetLetterSpacing(value);
197     component->SetTextStyle(textStyle);
198 }
199 
SetAdaptMinFontSize(const Dimension & value)200 void TextModelImpl::SetAdaptMinFontSize(const Dimension& value)
201 {
202     auto component = GetComponent();
203     CHECK_NULL_VOID(component);
204 
205     auto textStyle = component->GetTextStyle();
206     textStyle.SetAdaptMinFontSize(value);
207     component->SetTextStyle(textStyle);
208 }
209 
SetAdaptMaxFontSize(const Dimension & value)210 void TextModelImpl::SetAdaptMaxFontSize(const Dimension& value)
211 {
212     auto component = GetComponent();
213     CHECK_NULL_VOID(component);
214 
215     auto textStyle = component->GetTextStyle();
216     textStyle.SetAdaptMaxFontSize(value);
217     component->SetTextStyle(textStyle);
218 }
219 
SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value)220 void TextModelImpl::SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value) {}
221 
OnSetWidth()222 void TextModelImpl::OnSetWidth()
223 {
224     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
225     CHECK_NULL_VOID(box);
226     auto component = GetComponent();
227     CHECK_NULL_VOID(component);
228     component->SetMaxWidthLayout(box->GetWidthDimension().IsValid());
229 }
230 
OnSetHeight()231 void TextModelImpl::OnSetHeight()
232 {
233     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
234     CHECK_NULL_VOID(box);
235     box->SetBoxClipFlag(true);
236 }
237 
GetComponent()238 RefPtr<TextComponentV2> TextModelImpl::GetComponent()
239 {
240     auto* stack = ViewStackProcessor::GetInstance();
241     if (!stack) {
242         return nullptr;
243     }
244     auto component = AceType::DynamicCast<TextComponentV2>(stack->GetMainComponent());
245     return component;
246 }
247 
OnSetAlign()248 void TextModelImpl::OnSetAlign()
249 {
250     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
251     CHECK_NULL_VOID(box);
252     auto component = GetComponent();
253     CHECK_NULL_VOID(component);
254     auto alignment = box->GetAlignment();
255     if (NearEqual(alignment.GetHorizontal(), -1.0)) {
256         component->SetAlignment(TextAlign::LEFT);
257     } else if (NearEqual(alignment.GetHorizontal(), 0.0)) {
258         component->SetAlignment(TextAlign::CENTER);
259     } else if (NearEqual(alignment.GetHorizontal(), 1.0)) {
260         component->SetAlignment(TextAlign::RIGHT);
261     }
262 }
263 
SetOnClick(std::function<void (const BaseEventInfo *)> && click)264 void TextModelImpl::SetOnClick(std::function<void(const BaseEventInfo*)>&& click)
265 {
266     auto clickId = EventMarker(std::move(click));
267     auto gesture = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent();
268     if (gesture) {
269         gesture->SetOnClickId(clickId);
270     }
271     auto component = GetComponent();
272     if (component) {
273         component->SetOnClick(clickId);
274     }
275 
276     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
277     if (focusableComponent) {
278         focusableComponent->SetOnClickId(clickId);
279     }
280 }
281 
SetRemoteMessage(std::function<void ()> && event)282 void TextModelImpl::SetRemoteMessage(std::function<void()>&& event)
283 {
284     auto textComponent = GetComponent();
285     CHECK_NULL_VOID(textComponent);
286     textComponent->SetRemoteMessageEvent(EventMarker(std::move(event)));
287 }
288 
SetCopyOption(CopyOptions copyOption)289 void TextModelImpl::SetCopyOption(CopyOptions copyOption)
290 {
291     auto component = GetComponent();
292     CHECK_NULL_VOID(component);
293     component->SetCopyOption(copyOption);
294 }
295 
SetDraggable(bool draggable)296 void TextModelImpl::SetDraggable(bool draggable) {}
297 
SetOnDragStart(NG::OnDragStartFunc && onDragStart)298 void TextModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
299 {
300     auto component = GetComponent();
301     CHECK_NULL_VOID(component);
302     component->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart)));
303 }
304 
SetOnDragEnter(NG::OnDragDropFunc && onDragEnter)305 void TextModelImpl::SetOnDragEnter(NG::OnDragDropFunc&& onDragEnter)
306 {
307     auto component = GetComponent();
308     CHECK_NULL_VOID(component);
309     component->SetOnDragEnterId(onDragEnter);
310 }
311 
SetOnDragMove(NG::OnDragDropFunc && onDragMove)312 void TextModelImpl::SetOnDragMove(NG::OnDragDropFunc&& onDragMove)
313 {
314     auto component = GetComponent();
315     CHECK_NULL_VOID(component);
316     component->SetOnDragMoveId(onDragMove);
317 }
318 
SetOnDragLeave(NG::OnDragDropFunc && onDragLeave)319 void TextModelImpl::SetOnDragLeave(NG::OnDragDropFunc&& onDragLeave)
320 {
321     auto component = GetComponent();
322     CHECK_NULL_VOID(component);
323     component->SetOnDragLeaveId(onDragLeave);
324 }
325 
SetOnDrop(NG::OnDragDropFunc && onDrop)326 void TextModelImpl::SetOnDrop(NG::OnDragDropFunc&& onDrop)
327 {
328     auto component = GetComponent();
329     CHECK_NULL_VOID(component);
330     component->SetOnDropId(onDrop);
331 }
332 
SetMenuOptionItems(std::vector<NG::MenuOptionsParam> && menuOptionsItems)333 void TextModelImpl::SetMenuOptionItems(std::vector<NG::MenuOptionsParam>&& menuOptionsItems) {}
334 
335 } // namespace OHOS::Ace::Framework
336