• 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 "core/components_ng/pattern/text/text_model_ng.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/common/properties/alignment.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/base/view_abstract.h"
23 #include "core/components_ng/base/view_stack_processor.h"
24 #include "core/components_ng/pattern/text/text_pattern.h"
25 #include "core/components_v2/inspector/inspector_constants.h"
26 
27 namespace OHOS::Ace::NG {
Create(const std::string & content)28 void TextModelNG::Create(const std::string& content)
29 {
30     auto* stack = ViewStackProcessor::GetInstance();
31     auto nodeId = stack->ClaimNodeId();
32     auto frameNode =
33         FrameNode::GetOrCreateFrameNode(V2::TEXT_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<TextPattern>(); });
34     stack->Push(frameNode);
35 
36     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, Content, content);
37 }
38 
SetFont(const Font & value)39 void TextModelNG::SetFont(const Font& value)
40 {
41     if (value.fontSize.has_value()) {
42         SetFontSize(value.fontSize.value());
43     }
44     if (value.fontWeight.has_value()) {
45         SetFontWeight(value.fontWeight.value());
46     }
47     if (!value.fontFamilies.empty()) {
48         SetFontFamily(value.fontFamilies);
49     }
50     if (value.fontStyle.has_value()) {
51         SetItalicFontStyle(value.fontStyle.value());
52     }
53 }
54 
SetFontSize(const Dimension & value)55 void TextModelNG::SetFontSize(const Dimension& value)
56 {
57     if (!value.IsValid()) {
58         LOGE("FontSize value is not valid");
59         ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, FontSize, Dimension());
60         return;
61     }
62     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, FontSize, value);
63 }
64 
SetTextColor(const Color & value)65 void TextModelNG::SetTextColor(const Color& value)
66 {
67     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextColor, value);
68     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, ForegroundColor, value);
69     ACE_UPDATE_RENDER_CONTEXT(ForegroundColor, value);
70     ACE_RESET_RENDER_CONTEXT(RenderContext, ForegroundColorStrategy);
71     ACE_UPDATE_RENDER_CONTEXT(ForegroundColorFlag, true);
72 }
73 
SetTextShadow(const Shadow & value)74 void TextModelNG::SetTextShadow(const Shadow& value)
75 {
76     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextShadow, value);
77 }
78 
SetItalicFontStyle(Ace::FontStyle value)79 void TextModelNG::SetItalicFontStyle(Ace::FontStyle value)
80 {
81     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, ItalicFontStyle, value);
82 }
83 
SetFontWeight(Ace::FontWeight value)84 void TextModelNG::SetFontWeight(Ace::FontWeight value)
85 {
86     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, FontWeight, value);
87 }
88 
SetFontFamily(const std::vector<std::string> & value)89 void TextModelNG::SetFontFamily(const std::vector<std::string>& value)
90 {
91     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, FontFamily, value);
92 }
93 
SetTextAlign(Ace::TextAlign value)94 void TextModelNG::SetTextAlign(Ace::TextAlign value)
95 {
96     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextAlign, value);
97 }
98 
SetTextOverflow(Ace::TextOverflow value)99 void TextModelNG::SetTextOverflow(Ace::TextOverflow value)
100 {
101     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextOverflow, value);
102 }
103 
SetMaxLines(uint32_t value)104 void TextModelNG::SetMaxLines(uint32_t value)
105 {
106     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, MaxLines, value);
107 }
108 
SetTextIndent(const Dimension & value)109 void TextModelNG::SetTextIndent(const Dimension& value)
110 {
111     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextIndent, value);
112 }
113 
SetLineHeight(const Dimension & value)114 void TextModelNG::SetLineHeight(const Dimension& value)
115 {
116     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, LineHeight, value);
117 }
118 
SetTextDecoration(Ace::TextDecoration value)119 void TextModelNG::SetTextDecoration(Ace::TextDecoration value)
120 {
121     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextDecoration, value);
122 }
123 
SetTextDecorationColor(const Color & value)124 void TextModelNG::SetTextDecorationColor(const Color& value)
125 {
126     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextDecorationColor, value);
127 }
128 
SetBaselineOffset(const Dimension & value)129 void TextModelNG::SetBaselineOffset(const Dimension& value)
130 {
131     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, BaselineOffset, value);
132 }
133 
SetTextCase(Ace::TextCase value)134 void TextModelNG::SetTextCase(Ace::TextCase value)
135 {
136     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextCase, value);
137 }
138 
SetLetterSpacing(const Dimension & value)139 void TextModelNG::SetLetterSpacing(const Dimension& value)
140 {
141     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, LetterSpacing, value);
142 }
143 
SetAdaptMinFontSize(const Dimension & value)144 void TextModelNG::SetAdaptMinFontSize(const Dimension& value)
145 {
146     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, AdaptMinFontSize, value);
147 }
148 
SetAdaptMaxFontSize(const Dimension & value)149 void TextModelNG::SetAdaptMaxFontSize(const Dimension& value)
150 {
151     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, AdaptMaxFontSize, value);
152 }
153 
SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value)154 void TextModelNG::SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value)
155 {
156     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, HeightAdaptivePolicy, value);
157 }
158 
SetOnClick(std::function<void (const BaseEventInfo * info)> && click)159 void TextModelNG::SetOnClick(std::function<void(const BaseEventInfo* info)>&& click)
160 {
161     auto clickFunc = [func = std::move(click)](GestureEvent& info) { func(&info); };
162     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
163     CHECK_NULL_VOID(frameNode);
164     auto textPattern = frameNode->GetPattern<TextPattern>();
165     CHECK_NULL_VOID(textPattern);
166     textPattern->SetOnClickEvent(std::move(clickFunc));
167 }
168 
ClearOnClick()169 void TextModelNG::ClearOnClick()
170 {
171     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
172     CHECK_NULL_VOID(frameNode);
173     auto textPattern = frameNode->GetPattern<TextPattern>();
174     CHECK_NULL_VOID(textPattern);
175     textPattern->SetOnClickEvent(nullptr);
176 }
177 
SetRemoteMessage(std::function<void ()> && event)178 void TextModelNG::SetRemoteMessage(std::function<void()>&& event)
179 {
180     LOGE("no support RemoteMessage");
181 }
182 
SetCopyOption(CopyOptions copyOption)183 void TextModelNG::SetCopyOption(CopyOptions copyOption)
184 {
185     ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, CopyOption, copyOption);
186 }
187 
SetDraggable(bool draggable)188 void TextModelNG::SetDraggable(bool draggable)
189 {
190     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
191     CHECK_NULL_VOID(frameNode);
192     frameNode->SetDraggable(draggable);
193 }
194 
SetMenuOptionItems(std::vector<MenuOptionsParam> && menuOptionsItems)195 void TextModelNG::SetMenuOptionItems(std::vector<MenuOptionsParam>&& menuOptionsItems)
196 {
197     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
198     CHECK_NULL_VOID(frameNode);
199     auto textPattern = frameNode->GetPattern<TextPattern>();
200     textPattern->SetMenuOptionItems(std::move(menuOptionsItems));
201 }
202 
SetOnDragStart(NG::OnDragStartFunc && onDragStart)203 void TextModelNG::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
204 {
205     auto dragStart = [dragStartFunc = std::move(onDragStart)](
206                          const RefPtr<OHOS::Ace::DragEvent>& event, const std::string& extraParams) -> DragDropInfo {
207         auto dragInfo = dragStartFunc(event, extraParams);
208         DragDropInfo info;
209         info.extraInfo = dragInfo.extraInfo;
210         info.pixelMap = dragInfo.pixelMap;
211         info.customNode = AceType::DynamicCast<UINode>(dragInfo.node);
212         return info;
213     };
214     ViewAbstract::SetOnDragStart(std::move(dragStart));
215 }
216 
SetOnDragEnter(NG::OnDragDropFunc && onDragEnter)217 void TextModelNG::SetOnDragEnter(NG::OnDragDropFunc&& onDragEnter)
218 {
219     ViewAbstract::SetOnDragEnter(std::move(onDragEnter));
220 }
221 
SetOnDragMove(NG::OnDragDropFunc && onDragMove)222 void TextModelNG::SetOnDragMove(NG::OnDragDropFunc&& onDragMove)
223 {
224     ViewAbstract::SetOnDragMove(std::move(onDragMove));
225 }
226 
SetOnDragLeave(NG::OnDragDropFunc && onDragLeave)227 void TextModelNG::SetOnDragLeave(NG::OnDragDropFunc&& onDragLeave)
228 {
229     ViewAbstract::SetOnDragLeave(std::move(onDragLeave));
230 }
231 
SetOnDrop(NG::OnDragDropFunc && onDrop)232 void TextModelNG::SetOnDrop(NG::OnDragDropFunc&& onDrop)
233 {
234     ViewAbstract::SetOnDrop(std::move(onDrop));
235 }
236 } // namespace OHOS::Ace::NG
237