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 "frameworks/bridge/common/dom/dom_badge.h"
17 
18 namespace OHOS::Ace::Framework {
19 
DOMBadge(NodeId nodeId,const std::string & nodeName)20 DOMBadge::DOMBadge(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
21 {
22     badgeChild_ = AceType::MakeRefPtr<BadgeComponent>();
23 }
24 
ResetInitializedStyle()25 void DOMBadge::ResetInitializedStyle()
26 {
27     if (declaration_) {
28         declaration_->InitializeStyle();
29     }
30 }
31 
PrepareSpecializedComponent()32 void DOMBadge::PrepareSpecializedComponent()
33 {
34     auto declaration = AceType::DynamicCast<BadgeDeclaration>(declaration_);
35     badgeChild_->SetDeclaration(AceType::DynamicCast<BadgeDeclaration>(declaration));
36     if (!boxComponent_ || !declaration) {
37         return;
38     }
39     badgeChild_->SetTextDirection(declaration->IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
40     boxComponent_->SetAlignment(Alignment::TOP_LEFT);
41     auto edge = boxComponent_->GetPadding();
42     if (edge == Edge::NONE) {
43         return;
44     }
45     declaration->SetPadding(edge);
46     boxComponent_->SetPadding(Edge());
47 }
48 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)49 void DOMBadge::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
50 {
51     ACE_DCHECK(child);
52     if (badgeChild_->GetChild()) {
53         return;
54     }
55     badgeChild_->SetChild(child->GetRootComponent());
56 }
57 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)58 void DOMBadge::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
59 {
60     badgeChild_->SetChild(nullptr);
61 }
62 
SetBadgeConfig(const BadgeConfig & badgeConfig)63 void DOMBadge::SetBadgeConfig(const BadgeConfig& badgeConfig)
64 {
65     auto declaration = AceType::DynamicCast<BadgeDeclaration>(declaration_);
66     if (!declaration) {
67         return;
68     }
69 
70     if (badgeConfig.badgeColor.second) {
71         declaration->SetBadgeColor(badgeConfig.badgeColor.first);
72     }
73     if (badgeConfig.badgeSize.second) {
74         declaration->SetBadgeCircleSize(badgeConfig.badgeSize.first);
75     }
76     if (badgeConfig.textColor.second) {
77         declaration->SetBadgeTextColor(badgeConfig.textColor.first);
78     }
79     if (badgeConfig.textSize.second) {
80         declaration->SetBadgeFontSize(badgeConfig.textSize.first);
81     }
82 }
83 
84 } // namespace OHOS::Ace::Framework
85