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