• 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_slider.h"
17 
18 #include "base/utils/linear_map.h"
19 #include "base/utils/utils.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/slider/slider_theme.h"
22 #include "core/components/theme/theme_manager.h"
23 #include "frameworks/bridge/common/dom/dom_type.h"
24 #include "frameworks/bridge/common/utils/utils.h"
25 
26 namespace OHOS::Ace::Framework {
27 
DOMSlider(NodeId nodeId,const std::string & nodeName)28 DOMSlider::DOMSlider(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
29 {
30     // init component and default theme
31     sliderChild_ = AceType::MakeRefPtr<SliderComponent>(val_, step_, min_, max_);
32     watchSliderChild_ = AceType::MakeRefPtr<WatchSliderComponent>();
33     trackChild_ = sliderChild_->GetTrack();
34     blockChild_ = sliderChild_->GetBlock();
35     paddingChild_ = AceType::MakeRefPtr<PaddingComponent>();
36     if (SystemProperties::GetDeviceType() == DeviceType::WATCH) {
37         paddingChild_->SetChild(watchSliderChild_);
38     } else {
39         paddingChild_->SetChild(sliderChild_);
40     }
41 }
42 
InitializeStyle()43 void DOMSlider::InitializeStyle()
44 {
45     RefPtr<SliderTheme> theme = GetTheme<SliderTheme>();
46     if (!theme) {
47         return;
48     }
49     blockColor_ = theme->GetBlockColor();
50     color_ = theme->GetTrackBgColor();
51     selectColor_ = theme->GetTrackSelectedColor();
52     sliderChild_->InitStyle(theme);
53 }
54 
CallSpecializedMethod(const std::string & method,const std::string & args)55 void DOMSlider::CallSpecializedMethod(const std::string& method, const std::string& args)
56 {
57     if (method == DOM_ROTATION) {
58         auto controller = (SystemProperties::GetDeviceType() == DeviceType::WATCH)
59                               ? watchSliderChild_->GetRotationController()
60                               : sliderChild_->GetRotationController();
61         if (controller) {
62             controller->RequestRotation(true);
63         }
64     }
65 }
66 
ResetInitializedStyle()67 void DOMSlider::ResetInitializedStyle()
68 {
69     sliderChild_->InitStyle(GetTheme<SliderTheme>());
70 }
71 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)72 bool DOMSlider::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
73 {
74     // static linear map must be sorted by key.
75     static const LinearMapNode<void (*)(const std::string&, DOMSlider&)> sliderAttrsOperators[] = {
76         { DOM_MAX, [](const std::string& val, DOMSlider& slider) { slider.max_ = StringToDouble(val); } },
77         { DOM_MAX_ICON, [](const std::string& val, DOMSlider& slider) { slider.maxIconUrl_ = val; } },
78         { DOM_MIN, [](const std::string& val, DOMSlider& slider) { slider.min_ = StringToDouble(val); } },
79         { DOM_MIN_ICON, [](const std::string& val, DOMSlider& slider) { slider.minIconUrl_ = val; } },
80         { DOM_SLIDER_MODE, [](const std::string& val, DOMSlider& slider) {
81                 if (val == DOM_INSET) {
82                     slider.mode_ = SliderMode::INSET;
83                 } else {
84                     slider.mode_ = SliderMode::OUTSET;
85                 }
86             } },
87         { DOM_SHOW_STEPS, [](const std::string& val, DOMSlider& slider) { slider.showSteps_ = StringToBool(val); } },
88         { DOM_SHOW_TIPS, [](const std::string& val, DOMSlider& slider) { slider.showTips_ = StringToBool(val); } },
89         { DOM_STEP,
90             [](const std::string& val, DOMSlider& slider) {
91                 slider.step_ = StringToDouble(val) > 0 ? StringToDouble(val) : slider.step_;
92             } },
93         { DOM_TYPE,
94             [](const std::string& val, DOMSlider& slider) {
95                 if (val == DOM_CONTINUOUS) {
96                     slider.isContinuous_ = true;
97                 } else if (val == DOM_INTERMITTENT) {
98                     slider.isContinuous_ = false;
99                 } else {
100                     slider.isContinuous_ = true;
101                 }
102             } },
103         { DOM_VALUE, [](const std::string& val, DOMSlider& slider) {
104                 slider.val_ = StringToDouble(val);
105                 slider.sliderChild_->SetCurrentValue(StringToDouble(val));
106             } },
107     };
108     auto operatorIter =
109         BinarySearchFindIndex(sliderAttrsOperators, ArraySize(sliderAttrsOperators), attr.first.c_str());
110     if (operatorIter != -1) {
111         sliderAttrsOperators[operatorIter].value(attr.second, *this);
112         return true;
113     } else {
114         return false;
115     }
116 }
117 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)118 bool DOMSlider::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
119 {
120     static const LinearMapNode<void (*)(const std::string&, DOMSlider&)> sliderStylesOperators[] = {
121         { DOM_BLOCK_COLOR,
122             [](const std::string& val, DOMSlider& slider) { slider.blockColor_ = slider.ParseColor(val); } },
123         { DOM_COLOR, [](const std::string& val, DOMSlider& slider) { slider.color_ = slider.ParseColor(val); } },
124         { DOM_PADDING_LEFT,
125             [](const std::string& val, DOMSlider& slider) { slider.paddingLeft_ = slider.ParseDimension(val); } },
126         { DOM_PADDING_RIGHT,
127             [](const std::string& val, DOMSlider& slider) { slider.paddingRight_ = slider.ParseDimension(val); } },
128         { DOM_SELECTED_COLOR,
129             [](const std::string& val, DOMSlider& slider) { slider.selectColor_ = slider.ParseColor(val); } },
130     };
131     auto operatorIter =
132         BinarySearchFindIndex(sliderStylesOperators, ArraySize(sliderStylesOperators), style.first.c_str());
133     if (operatorIter != -1) {
134         sliderStylesOperators[operatorIter].value(style.second, *this);
135         return true;
136     }
137     return false;
138 }
139 
AddSpecializedEvent(int32_t pageId,const std::string & event)140 bool DOMSlider::AddSpecializedEvent(int32_t pageId, const std::string& event)
141 {
142     if (event == DOM_CHANGE) {
143         onMoveEndEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
144         sliderChild_->SetOnMovingEventId(onMoveEndEventId_);
145         watchSliderChild_->SetOnMoveEndEventId(onMoveEndEventId_);
146         return true;
147     }
148     return false;
149 }
150 
PrepareSpecializedComponent()151 void DOMSlider::PrepareSpecializedComponent()
152 {
153     auto min = min_;
154     min_ = (max_ < min) ? max_ : min;
155     max_ = (max_ < min) ? min : max_;
156     sliderChild_->SetMaxValue(max_);
157     sliderChild_->SetMinValue(min_);
158     if (declaration_) {
159         sliderChild_->SetDisable(declaration_->IsDisabled());
160     }
161     if (val_ < min_ || val_ > max_) {
162         val_ = (val_ < min_) ? min_ : max_;
163     }
164     if (LessOrEqual(step_, 0.0) || step_ > max_ - min_) {
165         step_ = max_ - min_;
166     }
167     watchSliderChild_->SetMinValue(min_);
168     watchSliderChild_->SetMaxValue(max_);
169     watchSliderChild_->SetValue(val_);
170     watchSliderChild_->SetMaxIconUrl(maxIconUrl_);
171     watchSliderChild_->SetMinIconUrl(minIconUrl_);
172     watchSliderChild_->SetBackgroundColor(color_);
173     watchSliderChild_->SetSelectColor(selectColor_);
174     watchSliderChild_->SetContinuous(isContinuous_);
175 
176     sliderChild_->SetShowSteps(showSteps_);
177     sliderChild_->SetShowTips(showTips_);
178     sliderChild_->SetSliderMode(mode_);
179     sliderChild_->SetStepValue(step_);
180     sliderChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
181     paddingChild_->SetPadding(Edge(paddingLeft_, 0.0_vp, paddingRight_, 0.0_vp));
182     trackChild_->SetBackgroundColor(color_);
183     trackChild_->SetSelectColor(selectColor_);
184     blockChild_->SetBlockColor(blockColor_);
185     sliderChild_->InitStyle(GetTheme<SliderTheme>());
186 }
187 
188 } // namespace OHOS::Ace::Framework
189