1 /* 2 * Copyright (c) 2022 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 SetFontSize(const Dimension & value)49void TextModelImpl::SetFontSize(const Dimension& value) 50 { 51 auto component = GetComponent(); 52 CHECK_NULL_VOID(component); 53 54 auto textStyle = component->GetTextStyle(); 55 textStyle.SetFontSize(value); 56 component->SetTextStyle(textStyle); 57 } 58 SetTextColor(const Color & value)59void TextModelImpl::SetTextColor(const Color& value) 60 { 61 auto component = GetComponent(); 62 CHECK_NULL_VOID(component); 63 64 auto textStyle = component->GetTextStyle(); 65 textStyle.SetTextColor(value); 66 component->SetTextStyle(textStyle); 67 } 68 SetItalicFontStyle(Ace::FontStyle value)69void TextModelImpl::SetItalicFontStyle(Ace::FontStyle value) 70 { 71 auto component = GetComponent(); 72 CHECK_NULL_VOID(component); 73 74 auto textStyle = component->GetTextStyle(); 75 textStyle.SetFontStyle(value); 76 component->SetTextStyle(textStyle); 77 } 78 SetFontWeight(Ace::FontWeight value)79void TextModelImpl::SetFontWeight(Ace::FontWeight value) 80 { 81 auto component = GetComponent(); 82 CHECK_NULL_VOID(component); 83 84 auto textStyle = component->GetTextStyle(); 85 textStyle.SetFontWeight(value); 86 component->SetTextStyle(textStyle); 87 } 88 SetFontFamily(const std::vector<std::string> & value)89void TextModelImpl::SetFontFamily(const std::vector<std::string>& value) 90 { 91 auto component = GetComponent(); 92 CHECK_NULL_VOID(component); 93 94 auto textStyle = component->GetTextStyle(); 95 textStyle.SetFontFamilies(value); 96 component->SetTextStyle(textStyle); 97 } 98 SetTextAlign(Ace::TextAlign value)99void TextModelImpl::SetTextAlign(Ace::TextAlign value) 100 { 101 auto component = GetComponent(); 102 CHECK_NULL_VOID(component); 103 104 auto textStyle = component->GetTextStyle(); 105 textStyle.SetTextAlign(value); 106 component->SetTextStyle(textStyle); 107 } 108 SetTextOverflow(Ace::TextOverflow value)109void TextModelImpl::SetTextOverflow(Ace::TextOverflow value) 110 { 111 auto component = GetComponent(); 112 CHECK_NULL_VOID(component); 113 auto textStyle = component->GetTextStyle(); 114 textStyle.SetTextOverflow(value); 115 component->SetTextStyle(textStyle); 116 } 117 SetMaxLines(uint32_t value)118void TextModelImpl::SetMaxLines(uint32_t value) 119 { 120 auto component = GetComponent(); 121 CHECK_NULL_VOID(component); 122 123 auto textStyle = component->GetTextStyle(); 124 textStyle.SetMaxLines(value); 125 component->SetTextStyle(textStyle); 126 } 127 SetLineHeight(const Dimension & value)128void TextModelImpl::SetLineHeight(const Dimension& value) 129 { 130 auto component = GetComponent(); 131 CHECK_NULL_VOID(component); 132 133 auto textStyle = component->GetTextStyle(); 134 textStyle.SetLineHeight(value); 135 component->SetTextStyle(textStyle); 136 } 137 SetTextDecoration(Ace::TextDecoration value)138void TextModelImpl::SetTextDecoration(Ace::TextDecoration value) 139 { 140 auto component = GetComponent(); 141 CHECK_NULL_VOID(component); 142 auto textStyle = component->GetTextStyle(); 143 textStyle.SetTextDecoration(value); 144 component->SetTextStyle(textStyle); 145 } 146 SetTextDecorationColor(const Color & value)147void TextModelImpl::SetTextDecorationColor(const Color& value) 148 { 149 auto component = GetComponent(); 150 CHECK_NULL_VOID(component); 151 auto textStyle = component->GetTextStyle(); 152 textStyle.SetTextDecorationColor(value); 153 component->SetTextStyle(textStyle); 154 } 155 SetBaselineOffset(const Dimension & value)156void TextModelImpl::SetBaselineOffset(const Dimension& value) 157 { 158 auto component = GetComponent(); 159 CHECK_NULL_VOID(component); 160 161 auto textStyle = component->GetTextStyle(); 162 textStyle.SetBaselineOffset(value); 163 component->SetTextStyle(textStyle); 164 } 165 SetTextCase(Ace::TextCase value)166void TextModelImpl::SetTextCase(Ace::TextCase value) 167 { 168 auto component = GetComponent(); 169 CHECK_NULL_VOID(component); 170 171 auto textStyle = component->GetTextStyle(); 172 textStyle.SetTextCase(value); 173 component->SetTextStyle(textStyle); 174 } 175 SetLetterSpacing(const Dimension & value)176void TextModelImpl::SetLetterSpacing(const Dimension& value) 177 { 178 auto component = GetComponent(); 179 CHECK_NULL_VOID(component); 180 181 auto textStyle = component->GetTextStyle(); 182 textStyle.SetLetterSpacing(value); 183 component->SetTextStyle(textStyle); 184 } 185 SetAdaptMinFontSize(const Dimension & value)186void TextModelImpl::SetAdaptMinFontSize(const Dimension& value) 187 { 188 auto component = GetComponent(); 189 CHECK_NULL_VOID(component); 190 191 auto textStyle = component->GetTextStyle(); 192 textStyle.SetAdaptMinFontSize(value); 193 component->SetTextStyle(textStyle); 194 } 195 SetAdaptMaxFontSize(const Dimension & value)196void TextModelImpl::SetAdaptMaxFontSize(const Dimension& value) 197 { 198 auto component = GetComponent(); 199 CHECK_NULL_VOID(component); 200 201 auto textStyle = component->GetTextStyle(); 202 textStyle.SetAdaptMaxFontSize(value); 203 component->SetTextStyle(textStyle); 204 } 205 OnSetWidth()206void TextModelImpl::OnSetWidth() 207 { 208 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 209 CHECK_NULL_VOID(box); 210 auto component = GetComponent(); 211 CHECK_NULL_VOID(component); 212 component->SetMaxWidthLayout(box->GetWidthDimension().IsValid()); 213 } 214 OnSetHeight()215void TextModelImpl::OnSetHeight() 216 { 217 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 218 CHECK_NULL_VOID(box); 219 box->SetBoxClipFlag(true); 220 } 221 GetComponent()222RefPtr<TextComponentV2> TextModelImpl::GetComponent() 223 { 224 auto* stack = ViewStackProcessor::GetInstance(); 225 if (!stack) { 226 return nullptr; 227 } 228 auto component = AceType::DynamicCast<TextComponentV2>(stack->GetMainComponent()); 229 return component; 230 } 231 OnSetAlign()232void TextModelImpl::OnSetAlign() 233 { 234 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent(); 235 CHECK_NULL_VOID(box); 236 auto component = GetComponent(); 237 CHECK_NULL_VOID(component); 238 auto alignment = box->GetAlignment(); 239 if (NearEqual(alignment.GetHorizontal(), -1.0)) { 240 component->SetAlignment(TextAlign::LEFT); 241 } else if (NearEqual(alignment.GetHorizontal(), 0.0)) { 242 component->SetAlignment(TextAlign::CENTER); 243 } else if (NearEqual(alignment.GetHorizontal(), 1.0)) { 244 component->SetAlignment(TextAlign::RIGHT); 245 } 246 } 247 SetOnClick(std::function<void (const BaseEventInfo *)> && click)248void TextModelImpl::SetOnClick(std::function<void(const BaseEventInfo*)>&& click) 249 { 250 auto clickId = EventMarker(std::move(click)); 251 auto gesture = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent(); 252 if (gesture) { 253 gesture->SetOnClickId(clickId); 254 } 255 auto component = GetComponent(); 256 if (component) { 257 component->SetOnClick(clickId); 258 } 259 260 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false); 261 if (focusableComponent) { 262 focusableComponent->SetOnClickId(clickId); 263 } 264 } 265 SetRemoteMessage(std::function<void ()> && event)266void TextModelImpl::SetRemoteMessage(std::function<void()>&& event) 267 { 268 auto textComponent = GetComponent(); 269 CHECK_NULL_VOID(textComponent); 270 textComponent->SetRemoteMessageEvent(EventMarker(std::move(event))); 271 } 272 SetCopyOption(CopyOptions copyOption)273void TextModelImpl::SetCopyOption(CopyOptions copyOption) 274 { 275 auto component = GetComponent(); 276 CHECK_NULL_VOID(component); 277 component->SetCopyOption(copyOption); 278 } 279 SetOnDragStart(NG::OnDragStartFunc && onDragStart)280void TextModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart) 281 { 282 auto component = GetComponent(); 283 CHECK_NULL_VOID(component); 284 component->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart))); 285 } 286 SetOnDragEnter(NG::OnDragDropFunc && onDragEnter)287void TextModelImpl::SetOnDragEnter(NG::OnDragDropFunc&& onDragEnter) 288 { 289 auto component = GetComponent(); 290 CHECK_NULL_VOID(component); 291 component->SetOnDragEnterId(onDragEnter); 292 } 293 SetOnDragMove(NG::OnDragDropFunc && onDragMove)294void TextModelImpl::SetOnDragMove(NG::OnDragDropFunc&& onDragMove) 295 { 296 auto component = GetComponent(); 297 CHECK_NULL_VOID(component); 298 component->SetOnDragMoveId(onDragMove); 299 } 300 SetOnDragLeave(NG::OnDragDropFunc && onDragLeave)301void TextModelImpl::SetOnDragLeave(NG::OnDragDropFunc&& onDragLeave) 302 { 303 auto component = GetComponent(); 304 CHECK_NULL_VOID(component); 305 component->SetOnDragLeaveId(onDragLeave); 306 } 307 SetOnDrop(NG::OnDragDropFunc && onDrop)308void TextModelImpl::SetOnDrop(NG::OnDragDropFunc&& onDrop) 309 { 310 auto component = GetComponent(); 311 CHECK_NULL_VOID(component); 312 component->SetOnDropId(onDrop); 313 } 314 315 } // namespace OHOS::Ace::Framework 316