1 /*
2  * Copyright (c) 2021-2022 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_TOGGLE_RENDER_TOGGLE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOGGLE_RENDER_TOGGLE_H
18 
19 #include "base/utils/system_properties.h"
20 #include "core/animation/animator.h"
21 #include "core/animation/keyframe_animation.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/toggle/toggle_component.h"
24 #include "core/gestures/click_recognizer.h"
25 #include "core/gestures/raw_recognizer.h"
26 #include "core/pipeline/base/render_node.h"
27 
28 namespace OHOS::Ace {
29 
30 class RenderToggle : public RenderNode {
31     DECLARE_ACE_TYPE(RenderToggle, RenderNode);
32 
33 public:
34     ~RenderToggle() override = default;
35 
36     static RefPtr<RenderNode> Create();
37 
38     void Update(const RefPtr<Component>& component) override;
39     void PerformLayout() override;
40     void UpdateFocusAnimation();
41 
GetToggleComponent()42     RefPtr<ToggleComponent> GetToggleComponent()
43     {
44         return toggleComponent_;
45     }
46 
47     std::string ProvideRestoreInfo() override;
48 
49 protected:
50     RenderToggle();
51     virtual Size Measure() = 0;
52     void OnTouchTestHit(
53         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
54     void HandleClickEvent();
55     void HandleTouchEvent(bool touched);
56     void HandleMoveEvent(const TouchEventInfo& info);
57     void OnMouseHoverEnterTest() override;
58     void OnMouseHoverExitTest() override;
59     void OnMouseClickDownAnimation() override;
60     void OnMouseClickUpAnimation() override;
SetOnChange(const std::function<void (bool)> & value)61     void SetOnChange(const std::function<void(bool)>& value)
62     {
63         onChangeToggle_ = value;
64     }
65 
66     RefPtr<ToggleComponent> toggleComponent_;
67 
68     RefPtr<ClickRecognizer> clickRecognizer_;
69     RefPtr<RawRecognizer> touchRecognizer_;
70     RefPtr<Animator> hoverControllerEnter_;
71     RefPtr<Animator> hoverControllerExit_;
72     RefPtr<Animator> clickControllerDown_;
73     RefPtr<Animator> clickControllerUp_;
74     RefPtr<KeyframeAnimation<float>> scaleAnimationEnter_;
75     RefPtr<KeyframeAnimation<float>> scaleAnimationExit_;
76     RefPtr<KeyframeAnimation<float>> scaleAnimationUp_;
77     RefPtr<KeyframeAnimation<float>> scaleAnimationDown_;
78 
79     bool widthDefined_ = false;
80     bool heightDefined_ = false;
81     bool isPressed_ = false;
82     bool isMoveEventValid_ = false;
83     bool isFocus_ = false;
84     float scale_ = 1.0f;
85 
86     Size toggleSize_;
87     std::function<void()> onClick_;
88     std::function<void(const std::string&)> onChange_;
89     std::function<void(bool)> onChangeToggle_;
90 
91 private:
92     void ResetController(RefPtr<Animator>& controller);
93     void CreateFloatAnimation(RefPtr<KeyframeAnimation<float>>& floatAnimation, float beginValue, float endValue);
94     void SetAccessibilityClickImpl();
95     void ApplyRestoreInfo();
96     bool disabled_ = false;
97 };
98 
99 } // namespace OHOS::Ace
100 
101 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOGGLE_RENDER_TOGGLE_H
102