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_BOX_RENDER_BOX_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_RENDER_BOX_H 18 19 #include "base/image/pixel_map.h" 20 #include "core/animation/animator.h" 21 #include "core/animation/keyframe_animation.h" 22 #include "core/components/box/box_component.h" 23 #include "core/components/box/render_box_base.h" 24 #include "core/components/common/properties/clip_path.h" 25 #include "core/components/common/properties/color.h" 26 #include "core/components/common/properties/decoration.h" 27 #include "core/components/image/render_image.h" 28 #include "core/gestures/click_recognizer.h" 29 #include "core/event/axis_event.h" 30 #include "core/gestures/raw_recognizer.h" 31 #include "base/window/drag_window.h" 32 #include "core/pipeline/base/constants.h" 33 34 namespace OHOS::Ace { 35 36 constexpr int32_t MAX_GESTURE_SIZE = 3; 37 constexpr int32_t DEFAULT_INDEX_VALUE = -1; 38 39 class ACE_EXPORT RenderBox : public RenderBoxBase, public DragDropEvent { 40 DECLARE_ACE_TYPE(RenderBox, RenderBoxBase, DragDropEvent); 41 42 public: 43 static RefPtr<RenderNode> Create(); 44 void Update(const RefPtr<Component>& component) override; 45 void OnPaintFinish() override; 46 47 void HandleAccessibilityFocusEvent(bool isAccessibilityFocus); 48 OnAttachContext()49 void OnAttachContext() override 50 { 51 RenderBoxBase::OnAttachContext(); 52 if (!backDecoration_) { 53 backDecoration_ = AceType::MakeRefPtr<Decoration>(); 54 } 55 backDecoration_->SetContextAndCallback(context_, [weak = WeakClaim(this)] { 56 auto renderBox = weak.Upgrade(); 57 if (renderBox) { 58 renderBox->OnAnimationCallback(); 59 } 60 }); 61 if (!frontDecoration_) { 62 frontDecoration_ = AceType::MakeRefPtr<Decoration>(); 63 } 64 frontDecoration_->SetContextAndCallback(context_, [weak = WeakClaim(this)] { 65 auto renderBox = weak.Upgrade(); 66 if (renderBox) { 67 renderBox->OnAnimationCallback(); 68 } 69 }); 70 } 71 72 void OnStatusStyleChanged(VisualState state) override; 73 void UpdateStyleFromRenderNode(PropertyAnimatableType type) override; 74 GetColor()75 const Color& GetColor() const override 76 { 77 if (backDecoration_) { 78 return backDecoration_->GetBackgroundColor(); 79 } 80 return Color::TRANSPARENT; 81 } 82 GetInspectorDirection()83 TextDirection GetInspectorDirection() const 84 { 85 return inspectorDirection_; 86 } 87 GetBackDecoration()88 RefPtr<Decoration> GetBackDecoration() 89 { 90 if (!backDecoration_) { 91 backDecoration_ = AceType::MakeRefPtr<Decoration>(); 92 } 93 return backDecoration_; 94 } 95 GetFrontDecoration()96 RefPtr<Decoration> GetFrontDecoration() const 97 { 98 return frontDecoration_; 99 } 100 NeedFocusBorder(bool needFocusBorder)101 virtual void NeedFocusBorder(bool needFocusBorder) 102 { 103 needFocusBorder_ = needFocusBorder; 104 MarkNeedRender(); 105 } 106 SetColor(const Color & color,bool isBackground)107 virtual void SetColor(const Color& color, bool isBackground) // add for animation 108 { 109 // create decoration automatically while user had not defined 110 if (isBackground) { 111 if (!backDecoration_) { 112 backDecoration_ = AceType::MakeRefPtr<Decoration>(); 113 } 114 backDecoration_->SetBackgroundColor(color); 115 } else { 116 if (!frontDecoration_) { 117 frontDecoration_ = AceType::MakeRefPtr<Decoration>(); 118 } 119 frontDecoration_->SetBackgroundColor(color); 120 } 121 MarkNeedRender(); 122 } 123 SetBackDecoration(const RefPtr<Decoration> & decoration)124 void SetBackDecoration(const RefPtr<Decoration>& decoration) // add for list, do not use to update background image 125 { 126 backDecoration_ = decoration; 127 MarkNeedRender(); 128 } 129 SetFrontDecoration(const RefPtr<Decoration> & decoration)130 void SetFrontDecoration(const RefPtr<Decoration>& decoration) // add for list 131 { 132 frontDecoration_ = decoration; 133 MarkNeedRender(); 134 } 135 NeedMaterial(bool needMaterial)136 void NeedMaterial(bool needMaterial) 137 { 138 needMaterial_ = needMaterial; 139 } 140 GetNeedMaterial()141 bool GetNeedMaterial() const 142 { 143 return needMaterial_; 144 } 145 void OnMouseHoverEnterAnimation() override; 146 void OnMouseHoverExitAnimation() override; 147 void StopMouseHoverAnimation() override; 148 149 // add for animation 150 virtual void SetBackgroundSize(const BackgroundImageSize& size); 151 BackgroundImagePosition GetBackgroundPosition() const; 152 virtual void SetBackgroundPosition(const BackgroundImagePosition& position); 153 BackgroundImageSize GetBackgroundSize() const; 154 virtual void SetShadow(const Shadow& shadow); 155 Shadow GetShadow() const; 156 void SetGrayScale(double scale); 157 double GetGrayScale(void) const; 158 void SetBrightness(double ness); 159 double GetBrightness(void) const; 160 void SetContrast(double trast); 161 double GetContrast(void) const; 162 void SetColorBlend(const Color& color); 163 Color GetColorBlend(void) const; 164 void SetSaturate(double rate); 165 double GetSaturate(void) const; 166 void SetSepia(double pia); 167 double GetSepia(void) const; 168 void SetInvert(double invert); 169 double GetInvert(void) const; 170 void SetHueRotate(float deg); 171 float GetHueRotate(void) const; 172 virtual void SetBorderWidth(double width, const BorderEdgeHelper& helper); 173 double GetBorderWidth(const BorderEdgeHelper& helper) const; 174 virtual void SetBorderColor(const Color& color, const BorderEdgeHelper& helper); 175 Color GetBorderColor(const BorderEdgeHelper& helper) const; 176 virtual void SetBorderStyle(BorderStyle borderStyle, const BorderEdgeHelper& helper); 177 BorderStyle GetBorderStyle(const BorderEdgeHelper& helper) const; 178 virtual void SetBorderRadius(double radius, const BorderRadiusHelper& helper); 179 double GetBorderRadius(const BorderRadiusHelper& helper) const; 180 virtual void SetBlurRadius(const AnimatableDimension& radius); 181 AnimatableDimension GetBlurRadius() const; 182 virtual void SetBackdropRadius(const AnimatableDimension& radius); 183 AnimatableDimension GetBackdropRadius() const; 184 virtual void SetWindowBlurProgress(double progress); 185 double GetWindowBlurProgress() const; 186 void CreateFloatAnimation(RefPtr<KeyframeAnimation<float>>& floatAnimation, float beginValue, float endValue); 187 188 Size GetBorderSize() const override; 189 ColorPropertyAnimatable::SetterMap GetColorPropertySetterMap() override; 190 ColorPropertyAnimatable::GetterMap GetColorPropertyGetterMap() override; 191 Offset GetGlobalOffsetExternal() const override; 192 void MouseHoverEnterTest() override; 193 void MouseHoverExitTest() override; 194 void AnimateMouseHoverEnter() override; 195 void AnimateMouseHoverExit() override; 196 bool HandleMouseEvent(const MouseEvent& event) override; 197 void HandleMouseHoverEvent(MouseState mouseState) override; 198 199 void OnTouchTestHit( 200 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 201 202 void AddRecognizerToResult( 203 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result); 204 GetOnMouseId()205 const OnMouseCallback& GetOnMouseId() const 206 { 207 return onMouse_; 208 } 209 GetOnLongPress()210 RefPtr<Gesture> GetOnLongPress() const 211 { 212 return onLongPressId_; 213 } 214 215 DragItemInfo GenerateDragItemInfo(const RefPtr<PipelineContext>& context, const GestureEvent& info) override; 216 void AddDataToClipboard(const RefPtr<PipelineContext>& context, const std::string& extraInfo, 217 const std::string& selectedText, const std::string& imageSrc) override; 218 219 void ResetController(RefPtr<Animator>& controller); 220 void CreateColorAnimation( 221 RefPtr<KeyframeAnimation<Color>>& colorAnimation, const Color& beginValue, const Color& endValue); 222 223 protected: 224 void ClearRenderObject() override; 225 226 Offset GetBorderOffset() const override; 227 Radius GetBorderRadius() const override; 228 void UpdateGestureRecognizer(const std::array<RefPtr<Gesture>, MAX_GESTURE_SIZE>& gestures); 229 bool ExistGestureRecognizer(); 230 231 // Remember clear all below members in ClearRenderObject(). 232 RefPtr<Decoration> backDecoration_; 233 RefPtr<Decoration> frontDecoration_; 234 RefPtr<RenderImage> renderImage_; 235 RefPtr<Animator> controllerEnter_; 236 RefPtr<Animator> controllerExit_; 237 RefPtr<KeyframeAnimation<Color>> colorAnimationEnter_; 238 RefPtr<KeyframeAnimation<Color>> colorAnimationExit_; 239 RefPtr<KeyframeAnimation<float>> scaleAnimationEnter_; 240 RefPtr<KeyframeAnimation<float>> scaleAnimationExit_; 241 Color hoverColorBegin_ = Color::TRANSPARENT; 242 Color hoverColor_ = Color::TRANSPARENT; 243 float scale_ = 1.0f; 244 bool isZoom = false; 245 bool isHoveredBoard_ = false; 246 bool isHoveredScale_ = false; 247 bool isAccessibilityFocus_ = false; 248 bool needFocusBorder_ = false; 249 bool needPaintDebugBoundary_ = false; 250 251 private: 252 void UpdateBackDecoration(const RefPtr<Decoration>& newDecoration); 253 void UpdateFrontDecoration(const RefPtr<Decoration>& newDecoration); 254 void HandleRemoteMessage(const ClickInfo& clickInfo); 255 #if defined(PREVIEW) 256 void CalculateScale(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size); 257 void CalculateRotate(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size); 258 void CalculateTranslate(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size); 259 #endif 260 void HandleTouchEvent(bool isTouchDown); 261 262 void SetAccessibilityFocusImpl(); 263 void SetAccessibilityClickImpl(); 264 265 // 0 - low priority gesture, 1 - high priority gesture, 2 - parallel priority gesture 266 std::array<RefPtr<GestureRecognizer>, MAX_GESTURE_SIZE> recognizers_; 267 268 RefPtr<GestureRecognizer> onClick_; 269 RefPtr<GestureRecognizer> onLongPress_; 270 RefPtr<GestureRecognizer> parallelRecognizer_; 271 RefPtr<RawRecognizer> touchRecognizer_; 272 RefPtr<StateAttributes<BoxStateAttribute>> stateAttributeList_; 273 OnHoverCallback onHover_; 274 OnMouseCallback onMouse_; 275 RefPtr<Gesture> onLongPressId_; 276 TextDirection inspectorDirection_ { TextDirection::LTR }; 277 278 // Drag event 279 void PanOnActionStart(const GestureEvent& info) override; 280 void PanOnActionUpdate(const GestureEvent& info) override; 281 void PanOnActionEnd(const GestureEvent& info) override; 282 void PanOnActionCancel() override; 283 void SetSelectedIndex(const GestureEvent& info); 284 void SetInsertIndex(const RefPtr<DragDropEvent>& targetDragDropNode, const GestureEvent& info); 285 OnTouchEventCallback onTouchUpId_; 286 OnTouchEventCallback onTouchDownId_; 287 OnTouchEventCallback onTouchMoveId_; 288 size_t selectedIndex_ = DEFAULT_INDEX; 289 int32_t insertIndex_ = DEFAULT_INDEX_VALUE; 290 std::function<void(const std::shared_ptr<ClickInfo>&)> remoteMessageEvent_; 291 bool enableDragStart_ = true; 292 bool needMaterial_ = false; 293 }; // class RenderBox 294 } // namespace OHOS::Ace 295 296 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_RENDER_BOX_H 297