• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (onChangeEvent_) {
40             onChangeEvent_(value);
41         }
42         constexpr int32_t BEGIN_MODE = 0;
43         CHECK_NULL_VOID(changeEvent_);
44         changeEvent_(value, mode);
45         if (mode > BEGIN_MODE) {
46             SetValue(value);
47         }
48     }
49 
SetValue(float value)50     void SetValue(float value)
51     {
52         value_ = value;
53     }
54 
GetValue()55     float GetValue() const
56     {
57         return value_;
58     }
59 
SetOnChangeEvent(SliderOnValueChangeEvent && onChangeEvent)60     void SetOnChangeEvent(SliderOnValueChangeEvent&& onChangeEvent)
61     {
62         onChangeEvent_ = std::move(onChangeEvent);
63     }
64 
65 private:
66     SliderOnChangeEvent changeEvent_;
67     SliderOnValueChangeEvent onChangeEvent_;
68     float value_ = .0f;
69     ACE_DISALLOW_COPY_AND_MOVE(SliderEventHub);
70 };
71 
72 } // namespace OHOS::Ace::NG
73 
74 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_EVENT_HUB_H
75