• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 LOGD("[BOX][Dep:%{public}d][LAYOUT]Add backDecoration automatically.", this->GetDepth());
114             }
115             backDecoration_->SetBackgroundColor(color);
116         } else {
117             if (!frontDecoration_) {
118                 frontDecoration_ = AceType::MakeRefPtr<Decoration>();
119                 LOGD("[BOX][Dep:%{public}d][LAYOUT]Add frontDecoration automatically.", this->GetDepth());
120             }
121             frontDecoration_->SetBackgroundColor(color);
122         }
123         MarkNeedRender();
124     }
125 
SetBackDecoration(const RefPtr<Decoration> & decoration)126     void SetBackDecoration(const RefPtr<Decoration>& decoration) // add for list, do not use to update background image
127     {
128         backDecoration_ = decoration;
129         MarkNeedRender();
130     }
131 
SetFrontDecoration(const RefPtr<Decoration> & decoration)132     void SetFrontDecoration(const RefPtr<Decoration>& decoration) // add for list
133     {
134         frontDecoration_ = decoration;
135         MarkNeedRender();
136     }
137 
NeedMaterial(bool needMaterial)138     void NeedMaterial(bool needMaterial)
139     {
140         needMaterial_ = needMaterial;
141     }
142 
GetNeedMaterial()143     bool GetNeedMaterial() const
144     {
145         return needMaterial_;
146     }
147     void OnMouseHoverEnterAnimation() override;
148     void OnMouseHoverExitAnimation() override;
149     void StopMouseHoverAnimation() override;
150 
151     // add for animation
152     virtual void SetBackgroundSize(const BackgroundImageSize& size);
153     BackgroundImagePosition GetBackgroundPosition() const;
154     virtual void SetBackgroundPosition(const BackgroundImagePosition& position);
155     BackgroundImageSize GetBackgroundSize() const;
156     virtual void SetShadow(const Shadow& shadow);
157     Shadow GetShadow() const;
158     void SetGrayScale(double scale);
159     double GetGrayScale(void) const;
160     void SetBrightness(double ness);
161     double GetBrightness(void) const;
162     void SetContrast(double trast);
163     double GetContrast(void) const;
164     void SetColorBlend(const Color& color);
165     Color GetColorBlend(void) const;
166     void SetSaturate(double rate);
167     double GetSaturate(void) const;
168     void SetSepia(double pia);
169     double GetSepia(void) const;
170     void SetInvert(double invert);
171     double GetInvert(void) const;
172     void SetHueRotate(float deg);
173     float GetHueRotate(void) const;
174     virtual void SetBorderWidth(double width, const BorderEdgeHelper& helper);
175     double GetBorderWidth(const BorderEdgeHelper& helper) const;
176     virtual void SetBorderColor(const Color& color, const BorderEdgeHelper& helper);
177     Color GetBorderColor(const BorderEdgeHelper& helper) const;
178     virtual void SetBorderStyle(BorderStyle borderStyle, const BorderEdgeHelper& helper);
179     BorderStyle GetBorderStyle(const BorderEdgeHelper& helper) const;
180     virtual void SetBorderRadius(double radius, const BorderRadiusHelper& helper);
181     double GetBorderRadius(const BorderRadiusHelper& helper) const;
182     virtual void SetBlurRadius(const AnimatableDimension& radius);
183     AnimatableDimension GetBlurRadius() const;
184     virtual void SetBackdropRadius(const AnimatableDimension& radius);
185     AnimatableDimension GetBackdropRadius() const;
186     virtual void SetWindowBlurProgress(double progress);
187     double GetWindowBlurProgress() const;
188     void CreateFloatAnimation(RefPtr<KeyframeAnimation<float>>& floatAnimation, float beginValue, float endValue);
189 
190     Size GetBorderSize() const override;
191     ColorPropertyAnimatable::SetterMap GetColorPropertySetterMap() override;
192     ColorPropertyAnimatable::GetterMap GetColorPropertyGetterMap() override;
193     Offset GetGlobalOffsetExternal() const override;
194     void MouseHoverEnterTest() override;
195     void MouseHoverExitTest() override;
196     void AnimateMouseHoverEnter() override;
197     void AnimateMouseHoverExit() override;
198     bool HandleMouseEvent(const MouseEvent& event) override;
199     void HandleMouseHoverEvent(MouseState mouseState) override;
200 
201     void OnTouchTestHit(
202         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
203 
204     void AddRecognizerToResult(
205         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result);
206 
GetOnMouseId()207     const OnMouseCallback& GetOnMouseId() const
208     {
209         return onMouse_;
210     }
211 
GetOnLongPress()212     RefPtr<Gesture> GetOnLongPress() const
213     {
214         return onLongPressId_;
215     }
216 
217     DragItemInfo GenerateDragItemInfo(const RefPtr<PipelineContext>& context, const GestureEvent& info) override;
218     void AddDataToClipboard(const RefPtr<PipelineContext>& context, const std::string& extraInfo,
219         const std::string& selectedText, const std::string& imageSrc) override;
220 
221     void ResetController(RefPtr<Animator>& controller);
222     void CreateColorAnimation(
223         RefPtr<KeyframeAnimation<Color>>& colorAnimation, const Color& beginValue, const Color& endValue);
224 
225 protected:
226     void ClearRenderObject() override;
227 
228     Offset GetBorderOffset() const override;
229     Radius GetBorderRadius() const override;
230     void UpdateGestureRecognizer(const std::array<RefPtr<Gesture>, MAX_GESTURE_SIZE>& gestures);
231     bool ExistGestureRecognizer();
232 
233     // Remember clear all below members in ClearRenderObject().
234     RefPtr<Decoration> backDecoration_;
235     RefPtr<Decoration> frontDecoration_;
236     RefPtr<RenderImage> renderImage_;
237     RefPtr<Animator> controllerEnter_;
238     RefPtr<Animator> controllerExit_;
239     RefPtr<KeyframeAnimation<Color>> colorAnimationEnter_;
240     RefPtr<KeyframeAnimation<Color>> colorAnimationExit_;
241     RefPtr<KeyframeAnimation<float>> scaleAnimationEnter_;
242     RefPtr<KeyframeAnimation<float>> scaleAnimationExit_;
243     Color hoverColorBegin_ = Color::TRANSPARENT;
244     Color hoverColor_ = Color::TRANSPARENT;
245     float scale_ = 1.0f;
246     bool isZoom = false;
247     bool isHoveredBoard_ = false;
248     bool isHoveredScale_ = false;
249     bool isAccessibilityFocus_ = false;
250     bool needFocusBorder_ = false;
251     bool needPaintDebugBoundary_ = false;
252 
253 private:
254     void UpdateBackDecoration(const RefPtr<Decoration>& newDecoration);
255     void UpdateFrontDecoration(const RefPtr<Decoration>& newDecoration);
256     void HandleRemoteMessage(const ClickInfo& clickInfo);
257 #if defined(PREVIEW)
258     void CalculateScale(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size);
259     void CalculateRotate(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size);
260     void CalculateTranslate(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size);
261 #endif
262     void HandleTouchEvent(bool isTouchDown);
263 
264     void SetAccessibilityFocusImpl();
265     void SetAccessibilityClickImpl();
266 
267     // 0 - low priority gesture, 1 - high priority gesture, 2 - parallel priority gesture
268     std::array<RefPtr<GestureRecognizer>, MAX_GESTURE_SIZE> recognizers_;
269 
270     RefPtr<GestureRecognizer> onClick_;
271     RefPtr<GestureRecognizer> onLongPress_;
272     RefPtr<GestureRecognizer> parallelRecognizer_;
273     RefPtr<RawRecognizer> touchRecognizer_;
274     RefPtr<StateAttributes<BoxStateAttribute>> stateAttributeList_;
275     OnHoverCallback onHover_;
276     OnMouseCallback onMouse_;
277     RefPtr<Gesture> onLongPressId_;
278     TextDirection inspectorDirection_ { TextDirection::LTR };
279 
280     // Drag event
281     void PanOnActionStart(const GestureEvent& info) override;
282     void PanOnActionUpdate(const GestureEvent& info) override;
283     void PanOnActionEnd(const GestureEvent& info) override;
284     void PanOnActionCancel() override;
285     void SetSelectedIndex(const GestureEvent& info);
286     void SetInsertIndex(const RefPtr<DragDropEvent>& targetDragDropNode, const GestureEvent& info);
287     OnTouchEventCallback onTouchUpId_;
288     OnTouchEventCallback onTouchDownId_;
289     OnTouchEventCallback onTouchMoveId_;
290     size_t selectedIndex_ = DEFAULT_INDEX;
291     int32_t insertIndex_ = DEFAULT_INDEX_VALUE;
292     std::function<void(const std::shared_ptr<ClickInfo>&)> remoteMessageEvent_;
293     bool enableDragStart_ = true;
294     bool needMaterial_ = false;
295 }; // class RenderBox
296 } // namespace OHOS::Ace
297 
298 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_RENDER_BOX_H
299