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)29void 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)49void TextModelImpl::SetFont(const Font& value) {} 50 SetFontSize(const Dimension & value)51void 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)61void 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)71void TextModelImpl::SetTextShadow(const std::vector<Shadow>& value) {} SetTextCaretColor(const Color & value)72void TextModelImpl::SetTextCaretColor(const Color& value) {} SetSelectedBackgroundColor(const Color & value)73void TextModelImpl::SetSelectedBackgroundColor(const Color& value) {} 74 SetItalicFontStyle(Ace::FontStyle value)75void 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)85void 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)95void TextModelImpl::SetMinFontScale(const float value) {} 96 SetMaxFontScale(const float value)97void TextModelImpl::SetMaxFontScale(const float value) {} 98 SetFontFamily(const std::vector<std::string> & value)99void 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)109void 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)119void 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)128void 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)138void 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)148void 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)158void 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)167void 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)176void 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)185void 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)195void 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)205void 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)215void TextModelImpl::SetLineSpacing(const Dimension& value) {} 216 SetAdaptMinFontSize(const Dimension & value)217void TextModelImpl::SetAdaptMinFontSize(const Dimension& value) 218 { 219 auto component = GetComponent(); 220 CHECK_NULL_VOID(component); 221 222 auto textStyle = component->GetTextStyle(); 223 textStyle.SetAdaptMinFontSize(value); 224 component->SetTextStyle(textStyle); 225 } 226 SetAdaptMaxFontSize(const Dimension & value)227void TextModelImpl::SetAdaptMaxFontSize(const Dimension& value) 228 { 229 auto component = GetComponent(); 230 CHECK_NULL_VOID(component); 231 232 auto textStyle = component->GetTextStyle(); 233 textStyle.SetAdaptMaxFontSize(value); 234 component->SetTextStyle(textStyle); 235 } 236 SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value)237void TextModelImpl::SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value) {} 238 SetTextDetectEnable(bool value)239void TextModelImpl::SetTextDetectEnable(bool value) {} 240 SetTextDetectConfig(const TextDetectConfig & textDetectConfig)241void TextModelImpl::SetTextDetectConfig(const TextDetectConfig& textDetectConfig) {} 242 OnSetWidth()243void TextModelImpl::OnSetWidth() 244 { 245 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 246 CHECK_NULL_VOID(box); 247 auto component = GetComponent(); 248 CHECK_NULL_VOID(component); 249 component->SetMaxWidthLayout(box->GetWidthDimension().IsValid()); 250 } 251 OnSetHeight()252void TextModelImpl::OnSetHeight() 253 { 254 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 255 CHECK_NULL_VOID(box); 256 box->SetBoxClipFlag(true); 257 } 258 GetComponent()259RefPtr<TextComponentV2> TextModelImpl::GetComponent() 260 { 261 auto* stack = ViewStackProcessor::GetInstance(); 262 if (!stack) { 263 return nullptr; 264 } 265 auto component = AceType::DynamicCast<TextComponentV2>(stack->GetMainComponent()); 266 return component; 267 } 268 OnSetAlign()269void TextModelImpl::OnSetAlign() 270 { 271 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 272 CHECK_NULL_VOID(box); 273 auto component = GetComponent(); 274 CHECK_NULL_VOID(component); 275 auto alignment = box->GetAlignment(); 276 if (NearEqual(alignment.GetHorizontal(), -1.0)) { 277 component->SetAlignment(TextAlign::LEFT); 278 } else if (NearEqual(alignment.GetHorizontal(), 0.0)) { 279 component->SetAlignment(TextAlign::CENTER); 280 } else if (NearEqual(alignment.GetHorizontal(), 1.0)) { 281 component->SetAlignment(TextAlign::RIGHT); 282 } 283 } 284 SetOnClick(std::function<void (BaseEventInfo *)> && click)285void TextModelImpl::SetOnClick(std::function<void(BaseEventInfo*)>&& click) 286 { 287 auto clickId = EventMarker(std::move(click)); 288 auto gesture = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent(); 289 if (gesture) { 290 gesture->SetOnClickId(clickId); 291 } 292 auto component = GetComponent(); 293 if (component) { 294 component->SetOnClick(clickId); 295 } 296 297 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false); 298 if (focusableComponent) { 299 focusableComponent->SetOnClickId(clickId); 300 } 301 } 302 SetRemoteMessage(std::function<void ()> && event)303void TextModelImpl::SetRemoteMessage(std::function<void()>&& event) 304 { 305 auto textComponent = GetComponent(); 306 CHECK_NULL_VOID(textComponent); 307 textComponent->SetRemoteMessageEvent(EventMarker(std::move(event))); 308 } 309 SetCopyOption(CopyOptions copyOption)310void TextModelImpl::SetCopyOption(CopyOptions copyOption) 311 { 312 auto component = GetComponent(); 313 CHECK_NULL_VOID(component); 314 component->SetCopyOption(copyOption); 315 } 316 SetOnDragStart(NG::OnDragStartFunc && onDragStart)317void TextModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart) 318 { 319 auto component = GetComponent(); 320 CHECK_NULL_VOID(component); 321 component->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart))); 322 } 323 SetOnDragEnter(NG::OnDragDropFunc && onDragEnter)324void TextModelImpl::SetOnDragEnter(NG::OnDragDropFunc&& onDragEnter) 325 { 326 auto component = GetComponent(); 327 CHECK_NULL_VOID(component); 328 component->SetOnDragEnterId(onDragEnter); 329 } 330 SetOnDragMove(NG::OnDragDropFunc && onDragMove)331void TextModelImpl::SetOnDragMove(NG::OnDragDropFunc&& onDragMove) 332 { 333 auto component = GetComponent(); 334 CHECK_NULL_VOID(component); 335 component->SetOnDragMoveId(onDragMove); 336 } 337 SetOnDragLeave(NG::OnDragDropFunc && onDragLeave)338void TextModelImpl::SetOnDragLeave(NG::OnDragDropFunc&& onDragLeave) 339 { 340 auto component = GetComponent(); 341 CHECK_NULL_VOID(component); 342 component->SetOnDragLeaveId(onDragLeave); 343 } 344 SetOnDrop(NG::OnDragDropFunc && onDrop)345void TextModelImpl::SetOnDrop(NG::OnDragDropFunc&& onDrop) 346 { 347 auto component = GetComponent(); 348 CHECK_NULL_VOID(component); 349 component->SetOnDropId(onDrop); 350 } 351 SetHalfLeading(bool halfLeading)352void TextModelImpl::SetHalfLeading(bool halfLeading) {} 353 } // namespace OHOS::Ace::Framework 354