1 /*
2  * Copyright (c) 2021-2023 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_COMMON_ACE_VIEW_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_ACE_VIEW_H
18 
19 #include <chrono>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/resource/asset_manager.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/common/container.h"
25 #include "core/common/draw_delegate.h"
26 #include "core/common/platform_res_register.h"
27 #include "core/common/platform_window.h"
28 
29 namespace OHOS::Rosen {
30 class RSTransaction;
31 }
32 
33 namespace OHOS::Ace {
34 
35 class AceView : public AceType {
36     DECLARE_ACE_TYPE(AceView, AceType);
37 
38 public:
39     enum class ViewType : int32_t {
40         SURFACE_VIEW = 0,
41         NATIVE_VIEW,
42         AGP_COMPONENT,
43     };
44 
45     AceView() = default;
46     virtual ~AceView() = default;
47 
QueryIfWindowInScreen()48     virtual bool QueryIfWindowInScreen()
49     {
50         return true;
51     }
RegisterScreenBroadcast()52     virtual void RegisterScreenBroadcast() {}
53     virtual void RegisterTouchEventCallback(TouchEventCallback&& callback) = 0;
54     virtual void RegisterDragEventCallback(DragEventCallBack&& callback) = 0;
55     virtual void RegisterKeyEventCallback(KeyEventCallback&& callback) = 0;
56     virtual void RegisterMouseEventCallback(MouseEventCallback&& callback) = 0;
57     virtual void RegisterAxisEventCallback(AxisEventCallback&& callback) = 0;
58     virtual void RegisterRotationEventCallback(RotationEventCallBack&& callback) = 0;
59     virtual void RegisterCardViewPositionCallback(CardViewPositionCallBack&& callback) = 0;
60     virtual void Launch() = 0;
61     virtual int32_t GetInstanceId() const = 0;
62     virtual const RefPtr<PlatformResRegister>& GetPlatformResRegister() const = 0;
63 
64     using CardViewAccessibilityParamsCallback = std::function<void(const std::string& key, bool focus)>;
65     virtual void RegisterCardViewAccessibilityParamsCallback(CardViewAccessibilityParamsCallback&& callback) = 0;
66 
67     using ViewChangeCallback = std::function<void(int32_t width, int32_t height,
68         WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)>;
69     virtual void RegisterViewChangeCallback(ViewChangeCallback&& callback) = 0;
70 
71     using ViewPositionChangeCallback = std::function<void(int32_t posX, int32_t posY)>;
72     virtual void RegisterViewPositionChangeCallback(ViewPositionChangeCallback&& callback) = 0;
73 
74     using DensityChangeCallback = std::function<void(double density)>;
75     virtual void RegisterDensityChangeCallback(DensityChangeCallback&& callback) = 0;
76 
77     using TransformHintChangeCallback = std::function<void(uint32_t transform)>;
78     virtual void RegisterTransformHintChangeCallback(TransformHintChangeCallback&& callback) = 0;
79 
80     using SystemBarHeightChangeCallback = std::function<void(double statusBar, double navigationBar)>;
81     virtual void RegisterSystemBarHeightChangeCallback(SystemBarHeightChangeCallback&& callback) = 0;
82 
83     using SurfaceDestroyCallback = std::function<void()>;
84     virtual void RegisterSurfaceDestroyCallback(SurfaceDestroyCallback&& callback) = 0;
85 
86     using IdleCallback = std::function<void(int64_t)>;
87     virtual void RegisterIdleCallback(IdleCallback&& callback) = 0;
88 
89     using PreDrawCallback = std::function<void()>;
RegisterPreDrawCallback(PreDrawCallback && callback)90     virtual void RegisterPreDrawCallback(PreDrawCallback&& callback) {}
91 
92     using RequestFrameCallback = std::function<void()>;
RegisterRequestFrameCallback(RequestFrameCallback && callback)93     virtual void RegisterRequestFrameCallback(RequestFrameCallback&& callback) {}
94 
95     virtual bool Dump(const std::vector<std::string>& params) = 0;
96 
97     // Use to receive event from pc previewer
HandleTouchEvent(const TouchEvent & touchEvent)98     virtual bool HandleTouchEvent(const TouchEvent& touchEvent)
99     {
100         return false;
101     }
102     // Use to receive key event from pc previewer
HandleKeyEvent(const KeyEvent & keyEvent)103     virtual bool HandleKeyEvent(const KeyEvent& keyEvent)
104     {
105         return false;
106     }
107     // Use to get native window handle by texture id
108     virtual const void* GetNativeWindowById(uint64_t textureId) = 0;
109 
110     virtual ViewType GetViewType() const = 0;
111     virtual std::unique_ptr<DrawDelegate> GetDrawDelegate() = 0;
112     virtual std::unique_ptr<PlatformWindow> GetPlatformWindow() = 0;
UpdateWindowBlurRegion(const std::vector<std::vector<float>> & blurRRects)113     virtual void UpdateWindowBlurRegion(const std::vector<std::vector<float>>& blurRRects) {}
UpdateWindowblurDrawOp()114     virtual void UpdateWindowblurDrawOp() {}
StartSystemDrag(const std::string & str,void * pixelMapManager,int32_t byteCount)115     virtual void StartSystemDrag(const std::string& str, void* pixelMapManager, int32_t byteCount) {}
InitDragListener()116     virtual void InitDragListener() {}
117 #ifdef WEARABLE_PRODUCT
CloseSwipeToRight(bool isEnabled)118     virtual void CloseSwipeToRight(bool isEnabled) {}
119 #endif
GetScale(float & scaleX,float & scaleY)120     virtual bool GetScale(float& scaleX, float& scaleY)
121     {
122         return false;
123     }
124 
SetBackgroundColor(const Color & color)125     void SetBackgroundColor(const Color& color)
126     {
127         backgroundColor_ = color;
128         if (!hasSet_) {
129             promise_.set_value(true);
130         }
131     }
132 
GetBackgroundColor()133     const Color& GetBackgroundColor()
134     {
135         if (!hasSet_) {
136             hasSet_ = future_.get();
137         }
138         return backgroundColor_;
139     }
140 
SetCachePath(const std::string & cachePath)141     void SetCachePath(const std::string& cachePath)
142     {
143         cachePath_ = cachePath;
144     }
145 
GetCachePath()146     const std::string& GetCachePath()
147     {
148         return cachePath_;
149     }
150 
SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)151     void SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)
152     {
153         createTime_ = time;
154     }
155 
SetFirstUpDating(std::chrono::time_point<std::chrono::high_resolution_clock> time)156     void SetFirstUpDating(std::chrono::time_point<std::chrono::high_resolution_clock> time)
157     {
158         firstUpdating_ = true;
159         firstUpdateBegin_ = time;
160     }
161 
SetSessionID(const std::string & sessionID)162     void SetSessionID(const std::string& sessionID)
163     {
164         sessionID_ = sessionID;
165     }
166 
GetWidth()167     int32_t GetWidth() const
168     {
169         return width_;
170     }
171 
GetHeight()172     int32_t GetHeight() const
173     {
174         return height_;
175     }
176 
GetPosX()177     int32_t GetPosX() const
178     {
179         return posX_;
180     }
181 
GetPosY()182     int32_t GetPosY() const
183     {
184         return posY_;
185     }
186 
187 protected:
188     std::chrono::time_point<std::chrono::high_resolution_clock> createTime_;
189     std::chrono::time_point<std::chrono::high_resolution_clock> firstUpdateBegin_;
190     std::string sessionID_;
191     bool firstUpdating_ = false;
192     int32_t width_ = 0;
193     int32_t height_ = 0;
194     int32_t posX_ = 0;
195     int32_t posY_ = 0;
196     uint32_t transform_ = 0;
197 
198 private:
199     std::mutex backgroundColorMutex_;
200     Color backgroundColor_;
201     std::string cachePath_;
202     std::promise<bool> promise_;
203     std::shared_future<bool> future_ = promise_.get_future();
204     bool hasSet_ = false;
205 
206     ACE_DISALLOW_COPY_AND_MOVE(AceView);
207 };
208 
209 } // namespace OHOS::Ace
210 
211 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_ACE_VIEW_H
212