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_CHECKABLE_RENDER_SWITCH_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_SWITCH_H
18 
19 #include "core/animation/animator.h"
20 #include "core/animation/curve_animation.h"
21 #include "core/components/checkable/checkable_component.h"
22 #include "core/components/checkable/render_checkable.h"
23 #include "core/components/text/render_text.h"
24 #include "core/components/text/text_component.h"
25 
26 namespace OHOS::Ace {
27 
28 class RenderSwitch : public RenderCheckable {
29     DECLARE_ACE_TYPE(RenderSwitch, RenderCheckable);
30 
31 public:
32     RenderSwitch() = default;
33     ~RenderSwitch() override;
34 
35     static RefPtr<RenderNode> Create();
36     void Update(const RefPtr<Component>& component) override;
37     void PerformLayout() override;
38 
39     void HandleClick() override;
40     void HandleDragStart(const Offset& updatePoint);
41     void HandleDragUpdate(const Offset& updatePoint);
42     void HandleDragEnd(const Offset& updatePoint);
43     void OnDrag(const Offset& updatePoint);
44     void UpdatePointPosition(const Offset& updatePoint);
45     void OpenSwitch();
46     void CloseSwitch();
47 
48     virtual Size CalculateTextSize(const std::string& text, RefPtr<RenderText>& renderText) = 0;
49     void PaintText(const Offset& textOffset, RenderContext& context) const;
50 
51     void InitCurrentPointPosition();
GetSwitchComponent()52     RefPtr<SwitchComponent> GetSwitchComponent() const
53     {
54         return component_;
55     }
56 
57     std::string ProvideRestoreInfo() override;
58 
59 protected:
60     void UpdateRenderText(const RefPtr<SwitchComponent>& component);
61     void InitRenderText();
62     void HandleDrag();
63     void UpdateAnimation();
64     void OnAnimationStop();
65     void UpdateAccessibilityAttr();
66 #ifndef WEARABLE_PRODUCT
67     void PrepareMultiModalEvent();
68     bool SubscribeMultiModal();
69     bool UnSubscribeMultiModal();
70 #endif
71     // switch attributes
72     bool oldChecked_ = true;
73     bool needReverse_ = false;
74     Dimension pointPadding_ = 0.0_px;
75     double pointRadius_ = 0.0;
76     double pointPositionDelta_ = 0.0;
77     double dragStartPosition_ = 0.0;
78     double currentPointOriginX_ = 0.0;
79     Size rawPointSize_;
80     Size switchSize_;
81 
82     // text attributes
83     bool showText_ = false;
84     Dimension pointTextPadding_ = 0.0_vp;
85     std::string textOn_ = "On";
86     std::string textOff_ = "Off";
87     TextStyle textStyle_;
88     RefPtr<RenderText> renderTextOn_;
89     RefPtr<RenderText> renderTextOff_;
90     Size textOnSize_;
91     Size textOffSize_;
92     Color textColorOn_ = Color::BLACK;
93     Color textColorOff_ = Color::BLACK;
94     RefPtr<TextComponent> textOnComponent_;
95     RefPtr<TextComponent> textOffComponent_;
96 #ifndef WEARABLE_PRODUCT
97     bool isSubscribeMultimodal_ = false;
98     VoiceEvent switchEvent_;
99     WeakPtr<MultiModalScene> multiModalScene_;
100     MultimodalEventCallback multimodalSwitchEvent_;
101 #endif
102     // animation control
103     RefPtr<Animator> controller_;
104     RefPtr<CurveAnimation<double>> translate_;
105     bool isSwitchDuringAnimation_ = false;
106     RefPtr<SwitchComponent> component_;
107 
108     bool isDragging = false;
109 private:
110     void SetAccessibilityClickImpl();
111     void ApplyRestoreInfo();
112 };
113 
114 } // namespace OHOS::Ace
115 
116 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_SWITCH_H
117