1 /*
2 * Copyright (c) 2021 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 "core/components/declaration/piece/piece_declaration.h"
17
18 #include "core/components/declaration/common/declaration_constants.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20
21 namespace OHOS::Ace {
22
23 using namespace Framework;
24
InitSpecialized()25 void PieceDeclaration::InitSpecialized()
26 {
27 AddCommonStyle(StyleTag::COMMON_IMAGE_STYLE);
28 AddSpecializedAttribute(DeclarationConstants::DEFAULT_PIECE_ATTR);
29 AddSpecializedStyle(DeclarationConstants::DEFAULT_PIECE_STYLE);
30 AddSpecializedEvent(DeclarationConstants::DEFAULT_PIECE_EVENT);
31 }
32
InitializeStyle()33 void PieceDeclaration::InitializeStyle()
34 {
35 auto theme = GetTheme<PieceTheme>();
36 if (!theme) {
37 return;
38 }
39
40 SetHasBoxStyle(true);
41 SetHasDecorationStyle(true);
42 auto& sizeStyle = MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
43 if (sizeStyle.IsValid()) {
44 sizeStyle.height = theme->GetHeight();
45 }
46
47 auto& paddingStyle = MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
48 if (paddingStyle.IsValid()) {
49 paddingStyle.padding.SetLeft(theme->GetPaddingHorizontal());
50 paddingStyle.padding.SetRight(theme->GetPaddingHorizontal());
51 paddingStyle.padding.SetTop(theme->GetPaddingVertical());
52 paddingStyle.padding.SetBottom(theme->GetPaddingVertical());
53 }
54
55 auto& borderStyle = MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
56 if (borderStyle.IsValid()) {
57 borderStyle.border.SetBorderRadius(Radius(theme->GetHeight() / 2.0));
58 }
59 GetBackDecoration()->SetBackgroundColor(theme->GetBackgroundColor());
60 SetTextStyle(theme->GetTextStyle());
61 SetInterval(theme->GetInterval());
62 SetIconResource(theme->GetIconResource());
63 SetIconSize(theme->GetIconSize());
64 SetHoverColor(theme->GetHoverColor());
65 }
66
InitializeStyle(RefPtr<PieceTheme> & theme)67 void PieceDeclaration::InitializeStyle(RefPtr<PieceTheme>& theme)
68 {
69 if (!theme) {
70 return;
71 }
72 SetHasBoxStyle(true);
73 SetHasDecorationStyle(true);
74 auto& sizeStyle = MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
75 if (sizeStyle.IsValid()) {
76 sizeStyle.height = theme->GetHeight();
77 }
78
79 auto& paddingStyle = MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
80 if (paddingStyle.IsValid()) {
81 paddingStyle.padding.SetLeft(theme->GetPaddingHorizontal());
82 paddingStyle.padding.SetRight(theme->GetPaddingHorizontal());
83 paddingStyle.padding.SetTop(theme->GetPaddingVertical());
84 paddingStyle.padding.SetBottom(theme->GetPaddingVertical());
85 }
86
87 auto& borderStyle = MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
88 if (borderStyle.IsValid()) {
89 borderStyle.border.SetBorderRadius(Radius(theme->GetHeight() / 2.0));
90 }
91
92 SetTextStyle(theme->GetTextStyle());
93 SetInterval(theme->GetInterval());
94 SetIconResource(theme->GetIconResource());
95 SetIconSize(theme->GetIconSize());
96 SetHoverColor(theme->GetHoverColor());
97 SetBackGroundColor(theme->GetBackgroundColor());
98 }
99
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)100 bool PieceDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
101 {
102 if (attr.first == DOM_PIECE_CONTENT) {
103 hasContent_ = !attr.second.empty();
104 SetContent(attr.second);
105 return true;
106 } else if (attr.first == DOM_PIECE_ICON) {
107 SetIcon(attr.second);
108 return true;
109 } else if (attr.first == DOM_PIECE_CLOSABLE) {
110 SetShowDelete(StringToBool(attr.second));
111 return true;
112 }
113 return false;
114 }
115
SetSpecializedStyle(const std::pair<std::string,std::string> & style)116 bool PieceDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
117 {
118 if (style.first == DOM_BACKGROUND || style.first == DOM_BACKGROUND_IMAGE) {
119 hasBackground_ = true;
120 }
121 return false;
122 }
123
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)124 bool PieceDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
125 {
126 if (event == DOM_PIECE_EVENT_CLOSE) {
127 SetOnDelete(EventMarker(eventId, event, pageId));
128 return true;
129 }
130 return false;
131 }
132
133 } // namespace OHOS::Ace
134