1 /* 2 * Copyright (c) 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/hyperlink_model_impl.h" 17 18 #include "bridge/declarative_frontend/jsview/js_view_abstract.h" 19 #include "core/components/hyperlink/hyperlink_component.h" 20 #include "core/components/text/text_component.h" 21 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h" 22 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h" 23 24 namespace OHOS::Ace::Framework { Create(const std::string & address,const std::string & summary)25void HyperlinkModelImpl::Create(const std::string& address, const std::string& summary) 26 { 27 std::list<RefPtr<Component>> children; 28 RefPtr<OHOS::Ace::HyperlinkComponent> component = AceType::MakeRefPtr<HyperlinkComponent>(children); 29 component->SetAddress(address); 30 if (!summary.empty()) { 31 component->SetSummary(summary); 32 } 33 34 ViewStackProcessor::GetInstance()->Push(component); 35 JSInteractableView::SetFocusable(false); 36 JSInteractableView::SetFocusNode(true); 37 } 38 Pop()39void HyperlinkModelImpl::Pop() 40 { 41 auto hyperlink = 42 AceType::DynamicCast<OHOS::Ace::HyperlinkComponent>(ViewStackProcessor::GetInstance()->GetMainComponent()); 43 JSContainerBase::Pop(); 44 if (hyperlink) { 45 std::string summary = hyperlink->GetSummary(); 46 std::list<RefPtr<Component>> children = hyperlink->GetChildren(); 47 std::list<RefPtr<Component>> flexChild; 48 if (!summary.empty()) { 49 RefPtr<OHOS::Ace::TextComponent> text = AceType::MakeRefPtr<TextComponent>(summary); 50 flexChild.emplace_back(text); 51 } 52 for (const auto& child : children) { 53 flexChild.emplace_back(child); 54 } 55 RefPtr<OHOS::Ace::ColumnComponent> columnComponent = 56 AceType::MakeRefPtr<OHOS::Ace::ColumnComponent>(FlexAlign::FLEX_START, FlexAlign::CENTER, flexChild); 57 columnComponent->SetMainAxisSize(MainAxisSize::MIN); 58 columnComponent->SetCrossAxisSize(CrossAxisSize::MIN); 59 hyperlink->ClearChildren(); 60 hyperlink->AppendChild(columnComponent); 61 } 62 } 63 SetColor(const Color & value)64void HyperlinkModelImpl::SetColor(const Color& value) 65 { 66 auto component = GetComponent(); 67 if (!component) { 68 LOGE("component is not valid"); 69 return; 70 } 71 component->SetColor(value); 72 } 73 GetComponent()74RefPtr<HyperlinkComponent> HyperlinkModelImpl::GetComponent() 75 { 76 auto stack = ViewStackProcessor::GetInstance(); 77 if (!stack) { 78 return nullptr; 79 } 80 return AceType::DynamicCast<HyperlinkComponent>(stack->GetMainComponent()); 81 } 82 } // namespace OHOS::Ace::Framework