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_V2_LIST_RENDER_LIST_ITEM_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_LIST_RENDER_LIST_ITEM_H 18 19 #include "base/geometry/dimension.h" 20 #include "core/components_v2/list/list_item_component.h" 21 #include "core/gestures/long_press_recognizer.h" 22 #include "core/gestures/drag_recognizer.h" 23 #include "core/pipeline/base/render_node.h" 24 #include "core/animation/spring_motion.h" 25 #include "core/components/list/list_item_theme.h" 26 27 namespace OHOS::Ace::V2 { 28 29 enum class ListItemChildType { 30 ITEM_CHILD, 31 SWIPER_START, 32 SWIPER_END 33 }; 34 35 enum class ListItemSwipeIndex { 36 SWIPER_END = -1, 37 ITEM_CHILD = 0, 38 SWIPER_START = 1, 39 }; 40 41 class RenderListItem : public RenderNode { 42 DECLARE_ACE_TYPE(V2::RenderListItem, RenderNode); 43 44 public: 45 static RefPtr<RenderNode> Create(); 46 47 RenderListItem() = default; 48 ~RenderListItem() override = default; 49 50 void Update(const RefPtr<Component>& component) override; 51 void PerformLayout() override; 52 void Paint(RenderContext& context, const Offset& offset) override; 53 void UpdateTouchRect() override; 54 IsForwardLayout()55 virtual bool IsForwardLayout() const 56 { 57 return true; 58 } 59 IsDeletable()60 bool IsDeletable() const 61 { 62 return component_ ? (component_->GetEditMode() & EditMode::DELETABLE) != 0 : false; 63 } 64 IsMovable()65 bool IsMovable() const 66 { 67 return component_ ? (component_->GetEditMode() & EditMode::MOVABLE) != 0 : false; 68 } 69 GetSticky()70 StickyMode GetSticky() const 71 { 72 return component_ ? component_->GetSticky() : StickyMode::NONE; 73 } 74 GetZIndex()75 int32_t GetZIndex() const 76 { 77 return component_ ? component_->GetZIndex() : 0; 78 } 79 80 void SetEditMode(bool editMode); 81 82 ACE_DEFINE_COMPONENT_EVENT(OnDeleteClick, void(RefPtr<RenderListItem>)); 83 ACE_DEFINE_COMPONENT_EVENT(OnSelect, void(RefPtr<RenderListItem>)); 84 GetComponent()85 RefPtr<Component> GetComponent() override 86 { 87 return component_; 88 } 89 GetEditMode()90 int32_t GetEditMode() const 91 { 92 return component_->GetEditMode(); 93 } 94 GetSelectable()95 bool GetSelectable() const 96 { 97 return selectable_; 98 } 99 GetOnSelectId()100 OnSelectFunc GetOnSelectId() const 101 { 102 return onSelectId_; 103 } 104 MarkIsSelected(bool isSelected)105 void MarkIsSelected(bool isSelected) 106 { 107 isSelected_ = isSelected; 108 } 109 IsSelected()110 bool IsSelected() const 111 { 112 return isSelected_; 113 } 114 IsDragStart()115 bool IsDragStart() const 116 { 117 return isDragStart_; 118 } 119 GetBorderRadius()120 Dimension GetBorderRadius() const 121 { 122 return borderRadius_; 123 } SetIndex(int index)124 void SetIndex(int index) 125 { 126 index_ = index; 127 } GetIndex()128 int GetIndex() const 129 { 130 return index_; 131 } 132 RegisterGetChildCallback(std::function<RefPtr<RenderNode> (ListItemChildType)> callback)133 void RegisterGetChildCallback(std::function<RefPtr<RenderNode>(ListItemChildType)> callback) 134 { 135 getChildCallback_ = std::move(callback); 136 } 137 GetSwiperStartRenderNode()138 RefPtr<RenderNode> GetSwiperStartRenderNode() const 139 { 140 return getChildCallback_ ? getChildCallback_(ListItemChildType::SWIPER_START) : nullptr; 141 } 142 GetSwiperEndRenderNode()143 RefPtr<RenderNode> GetSwiperEndRenderNode() const 144 { 145 return getChildCallback_ ? getChildCallback_(ListItemChildType::SWIPER_END) : nullptr; 146 } 147 GetItemChildRenderNode()148 RefPtr<RenderNode> GetItemChildRenderNode() const 149 { 150 return getChildCallback_ ? getChildCallback_(ListItemChildType::ITEM_CHILD) : nullptr; 151 } 152 153 template<class T> MakeValue(double mainValue,double crossValue)154 T MakeValue(double mainValue, double crossValue) const 155 { 156 return IsVertical() ? T(crossValue, mainValue) : T(mainValue, crossValue); 157 } 158 GetMainSize(const Size & size)159 double GetMainSize(const Size& size) const 160 { 161 return IsVertical() ? size.Height() : size.Width(); 162 } 163 GetCrossSize(const Size & size)164 double GetCrossSize(const Size& size) const 165 { 166 return IsVertical() ? size.Width() : size.Height(); 167 } 168 GetMainAxis(const Offset & size)169 double GetMainAxis(const Offset& size) const 170 { 171 return IsVertical() ? size.GetY() : size.GetX(); 172 } 173 GetCrossAxis(const Offset & size)174 double GetCrossAxis(const Offset& size) const 175 { 176 return IsVertical() ? size.GetX() : size.GetY(); 177 } 178 179 protected: 180 void OnTouchTestHit( 181 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 182 183 private: 184 int index_ = -1; 185 void CreateDeleteButton(); 186 void PerfLayoutSwiperMode(); 187 void InitDragRecognizer(); 188 void HandleDragStart(const DragStartInfo& info); 189 void HandleDragUpdate(const DragUpdateInfo& info); 190 void HandleDragEnd(const DragEndInfo& info); 191 void StartSpringMotion(double start, double end, double velocity); 192 double GetFriction(); 193 void UpdatePostion(double delta); 194 double CalculateFriction(double gamma); 195 bool IsVertical() const; 196 197 RefPtr<ListItemComponent> component_; 198 std::function<RefPtr<RenderNode>(ListItemChildType)> getChildCallback_; 199 RefPtr<RenderNode> swiperStart_; 200 RefPtr<RenderNode> swiperEnd_; 201 202 RefPtr<RenderNode> child_; 203 RefPtr<RenderNode> button_; 204 bool editMode_ = false; 205 206 RefPtr<LongPressRecognizer> longPressRecognizer_; 207 OnSelectFunc onSelectId_; 208 bool selectable_ = true; 209 bool isSelected_ = false; 210 bool isDragStart_ = false; 211 Dimension borderRadius_; 212 213 double curOffset_ = 0.0; 214 RefPtr<DragRecognizer> dragDetector_; 215 RefPtr<Animator> springController_; 216 RefPtr<SpringMotion> springMotion_; 217 ListItemSwipeIndex swipeIndex = ListItemSwipeIndex::ITEM_CHILD; 218 Size startSize_; 219 Size endSize_; 220 SwipeEdgeEffect edgeEffect_ = SwipeEdgeEffect::Spring; 221 RefPtr<ListItemTheme> theme_; 222 223 ACE_DISALLOW_COPY_AND_MOVE(RenderListItem); 224 }; 225 226 } // namespace OHOS::Ace::V2 227 228 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_LIST_RENDER_LIST_ITEM_H 229