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