• 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_BOX_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_H
18 
19 #include "core/components/box/box_base_component.h"
20 #include "core/components/box/drag_drop_event.h"
21 #include "core/components/common/properties/animatable_color.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/common/properties/decoration.h"
24 #include "core/gestures/gesture_group.h"
25 #include "core/gestures/raw_recognizer.h"
26 
27 namespace OHOS::Ace {
28 
29 using OnHoverCallback = std::function<void(bool, HoverInfo& info)>;
30 using OnHoverMoveCallback = std::function<void(HoverInfo& info)>;
31 using OnMouseCallback = std::function<void(MouseInfo& info)>;
32 using OnNewDragFunc = std::function<void(const RefPtr<OHOS::Ace::DragEvent>&)>;
33 using OnPreDragFunc = std::function<void(const PreDragStatus)>;
34 
35 enum class BoxStateAttribute {
36     ASPECT_RATIO,
37     BORDER,
38     COLOR,
39     BORDER_COLOR,
40     BORDER_RADIUS,
41     BORDER_STYLE,
42     BORDER_WIDTH,
43     GRADIENT,
44     HEIGHT,
45     WIDTH,
46 };
47 
48 // A component can box others components.
49 class ACE_EXPORT BoxComponent : public BoxBaseComponent {
50     DECLARE_ACE_TYPE(BoxComponent, BoxBaseComponent);
51 
52 public:
53     RefPtr<Element> CreateElement() override;
54     RefPtr<RenderNode> CreateRenderNode() override;
55 
GetBackDecoration()56     RefPtr<Decoration> GetBackDecoration() const
57     {
58         return backDecoration_;
59     }
60 
GetFrontDecoration()61     RefPtr<Decoration> GetFrontDecoration() const
62     {
63         return frontDecoration_;
64     }
65 
GetColor()66     const Color& GetColor() const
67     {
68         if (backDecoration_) {
69             return backDecoration_->GetBackgroundColor();
70         }
71         return Color::TRANSPARENT;
72     }
73 
GetDecorationUpdateFlag()74     bool GetDecorationUpdateFlag() const
75     {
76         return decorationUpdateFlag_;
77     }
78 
GetMouseAnimationType()79     HoverAnimationType GetMouseAnimationType() const
80     {
81         return animationType_;
82     }
83 
SetBackDecoration(const RefPtr<Decoration> & decoration)84     void SetBackDecoration(const RefPtr<Decoration>& decoration)
85     {
86         backDecoration_ = decoration;
87         SetDecorationUpdateFlag(true);
88     }
89 
SetFrontDecoration(const RefPtr<Decoration> & decoration)90     void SetFrontDecoration(const RefPtr<Decoration>& decoration)
91     {
92         frontDecoration_ = decoration;
93         SetDecorationUpdateFlag(true);
94     }
95 
96     void SetColor(const Color& color, const AnimationOption& option = AnimationOption())
97     {
98         if (!backDecoration_) {
99             backDecoration_ = AceType::MakeRefPtr<Decoration>();
100         }
101         backDecoration_->SetBackgroundColor(color, option);
102     }
103 
SetColor(const AnimatableColor & color)104     void SetColor(const AnimatableColor& color)
105     {
106         if (!backDecoration_) {
107             backDecoration_ = AceType::MakeRefPtr<Decoration>();
108         }
109         backDecoration_->SetBackgroundColor(color);
110     }
111 
SetDecorationUpdateFlag(bool flag)112     void SetDecorationUpdateFlag(bool flag)
113     {
114         decorationUpdateFlag_ = flag;
115     }
116 
SetMouseAnimationType(HoverAnimationType animationType)117     void SetMouseAnimationType(HoverAnimationType animationType)
118     {
119         animationType_ = animationType;
120     }
121 
SetInspectorDirection(TextDirection direction)122     void SetInspectorDirection(TextDirection direction)
123     {
124         inspectorDirection_ = direction;
125     }
126 
GetInspectorDirection()127     TextDirection GetInspectorDirection() const
128     {
129         return inspectorDirection_;
130     }
131 
SetOnHoverId(const OnHoverCallback & onHoverId)132     void SetOnHoverId(const OnHoverCallback& onHoverId)
133     {
134         onHoverId_ = onHoverId;
135     }
136 
SetOnHoverMoveId(const OnHoverMoveCallback & onHoverMoveId)137     void SetOnHoverMoveId(const OnHoverMoveCallback& onHoverMoveId)
138     {
139         onHoverMoveId_ = onHoverMoveId;
140     }
141 
GetOnHoverId()142     OnHoverCallback GetOnHoverId() const
143     {
144         return onHoverId_;
145     }
146 
SetOnMouseId(const OnMouseCallback & onMouseId)147     void SetOnMouseId(const OnMouseCallback& onMouseId)
148     {
149         onMouseId_ = onMouseId;
150     }
151 
GetOnMouseId()152     OnMouseCallback GetOnMouseId() const
153     {
154         return onMouseId_;
155     }
156 
SetOnTouchMoveId(const OnTouchEventCallback & onTouchMoveId)157     void SetOnTouchMoveId(const OnTouchEventCallback& onTouchMoveId)
158     {
159         onTouchMoveId_ = onTouchMoveId;
160     }
161 
GetOnTouchMoveId()162     const OnTouchEventCallback& GetOnTouchMoveId() const
163     {
164         return onTouchMoveId_;
165     }
166 
SetOnTouchUpId(const OnTouchEventCallback & onTouchUpId)167     void SetOnTouchUpId(const OnTouchEventCallback& onTouchUpId)
168     {
169         onTouchUpId_ = onTouchUpId;
170     }
171 
GetOnTouchUpId()172     const OnTouchEventCallback& GetOnTouchUpId() const
173     {
174         return onTouchUpId_;
175     }
176 
SetOnTouchDownId(const OnTouchEventCallback & onTouchDownId)177     void SetOnTouchDownId(const OnTouchEventCallback& onTouchDownId)
178     {
179         onTouchDownId_ = onTouchDownId;
180     }
181 
GetOnTouchDownId()182     const OnTouchEventCallback& GetOnTouchDownId() const
183     {
184         return onTouchDownId_;
185     }
186 
GetOnClick()187     RefPtr<Gesture> GetOnClick() const
188     {
189         return onClickId_;
190     }
191 
SetOnClick(const RefPtr<Gesture> & onClickId)192     void SetOnClick(const RefPtr<Gesture>& onClickId)
193     {
194         onClickId_ = onClickId;
195     }
196 
AddGesture(GesturePriority priority,RefPtr<Gesture> gesture)197     void AddGesture(GesturePriority priority, RefPtr<Gesture> gesture)
198     {
199         gestures_[static_cast<int32_t>(priority)] = gesture;
200     }
201 
GetGestures()202     const std::array<RefPtr<Gesture>, 3>& GetGestures() const
203     {
204         return gestures_;
205     }
206 
GetOnDomDragEnter()207     const EventMarker& GetOnDomDragEnter() const
208     {
209         return onDomDragEnterId_;
210     }
211 
SetOnDomDragEnter(const EventMarker & value)212     void SetOnDomDragEnter(const EventMarker& value)
213     {
214         onDomDragEnterId_ = value;
215     }
216 
GetOnDomDragOver()217     const EventMarker& GetOnDomDragOver() const
218     {
219         return onDomDragOverId_;
220     }
221 
SetOnDomDragOver(const EventMarker & value)222     void SetOnDomDragOver(const EventMarker& value)
223     {
224         onDomDragOverId_ = value;
225     }
226 
GetOnDomDragLeave()227     const EventMarker& GetOnDomDragLeave() const
228     {
229         return onDomDragLeaveId_;
230     }
231 
SetOnDomDragLeave(const EventMarker & value)232     void SetOnDomDragLeave(const EventMarker& value)
233     {
234         onDomDragLeaveId_ = value;
235     }
236 
GetOnDomDragDrop()237     const EventMarker& GetOnDomDragDrop() const
238     {
239         return onDomDragDropId_;
240     }
241 
SetOnDomDragDrop(const EventMarker & value)242     void SetOnDomDragDrop(const EventMarker& value)
243     {
244         onDomDragDropId_ = value;
245     }
246 
SetGeometryTransitionId(const std::string & id)247     void SetGeometryTransitionId(const std::string& id)
248     {
249         geometryTransitionId_ = id;
250     }
251 
GetGeometryTransitionId()252     std::string GetGeometryTransitionId() const
253     {
254         return geometryTransitionId_;
255     }
256 
GetStateAttributes()257     RefPtr<StateAttributes<BoxStateAttribute>> GetStateAttributes()
258     {
259         if (stateAttributeList_ == nullptr) {
260             stateAttributeList_ = MakeRefPtr<StateAttributes<BoxStateAttribute>>();
261         }
262         return stateAttributeList_;
263     }
264 
HasStateAttributes()265     bool HasStateAttributes()
266     {
267         return stateAttributeList_ != nullptr;
268     }
269 
GetOnDragStartId()270     const OnDragFunc& GetOnDragStartId() const
271     {
272         return onDragStartId_;
273     }
274 
SetOnDragStartId(const OnDragFunc & onDragStartId)275     void SetOnDragStartId(const OnDragFunc& onDragStartId)
276     {
277         onDragStartId_ = onDragStartId;
278     }
279 
GetOnDragEnterId()280     const OnDropFunc& GetOnDragEnterId() const
281     {
282         return onDragEnterId_;
283     }
284 
SetOnDragEnterId(const OnDropFunc & onDragEnterId)285     void SetOnDragEnterId(const OnDropFunc& onDragEnterId)
286     {
287         onDragEnterId_ = onDragEnterId;
288     }
289 
GetOnDragMoveId()290     const OnDropFunc& GetOnDragMoveId() const
291     {
292         return onDragMoveId_;
293     }
294 
SetOnDragMoveId(const OnDropFunc & onDragMoveId)295     void SetOnDragMoveId(const OnDropFunc& onDragMoveId)
296     {
297         onDragMoveId_ = onDragMoveId;
298     }
299 
GetOnDragEndId()300     const OnNewDragFunc& GetOnDragEndId() const
301     {
302         return onDragEndId_;
303     }
304 
SetOnDragEndId(const OnNewDragFunc & onDragEndId)305     void SetOnDragEndId(const OnNewDragFunc& onDragEndId)
306     {
307         onDragEndId_ = onDragEndId;
308     }
309 
GetOnPreDragId()310     const OnPreDragFunc& GetOnPreDragId() const
311     {
312         return onPreDragId_;
313     }
314 
SetOnPreDragId(const OnPreDragFunc & onPreDragId)315     void SetOnPreDragId(const OnPreDragFunc& onPreDragId)
316     {
317         onPreDragId_ = onPreDragId;
318     }
319 
GetOnDragLeaveId()320     const OnDropFunc& GetOnDragLeaveId() const
321     {
322         return onDragLeaveId_;
323     }
324 
SetOnDragLeaveId(const OnDropFunc & onDragLeaveId)325     void SetOnDragLeaveId(const OnDropFunc& onDragLeaveId)
326     {
327         onDragLeaveId_ = onDragLeaveId;
328     }
329 
GetOnDropId()330     const OnDropFunc& GetOnDropId() const
331     {
332         return onDropId_;
333     }
334 
SetOnDropId(const OnDropFunc & onDropId)335     void SetOnDropId(const OnDropFunc& onDropId)
336     {
337         onDropId_ = onDropId;
338     }
339 
GetRemoteMessageEvent()340     const EventMarker& GetRemoteMessageEvent() const
341     {
342         return remoteMessageId_;
343     }
344 
SetRemoteMessageEvent(const EventMarker & eventId)345     void SetRemoteMessageEvent(const EventMarker& eventId)
346     {
347         remoteMessageId_ = eventId;
348     }
349 
GetOnLongPress()350     RefPtr<Gesture> GetOnLongPress() const
351     {
352         return onLongPressId_;
353     }
354 
SetOnLongPress(const RefPtr<Gesture> & onLongPressId)355     void SetOnLongPress(const RefPtr<Gesture>& onLongPressId)
356     {
357         onLongPressId_ = onLongPressId;
358     }
359 
HasBackgroundColor()360     bool HasBackgroundColor() const
361     {
362         return hasBackgroundColor_;
363     }
364 
SetHasBackgroundColor(bool hasBackgroundColor)365     void SetHasBackgroundColor(bool hasBackgroundColor)
366     {
367         hasBackgroundColor_ = hasBackgroundColor;
368     }
369 
SetEnableDebugBoundary(bool enableDebugBoundary)370     void SetEnableDebugBoundary(bool enableDebugBoundary)
371     {
372         enableDebugBoundary_ = enableDebugBoundary;
373     }
374 
GetEnableDebugBoundary()375     bool GetEnableDebugBoundary()
376     {
377         return enableDebugBoundary_;
378     }
379 
SetEnableDragStart(bool enableDragStart)380     void SetEnableDragStart(bool enableDragStart)
381     {
382         enableDragStart_ = enableDragStart;
383     }
384 
GetEnableDragStart()385     bool GetEnableDragStart() const
386     {
387         return enableDragStart_;
388     }
389 
NeedMaterial(bool needMaterial)390     void NeedMaterial(bool needMaterial)
391     {
392         needMaterial_ = needMaterial;
393     }
394 
GetNeedMaterial()395     bool GetNeedMaterial() const
396     {
397         return needMaterial_;
398     }
399 private:
400     RefPtr<Decoration> backDecoration_;
401     RefPtr<Decoration> frontDecoration_;
402     bool decorationUpdateFlag_ = false;
403     HoverAnimationType animationType_ = HoverAnimationType::UNKNOWN;
404     OnHoverCallback onHoverId_;
405     OnHoverMoveCallback onHoverMoveId_;
406     OnMouseCallback onMouseId_;
407     OnTouchEventCallback onTouchMoveId_;
408     OnTouchEventCallback onTouchUpId_;
409     OnTouchEventCallback onTouchDownId_;
410     RefPtr<Gesture> onClickId_;
411     RefPtr<Gesture> onLongPressId_;
412     std::array<RefPtr<Gesture>, 3> gestures_;
413     EventMarker onDomDragEnterId_;
414     EventMarker onDomDragOverId_;
415     EventMarker onDomDragLeaveId_;
416     EventMarker onDomDragDropId_;
417     std::string geometryTransitionId_;
418     TextDirection inspectorDirection_ { TextDirection::LTR };
419     RefPtr<StateAttributes<BoxStateAttribute>> stateAttributeList_ = nullptr;
420     EventMarker remoteMessageId_;
421     bool hasBackgroundColor_;
422     bool enableDebugBoundary_ = false;
423     bool enableDragStart_ = true;
424     OnDragFunc onDragStartId_;
425     OnDropFunc onDragEnterId_;
426     OnDropFunc onDragMoveId_;
427     OnDropFunc onDragLeaveId_;
428     OnDropFunc onDropId_;
429     OnNewDragFunc onDragEndId_;
430     OnPreDragFunc onPreDragId_;
431     bool needMaterial_ = false;
432 };
433 
434 } // namespace OHOS::Ace
435 
436 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_H
437