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