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_NG_GESTURES_GESTURE_INFO_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_GESTURE_INFO_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/utils/macros.h"
24 #include "core/components_ng/event/gesture_info.h"
25 #include "core/components_ng/property/border_property.h"
26 #include "core/gestures/gesture_event.h"
27 #include "core/gestures/gesture_info.h"
28 #include "core/gestures/gesture_type.h"
29 #include "core/gestures/velocity.h"
30 #include "core/gestures/velocity_tracker.h"
31 #include "core/components/common/properties/common_decoration.h"
32 #include "core/components/common/properties/shadow.h"
33 
34 namespace OHOS::Ace::NG {
35 
36 class NGGestureRecognizer;
37 
38 enum class DragPreviewMode : int32_t {
39     AUTO = 1,
40     DISABLE_SCALE = 2,
41     ENABLE_DEFAULT_SHADOW = 3,
42     ENABLE_DEFAULT_RADIUS = 4,
43 };
44 
45 struct BlurBackGroundInfo {
46     EffectOption  backGroundEffect;
ToJsonValueBlurBackGroundInfo47     void ToJsonValue(const std::unique_ptr<JsonValue>& json)
48     {
49         json->Put("blur_radius", backGroundEffect.radius.Value());
50         json->Put("blur_staturation", backGroundEffect.saturation);
51         json->Put("blur_brightness", backGroundEffect.brightness);
52         int32_t blurColor = static_cast<int32_t> (backGroundEffect.color.GetValue());
53         json->Put("blur_color", blurColor);
54         json->Put("blur_style", static_cast<int32_t>(backGroundEffect.adaptiveColor));
55         constexpr int32_t GRAYSCALE_MAX_VALUE = 2;
56         if (backGroundEffect.blurOption.grayscale.size() >= GRAYSCALE_MAX_VALUE) {
57             json->Put("blur_coef1", backGroundEffect.blurOption.grayscale[0]);
58             json->Put("blur_coef2", backGroundEffect.blurOption.grayscale[1]);
59         } else {
60             json->Put("blur_coef1", 0);
61             json->Put("blur_coef2", 0);
62         }
63     }
64 };
65 
66 struct OptionsAfterApplied {
67     double opacity { 0.0 };
68     std::optional<Shadow> shadow;
69     std::string shadowPath;
70     std::optional<BorderRadiusProperty> borderRadius;
71     BlurBackGroundInfo blurbgEffect;
72 };
73 
74 struct DragPreviewOption {
75     bool isScaleEnabled = true;
76     bool defaultAnimationBeforeLifting = false;
77     bool isMultiSelectionEnabled = false;
78     bool isNumber = false;
79     bool isDefaultShadowEnabled = false;
80     bool isDefaultRadiusEnabled = false;
81     bool isDragPreviewEnabled = true;
82     union {
83         int32_t badgeNumber;
84         bool isShowBadge;
85     };
GetCustomerBadgeNumberDragPreviewOption86     std::optional<int32_t> GetCustomerBadgeNumber()
87     {
88         if (isNumber) {
89             return badgeNumber > 1 ? badgeNumber : 1;
90         } else if (!isShowBadge) {
91             return 1;
92         }
93         return std::nullopt;
94     }
95     std::function<void(WeakPtr<NG::FrameNode>)> onApply;
96     OptionsAfterApplied options; // options from modifier after applied
ResetDragPreviewModeDragPreviewOption97     void ResetDragPreviewMode()
98     {
99         isScaleEnabled = true;
100         isDefaultShadowEnabled = false;
101         isDefaultRadiusEnabled = false;
102     }
103 };
104 
105 class ACE_EXPORT Gesture : public virtual AceType {
106     DECLARE_ACE_TYPE(Gesture, AceType);
107 
108 public:
109     Gesture() = default;
Gesture(int32_t fingers)110     explicit Gesture(int32_t fingers) : fingers_(fingers) {}
111     ~Gesture() override = default;
112 
SetOnActionId(const GestureEventFunc & onActionId)113     void SetOnActionId(const GestureEventFunc& onActionId)
114     {
115         onActionId_ = std::make_shared<GestureEventFunc>(onActionId);
116     }
GetOnActionId()117     std::shared_ptr<GestureEventFunc> GetOnActionId()
118     {
119         return onActionId_;
120     }
SetOnActionStartId(const GestureEventFunc & onActionStartId)121     void SetOnActionStartId(const GestureEventFunc& onActionStartId)
122     {
123         onActionStartId_ = std::make_unique<GestureEventFunc>(onActionStartId);
124     }
SetOnActionUpdateId(const GestureEventFunc & onActionUpdateId)125     void SetOnActionUpdateId(const GestureEventFunc& onActionUpdateId)
126     {
127         onActionUpdateId_ = std::make_unique<GestureEventFunc>(onActionUpdateId);
128     }
SetOnActionEndId(const GestureEventFunc & onActionEndId)129     void SetOnActionEndId(const GestureEventFunc& onActionEndId)
130     {
131         onActionEndId_ = std::make_unique<GestureEventFunc>(onActionEndId);
132     }
SetOnActionCancelId(const GestureEventNoParameter & onActionCancelId)133     void SetOnActionCancelId(const GestureEventNoParameter& onActionCancelId)
134     {
135         onActionCancelId_ = std::make_unique<GestureEventNoParameter>(onActionCancelId);
136     }
SetPriority(GesturePriority priority)137     void SetPriority(GesturePriority priority)
138     {
139         priority_ = priority;
140     }
SetGestureMask(GestureMask gestureMask)141     void SetGestureMask(GestureMask gestureMask)
142     {
143         gestureMask_ = gestureMask;
144     }
145 
GetPriority()146     GesturePriority GetPriority() const
147     {
148         return priority_;
149     }
150 
GetGestureMask()151     GestureMask GetGestureMask() const
152     {
153         return gestureMask_;
154     }
155 
GetFingers()156     int32_t GetFingers() const
157     {
158         return fingers_;
159     }
160 
SetTag(std::string tag)161     void SetTag(std::string tag)
162     {
163         if (gestureInfo_) {
164             gestureInfo_->SetTag(std::move(tag));
165         } else {
166             gestureInfo_ = MakeRefPtr<GestureInfo>(tag);
167         }
168     }
169 
SetAllowedTypes(std::set<SourceTool> allowedTypes)170     void SetAllowedTypes(std::set<SourceTool> allowedTypes)
171     {
172         if (gestureInfo_) {
173             gestureInfo_->SetAllowedTypes(std::move(allowedTypes));
174         } else {
175             gestureInfo_ = MakeRefPtr<GestureInfo>(allowedTypes);
176         }
177     }
178 
GetTag()179     std::optional<std::string> GetTag()
180     {
181         if (gestureInfo_) {
182             return gestureInfo_->GetTag();
183         }
184         return std::nullopt;
185     }
186 
GetAllowedTypes()187     std::set<SourceTool> GetAllowedTypes()
188     {
189         if (gestureInfo_) {
190             return gestureInfo_->GetAllowedTypes();
191         }
192         return {};
193     }
194 
SizeofMe()195     virtual int32_t SizeofMe()
196     {
197         return 0;
198     }
199 
Serialize(char * p)200     virtual int32_t Serialize(char* p)
201     {
202         return 0;
203     }
204 
Deserialize(const char * p)205     virtual int32_t Deserialize(const char* p)
206     {
207         return 0;
208     }
209 
SetHeader(char * buff,GestureType type,int32_t len)210     char* SetHeader(char* buff, GestureType type, int32_t len)
211     {
212         if (buff == nullptr) {
213             return nullptr;
214         }
215         *reinterpret_cast<GestureType*>(buff) = type;
216         buff += sizeof(GestureType);
217         *reinterpret_cast<int32_t*>(buff) = len;
218         buff += sizeof(int32_t);
219         return buff;
220     }
221 
SetUserData(void * userData)222     void SetUserData(void* userData)
223     {
224         userData_ = userData;
225     }
226 
227     virtual RefPtr<NGGestureRecognizer> CreateRecognizer() = 0;
228 
SetDisposeTag(bool tag)229     void SetDisposeTag(bool tag)
230     {
231         if (gestureInfo_) {
232             gestureInfo_->SetDisposeTag(tag);
233         }
234     }
235 
236 protected:
237     int32_t fingers_ = 1;
238     GesturePriority priority_ = GesturePriority::Low;
239     GestureMask gestureMask_ = GestureMask::Normal;
240     std::shared_ptr<GestureEventFunc> onActionId_;
241     std::unique_ptr<GestureEventFunc> onActionStartId_;
242     std::unique_ptr<GestureEventFunc> onActionUpdateId_;
243     std::unique_ptr<GestureEventFunc> onActionEndId_;
244     std::unique_ptr<GestureEventNoParameter> onActionCancelId_;
245     RefPtr<GestureInfo> gestureInfo_;
246     void* userData_ = nullptr;
247 };
248 } // namespace OHOS::Ace::NG
249 
250 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_GESTURE_INFO_H
251