• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/components_ng/pattern/button/button_view.h"
16 
17 #include "base/geometry/dimension.h"
18 #include "base/memory/ace_type.h"
19 #include "core/components/button/button_theme.h"
20 #include "core/components_ng/base/frame_node.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/components_ng/pattern/button/button_accessibility_property.h"
23 #include "core/components_ng/pattern/button/button_event_hub.h"
24 #include "core/components_ng/pattern/button/button_layout_property.h"
25 #include "core/components_ng/pattern/button/button_pattern.h"
26 #include "core/components_ng/pattern/text/text_pattern.h"
27 #include "core/components_v2/inspector/inspector_constants.h"
28 #include "core/pipeline_ng/ui_task_scheduler.h"
29 
30 namespace OHOS::Ace::NG {
CreateWithLabel(const std::string & label)31 void ButtonView::CreateWithLabel(const std::string& label)
32 {
33     auto* stack = ViewStackProcessor::GetInstance();
34     auto nodeId = stack->ClaimNodeId();
35     auto buttonNode = FrameNode::GetOrCreateFrameNode(
36         V2::BUTTON_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
37     CHECK_NULL_VOID(buttonNode);
38     if (buttonNode->GetChildren().empty()) {
39         auto textNode = FrameNode::CreateFrameNode(
40             V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
41         CHECK_NULL_VOID(textNode);
42         textNode->SetInternal();
43         SetTextDefaultStyle(textNode, label);
44         buttonNode->AddChild(textNode);
45     }
46     auto buttonAccessibilityProperty = buttonNode->GetAccessibilityProperty<ButtonAccessibilityProperty>();
47     CHECK_NULL_VOID(buttonAccessibilityProperty);
48     buttonAccessibilityProperty->SetText(label);
49     stack->Push(buttonNode);
50     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, Label, label);
51     auto buttonTheme = PipelineBase::GetCurrentContext()->GetTheme<ButtonTheme>();
52     CHECK_NULL_VOID(buttonTheme);
53     auto padding = buttonTheme->GetPadding();
54     PaddingProperty defaultPadding({ CalcLength(padding.Left()), CalcLength(padding.Right()), CalcLength(padding.Top()),
55         CalcLength(padding.Bottom()) });
56     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, Padding, defaultPadding);
57 }
58 
Create(const std::string & tagName)59 void ButtonView::Create(const std::string& tagName)
60 {
61     auto* stack = ViewStackProcessor::GetInstance();
62     auto nodeId = stack->ClaimNodeId();
63     auto frameNode =
64         FrameNode::GetOrCreateFrameNode(tagName, nodeId, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
65     stack->Push(frameNode);
66 }
67 
SetTextDefaultStyle(const RefPtr<FrameNode> & textNode,const std::string & label)68 void ButtonView::SetTextDefaultStyle(const RefPtr<FrameNode>& textNode, const std::string& label)
69 {
70     CHECK_NULL_VOID(textNode);
71     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
72     CHECK_NULL_VOID(textLayoutProperty);
73     auto buttonTheme = PipelineBase::GetCurrentContext()->GetTheme<ButtonTheme>();
74     auto textStyle = buttonTheme->GetTextStyle();
75     textLayoutProperty->UpdateContent(label);
76     textLayoutProperty->UpdateTextOverflow(TextOverflow::ELLIPSIS);
77     textLayoutProperty->UpdateMaxLines(buttonTheme->GetTextMaxLines());
78     textLayoutProperty->UpdateFontSize(textStyle.GetFontSize());
79     textLayoutProperty->UpdateTextColor(textStyle.GetTextColor());
80     textLayoutProperty->UpdateFontWeight(textStyle.GetFontWeight());
81 }
82 
SetType(ButtonType buttonType)83 void ButtonView::SetType(ButtonType buttonType)
84 {
85     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, Type, buttonType);
86 }
87 
SetStateEffect(bool stateEffect)88 void ButtonView::SetStateEffect(bool stateEffect)
89 {
90     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
91     CHECK_NULL_VOID(frameNode);
92     auto buttonEventHub = frameNode->GetEventHub<ButtonEventHub>();
93     CHECK_NULL_VOID(buttonEventHub);
94     buttonEventHub->SetStateEffect(stateEffect);
95 }
96 
SetFontSize(const Dimension & fontSize)97 void ButtonView::SetFontSize(const Dimension& fontSize)
98 {
99     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, FontSize, fontSize);
100 }
101 
SetFontWeight(Ace::FontWeight fontWeight)102 void ButtonView::SetFontWeight(Ace::FontWeight fontWeight)
103 {
104     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, FontWeight, fontWeight);
105 }
106 
SetFontStyle(Ace::FontStyle fontStyle)107 void ButtonView::SetFontStyle(Ace::FontStyle fontStyle)
108 {
109     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, FontStyle, fontStyle);
110 }
111 
SetFontFamily(const std::vector<std::string> & fontFamilies)112 void ButtonView::SetFontFamily(const std::vector<std::string>& fontFamilies)
113 {
114     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, FontFamily, fontFamilies);
115 }
116 
SetFontColor(const Color & textColor)117 void ButtonView::SetFontColor(const Color& textColor)
118 {
119     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, FontColor, textColor);
120 }
121 
SetBorderRadius(const Dimension & radius)122 void ButtonView::SetBorderRadius(const Dimension& radius)
123 {
124     ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, BorderRadius, radius);
125 }
126 
127 } // namespace OHOS::Ace::NG
128