From 7753a274b8fc58924bea620b5ba0637216d3e3b7 Mon Sep 17 00:00:00 2001 From: jinweiliu Date: Thu, 16 Mar 2023 11:21:09 +0000 Subject: [PATCH] fix hyperlink Signed-off-by: jinweiliu Change-Id: Id8a8d5a969d671779c5079562f431a7a6b11d6a9 --- adapter/ohos/entrance/ace_ability.cpp | 4 +- adapter/ohos/entrance/ui_content_impl.cpp | 7 +- .../bridge/declarative_frontend/BUILD.gn | 1 + .../jsview/js_hyperlink.cpp | 128 ++++++++---------- .../jsview/js_hyperlink.h | 18 ++- .../jsview/models/hyperlink_model_impl.cpp | 88 ++++++++++++ .../jsview/models/hyperlink_model_impl.h | 34 +++++ .../core/components_ng/pattern/BUILD.gn | 2 + .../hyperlink/hyperlink_layout_property.h | 66 +++++++++ .../pattern/hyperlink/hyperlink_model.h | 38 ++++++ .../pattern/hyperlink/hyperlink_model_ng.cpp | 54 ++++++++ .../pattern/hyperlink/hyperlink_model_ng.h | 34 +++++ .../pattern/hyperlink/hyperlink_pattern.cpp | 60 ++++++++ .../pattern/hyperlink/hyperlink_pattern.h | 56 ++++++++ 14 files changed, 503 insertions(+), 87 deletions(-) create mode 100644 frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.cpp create mode 100644 frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h create mode 100644 frameworks/core/components_ng/pattern/hyperlink/hyperlink_layout_property.h create mode 100644 frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h create mode 100644 frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.cpp create mode 100644 frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h create mode 100644 frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.cpp create mode 100644 frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.h diff --git a/adapter/ohos/entrance/ace_ability.cpp b/adapter/ohos/entrance/ace_ability.cpp index d4d1dd1bc90..e17f65deb36 100644 --- a/adapter/ohos/entrance/ace_ability.cpp +++ b/adapter/ohos/entrance/ace_ability.cpp @@ -57,6 +57,7 @@ namespace { const std::string ABS_BUNDLE_CODE_PATH = "/data/app/el1/bundle/public/"; const std::string LOCAL_BUNDLE_CODE_PATH = "/data/storage/el1/bundle/"; const std::string FILE_SEPARATOR = "/"; +const std::string ACTION_VIEWDATA = "ohos.want.action.viewData"; static int32_t g_instanceId = 0; FrontendType GetFrontendType(const std::string& frontendType) @@ -359,7 +360,8 @@ void AceAbility::OnStart(const Want& want) [this](const std::string& address) { AAFwk::Want want; want.AddEntity(Want::ENTITY_BROWSER); - want.SetParam("address", address); + want.SetUri(address); + want.SetAction(ACTION_VIEWDATA); this->StartAbility(want); }), false, useNewPipe); diff --git a/adapter/ohos/entrance/ui_content_impl.cpp b/adapter/ohos/entrance/ui_content_impl.cpp index 9e34c2ef25c..00cefbda27d 100644 --- a/adapter/ohos/entrance/ui_content_impl.cpp +++ b/adapter/ohos/entrance/ui_content_impl.cpp @@ -67,6 +67,7 @@ const std::string ABS_BUNDLE_CODE_PATH = "/data/app/el1/bundle/public/"; const std::string LOCAL_BUNDLE_CODE_PATH = "/data/storage/el1/bundle/"; const std::string FILE_SEPARATOR = "/"; const std::string START_PARAMS_KEY = "__startParams"; +const std::string ACTION_VIEWDATA = "ohos.want.action.viewData"; } // namespace @@ -618,7 +619,8 @@ void UIContentImpl::CommonInitializeForm(OHOS::Rosen::Window* window, LOGI("start ability with url = %{private}s", address.c_str()); AAFwk::Want want; want.AddEntity(Want::ENTITY_BROWSER); - want.SetParam("address", address); + want.SetUri(address); + want.SetAction(ACTION_VIEWDATA); abilityContext->StartAbility(want, REQUEST_CODE); }), false, false, useNewPipe); @@ -1053,7 +1055,8 @@ void UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window, const std::str LOGI("start ability with url = %{private}s", address.c_str()); AAFwk::Want want; want.AddEntity(Want::ENTITY_BROWSER); - want.SetParam("address", address); + want.SetUri(address); + want.SetAction(ACTION_VIEWDATA); abilityContext->StartAbility(want, REQUEST_CODE); }), false, false, useNewPipe); diff --git a/frameworks/bridge/declarative_frontend/BUILD.gn b/frameworks/bridge/declarative_frontend/BUILD.gn index 613190a1069..219d5fc7aed 100644 --- a/frameworks/bridge/declarative_frontend/BUILD.gn +++ b/frameworks/bridge/declarative_frontend/BUILD.gn @@ -275,6 +275,7 @@ template("declarative_js_engine") { "jsview/models/grid_item_model_impl.cpp", "jsview/models/grid_model_impl.cpp", "jsview/models/grid_row_model_impl.cpp", + "jsview/models/hyperlink_model_impl.cpp", "jsview/models/if_else_model_impl.cpp", "jsview/models/image_model_impl.cpp", "jsview/models/lazy_for_each_model_impl.cpp", diff --git a/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.cpp b/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.cpp index 2c5df01a22c..cb483bd7dc8 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,66 +15,63 @@ #include "frameworks/bridge/declarative_frontend/jsview/js_hyperlink.h" -#include "base/log/ace_trace.h" -#include "core/components/hyperlink/hyperlink_component.h" -#include "core/components/text/text_component.h" -#include "frameworks/bridge/common/utils/utils.h" #include "frameworks/bridge/declarative_frontend/engine/bindings.h" -#include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h" #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" -#include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" -#include "frameworks/bridge/declarative_frontend/view_stack_processor.h" +#include "frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h" +#include "frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h" +#include "frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h" -namespace OHOS::Ace::Framework { +namespace OHOS::Ace { -void JSHyperlink::Create(const JSCallbackInfo& args) +std::unique_ptr HyperlinkModel::instance_ = nullptr; + +HyperlinkModel* HyperlinkModel::GetInstance() { - std::list> children; - RefPtr component = AceType::MakeRefPtr(children); - if (args.Length() == 1) { - std::string address; - if (ParseJsString(args[0], address)) { - component->SetAddress(address); - } - } else if (args.Length() == 2) { - std::string address; - if (ParseJsString(args[0], address)) { - component->SetAddress(address); - } - std::string summary; - if (ParseJsString(args[1], summary)) { - component->SetSummary(summary); + if (!instance_) { +#ifdef NG_BUILD + instance_.reset(new NG::HyperlinkModelNG()); +#else + if (Container::IsCurrentUseNewPipeline()) { + instance_.reset(new NG::HyperlinkModelNG()); + } else { + instance_.reset(new Framework::HyperlinkModelImpl()); } +#endif } + return instance_.get(); +} + +} // namespace OHOS::Ace + +namespace OHOS::Ace::Framework { + +void JSHyperlink::JSBind(BindingTarget globalObj) +{ + JSClass::Declare("Hyperlink"); + + MethodOptions opt = MethodOptions::NONE; + JSClass::StaticMethod("create", &JSHyperlink::Create, opt); + JSClass::StaticMethod("color", &JSHyperlink::SetColor, opt); + JSClass::StaticMethod("pop", &JSHyperlink::Pop); - ViewStackProcessor::GetInstance()->Push(component); - JSInteractableView::SetFocusable(false); - JSInteractableView::SetFocusNode(true); + JSClass::Inherit(); + JSClass::Inherit(); + JSClass::Bind(globalObj); } -void JSHyperlink::Pop() +void JSHyperlink::Create(const JSCallbackInfo& args) { - auto hyperlink = - AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); - JSContainerBase::Pop(); - if (hyperlink) { - std::string summary = hyperlink->GetSummary(); - std::list> children = hyperlink->GetChildren(); - std::list> flexChild; - if (!summary.empty()) { - RefPtr text = AceType::MakeRefPtr(summary); - flexChild.emplace_back(text); - } - for (const auto& child : children) { - flexChild.emplace_back(child); - } - RefPtr columnComponent = - AceType::MakeRefPtr(FlexAlign::FLEX_START, FlexAlign::CENTER, flexChild); - columnComponent->SetMainAxisSize(MainAxisSize::MIN); - columnComponent->SetCrossAxisSize(CrossAxisSize::MIN); - hyperlink->ClearChildren(); - hyperlink->AppendChild(columnComponent); + std::string address; + ParseJsString(args[0], address); + + std::string summary; + if (args.Length() == 2) { + ParseJsString(args[1], summary); } + + HyperlinkModel::GetInstance()->Create(address, summary); } void JSHyperlink::SetColor(const JSCallbackInfo& info) @@ -87,34 +84,17 @@ void JSHyperlink::SetColor(const JSCallbackInfo& info) if (!ParseJsColor(info[0], color)) { return; } - auto component = GetComponent(); - if (!component) { - LOGE("component is not valid"); - return; - } - component->SetColor(color); + HyperlinkModel::GetInstance()->SetColor(color); } -void JSHyperlink::JSBind(BindingTarget globalObj) -{ - JSClass::Declare("Hyperlink"); - - MethodOptions opt = MethodOptions::NONE; - JSClass::StaticMethod("create", &JSHyperlink::Create, opt); - JSClass::StaticMethod("color", &JSHyperlink::SetColor, opt); - JSClass::StaticMethod("pop", &JSHyperlink::Pop, opt); - JSClass::Inherit(); - JSClass::Inherit(); - JSClass::Bind<>(globalObj); -} - -RefPtr JSHyperlink::GetComponent() +void JSHyperlink::Pop() { - auto stack = ViewStackProcessor::GetInstance(); - if (!stack) { - return nullptr; + if (Container::IsCurrentUseNewPipeline()) { + JSViewAbstract::Pop(); + return; } - return AceType::DynamicCast(stack->GetMainComponent()); + + HyperlinkModel::GetInstance()->Pop(); } -} // namespace OHOS::Ace::Framework \ No newline at end of file +} // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.h b/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.h index 2a9028276eb..34baabe1a18 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.h +++ b/frameworks/bridge/declarative_frontend/jsview/js_hyperlink.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,22 +18,20 @@ #include -#include "base/log/ace_trace.h" -#include "core/components/common/layout/constants.h" -#include "core/components/hyperlink/hyperlink_component.h" -#include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h" namespace OHOS::Ace::Framework { -class JSHyperlink : public JSContainerBase { +class JSHyperlink : public JSViewAbstract, public JSInteractableView { public: - static void Create(const JSCallbackInfo& args); static void JSBind(BindingTarget globalObj); - static void Pop(); + + static void Create(const JSCallbackInfo& args); static void SetColor(const JSCallbackInfo& info); - static RefPtr GetComponent(); + static void Pop(); }; } // namespace OHOS::Ace::Framework -#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_HYPERLINK_H \ No newline at end of file +#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_HYPERLINK_H diff --git a/frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.cpp b/frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.cpp new file mode 100644 index 00000000000..975a533388e --- /dev/null +++ b/frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.cpp @@ -0,0 +1,88 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h" + +#include "bridge/declarative_frontend/jsview/js_view_abstract.h" +#include "core/components/hyperlink/hyperlink_component.h" +#include "core/components/text/text_component.h" +#include "frameworks/bridge/common/utils/utils.h" +#include "frameworks/bridge/declarative_frontend/engine/bindings.h" +#include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h" +#include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" +#include "frameworks/bridge/declarative_frontend/view_stack_processor.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h" + +namespace OHOS::Ace::Framework { +void HyperlinkModelImpl::Create(const std::string& address, const std::string& summary) +{ + std::list> children; + RefPtr component = AceType::MakeRefPtr(children); + component->SetAddress(address); + if (!summary.empty()) { + component->SetSummary(summary); + } + + ViewStackProcessor::GetInstance()->Push(component); + JSInteractableView::SetFocusable(false); + JSInteractableView::SetFocusNode(true); +} + +void HyperlinkModelImpl::Pop() +{ + auto hyperlink = + AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); + JSContainerBase::Pop(); + if (hyperlink) { + std::string summary = hyperlink->GetSummary(); + std::list> children = hyperlink->GetChildren(); + std::list> flexChild; + if (!summary.empty()) { + RefPtr text = AceType::MakeRefPtr(summary); + flexChild.emplace_back(text); + } + for (const auto& child : children) { + flexChild.emplace_back(child); + } + RefPtr columnComponent = + AceType::MakeRefPtr(FlexAlign::FLEX_START, FlexAlign::CENTER, flexChild); + columnComponent->SetMainAxisSize(MainAxisSize::MIN); + columnComponent->SetCrossAxisSize(CrossAxisSize::MIN); + hyperlink->ClearChildren(); + hyperlink->AppendChild(columnComponent); + } +} + +void HyperlinkModelImpl::SetColor(const Color& value) +{ + auto component = GetComponent(); + if (!component) { + LOGE("component is not valid"); + return; + } + component->SetColor(value); +} + +RefPtr HyperlinkModelImpl::GetComponent() +{ + auto stack = ViewStackProcessor::GetInstance(); + if (!stack) { + return nullptr; + } + return AceType::DynamicCast(stack->GetMainComponent()); +} +} // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h b/frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h new file mode 100644 index 00000000000..baa4ea61e12 --- /dev/null +++ b/frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MODELS_HYPERLINK_MODEL_IMPL_H +#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MODELS_HYPERLINK_MODEL_IMPL_H + +#include "core/components_ng/pattern/hyperlink/hyperlink_model.h" +#include "core/components/hyperlink/hyperlink_component.h" + +namespace OHOS::Ace::Framework { +class ACE_EXPORT HyperlinkModelImpl : public HyperlinkModel { +public: + void Create(const std::string& address, const std::string& summary) override; + void SetColor(const Color& value) override; + void Pop() override; + +private: + RefPtr GetComponent(); +}; +} // namespace OHOS::Ace::Framework + +#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MODELS_HYPERLINK_MODEL_IMPL_H diff --git a/frameworks/core/components_ng/pattern/BUILD.gn b/frameworks/core/components_ng/pattern/BUILD.gn index b38df514255..f72f21c3ce0 100644 --- a/frameworks/core/components_ng/pattern/BUILD.gn +++ b/frameworks/core/components_ng/pattern/BUILD.gn @@ -106,6 +106,8 @@ build_component_ng("pattern_ng") { "grid_row/grid_row_layout_algorithm.cpp", "grid_row/grid_row_layout_property.cpp", "grid_row/grid_row_model_ng.cpp", + "hyperlink/hyperlink_model_ng.cpp", + "hyperlink/hyperlink_pattern.cpp", "image/image_layout_algorithm.cpp", "image/image_model_ng.cpp", "image/image_paint_method.cpp", diff --git a/frameworks/core/components_ng/pattern/hyperlink/hyperlink_layout_property.h b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_layout_property.h new file mode 100644 index 00000000000..fbccfacc542 --- /dev/null +++ b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_layout_property.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_HYPERLINK_LAYOUT_PROPERTY_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_HYPERLINK_LAYOUT_PROPERTY_H + +#include + +#include "core/components_ng/layout/layout_property.h" +#include "core/components_ng/pattern/text/text_layout_property.h" +#include "core/components_ng/property/property.h" + +namespace OHOS::Ace::NG { +class ACE_EXPORT HyperlinkLayoutProperty : public TextLayoutProperty { + DECLARE_ACE_TYPE(HyperlinkLayoutProperty, TextLayoutProperty); + +public: + HyperlinkLayoutProperty() = default; + + ~HyperlinkLayoutProperty() override = default; + + RefPtr Clone() const override + { + auto value = MakeRefPtr(); + TextLayoutProperty::Clone(value); + value->LayoutProperty::UpdateLayoutProperty(DynamicCast(this)); + value->propColor_ = CloneColor(); + value->propContent_ = CloneContent(); + return value; + } + + void Reset() override + { + TextLayoutProperty::Reset(); + ResetColor(); + ResetContent(); + } + + void ToJsonValue(std::unique_ptr& json) const override + { + LayoutProperty::ToJsonValue(json); + json->Put("color", propColor_.value_or(Color::BLUE).ColorToString().c_str()); + json->Put("content", propContent_.value_or("").c_str()); + } + + ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Color, Color, PROPERTY_UPDATE_MEASURE); + ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Content, std::string, PROPERTY_UPDATE_MEASURE); + +private: + ACE_DISALLOW_COPY_AND_MOVE(HyperlinkLayoutProperty); +}; +} // namespace OHOS::Ace::NG + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_HYPERLINK_LAYOUT_PROPERTY_H diff --git a/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h new file mode 100644 index 00000000000..65b8d202281 --- /dev/null +++ b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_HYPERLINK_HYPERLINK_MODEL_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_HYPERLINK_HYPERLINK_MODEL_H + +#include "base/utils/macros.h" +#include "core/components/common/layout/constants.h" +#include "core/components/common/properties/color.h" + +namespace OHOS::Ace { +class ACE_EXPORT HyperlinkModel { +public: + static HyperlinkModel* GetInstance(); + virtual ~HyperlinkModel() = default; + + virtual void Create(const std::string& address, const std::string& summary) = 0; + virtual void Pop() = 0; + virtual void SetColor(const Color& value) = 0; + +private: + static std::unique_ptr instance_; +}; +} // namespace OHOS::Ace + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_HYPERLINK_HYPERLINK_MODEL_H diff --git a/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.cpp b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.cpp new file mode 100644 index 00000000000..0fc1ddc5d98 --- /dev/null +++ b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/components_ng/pattern/hyperlink/hyperlink_model_ng.h" + +#include "core/components_ng/base/view_stack_processor.h" +#include "core/components_ng/pattern/hyperlink/hyperlink_pattern.h" + +namespace OHOS::Ace::NG { +void HyperlinkModelNG::Create(const std::string& address, const std::string& content) +{ + auto* stack = ViewStackProcessor::GetInstance(); + auto nodeId = stack->ClaimNodeId(); + auto hyperlinkNode = FrameNode::GetOrCreateFrameNode( + V2::HYPERLINK_ETS_TAG, nodeId, [address]() { return AceType::MakeRefPtr(address); }); + + stack->Push(hyperlinkNode); + SetTextStyle(hyperlinkNode, content); +} + +void HyperlinkModelNG::SetColor(const Color& value) +{ + LOGI("Hyperlink setColor."); + ACE_UPDATE_LAYOUT_PROPERTY(TextLayoutProperty, TextColor, value); +} + +void HyperlinkModelNG::SetTextStyle(const RefPtr& hyperlinkNode, const std::string& content) +{ + CHECK_NULL_VOID(hyperlinkNode); + auto textLayoutProperty = hyperlinkNode->GetLayoutProperty(); + CHECK_NULL_VOID(textLayoutProperty); + auto textStyle = PipelineBase::GetCurrentContext()->GetTheme()->GetTextStyle(); + textLayoutProperty->UpdateContent(content); + textLayoutProperty->UpdateTextOverflow(TextOverflow::ELLIPSIS); + textLayoutProperty->UpdateFontSize(textStyle.GetFontSize()); + textLayoutProperty->UpdateTextColor(textStyle.GetTextColor()); + textLayoutProperty->UpdateFontWeight(textStyle.GetFontWeight()); + textLayoutProperty->UpdateTextDecoration(TextDecoration::UNDERLINE); + hyperlinkNode->MarkModifyDone(); + hyperlinkNode->MarkDirtyNode(); +} +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h new file mode 100644 index 00000000000..2edfe3c8b47 --- /dev/null +++ b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_HYPERLINK_HYPERLINK_MODEL_NG_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_HYPERLINK_HYPERLINK_MODEL_NG_H + +#include "core/components_ng/base/frame_node.h" +#include "core/components_ng/pattern/hyperlink/hyperlink_model.h" + +namespace OHOS::Ace::NG { +class ACE_EXPORT HyperlinkModelNG : public HyperlinkModel { +public: + void Create(const std::string& address, const std::string& content) override; + void Pop() override {}; + void SetColor(const Color& value) override; + +private: + void SetTextStyle(const RefPtr& textNode, const std::string& content); +}; +} // namespace OHOS::Ace::NG + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_HYPERLINK_HYPERLINK_MODEL_NG_H diff --git a/frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.cpp b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.cpp new file mode 100644 index 00000000000..d3108058bc6 --- /dev/null +++ b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/components_ng/pattern/hyperlink/hyperlink_pattern.h" + +namespace OHOS::Ace::NG { +void HyperlinkPattern::OnAttachToFrameNode() {} + +void HyperlinkPattern::OnModifyDone() +{ + TextPattern::OnModifyDone(); + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto hub = host->GetEventHub(); + CHECK_NULL_VOID(hub); + + auto gestureHub = hub->GetOrCreateGestureEventHub(); + CHECK_NULL_VOID(gestureHub); + InitClickEvent(gestureHub); +} + +void HyperlinkPattern::LinkToAddress() +{ +#if defined(PREVIEW) + LOGW("Unable to jump in preview."); + return; +#else + auto pipeline = PipelineContext::GetCurrentContext(); + CHECK_NULL_VOID(pipeline); + pipeline->HyperlinkStartAbility(address_); +#endif +} + +void HyperlinkPattern::InitClickEvent(const RefPtr& gestureHub) +{ + auto clickCallback = [weak = WeakClaim(this)](GestureEvent& /* info */) { + auto hyperlinkPattern = weak.Upgrade(); + CHECK_NULL_VOID(hyperlinkPattern); + hyperlinkPattern->LinkToAddress(); + }; + gestureHub->SetUserOnClick(std::move(clickCallback)); +} + +void HyperlinkPattern::ToJsonValue(std::unique_ptr& json) const +{ + json->Put("address", address_.c_str()); +} +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.h b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.h new file mode 100644 index 00000000000..48aed687431 --- /dev/null +++ b/frameworks/core/components_ng/pattern/hyperlink/hyperlink_pattern.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_HYPERLINK_HYPERLINK_PATTERN_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_HYPERLINK_HYPERLINK_PATTERN_H + +#include "core/components_ng/pattern/hyperlink/hyperlink_layout_property.h" +#include "core/components_ng/pattern/text/text_pattern.h" + +namespace OHOS::Ace::NG { +class HyperlinkPattern : public TextPattern { + DECLARE_ACE_TYPE(HyperlinkPattern, TextPattern); + +public: + explicit HyperlinkPattern(const std::string& address) : address_(address) {}; + ~HyperlinkPattern() override = default; + + RefPtr CreateLayoutProperty() override + { + return MakeRefPtr(); + } + + const std::string& GetAddress() + { + return address_; + } + +private: + void LinkToAddress(); + void OnAttachToFrameNode() override; + void OnModifyDone() override; + + void InitClickEvent(const RefPtr& gestureHub); + void ToJsonValue(std::unique_ptr& json) const override; + + RefPtr onHoverEvent_; + + // Mark the address in the pattern, used to link to the website. + std::string address_; + ACE_DISALLOW_COPY_AND_MOVE(HyperlinkPattern); +}; +} // namespace OHOS::Ace::NG + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_HYPERLINK_HYPERLINK_PATTERN_H -- Gitee