• 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 
33 namespace OHOS::Ace {
34 
35 using UpdateBuilderFunc = std::function<void(const Dimension&, const Dimension&)>;
36 constexpr int32_t MAX_GESTURE_SIZE = 3;
37 
38 class ACE_EXPORT RenderBox : public RenderBoxBase {
39     DECLARE_ACE_TYPE(RenderBox, RenderBoxBase);
40 
41 public:
42     static RefPtr<RenderNode> Create();
43     void Update(const RefPtr<Component>& component) override;
44     void OnPaintFinish() override;
45 
46     void HandleAccessibilityFocusEvent(bool isAccessibilityFocus);
47 
OnAttachContext()48     void OnAttachContext() override
49     {
50         RenderBoxBase::OnAttachContext();
51         if (!backDecoration_) {
52             backDecoration_ = AceType::MakeRefPtr<Decoration>();
53         }
54         backDecoration_->SetContextAndCallback(context_, [weak = WeakClaim(this)] {
55             auto renderBox = weak.Upgrade();
56             if (renderBox) {
57                 renderBox->OnAnimationCallback();
58             }
59         });
60         if (!frontDecoration_) {
61             frontDecoration_ = AceType::MakeRefPtr<Decoration>();
62         }
63         frontDecoration_->SetContextAndCallback(context_, [weak = WeakClaim(this)] {
64             auto renderBox = weak.Upgrade();
65             if (renderBox) {
66                 renderBox->OnAnimationCallback();
67             }
68         });
69     }
70 
71     void OnStatusStyleChanged(VisualState state) override;
72     void UpdateStyleFromRenderNode(PropertyAnimatableType type) override;
73 
GetColor()74     const Color& GetColor() const override
75     {
76         if (backDecoration_) {
77             return backDecoration_->GetBackgroundColor();
78         }
79         return Color::TRANSPARENT;
80     }
81 
GetInspectorDirection()82     TextDirection GetInspectorDirection() const
83     {
84         return inspectorDirection_;
85     }
86 
GetBackDecoration()87     RefPtr<Decoration> GetBackDecoration()
88     {
89         if (!backDecoration_) {
90             backDecoration_ = AceType::MakeRefPtr<Decoration>();
91         }
92         return backDecoration_;
93     }
94 
GetFrontDecoration()95     RefPtr<Decoration> GetFrontDecoration() const
96     {
97         return frontDecoration_;
98     }
99 
SetColor(const Color & color,bool isBackground)100     virtual void SetColor(const Color& color, bool isBackground) // add for animation
101     {
102         // create decoration automatically while user had not defined
103         if (isBackground) {
104             if (!backDecoration_) {
105                 backDecoration_ = AceType::MakeRefPtr<Decoration>();
106                 LOGD("[BOX][Dep:%{public}d][LAYOUT]Add backDecoration automatically.", this->GetDepth());
107             }
108             backDecoration_->SetBackgroundColor(color);
109         } else {
110             if (!frontDecoration_) {
111                 frontDecoration_ = AceType::MakeRefPtr<Decoration>();
112                 LOGD("[BOX][Dep:%{public}d][LAYOUT]Add frontDecoration automatically.", this->GetDepth());
113             }
114             frontDecoration_->SetBackgroundColor(color);
115         }
116         MarkNeedRender();
117     }
118 
SetBackDecoration(const RefPtr<Decoration> & decoration)119     void SetBackDecoration(const RefPtr<Decoration>& decoration) // add for list, do not use to update background image
120     {
121         backDecoration_ = decoration;
122         MarkNeedRender();
123     }
124 
SetFrontDecoration(const RefPtr<Decoration> & decoration)125     void SetFrontDecoration(const RefPtr<Decoration>& decoration) // add for list
126     {
127         frontDecoration_ = decoration;
128         MarkNeedRender();
129     }
130 
131     void OnMouseHoverEnterAnimation() override;
132     void OnMouseHoverExitAnimation() override;
133     void StopMouseHoverAnimation() override;
134 
135     // add for animation
136     virtual void SetBackgroundSize(const BackgroundImageSize& size);
137     BackgroundImagePosition GetBackgroundPosition() const;
138     virtual void SetBackgroundPosition(const BackgroundImagePosition& position);
139     BackgroundImageSize GetBackgroundSize() const;
140     virtual void SetShadow(const Shadow& shadow);
141     Shadow GetShadow() const;
142     void SetGrayScale(double scale);
143     double GetGrayScale(void) const;
144     void SetBrightness(double ness);
145     double GetBrightness(void) const;
146     void SetContrast(double trast);
147     double GetContrast(void) const;
148     void SetColorBlend(const Color& color);
149     Color GetColorBlend(void) const;
150     void SetSaturate(double rate);
151     double GetSaturate(void) const;
152     void SetSepia(double pia);
153     double GetSepia(void) const;
154     void SetInvert(double invert);
155     double GetInvert(void) const;
156     void SetHueRotate(float deg);
157     float GetHueRotate(void) const;
158     virtual void SetBorderWidth(double width, const BorderEdgeHelper& helper);
159     double GetBorderWidth(const BorderEdgeHelper& helper) const;
160     virtual void SetBorderColor(const Color& color, const BorderEdgeHelper& helper);
161     Color GetBorderColor(const BorderEdgeHelper& helper) const;
162     virtual void SetBorderStyle(BorderStyle borderStyle, const BorderEdgeHelper& helper);
163     BorderStyle GetBorderStyle(const BorderEdgeHelper& helper) const;
164     virtual void SetBorderRadius(double radius, const BorderRadiusHelper& helper);
165     double GetBorderRadius(const BorderRadiusHelper& helper) const;
166     virtual void SetBlurRadius(const AnimatableDimension& radius);
167     AnimatableDimension GetBlurRadius() const;
168     virtual void SetBackdropRadius(const AnimatableDimension& radius);
169     AnimatableDimension GetBackdropRadius() const;
170     virtual void SetWindowBlurProgress(double progress);
171     double GetWindowBlurProgress() const;
172     void CreateFloatAnimation(RefPtr<KeyframeAnimation<float>>& floatAnimation, float beginValue, float endValue);
173 
174     Size GetBorderSize() const override;
175     ColorPropertyAnimatable::SetterMap GetColorPropertySetterMap() override;
176     ColorPropertyAnimatable::GetterMap GetColorPropertyGetterMap() override;
177     Offset GetGlobalOffsetExternal() const override;
178     Offset GetGlobalOffset() const override;
179     void MouseHoverEnterTest() override;
180     void MouseHoverExitTest() override;
181     void AnimateMouseHoverEnter() override;
182     void AnimateMouseHoverExit() override;
183     bool HandleMouseEvent(const MouseEvent& event) override;
184     void HandleMouseHoverEvent(MouseState mouseState) override;
185     WeakPtr<RenderNode> CheckHoverNode() override;
186 
187     bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict,
188         TouchTestResult& result) override;
189 
190     void OnTouchTestHit(
191         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
192 
193     void OnTouchTestHierarchy(const Offset& coordinateOffset, const TouchRestrict& touchRestrict,
194         const std::vector<RefPtr<GestureRecognizer>>& innerRecognizers, TouchTestResult& result);
195 
GetOnDragEnter()196     const OnDropFunc& GetOnDragEnter() const
197     {
198         return onDragEnter_;
199     }
200 
GetOnDragMove()201     const OnDropFunc& GetOnDragMove() const
202     {
203         return onDragMove_;
204     }
205 
GetOnDragLeave()206     const OnDropFunc& GetOnDragLeave() const
207     {
208         return onDragLeave_;
209     }
210 
GetOnDrop()211     const OnDropFunc& GetOnDrop() const
212     {
213         return onDrop_;
214     }
215 
216     void AddRecognizerToResult(
217         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result);
218 
SetLocalPoint(const Point & localPoint)219     void SetLocalPoint(const Point& localPoint)
220     {
221         localPoint_ = localPoint;
222     }
223 
GetLocalPoint()224     const Point& GetLocalPoint() const
225     {
226         return localPoint_;
227     }
228 
GetUpdateBuilderFuncId()229     const UpdateBuilderFunc& GetUpdateBuilderFuncId() const
230     {
231         return updateBuilder_;
232     }
233 
SetUpdateBuilderFuncId(const UpdateBuilderFunc & updateBuilder)234     void SetUpdateBuilderFuncId(const UpdateBuilderFunc& updateBuilder)
235     {
236         updateBuilder_ = updateBuilder;
237     }
238 
SetPreTargetRenderBox(const RefPtr<RenderBox> & preTargetRenderBox)239     void SetPreTargetRenderBox(const RefPtr<RenderBox>& preTargetRenderBox)
240     {
241         preTargetRenderBox_ = preTargetRenderBox;
242     }
243 
GetPreTargetRenderBox()244     const RefPtr<RenderBox> GetPreTargetRenderBox() const
245     {
246         return preTargetRenderBox_;
247     }
248 
GetOnMouseId()249     const OnMouseCallback& GetOnMouseId() const
250     {
251         return onMouse_;
252     }
253 
GetOnLongPress()254     RefPtr<Gesture> GetOnLongPress() const
255     {
256         return onLongPressId_;
257     }
258 
259     DragItemInfo GenerateDragItemInfo(const RefPtr<PipelineContext>& context, const GestureEvent& info);
260     void AddDataToClipboard(const RefPtr<PipelineContext>& context, const std::string& extraInfo);
261     RefPtr<RenderBox> FindTargetRenderBox(const RefPtr<PipelineContext> context, const GestureEvent& info);
262 
263     void ResetController(RefPtr<Animator>& controller);
264     void CreateColorAnimation(
265         RefPtr<KeyframeAnimation<Color>>& colorAnimation, const Color& beginValue, const Color& endValue);
266 
267 protected:
268     void ClearRenderObject() override;
269 
270     Offset GetBorderOffset() const override;
271     Radius GetBorderRadius() const override;
272     void UpdateGestureRecognizer(const std::vector<RefPtr<Gesture>>& gestures);
273     void UpdateGestureRecognizerHierarchy(const std::vector<std::pair<GesturePriority,
274             std::vector<RefPtr<Gesture>>>>& gestures);
275     bool ExistGestureRecognizer();
276 
277     // Remember clear all below members in ClearRenderObject().
278     RefPtr<Decoration> backDecoration_;
279     RefPtr<Decoration> frontDecoration_;
280     RefPtr<RenderImage> renderImage_;
281     RefPtr<Animator> controllerEnter_;
282     RefPtr<Animator> controllerExit_;
283     RefPtr<KeyframeAnimation<Color>> colorAnimationEnter_;
284     RefPtr<KeyframeAnimation<Color>> colorAnimationExit_;
285     RefPtr<KeyframeAnimation<float>> scaleAnimationEnter_;
286     RefPtr<KeyframeAnimation<float>> scaleAnimationExit_;
287     HoverAnimationType animationType_ = HoverAnimationType::NONE;
288     Color hoverColorBegin_ = Color::TRANSPARENT;
289     Color hoverColor_ = Color::TRANSPARENT;
290     float scale_ = 1.0f;
291     bool isZoom = false;
292     bool isHoveredBoard = false;
293     bool isHoveredScale = false;
294     bool isAccessibilityFocus_ = false;
295 
296 private:
297     void UpdateBackDecoration(const RefPtr<Decoration>& newDecoration);
298     void UpdateFrontDecoration(const RefPtr<Decoration>& newDecoration);
299     void HandleRemoteMessage(const ClickInfo& clickInfo);
300 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
301     void CalculateScale(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size);
302     void CalculateRotate(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size);
303     void CalculateTranslate(RefPtr<AccessibilityNode> node, Offset& globalOffset, Size& size);
304 #endif
305     void HandleTouchEvent(bool isTouchDown);
306 
307     void SetAccessibilityFocusImpl();
308     void SendAccessibilityEvent(const std::string& eventType);
309 
310     std::vector<RefPtr<GestureRecognizer>> recognizers_;
311     std::vector<std::pair<GesturePriority, std::vector<RefPtr<GestureRecognizer>>>> recognizerHierarchy_;
312 
313     RefPtr<GestureRecognizer> onClick_;
314     RefPtr<GestureRecognizer> onLongPress_;
315     RefPtr<RawRecognizer> touchRecognizer_;
316     RefPtr<StateAttributes<BoxStateAttribute>> stateAttributeList_;
317     OnHoverCallback onHover_;
318     OnMouseCallback onMouse_;
319     RefPtr<Gesture> onLongPressId_;
320     TextDirection inspectorDirection_ { TextDirection::LTR };
321 
322     // Drag event
323     void CreateDragDropRecognizer();
324     void PanOnActionStart(const GestureEvent& info);
325     void PanOnActionUpdate(const GestureEvent& info);
326     void PanOnActionEnd(const GestureEvent& info);
327     void SetSelectedIndex(const GestureEvent& info);
328     void SetInsertIndex(const RefPtr<RenderBox>& targetRenderBox, const GestureEvent& info);
329     RefPtr<GestureRecognizer> dragDropGesture_;
330     OnDragFunc onDragStart_;
331     OnDropFunc onDragEnter_;
332     OnDropFunc onDragMove_;
333     OnDropFunc onDragLeave_;
334     OnDropFunc onDrop_;
335     Point localPoint_;
336     UpdateBuilderFunc updateBuilder_;
337     RefPtr<RenderBox> preTargetRenderBox_;
338     RefPtr<RenderBox> initialRenderBox_;
339     Size selectedItemSize_;
340     size_t selectedIndex_ = DEFAULT_INDEX;
341     size_t insertIndex_ = DEFAULT_INDEX;
342     std::function<void(const std::shared_ptr<ClickInfo>&)> remoteMessageEvent_;
343 
344     RefPtr<DragWindow> dragWindow_;
345     bool isDragRenderBox_ = false;
346     bool hasDragItem_ = false;
347     bool enableDragStart_ = true;
348 }; // class RenderBox
349 } // namespace OHOS::Ace
350 
351 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_RENDER_BOX_H
352