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_V2_INDEXER_RENDER_INDEXER_ITEM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INDEXER_RENDER_INDEXER_ITEM_H
18 
19 #include "base/utils/string_utils.h"
20 #include "core/animation/animator.h"
21 #include "core/animation/keyframe_animation.h"
22 #include "core/components/box/render_box.h"
23 #include "core/components/text/text_component.h"
24 #include "core/components/transform/render_transform.h"
25 
26 namespace OHOS::Ace::V2 {
27 class RenderIndexerItem : public RenderNode {
28     DECLARE_ACE_TYPE(RenderIndexerItem, RenderNode);
29 
30 public:
31     static RefPtr<RenderNode> Create();
32     void Update(const RefPtr<Component>& component) override;
33     void PerformLayout() override;
34     void UpdateItemStyle();
35 
36     void MarkItemInAnimation(bool inAnimation = true)
37     {
38         isAnimating_ = inAnimation;
39     }
40 
GetItemType()41     int32_t GetItemType() const
42     {
43         return itemType_;
44     }
45 
IsItemPrimary()46     bool IsItemPrimary() const
47     {
48         return isPrimary_;
49     }
50 
GetSectionText()51     const std::string GetSectionText() const
52     {
53         return StringUtils::Str16ToStr8(strText_);
54     }
55 
GetSectionIndex()56     int32_t GetSectionIndex() const
57     {
58         return sectionIndex_;
59     }
60 
SetSectionIndex(int32_t index)61     void SetSectionIndex(int32_t index)
62     {
63         sectionIndex_ = index;
64     }
65 
GetClicked()66     bool GetClicked() const
67     {
68         return clicked_;
69     }
70 
SetFocused(bool getFocus)71     void SetFocused(bool getFocus)
72     {
73         focused_ = getFocus;
74         UpdateItemStyle();
75     }
76 
SetClicked(bool clicked)77     void SetClicked(bool clicked)
78     {
79         bool preClicked = clicked_;
80         clicked_ = clicked;
81         if (clicked_ != preClicked) {
82             UpdateItemStyle();
83         }
84     }
85 
SetRotate(double angle)86     void SetRotate(double angle)
87     {
88         RefPtr<RenderTransform> rotate = AceType::DynamicCast<RenderTransform>(GetChildren().front());
89         if (rotate) {
90             rotateAngle_ = angle;
91             rotate->ResetTransform();
92             rotate->RotateZ(angle);
93         }
94     }
95 
GetNormalTextStyle()96     const TextStyle& GetNormalTextStyle() const
97     {
98         return normalStyle_;
99     }
100 
GetActiveTextStyle()101     const TextStyle& GetActiveTextStyle() const
102     {
103         return activeStyle_;
104     }
105 
GetSelectedBackgroundColor()106     Color GetSelectedBackgroundColor() const
107     {
108         return selectedBgColor_;
109     }
110 
GetItemSize()111     const Dimension& GetItemSize() const
112     {
113         return itemSize_;
114     }
115 
116 protected:
117     bool MouseHoverTest(const Point& parentLocalPoint) override;
118     void OnMouseHoverEnterTest() override;
119     void OnMouseHoverExitTest() override;
120 
121 private:
122     void StartHoverAnimation(RefPtr<Animator> controller,
123         RefPtr<KeyframeAnimation<Color>>& colorAnimation);
124     void ResetController(RefPtr<Animator>& controller);
125     void CreateColorAnimation(RefPtr<KeyframeAnimation<Color>>& colorAnimation, const Color& beginValue,
126         const Color& endValue);
127 
128     bool clicked_ = false;
129     bool focused_ = false;
130     int32_t itemType_ = 0;
131     int32_t sectionIndex_ = 0;
132     RefPtr<TextComponent> textComponent_;
133     RefPtr<Animator> controllerEnter_;
134     RefPtr<Animator> controllerExit_;
135     RefPtr<KeyframeAnimation<Color>> colorAnimationEnter_;
136     RefPtr<KeyframeAnimation<Color>> colorAnimationExit_;
137     TextStyle normalStyle_;
138     TextStyle activeStyle_;
139     Color selectedBgColor_;
140     Dimension itemSize_;
141     bool rotate_ = false;
142     bool isPrimary_ = false;
143     bool isAnimating_ = false;
144     double rotateAngle_ = 0.0;
145     std::u16string strText_;
146 }; // class RenderIndexerItem
147 } // namespace OHOS::Ace
148 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INDEXER_RENDER_INDEXER_ITEM_H