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_INDEXER_V2_RENDER_INDEXER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_V2_RENDER_INDEXER_H
18 
19 #include "base/geometry/size.h"
20 #include "core/components/box/render_box_base.h"
21 #include "core/components/common/rotation/rotation_node.h"
22 #include "core/components/display/render_display.h"
23 #include "core/components/focus_animation/render_focus_animation.h"
24 #include "core/components_v2/indexer/indexer_component.h"
25 #include "core/components_v2/indexer/render_indexer_item.h"
26 #include "core/components_v2/indexer/indexer_event_info.h"
27 #include "core/components_v2/indexer/render_popup_list.h"
28 #include "core/gestures/drag_recognizer.h"
29 #include "core/gestures/raw_recognizer.h"
30 #include "core/pipeline/base/render_node.h"
31 
32 namespace OHOS::Ace::V2 {
33 inline constexpr int32_t TABLE_ANGLE = 0;
34 inline constexpr int32_t TABLE_POSITION_X = 1;
35 inline constexpr int32_t TABLE_POSITION_Y = 2;
36 inline constexpr int32_t TABLE_INDEX_COUNT = 3;
37 inline constexpr int32_t INDEXER_BUBBLE_ANIMATION_DURATION = 3000;
38 inline constexpr double ROTATION_THRESHOLD = 90.0;
39 inline constexpr double HALF = 0.5;
40 inline constexpr double DOUBLE = 2.0;
41 
42 class RenderIndexer : public RenderNode, public RotationNode {
43     DECLARE_ACE_TYPE(RenderIndexer, RenderNode);
44 
45 public:
46     using OnSelectedFunc = std::function<void(std::shared_ptr<IndexerEventInfo>&)>;
47     using OnRequestPopupDataCallbackFunc = std::function<void(std::vector<std::string>&)>;
48 
49     RenderIndexer();
50     ~RenderIndexer() override = default;
51 
52     static RefPtr<RenderNode> Create();
53 
54     void Update(const RefPtr<Component>& component) override;
55     void PerformLayout() override;
56     void OnTouchTestHit(
57         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
58     virtual bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict,
59         TouchTestResult& result) override;
OnRotation(const RotationEvent & event)60     virtual bool OnRotation(const RotationEvent& event) override
61     {
62         return true;
63     }
64     virtual void HandleTouchDown(const TouchEventInfo& info);
65     virtual void HandleTouchUp(const TouchEventInfo& info);
66     virtual void HandleTouchMove(const TouchEventInfo& info);
67     virtual int32_t GetTouchedItemIndex(const Offset& touchPosition);
HandleListMoveItems(int32_t curHeadIndex)68     virtual void HandleListMoveItems(int32_t curHeadIndex) {}
UpdateCurrentSectionItem(int32_t curSection)69     virtual void UpdateCurrentSectionItem(int32_t curSection) {}
BeginFocusAnimation(int32_t originIndex,int32_t targetIndex)70     virtual void BeginFocusAnimation(int32_t originIndex, int32_t targetIndex) {}
71     virtual bool IsValidBubbleBox();
72     virtual bool IsValidPopupList();
73 
NeedProcess(const RefPtr<RenderNode> & item)74     virtual bool NeedProcess(const RefPtr<RenderNode>& item) const
75     {
76         return true;
77     }
NeedRotation()78     virtual bool NeedRotation() const
79     {
80         return false;
81     }
82 
GetFocusIndex()83     int32_t GetFocusIndex() const
84     {
85         return focusedItem_;
86     }
87 
SetFocusIndex(int32_t index)88     void SetFocusIndex(int32_t index)
89     {
90         focusedItem_ = index;
91     }
92 
ClearFocusAnimation()93     void ClearFocusAnimation()
94     {
95         auto context = context_.Upgrade();
96         if (!context) {
97             LOGE("Pipeline context upgrade fail!");
98             return;
99         }
100         if (!context->GetRenderFocusAnimation()) {
101             LOGE("focusAnimation is null!");
102             return;
103         }
104         context->CancelFocusAnimation();
105     }
106 
GetBubbleEnabled()107     bool GetBubbleEnabled() const
108     {
109         return bubbleEnabled_;
110     }
111 
GetAlignStyle()112     AlignStyle GetAlignStyle() const
113     {
114         return alignStyle_;
115     }
116 
GetBubbleBackgroundColor()117     Color& GetBubbleBackgroundColor()
118     {
119         return  color_;
120     }
121 
GetBubbleText()122     RefPtr<TextComponent> GetBubbleText() const
123     {
124         return bubbleText_;
125     }
126 
GetArrayValue()127     std::vector<std::u16string> GetArrayValue() const
128     {
129         return valueArray_;
130     }
131 
132     RefPtr<RenderIndexerItem> GetSpecificItem(int32_t index) const;
133 
134     void HandleFocusAnimation(const Size& size, const Offset& offset);
135     void MoveSectionWithIndexer(int32_t curSection);
136 
137 protected:
138     void HandleTouched(const Offset& touchPosition);
139     void MoveList(int32_t index);
140 
141     void InitFocusedItem();
142     void LayoutPopup();
143     void UpdateItems();
144     void UpdateBubbleText();
145     void BuildBubbleAnimation();
146     void BeginBubbleAnimation();
147     int32_t GetItemIndex(int32_t index); // get first list item index in specified section
148 
149     void OnSelected(int32_t selected) const;
150     void OnRequestPopupData(int32_t selected);
151 
152     bool GetBubbleRect(Rect& rect);
153     bool GetPopupListRect(Rect& rect);
154 
155     int32_t focusedItem_ = -1; // the item which set high light
156     int32_t nonItemCount_ = 0;
157     int32_t itemCount_ = INDEXER_ITEM_MAX_COUNT;
158 
159     bool clicked_ = false;
160     bool bubbleEnabled_ = true;
161     bool popupListEnabled_ = false;
162     bool touchBubbleDisplay = false;
163     bool touchPopupListDisplay = false;
164 
165     double itemSize_ = INDEXER_CIRCLE_ITEM_SIZE;
166     double itemSizeRender_ = INDEXER_CIRCLE_ITEM_SIZE;
167     double paddingX_ = INDEXER_DEFAULT_PADDING_X;
168     double paddingY_ = INDEXER_DEFAULT_PADDING_Y;
169     AlignStyle alignStyle_ = AlignStyle::RIGHT;
170     Color color_;
171 
172     RefPtr<RawRecognizer> touchRecognizer_;
173     RefPtr<TextComponent> bubbleText_;
174     RefPtr<RenderBox> bubbleBox_;                   // reference of the bubble box render node
175     RefPtr<RenderDisplay> bubbleDisplay_;           // reference of the bubble box render node
176     RefPtr<RenderPopupList> popupList_;             // reference of the popup list render node
177     RefPtr<RenderDisplay> popupListDisplay_;        // reference of the popup list render node
178     RefPtr<Animator> bubbleController_;             // control bubble appear and disappear
179     Offset touchPostion_;
180     std::list<RefPtr<RenderNode>> items_;
181     std::vector<std::u16string> valueArray_;
182 
183     OnSelectedFunc selectedEventFun_;
184     IndexerComponent::OnRequestPopupDataFunc requestPopupDataEventFun_;
185 };
186 } // namespace OHOS::Ace::V2
187 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_V2_RENDER_INDEXER_H