• 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         LOGD("Rotation focus slider");
59         auto controller = (SystemProperties::GetDeviceType() == DeviceType::WATCH)
60                               ? watchSliderChild_->GetRotationController()
61                               : sliderChild_->GetRotationController();
62         if (controller) {
63             LOGD("Rotation focus request");
64             controller->RequestRotation(true);
65         }
66     }
67 }
68 
ResetInitializedStyle()69 void DOMSlider::ResetInitializedStyle()
70 {
71     sliderChild_->InitStyle(GetTheme<SliderTheme>());
72 }
73 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)74 bool DOMSlider::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
75 {
76     // static linear map must be sorted by key.
77     static const LinearMapNode<void (*)(const std::string&, DOMSlider&)> sliderAttrsOperators[] = {
78         { DOM_MAX, [](const std::string& val, DOMSlider& slider) { slider.max_ = StringToDouble(val); } },
79         { DOM_MAX_ICON, [](const std::string& val, DOMSlider& slider) { slider.maxIconUrl_ = val; } },
80         { DOM_MIN, [](const std::string& val, DOMSlider& slider) { slider.min_ = StringToDouble(val); } },
81         { DOM_MIN_ICON, [](const std::string& val, DOMSlider& slider) { slider.minIconUrl_ = val; } },
82         { DOM_SLIDER_MODE, [](const std::string& val, DOMSlider& slider) {
83                 if (val == DOM_INSET) {
84                     slider.mode_ = SliderMode::INSET;
85                 } else {
86                     slider.mode_ = SliderMode::OUTSET;
87                 }
88             } },
89         { DOM_SHOW_STEPS, [](const std::string& val, DOMSlider& slider) { slider.showSteps_ = StringToBool(val); } },
90         { DOM_SHOW_TIPS, [](const std::string& val, DOMSlider& slider) { slider.showTips_ = StringToBool(val); } },
91         { DOM_STEP,
92             [](const std::string& val, DOMSlider& slider) {
93                 slider.step_ = StringToDouble(val) > 0 ? StringToDouble(val) : slider.step_;
94             } },
95         { DOM_TYPE,
96             [](const std::string& val, DOMSlider& slider) {
97                 if (val == DOM_CONTINUOUS) {
98                     slider.isContinuous_ = true;
99                 } else if (val == DOM_INTERMITTENT) {
100                     slider.isContinuous_ = false;
101                 } else {
102                     slider.isContinuous_ = true;
103                 }
104             } },
105         { DOM_VALUE, [](const std::string& val, DOMSlider& slider) {
106                 slider.val_ = StringToDouble(val);
107                 slider.sliderChild_->SetCurrentValue(StringToDouble(val));
108             } },
109     };
110     auto operatorIter =
111         BinarySearchFindIndex(sliderAttrsOperators, ArraySize(sliderAttrsOperators), attr.first.c_str());
112     if (operatorIter != -1) {
113         sliderAttrsOperators[operatorIter].value(attr.second, *this);
114         return true;
115     } else {
116         return false;
117     }
118 }
119 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)120 bool DOMSlider::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
121 {
122     static const LinearMapNode<void (*)(const std::string&, DOMSlider&)> sliderStylesOperators[] = {
123         { DOM_BLOCK_COLOR,
124             [](const std::string& val, DOMSlider& slider) { slider.blockColor_ = slider.ParseColor(val); } },
125         { DOM_COLOR, [](const std::string& val, DOMSlider& slider) { slider.color_ = slider.ParseColor(val); } },
126         { DOM_PADDING_LEFT,
127             [](const std::string& val, DOMSlider& slider) { slider.paddingLeft_ = slider.ParseDimension(val); } },
128         { DOM_PADDING_RIGHT,
129             [](const std::string& val, DOMSlider& slider) { slider.paddingRight_ = slider.ParseDimension(val); } },
130         { DOM_SELECTED_COLOR,
131             [](const std::string& val, DOMSlider& slider) { slider.selectColor_ = slider.ParseColor(val); } },
132     };
133     auto operatorIter =
134         BinarySearchFindIndex(sliderStylesOperators, ArraySize(sliderStylesOperators), style.first.c_str());
135     if (operatorIter != -1) {
136         sliderStylesOperators[operatorIter].value(style.second, *this);
137         return true;
138     }
139     return false;
140 }
141 
AddSpecializedEvent(int32_t pageId,const std::string & event)142 bool DOMSlider::AddSpecializedEvent(int32_t pageId, const std::string& event)
143 {
144     LOGD("DOMSlider AddEvent");
145     if (event == DOM_CHANGE) {
146         onMoveEndEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
147         sliderChild_->SetOnMovingEventId(onMoveEndEventId_);
148         watchSliderChild_->SetOnMoveEndEventId(onMoveEndEventId_);
149         return true;
150     }
151     return false;
152 }
153 
PrepareSpecializedComponent()154 void DOMSlider::PrepareSpecializedComponent()
155 {
156     auto min = min_;
157     min_ = (max_ < min) ? max_ : min;
158     max_ = (max_ < min) ? min : max_;
159     sliderChild_->SetMaxValue(max_);
160     sliderChild_->SetMinValue(min_);
161     if (declaration_) {
162         sliderChild_->SetDisable(declaration_->IsDisabled());
163     }
164     if (val_ < min_ || val_ > max_) {
165         val_ = (val_ < min_) ? min_ : max_;
166     }
167     if (LessOrEqual(step_, 0.0) || step_ > max_ - min_) {
168         step_ = max_ - min_;
169     }
170     watchSliderChild_->SetMinValue(min_);
171     watchSliderChild_->SetMaxValue(max_);
172     watchSliderChild_->SetValue(val_);
173     watchSliderChild_->SetMaxIconUrl(maxIconUrl_);
174     watchSliderChild_->SetMinIconUrl(minIconUrl_);
175     watchSliderChild_->SetBackgroundColor(color_);
176     watchSliderChild_->SetSelectColor(selectColor_);
177     watchSliderChild_->SetContinuous(isContinuous_);
178 
179     sliderChild_->SetShowSteps(showSteps_);
180     sliderChild_->SetShowTips(showTips_);
181     sliderChild_->SetSliderMode(mode_);
182     sliderChild_->SetStepValue(step_);
183     sliderChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
184     paddingChild_->SetPadding(Edge(paddingLeft_, 0.0_vp, paddingRight_, 0.0_vp));
185     trackChild_->SetBackgroundColor(color_);
186     trackChild_->SetSelectColor(selectColor_);
187     blockChild_->SetBlockColor(blockColor_);
188     sliderChild_->InitStyle(GetTheme<SliderTheme>());
189 }
190 
191 } // namespace OHOS::Ace::Framework
192