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) {} 72 SetItalicFontStyle(Ace::FontStyle value)73void 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)83void 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 SetMinFontScale(const float value)93void TextModelImpl::SetMinFontScale(const float value) {} 94 SetMaxFontScale(const float value)95void TextModelImpl::SetMaxFontScale(const float value) {} 96 SetFontFamily(const std::vector<std::string> & value)97void TextModelImpl::SetFontFamily(const std::vector<std::string>& value) 98 { 99 auto component = GetComponent(); 100 CHECK_NULL_VOID(component); 101 102 auto textStyle = component->GetTextStyle(); 103 textStyle.SetFontFamilies(value); 104 component->SetTextStyle(textStyle); 105 } 106 SetTextAlign(Ace::TextAlign value)107void TextModelImpl::SetTextAlign(Ace::TextAlign value) 108 { 109 auto component = GetComponent(); 110 CHECK_NULL_VOID(component); 111 112 auto textStyle = component->GetTextStyle(); 113 textStyle.SetTextAlign(value); 114 component->SetTextStyle(textStyle); 115 } 116 SetTextOverflow(Ace::TextOverflow value)117void TextModelImpl::SetTextOverflow(Ace::TextOverflow value) 118 { 119 auto component = GetComponent(); 120 CHECK_NULL_VOID(component); 121 auto textStyle = component->GetTextStyle(); 122 textStyle.SetTextOverflow(value); 123 component->SetTextStyle(textStyle); 124 } 125 SetMaxLines(uint32_t value)126void TextModelImpl::SetMaxLines(uint32_t value) 127 { 128 auto component = GetComponent(); 129 CHECK_NULL_VOID(component); 130 131 auto textStyle = component->GetTextStyle(); 132 textStyle.SetMaxLines(value); 133 component->SetTextStyle(textStyle); 134 } 135 SetTextIndent(const Dimension & value)136void TextModelImpl::SetTextIndent(const Dimension& value) 137 { 138 auto component = GetComponent(); 139 CHECK_NULL_VOID(component); 140 141 auto textStyle = component->GetTextStyle(); 142 textStyle.SetTextIndent(value); 143 component->SetTextStyle(textStyle); 144 } 145 SetLineHeight(const Dimension & value)146void TextModelImpl::SetLineHeight(const Dimension& value) 147 { 148 auto component = GetComponent(); 149 CHECK_NULL_VOID(component); 150 151 auto textStyle = component->GetTextStyle(); 152 textStyle.SetLineHeight(value); 153 component->SetTextStyle(textStyle); 154 } 155 SetTextDecoration(Ace::TextDecoration value)156void TextModelImpl::SetTextDecoration(Ace::TextDecoration value) 157 { 158 auto component = GetComponent(); 159 CHECK_NULL_VOID(component); 160 auto textStyle = component->GetTextStyle(); 161 textStyle.SetTextDecoration(value); 162 component->SetTextStyle(textStyle); 163 } 164 SetTextDecorationColor(const Color & value)165void TextModelImpl::SetTextDecorationColor(const Color& value) 166 { 167 auto component = GetComponent(); 168 CHECK_NULL_VOID(component); 169 auto textStyle = component->GetTextStyle(); 170 textStyle.SetTextDecorationColor(value); 171 component->SetTextStyle(textStyle); 172 } 173 SetTextDecorationStyle(TextDecorationStyle value)174void TextModelImpl::SetTextDecorationStyle(TextDecorationStyle value) 175 { 176 auto component = GetComponent(); 177 CHECK_NULL_VOID(component); 178 auto textStyle = component->GetTextStyle(); 179 textStyle.SetTextDecorationStyle(value); 180 component->SetTextStyle(textStyle); 181 } 182 SetBaselineOffset(const Dimension & value)183void TextModelImpl::SetBaselineOffset(const Dimension& value) 184 { 185 auto component = GetComponent(); 186 CHECK_NULL_VOID(component); 187 188 auto textStyle = component->GetTextStyle(); 189 textStyle.SetBaselineOffset(value); 190 component->SetTextStyle(textStyle); 191 } 192 SetTextCase(Ace::TextCase value)193void TextModelImpl::SetTextCase(Ace::TextCase value) 194 { 195 auto component = GetComponent(); 196 CHECK_NULL_VOID(component); 197 198 auto textStyle = component->GetTextStyle(); 199 textStyle.SetTextCase(value); 200 component->SetTextStyle(textStyle); 201 } 202 SetLetterSpacing(const Dimension & value)203void TextModelImpl::SetLetterSpacing(const Dimension& value) 204 { 205 auto component = GetComponent(); 206 CHECK_NULL_VOID(component); 207 208 auto textStyle = component->GetTextStyle(); 209 textStyle.SetLetterSpacing(value); 210 component->SetTextStyle(textStyle); 211 } 212 SetLineSpacing(const Dimension & value)213void TextModelImpl::SetLineSpacing(const Dimension& value) {} 214 SetAdaptMinFontSize(const Dimension & value)215void TextModelImpl::SetAdaptMinFontSize(const Dimension& value) 216 { 217 auto component = GetComponent(); 218 CHECK_NULL_VOID(component); 219 220 auto textStyle = component->GetTextStyle(); 221 textStyle.SetAdaptMinFontSize(value); 222 component->SetTextStyle(textStyle); 223 } 224 SetAdaptMaxFontSize(const Dimension & value)225void TextModelImpl::SetAdaptMaxFontSize(const Dimension& value) 226 { 227 auto component = GetComponent(); 228 CHECK_NULL_VOID(component); 229 230 auto textStyle = component->GetTextStyle(); 231 textStyle.SetAdaptMaxFontSize(value); 232 component->SetTextStyle(textStyle); 233 } 234 SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value)235void TextModelImpl::SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value) {} 236 SetTextDetectEnable(bool value)237void TextModelImpl::SetTextDetectEnable(bool value) {} 238 SetTextDetectConfig(const TextDetectConfig & textDetectConfig)239void TextModelImpl::SetTextDetectConfig(const TextDetectConfig& textDetectConfig) {} 240 OnSetWidth()241void TextModelImpl::OnSetWidth() 242 { 243 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 244 CHECK_NULL_VOID(box); 245 auto component = GetComponent(); 246 CHECK_NULL_VOID(component); 247 component->SetMaxWidthLayout(box->GetWidthDimension().IsValid()); 248 } 249 OnSetHeight()250void TextModelImpl::OnSetHeight() 251 { 252 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 253 CHECK_NULL_VOID(box); 254 box->SetBoxClipFlag(true); 255 } 256 GetComponent()257RefPtr<TextComponentV2> TextModelImpl::GetComponent() 258 { 259 auto* stack = ViewStackProcessor::GetInstance(); 260 if (!stack) { 261 return nullptr; 262 } 263 auto component = AceType::DynamicCast<TextComponentV2>(stack->GetMainComponent()); 264 return component; 265 } 266 OnSetAlign()267void TextModelImpl::OnSetAlign() 268 { 269 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 270 CHECK_NULL_VOID(box); 271 auto component = GetComponent(); 272 CHECK_NULL_VOID(component); 273 auto alignment = box->GetAlignment(); 274 if (NearEqual(alignment.GetHorizontal(), -1.0)) { 275 component->SetAlignment(TextAlign::LEFT); 276 } else if (NearEqual(alignment.GetHorizontal(), 0.0)) { 277 component->SetAlignment(TextAlign::CENTER); 278 } else if (NearEqual(alignment.GetHorizontal(), 1.0)) { 279 component->SetAlignment(TextAlign::RIGHT); 280 } 281 } 282 SetOnClick(std::function<void (BaseEventInfo *)> && click)283void TextModelImpl::SetOnClick(std::function<void(BaseEventInfo*)>&& click) 284 { 285 auto clickId = EventMarker(std::move(click)); 286 auto gesture = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent(); 287 if (gesture) { 288 gesture->SetOnClickId(clickId); 289 } 290 auto component = GetComponent(); 291 if (component) { 292 component->SetOnClick(clickId); 293 } 294 295 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false); 296 if (focusableComponent) { 297 focusableComponent->SetOnClickId(clickId); 298 } 299 } 300 SetRemoteMessage(std::function<void ()> && event)301void TextModelImpl::SetRemoteMessage(std::function<void()>&& event) 302 { 303 auto textComponent = GetComponent(); 304 CHECK_NULL_VOID(textComponent); 305 textComponent->SetRemoteMessageEvent(EventMarker(std::move(event))); 306 } 307 SetCopyOption(CopyOptions copyOption)308void TextModelImpl::SetCopyOption(CopyOptions copyOption) 309 { 310 auto component = GetComponent(); 311 CHECK_NULL_VOID(component); 312 component->SetCopyOption(copyOption); 313 } 314 SetOnDragStart(NG::OnDragStartFunc && onDragStart)315void TextModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart) 316 { 317 auto component = GetComponent(); 318 CHECK_NULL_VOID(component); 319 component->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart))); 320 } 321 SetOnDragEnter(NG::OnDragDropFunc && onDragEnter)322void TextModelImpl::SetOnDragEnter(NG::OnDragDropFunc&& onDragEnter) 323 { 324 auto component = GetComponent(); 325 CHECK_NULL_VOID(component); 326 component->SetOnDragEnterId(onDragEnter); 327 } 328 SetOnDragMove(NG::OnDragDropFunc && onDragMove)329void TextModelImpl::SetOnDragMove(NG::OnDragDropFunc&& onDragMove) 330 { 331 auto component = GetComponent(); 332 CHECK_NULL_VOID(component); 333 component->SetOnDragMoveId(onDragMove); 334 } 335 SetOnDragLeave(NG::OnDragDropFunc && onDragLeave)336void TextModelImpl::SetOnDragLeave(NG::OnDragDropFunc&& onDragLeave) 337 { 338 auto component = GetComponent(); 339 CHECK_NULL_VOID(component); 340 component->SetOnDragLeaveId(onDragLeave); 341 } 342 SetOnDrop(NG::OnDragDropFunc && onDrop)343void TextModelImpl::SetOnDrop(NG::OnDragDropFunc&& onDrop) 344 { 345 auto component = GetComponent(); 346 CHECK_NULL_VOID(component); 347 component->SetOnDropId(onDrop); 348 } 349 SetHalfLeading(bool halfLeading)350void TextModelImpl::SetHalfLeading(bool halfLeading) {} 351 } // namespace OHOS::Ace::Framework 352