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_divider.h"
17 
18 #include "core/components/divider/divider_theme.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20 
21 namespace OHOS::Ace::Framework {
22 
DOMDivider(NodeId nodeId,const std::string & nodeName)23 DOMDivider::DOMDivider(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
24 {
25     dividerChild_ = AceType::MakeRefPtr<DividerComponent>();
26 }
27 
InitializeStyle()28 void DOMDivider::InitializeStyle()
29 {
30     auto theme = GetTheme<DividerTheme>();
31     if (theme) {
32         dividerChild_->SetDividerColor(theme->GetColor());
33     }
34 }
35 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)36 bool DOMDivider::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
37 {
38     static const LinearMapNode<void (*)(const std::string&, DOMDivider&)> driverStyleOperators[] = {
39         { DOM_COLOR, [](const std::string& val,
40                      DOMDivider& divider) { divider.dividerChild_->SetDividerColor(divider.ParseColor(val)); } },
41         { DOM_DIVIDER_LINE_CAP,
42             [](const std::string& val, DOMDivider& divider) {
43                 if (val == "butt") {
44                     divider.dividerChild_->SetLineCap(LineCap::BUTT);
45                 } else if (val == "round") {
46                     divider.dividerChild_->SetLineCap(LineCap::ROUND);
47                 } else if (val == "square") {
48                     divider.dividerChild_->SetLineCap(LineCap::SQUARE);
49                 } else {
50                     LOGW("not support value: %{public}s", val.c_str());
51                 }
52             } },
53         { DOM_DIVIDER_STROKE_WIDTH,
54             [](const std::string& val, DOMDivider& divider) {
55                 divider.dividerChild_->SetStrokeWidth(divider.ParseDimension(val));
56             } },
57     };
58     auto operatorIter =
59         BinarySearchFindIndex(driverStyleOperators, ArraySize(driverStyleOperators), style.first.c_str());
60     if (operatorIter != -1) {
61         driverStyleOperators[operatorIter].value(style.second, *this);
62         return true;
63     }
64     return false;
65 }
66 
PrepareSpecializedComponent()67 void DOMDivider::PrepareSpecializedComponent()
68 {
69     if (dividerChild_->IsVertical()) {
70         boxComponent_->SetFlex(BoxFlex::FLEX_Y);
71     } else {
72         boxComponent_->SetFlex(BoxFlex::FLEX_X);
73     }
74 }
75 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)76 bool DOMDivider::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
77 {
78     if (attr.first == DOM_DIVIDER_VERTICAL) {
79         dividerChild_->SetVertical(StringToBool(attr.second));
80         return true;
81     }
82     return false;
83 }
84 
ResetInitializedStyle()85 void DOMDivider::ResetInitializedStyle()
86 {
87     InitializeStyle();
88 }
89 
90 } // namespace OHOS::Ace::Framework