1 /* 2 * Copyright (c) 2022-2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_MODEL_H 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 #include <optional> 23 24 #include "base/geometry/axis.h" 25 #include "base/memory/ace_type.h" 26 #include "base/utils/macros.h" 27 #include "core/common/resource/resource_object.h" 28 #include "core/components/common/properties/clip_path.h" 29 #include "core/components/common/properties/color.h" 30 #include "core/components_ng/property/gradient_property.h" 31 #include "core/components_ng/base/ui_node.h" 32 #include "core/components_ng/pattern/slider/slider_custom_content_options.h" 33 34 #ifdef SUPPORT_DIGITAL_CROWN 35 #include "core/event/crown_event.h" 36 #endif 37 38 namespace OHOS::Ace { 39 enum class SliderColorType { 40 BLOCK_COLOR, 41 TRACK_COLOR, 42 SELECT_COLOR, 43 BLOCK_BORDER_COLOR, 44 STEP_COLOR 45 }; 46 class ACE_FORCE_EXPORT SliderModel { 47 public: 48 enum class SliderMode { 49 OUTSET, // block on track, track is thin 50 INSET, // block inside track, track is rough 51 NONE, // no block 52 CAPSULE, // capsule slider. 53 }; 54 55 enum class BlockStyleType { 56 DEFAULT, 57 IMAGE, 58 SHAPE, 59 }; 60 61 enum class SliderInteraction { 62 SLIDE_AND_CLICK, 63 SLIDE_ONLY, 64 SLIDE_AND_CLICK_UP, 65 }; 66 67 // need check 68 enum class SliderChangeMode { 69 BEGIN, 70 MOVING, 71 END, 72 CLICK, 73 }; 74 75 class SliderValidRange final : public AceType { 76 public: 77 SliderValidRange() = default; SliderValidRange(float from,float to)78 SliderValidRange(float from, float to) : fromValue(from), toValue(to) {} 79 ~SliderValidRange() = default; GetFromValue()80 float GetFromValue() const 81 { 82 return fromValue; 83 } GetToValue()84 float GetToValue() const 85 { 86 return toValue; 87 } HasValidValues()88 bool HasValidValues() const 89 { 90 return std::isfinite(fromValue) && std::isfinite(toValue); 91 } ToString()92 std::string ToString() const 93 { 94 std::stringstream ss; 95 ss << "from: " << fromValue << " to: " << toValue; 96 return ss.str(); 97 } 98 99 private: 100 float fromValue = std::numeric_limits<float>::quiet_NaN(); 101 float toValue = std::numeric_limits<float>::quiet_NaN(); 102 }; 103 104 using SliderStepItemAccessibility = std::string; 105 using SliderShowStepOptions = std::unordered_map<uint32_t, SliderStepItemAccessibility>; 106 107 static SliderModel* GetInstance(); 108 virtual ~SliderModel() = default; 109 110 virtual void Create(float value, float step, float min, float max) = 0; 111 virtual void SetSliderMode(const SliderMode& value) = 0; 112 virtual void SetDirection(Axis value) = 0; 113 virtual void SetReverse(bool value) = 0; 114 virtual void SetBlockColor(const Color& value) = 0; 115 virtual void SetTrackBackgroundColor(const Color& value) = 0; 116 virtual void SetTrackBackgroundColor(const NG::Gradient& value, bool isResourceColor = false) = 0; 117 virtual void SetSelectColor(const Color& value) = 0; 118 virtual void SetSelectColor(const NG::Gradient& value, bool isResourceColor = false) = 0; 119 virtual void SetMinLabel(float value) = 0; 120 virtual void SetMaxLabel(float value) = 0; SetMinResponsiveDistance(float value)121 virtual void SetMinResponsiveDistance(float value) {}; 122 virtual void SetShowSteps(bool value, const std::optional<SliderShowStepOptions>& options = std::nullopt) = 0; 123 virtual void SetShowTips(bool value, const std::optional<std::string>& content) = 0; 124 virtual void SetThickness(const Dimension& value) = 0; 125 virtual void SetBlockBorderColor(const Color& value) = 0; 126 virtual void SetBlockBorderWidth(const Dimension& value) = 0; 127 virtual void SetStepColor(const Color& value) = 0; 128 virtual void SetTrackBorderRadius(const Dimension& value) = 0; 129 virtual void SetSelectedBorderRadius(const Dimension& value) = 0; 130 virtual void SetBlockSize(const Dimension& width, const Dimension& height) = 0; 131 virtual void SetBlockType(BlockStyleType value) = 0; 132 virtual void SetBlockImage( 133 const std::string& value, const std::string& bundleName, const std::string& moduleName) = 0; 134 virtual void SetBlockShape(const RefPtr<BasicShape>& value) = 0; 135 virtual void SetStepSize(const Dimension& value) = 0; SetSliderInteractionMode(SliderInteraction mode)136 virtual void SetSliderInteractionMode(SliderInteraction mode) {}; 137 virtual void SetOnChange(std::function<void(float, int32_t)>&& eventOnChange) = 0; 138 virtual void SetOnChangeEvent(std::function<void(float)>&& onChangeEvent) = 0; SetValidSlideRange(float fromValue,float toValue)139 virtual void SetValidSlideRange(float fromValue, float toValue) {}; 140 virtual void SetPrefix(const RefPtr<NG::UINode>& content, const NG::SliderPrefixOptions& options) = 0; 141 virtual void SetSuffix(const RefPtr<NG::UINode>& content, const NG::SliderSuffixOptions& options) = 0; 142 virtual void CreateWithColorResourceObj(const RefPtr<ResourceObject>& resObj, 143 const SliderColorType sliderColorType) = 0; 144 virtual void CreateWithMediaResourceObj(const RefPtr<ResourceObject>& resObj, const std::string& bundleName, 145 const std::string& moduleName) = 0; 146 virtual void CreateWithStringResourceObj(const RefPtr<ResourceObject>& resObj, const bool isShowTips) = 0; 147 #ifdef SUPPORT_DIGITAL_CROWN SetDigitalCrownSensitivity(CrownSensitivity sensitivity)148 virtual void SetDigitalCrownSensitivity(CrownSensitivity sensitivity) {}; 149 #endif 150 151 virtual void ResetBlockBorderColor() = 0; 152 virtual void ResetBlockBorderWidth() = 0; 153 virtual void ResetStepColor() = 0; 154 virtual void ResetTrackBorderRadius() = 0; 155 virtual void ResetSelectedBorderRadius() = 0; 156 virtual void ResetBlockSize() = 0; 157 virtual void ResetBlockType() = 0; 158 virtual void ResetBlockImage() = 0; 159 virtual void ResetBlockShape() = 0; 160 virtual void ResetStepSize() = 0; 161 virtual void ResetSliderInteractionMode() = 0; 162 virtual void ResetMinResponsiveDistance() = 0; 163 virtual void ResetValidSlideRange() = 0; ResetBlockColor()164 virtual void ResetBlockColor() {}; ResetTrackColor()165 virtual void ResetTrackColor() {}; ResetSelectColor()166 virtual void ResetSelectColor() {}; 167 virtual void SetEnableHapticFeedback(bool isEnableHapticFeedback) = 0; 168 #ifdef SUPPORT_DIGITAL_CROWN 169 virtual void ResetDigitalCrownSensitivity() = 0; 170 #endif 171 172 private: 173 static std::unique_ptr<SliderModel> instance_; 174 static std::mutex mutex_; 175 }; 176 177 } // namespace OHOS::Ace 178 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_MODEL_H 179