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 #include "core/components_ng/pattern/toast/toast_view.h"
16
17 #include "base/geometry/dimension.h"
18 #include "base/memory/referenced.h"
19 #include "base/utils/utils.h"
20 #include "core/components/common/properties/shadow_config.h"
21 #include "core/components/toast/toast_theme.h"
22 #include "core/components_ng/pattern/text/text_layout_property.h"
23 #include "core/components_ng/pattern/text/text_pattern.h"
24 #include "core/components_ng/pattern/toast/toast_layout_property.h"
25 #include "core/components_ng/pattern/toast/toast_pattern.h"
26 #include "core/components_ng/property/property.h"
27 #include "core/components_v2/inspector/inspector_constants.h"
28 #include "core/pipeline/base/element_register.h"
29 #include "core/pipeline/pipeline_base.h"
30
31 namespace OHOS::Ace::NG {
CreateToastNode(const std::string & message,const std::string & bottom,bool isRightToLeft,const ToastShowMode & showMode)32 RefPtr<FrameNode> ToastView::CreateToastNode(
33 const std::string& message, const std::string& bottom, bool isRightToLeft, const ToastShowMode& showMode)
34 {
35 auto context = PipelineBase::GetCurrentContext();
36 CHECK_NULL_RETURN(context, nullptr);
37 auto toastTheme = context->GetTheme<ToastTheme>();
38 CHECK_NULL_RETURN(toastTheme, nullptr);
39
40 auto textId = ElementRegister::GetInstance()->MakeUniqueId();
41 auto toastId = ElementRegister::GetInstance()->MakeUniqueId();
42 // make toast node
43 ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::TOAST_ETS_TAG, toastId);
44 auto toastNode = FrameNode::CreateFrameNode(V2::TOAST_ETS_TAG, toastId, AceType::MakeRefPtr<ToastPattern>());
45 CHECK_NULL_RETURN(toastNode, nullptr);
46 auto toastProperty = toastNode->GetLayoutProperty<ToastLayoutProperty>();
47 CHECK_NULL_RETURN(toastProperty, nullptr);
48 auto toastContext = toastNode->GetRenderContext();
49 CHECK_NULL_RETURN(toastContext, nullptr);
50 auto toastAccessibilityProperty = toastNode->GetAccessibilityProperty<AccessibilityProperty>();
51 CHECK_NULL_RETURN(toastAccessibilityProperty, nullptr);
52 toastAccessibilityProperty->SetText(message);
53 // create text in toast
54 auto textNode = FrameNode::CreateFrameNode(V2::TEXT_ETS_TAG, textId, AceType::MakeRefPtr<TextPattern>());
55 CHECK_NULL_RETURN(textNode, nullptr);
56 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
57 CHECK_NULL_RETURN(textLayoutProperty, nullptr);
58 auto pattern = toastNode->GetPattern<ToastPattern>();
59 CHECK_NULL_RETURN(pattern, nullptr);
60 pattern->SetTextNode(textNode);
61 UpdateTextLayoutProperty(textNode, message, isRightToLeft);
62 UpdateTextContext(textNode);
63 textNode->MountToParent(toastNode);
64
65 toastProperty->UpdateBottom(StringUtils::StringToDimensionWithThemeValue(bottom, true, toastTheme->GetBottom()));
66 toastProperty->UpdateShowMode(showMode);
67 toastNode->GetEventHub<EventHub>()->GetOrCreateGestureEventHub()->SetHitTestMode(HitTestMode::HTMTRANSPARENT);
68 toastNode->MarkModifyDone();
69 return toastNode;
70 }
71
UpdateTextLayoutProperty(const RefPtr<FrameNode> & textNode,const std::string & message,bool isRightToLeft)72 void ToastView::UpdateTextLayoutProperty(
73 const RefPtr<FrameNode>& textNode, const std::string& message, bool isRightToLeft)
74 {
75 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
76 CHECK_NULL_VOID(textLayoutProperty);
77 auto context = PipelineBase::GetCurrentContext();
78 CHECK_NULL_VOID(context);
79 auto toastTheme = context->GetTheme<ToastTheme>();
80 CHECK_NULL_VOID(toastTheme);
81 auto fontWeight = toastTheme->GetTextStyle().GetFontWeight();
82 auto textColor = toastTheme->GetTextStyle().GetTextColor();
83 auto fontSize = toastTheme->GetTextStyle().GetFontSize();
84 auto padding = toastTheme->GetPadding();
85 PaddingProperty paddings;
86 paddings.top = NG::CalcLength(padding.Top());
87 paddings.bottom = NG::CalcLength(padding.Bottom());
88 paddings.left = NG::CalcLength(padding.Left());
89 paddings.right = NG::CalcLength(padding.Right());
90
91 textLayoutProperty->UpdateContent(message);
92 textLayoutProperty->UpdateTextColor(textColor);
93 textLayoutProperty->UpdateTextAlign(TextAlign::CENTER);
94 textLayoutProperty->UpdateFontWeight(fontWeight);
95 textLayoutProperty->UpdateFontSize(fontSize);
96 textLayoutProperty->UpdateLayoutDirection((isRightToLeft ? TextDirection::RTL : TextDirection::LTR));
97 textLayoutProperty->UpdatePadding(paddings);
98 }
UpdateTextContext(const RefPtr<FrameNode> & textNode)99 void ToastView::UpdateTextContext(const RefPtr<FrameNode>& textNode)
100 {
101 auto textContext = textNode->GetRenderContext();
102 CHECK_NULL_VOID(textContext);
103 auto pipelineContext = PipelineBase::GetCurrentContext();
104 CHECK_NULL_VOID(pipelineContext);
105 auto toastTheme = pipelineContext->GetTheme<ToastTheme>();
106 CHECK_NULL_VOID(toastTheme);
107 auto radius = toastTheme->GetRadius();
108 auto toastBackgroundColor = toastTheme->GetBackgroundColor();
109 BorderRadiusProperty borderRadius;
110 borderRadius.SetRadius(Dimension(radius.GetX().ConvertToPx()));
111 textContext->UpdateBackgroundColor(toastBackgroundColor);
112 textContext->UpdateBorderRadius(borderRadius);
113 textContext->UpdateBackShadow(ShadowConfig::DefaultShadowL);
114 }
115 } // namespace OHOS::Ace::NG
116