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