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_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components_ng/event/event_hub.h" 21 22 namespace OHOS::Ace::NG { 23 24 using SliderOnChangeEvent = std::function<void(float, int32_t)>; 25 using SliderOnValueChangeEvent = std::function<void(float)>; 26 27 class SliderEventHub : public EventHub { 28 DECLARE_ACE_TYPE(SliderEventHub, EventHub) 29 public: 30 SliderEventHub() = default; 31 ~SliderEventHub() override = default; SetOnChange(SliderOnChangeEvent && changeEvent)32 void SetOnChange(SliderOnChangeEvent&& changeEvent) 33 { 34 changeEvent_ = std::move(changeEvent); 35 } 36 FireChangeEvent(float value,int32_t mode)37 void FireChangeEvent(float value, int32_t mode) 38 { 39 constexpr int32_t BEGIN_MODE = 0; 40 constexpr int32_t END_MODE = 2; 41 if (mode == END_MODE) { 42 inAction_ = false; 43 } 44 if (mode == BEGIN_MODE) { 45 inAction_ = true; 46 } 47 if (onChangeEvent_) { 48 onChangeEvent_(value); 49 } 50 CHECK_NULL_VOID(changeEvent_); 51 changeEvent_(value, mode); 52 if (mode > BEGIN_MODE) { 53 value_ = value; 54 } 55 } 56 SetValue(float value)57 void SetValue(float value) 58 { 59 if (!inAction_) { 60 value_ = value; 61 } 62 } 63 GetValue()64 float GetValue() const 65 { 66 return value_; 67 } 68 SetOnChangeEvent(SliderOnValueChangeEvent && onChangeEvent)69 void SetOnChangeEvent(SliderOnValueChangeEvent&& onChangeEvent) 70 { 71 onChangeEvent_ = std::move(onChangeEvent); 72 } 73 74 private: 75 bool inAction_ = false; 76 SliderOnChangeEvent changeEvent_; 77 SliderOnValueChangeEvent onChangeEvent_; 78 float value_ = .0f; 79 ACE_DISALLOW_COPY_AND_MOVE(SliderEventHub); 80 }; 81 82 } // namespace OHOS::Ace::NG 83 84 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_EVENT_HUB_H 85