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_tool_bar.h"
17
18 #include "frameworks/bridge/common/utils/utils.h"
19
20 namespace OHOS::Ace::Framework {
21
DOMToolBar(NodeId nodeId,const std::string & nodeName)22 DOMToolBar::DOMToolBar(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
23 {
24 toolBarChild_ = AceType::MakeRefPtr<ToolBarComponent>(std::list<RefPtr<Component>>());
25 }
26
InitializeStyle()27 void DOMToolBar::InitializeStyle()
28 {
29 RefPtr<ToolBarTheme> theme = GetTheme<ToolBarTheme>();
30 if (!theme || !declaration_) {
31 LOGE("ToolBarTheme or declaration is null");
32 return;
33 }
34 declaration_->GetBackDecoration()->SetBackgroundColor(theme->GetToolBarBgColor());
35 declaration_->SetHasDecorationStyle(true);
36 }
37
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)38 void DOMToolBar::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
39 {
40 AddChildNode(child, slot, false);
41 }
42
OnChildNodeRemoved(const RefPtr<DOMNode> & child)43 void DOMToolBar::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
44 {
45 Rebuild();
46 }
47
Rebuild()48 void DOMToolBar::Rebuild()
49 {
50 toolBarChild_->ClearChildren();
51 menuChildren_.clear();
52
53 if (GetChildList().empty()) {
54 return;
55 }
56
57 for (const auto& pos : GetChildList()) {
58 AddChildNode(pos, TOOL_BAR_INVALID_INDEX, true);
59 }
60 }
61
AddChildNode(const RefPtr<DOMNode> & child,int32_t slot,bool isRebuild)62 void DOMToolBar::AddChildNode(const RefPtr<DOMNode>& child, int32_t slot, bool isRebuild)
63 {
64 if (!child) {
65 LOGE("Child is nullptr, add node failed, slot:%{public}d.", slot);
66 return;
67 }
68
69 RefPtr<DOMToolBarItem> domToolBarItem = AceType::DynamicCast<DOMToolBarItem>(child);
70 if (!domToolBarItem || !domToolBarItem->GetSpecializedComponent()) {
71 return;
72 }
73 if (isRebuild) {
74 domToolBarItem->SetIsEndItem(false);
75 domToolBarItem->MarkNeedUpdate();
76 }
77
78 if (toolBarChild_->GetChildren().size() > TOOL_BAR_DEFAULT_SIZE) {
79 menuChildren_.emplace_back(domToolBarItem->BuildOptionComponent());
80 return;
81 }
82
83 domToolBarItem->SetPosition(GetPosition());
84 if (toolBarChild_->GetChildren().size() == TOOL_BAR_DEFAULT_SIZE) {
85 if (preToolBarItem_) {
86 RefPtr<DOMToolBarItem> preDomToolBarItem = AceType::DynamicCast<DOMToolBarItem>(preToolBarItem_);
87 if (preDomToolBarItem) {
88 RefPtr<ToolBarItemComponent> toolBarItemComponent =
89 AceType::DynamicCast<ToolBarItemComponent>(preDomToolBarItem->GetSpecializedComponent());
90 if (toolBarItemComponent) {
91 preDomToolBarItem->SetIsEndItem(true);
92 toolBarItemComponent->SetOptionChildrenCallBack([this]() { return GetMenuChildren(); });
93 toolBarItemComponent->SetClickedEventId(EventMarker());
94 menuChildren_.emplace_back(preDomToolBarItem->BuildOptionComponent());
95 }
96 }
97 }
98 menuChildren_.emplace_back(domToolBarItem->BuildOptionComponent());
99 preToolBarItem_ = nullptr;
100 return;
101 }
102
103 if (slot == TOOL_BAR_INVALID_INDEX) {
104 toolBarChild_->AppendChild(child->GetRootComponent());
105 } else {
106 toolBarChild_->InsertChild(slot, child->GetRootComponent());
107 }
108 preToolBarItem_ = child;
109 }
110
PrepareSpecializedComponent()111 void DOMToolBar::PrepareSpecializedComponent()
112 {
113 if (toolBarChild_) {
114 toolBarChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
115 }
116 }
117
118 } // namespace
119