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