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