• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)32 RefPtr<FrameNode> ToastView::CreateToastNode(const std::string& message, const std::string& bottom, bool isRightToLeft)
33 {
34     auto context = PipelineBase::GetCurrentContext();
35     CHECK_NULL_RETURN(context, nullptr);
36     auto toastTheme = context->GetTheme<ToastTheme>();
37     CHECK_NULL_RETURN(toastTheme, nullptr);
38 
39     auto textId = ElementRegister::GetInstance()->MakeUniqueId();
40     auto toastId = ElementRegister::GetInstance()->MakeUniqueId();
41     // make toast node
42     auto toastNode = FrameNode::CreateFrameNode(V2::TOAST_ETS_TAG, toastId, AceType::MakeRefPtr<ToastPattern>());
43     CHECK_NULL_RETURN(toastNode, nullptr);
44     auto toastProperty = toastNode->GetLayoutProperty<ToastLayoutProperty>();
45     CHECK_NULL_RETURN(toastProperty, nullptr);
46     auto toastContext = toastNode->GetRenderContext();
47     CHECK_NULL_RETURN(toastContext, nullptr);
48     auto toastAccessibilityProperty = toastNode->GetAccessibilityProperty<AccessibilityProperty>();
49     CHECK_NULL_RETURN(toastAccessibilityProperty, nullptr);
50     toastAccessibilityProperty->SetText(message);
51     // create text in toast
52     auto textNode = FrameNode::CreateFrameNode(V2::TEXT_ETS_TAG, textId, AceType::MakeRefPtr<TextPattern>());
53     CHECK_NULL_RETURN(textNode, nullptr);
54     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
55     CHECK_NULL_RETURN(textLayoutProperty, nullptr);
56     auto pattern = toastNode->GetPattern<ToastPattern>();
57     CHECK_NULL_RETURN(pattern, nullptr);
58     pattern->SetTextNode(textNode);
59     UpdateTextLayoutProperty(textNode, message, isRightToLeft);
60     UpdateTextContext(textNode);
61     textNode->MountToParent(toastNode);
62 
63     toastProperty->UpdateBottom(StringUtils::StringToDimensionWithThemeValue(bottom, true, toastTheme->GetBottom()));
64     toastNode->GetEventHub<EventHub>()->GetOrCreateGestureEventHub()->SetHitTestMode(HitTestMode::HTMTRANSPARENT);
65     toastNode->MarkModifyDone();
66     return toastNode;
67 }
68 
UpdateTextLayoutProperty(const RefPtr<FrameNode> & textNode,const std::string & message,bool isRightToLeft)69 void ToastView::UpdateTextLayoutProperty(
70     const RefPtr<FrameNode>& textNode, const std::string& message, bool isRightToLeft)
71 {
72     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
73     CHECK_NULL_VOID(textLayoutProperty);
74     auto context = PipelineBase::GetCurrentContext();
75     CHECK_NULL_VOID(context);
76     auto toastTheme = context->GetTheme<ToastTheme>();
77     CHECK_NULL_VOID(toastTheme);
78     auto fontWeight = toastTheme->GetTextStyle().GetFontWeight();
79     auto textColor = toastTheme->GetTextStyle().GetTextColor();
80     auto fontSize = toastTheme->GetTextStyle().GetFontSize();
81     auto padding = toastTheme->GetPadding();
82     PaddingProperty paddings;
83     paddings.top = NG::CalcLength(padding.Top());
84     paddings.bottom = NG::CalcLength(padding.Bottom());
85     paddings.left = NG::CalcLength(padding.Left());
86     paddings.right = NG::CalcLength(padding.Right());
87 
88     textLayoutProperty->UpdateContent(message);
89     textLayoutProperty->UpdateTextColor(textColor);
90     textLayoutProperty->UpdateTextAlign(TextAlign::CENTER);
91     textLayoutProperty->UpdateFontWeight(fontWeight);
92     textLayoutProperty->UpdateFontSize(fontSize);
93     textLayoutProperty->UpdateLayoutDirection((isRightToLeft ? TextDirection::RTL : TextDirection::LTR));
94     textLayoutProperty->UpdatePadding(paddings);
95 }
UpdateTextContext(const RefPtr<FrameNode> & textNode)96 void ToastView::UpdateTextContext(const RefPtr<FrameNode>& textNode)
97 {
98     auto textContext = textNode->GetRenderContext();
99     CHECK_NULL_VOID(textContext);
100     auto pipelineContext = PipelineBase::GetCurrentContext();
101     CHECK_NULL_VOID(pipelineContext);
102     auto toastTheme = pipelineContext->GetTheme<ToastTheme>();
103     CHECK_NULL_VOID(toastTheme);
104     auto radius = toastTheme->GetRadius();
105     auto toastBackgroundColor = toastTheme->GetBackgroundColor();
106     BorderRadiusProperty borderRadius;
107     borderRadius.SetRadius(Dimension(radius.GetX().ConvertToPx()));
108     textContext->UpdateBackgroundColor(toastBackgroundColor);
109     textContext->UpdateBorderRadius(borderRadius);
110     textContext->UpdateBackShadow(ShadowConfig::DefaultShadowL);
111 }
112 } // namespace OHOS::Ace::NG
113