• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_RENDER_BUTTON_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_RENDER_BUTTON_H
18 
19 #include "base/utils/system_properties.h"
20 #include "core/animation/animator.h"
21 #include "core/animation/curve_animation.h"
22 #include "core/animation/keyframe_animation.h"
23 #include "core/components/button/button_component.h"
24 #include "core/components/common/properties/color.h"
25 #include "core/components/common/properties/edge.h"
26 #include "core/components/touch_listener/render_touch_listener.h"
27 #include "core/gestures/click_recognizer.h"
28 #include "core/gestures/raw_recognizer.h"
29 #include "core/pipeline/base/render_node.h"
30 
31 namespace OHOS::Ace {
32 
33 constexpr Dimension ARC_BUTTON_WIDTH = 169.0_vp;
34 constexpr Dimension ARC_BUTTON_HEIGHT = 48.0_vp;
35 
36 // Definition for animation
37 constexpr double INIT_SCALE = 1.0;
38 
39 class RenderButton : public RenderNode {
40     DECLARE_ACE_TYPE(RenderButton, RenderNode);
41 
42 public:
43     ~RenderButton() override = default;
44 
45     static RefPtr<RenderNode> Create();
46 
47     void Update(const RefPtr<Component>& component) override;
48     void PerformLayout() override;
49     void OnPaintFinish() override;
50     virtual void OnStatusStyleChanged(const VisualState state) override;
51 
OnAttachContext()52     void OnAttachContext() override
53     {
54         width_.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
55             auto renderButton = weak.Upgrade();
56             if (renderButton) {
57                 renderButton->MarkNeedLayout();
58             }
59         });
60         height_.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
61             auto renderButton = weak.Upgrade();
62             if (renderButton) {
63                 renderButton->MarkNeedLayout();
64             }
65         });
66         backgroundColor_.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
67             auto renderButton = weak.Upgrade();
68             if (renderButton) {
69                 renderButton->MarkNeedLayout();
70             }
71         });
72         clickedColor_.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
73             auto renderButton = weak.Upgrade();
74             if (renderButton) {
75                 renderButton->MarkNeedLayout();
76             }
77         });
78     }
79 
80     void HandleFocusEvent(bool isFocus);
81     void HandleClickEvent(const ClickInfo& info);
82     void HandleClickEvent();
83     void HandleKeyEnterEvent(const ClickInfo& info);
84     void HandleKeyEnterEvent();
85     void HandleRemoteMessageEvent(const ClickInfo& info);
86     void HandleRemoteMessageEvent();
87     void DisplayFocusAnimation();
88     void PlayFocusAnimation(bool isFocus);
89     void AnimateMouseHoverEnter() override;
90     void AnimateMouseHoverExit() override;
91     WeakPtr<RenderNode> CheckHoverNode() override;
92 
IsDisabled()93     bool IsDisabled() const
94     {
95         return isDisabled_;
96     }
97 
SetClickedColor(const Color & clickColor)98     void SetClickedColor(const Color& clickColor)
99     {
100         // do not trigger animation
101         clickedColor_.SetValue(clickColor.GetValue());
102         setClickColor_ = true;
103     }
104 
GetButtonType()105     ButtonType GetButtonType() const
106     {
107         return type_;
108     }
109 
GetStateEffect()110     bool GetStateEffect() const
111     {
112         return stateEffect_;
113     }
GetComponent()114     RefPtr<Component> GetComponent() override
115     {
116         return buttonComponent_;
117     }
GetClickedColor()118     Color GetClickedColor()
119     {
120         return clickedColor_;
121     }
122 
GetBackgroundColor()123     Color GetBackgroundColor() const
124     {
125         return buttonComponent_->GetBackgroundColor();
126     }
127 
GetRadius()128     double GetRadius() const
129     {
130         return rrectRadius_;
131     }
132 
133     void SendAccessibilityEvent();
134 
135 protected:
136     RenderButton();
137     virtual Size Measure() = 0;
138     void OnTouchTestHit(
139         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
140 
141     void Initialize();
142     void InitAccessibilityEventListener();
143     void HandleTouchEvent(bool isTouch);
144     void HandleMoveEvent(const TouchEventInfo& info);
145     void UpdateDownloadStyles(const RefPtr<ButtonComponent>& button);
146     void SetProgress(uint32_t progress);
147     void OnMouseHoverEnterTest() override;
148     void OnMouseHoverExitTest() override;
149     void OnMouseClickDownAnimation() override;
150     void OnMouseClickUpAnimation() override;
151 
NeedLayoutExtendToParant()152     bool NeedLayoutExtendToParant() const
153     {
154         return (layoutFlag_ & LAYOUT_FLAG_EXTEND_TO_PARENT) == LAYOUT_FLAG_EXTEND_TO_PARENT;
155     }
156 
157     RefPtr<ButtonComponent> buttonComponent_;
158     RefPtr<RawRecognizer> touchRecognizer_;
159     RefPtr<ClickRecognizer> clickRecognizer_;
160     RefPtr<Animator> progressController_;
161 
162     ButtonType type_ = ButtonType::NORMAL;
163     Size buttonSize_;
164     Offset offsetDelta_;
165     double rrectRadius_ = 0.0;
166     double widthDelta_ = 0.0;
167     double progressWidth_ = 0.0;
168     double progressPercent_ = 0.0;
169     double progressDiameter_ = 0.0;
170     double opacity_ = 1.0;
171     double maskingOpacity_ = 0.0;
172     double ratio_ = 0.0;
173     double previousValue_ = 0.0;
174     double percentChange_ = 0.0;
175     float scale_ = 1.0f;
176     bool stateEffect_ = true;
177 
178     bool needUpdateAnimation_ = false;
179     bool isInnerBorder_ = false;
180     bool isClicked_ = false;
181     bool isDisabled_ = false;
182     bool isFocus_ = false;
183     bool isWatch_ = false;
184     bool isTv_ = false;
185     bool isPhone_ = false;
186     bool widthDefined_ = false;
187     bool heightDefined_ = false;
188     bool progressDisplay_ = false;
189     bool animationRunning_ = false;
190     bool isLastFrame_ = false;
191     bool isOpacityAnimation_ = false;
192     bool isTouchAnimation_ = false;
193     bool isHover_ = false;
194     bool needFocusColor_ = false;
195     bool needHoverColor_ = false;
196     bool isMoveEventValid_ = false;
197     bool setClickColor_ = false;
198     uint32_t layoutFlag_ = 0;
199 
200     Color focusAnimationColor_;
201     Color progressColor_;
202     Color progressFocusColor_;
203     Color defaultClickedColor_;
204     AnimatableColor clickedColor_;
205     AnimatableColor backgroundColor_;
206     BorderEdge borderEdge_;
207     std::function<void(const ClickInfo&)> onClickWithInfo_;
208     std::function<void()> onClick_;
209     std::chrono::steady_clock::time_point previousUpdateTime_ = std::chrono::steady_clock::now();
210     std::chrono::duration<double> animationDuring_;
211 
212 private:
213     void UpdateAnimationParam(double value);
214     void UpdateFocusAnimation(double value);
215     void UpdateProgressAnimation();
216     void PlayAnimation(double start, double end, int32_t duration, const FillMode& fillMode = FillMode::FORWARDS);
217     void PlayTouchAnimation();
218     void PlayClickAnimation();
219     void PlayClickScaleAnimation(float keyTime, int32_t duration);
220     void UpdateAccessibility();
221     void SetChildrenLayoutSize();
222     void SetChildrenAlignment();
223     Size CalculateLayoutSize();
224     bool NeedAdaptiveChild();
225     bool NeedConstrain();
226     void ResetController(RefPtr<Animator>& controller);
227     void CreateFloatAnimation(RefPtr<KeyframeAnimation<float>>& floatAnimation, float beginValue, float endValue);
228 
229     AnimatableDimension width_;
230     AnimatableDimension height_;
231     Dimension minWidth_;
232     bool valueChanged_ = false;
233     bool isClickAnimation_ = false;
234     double startValue_ = 0.0;
235     double endValue_ = 0.0;
236     Edge arcInitEdge_;
237     Size layoutSize_;
238     Size childrenSize_;
239 
240     RefPtr<Animator> controller_;
241     RefPtr<Animator> hoverControllerEnter_;
242     RefPtr<Animator> hoverControllerExit_;
243     RefPtr<Animator> clickControllerDown_;
244     RefPtr<Animator> clickControllerUp_;
245     RefPtr<KeyframeAnimation<float>> scaleAnimationEnter_;
246     RefPtr<KeyframeAnimation<float>> scaleAnimationExit_;
247     RefPtr<KeyframeAnimation<float>> scaleAnimationUp_;
248     RefPtr<KeyframeAnimation<float>> scaleAnimationDown_;
249 };
250 
251 } // namespace OHOS::Ace
252 
253 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_RENDER_BUTTON_H
254