1 /* 2 * Copyright (c) 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_ADAPTER_OHOS_ENTRANCE_ACE_VIEW_OHOS_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_VIEW_OHOS_H 18 19 #include <functional> 20 #include <memory> 21 22 #include "interfaces/inner_api/ace/viewport_config.h" 23 #include "key_event.h" 24 #include "wm/window.h" 25 26 #include "base/memory/referenced.h" 27 #include "base/perfmonitor/perf_monitor.h" 28 #include "base/utils/noncopyable.h" 29 #include "core/common/ace_view.h" 30 #include "core/common/platform_res_register.h" 31 #include "core/common/thread_model_impl.h" 32 #include "core/components_ng/base/frame_node.h" 33 #include "core/event/key_event_recognizer.h" 34 35 namespace OHOS::Ace::Platform { 36 37 using ReleaseCallback = std::function<void()>; 38 39 class ACE_FORCE_EXPORT AceViewOhos : public AceView, public Referenced { 40 public: 41 explicit AceViewOhos(int32_t id, std::unique_ptr<ThreadModelImpl> threadModelImpl); 42 ~AceViewOhos() override = default; 43 static AceViewOhos* CreateView( 44 int32_t instanceId, bool useCurrentEventRunner = false, bool usePlatformThread = false); 45 static void SurfaceCreated(AceViewOhos* view, OHOS::sptr<OHOS::Rosen::Window> window); 46 static void SurfaceChanged(AceViewOhos* view, int32_t width, int32_t height, int32_t orientation, 47 WindowSizeChangeReason type = WindowSizeChangeReason::UNDEFINED, 48 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr); 49 static void SurfacePositionChanged(AceViewOhos* view, int32_t posX, int32_t posY); 50 static void SetViewportMetrics(AceViewOhos* view, const ViewportConfig& config); 51 52 static void DispatchTouchEvent(AceViewOhos* view, const std::shared_ptr<MMI::PointerEvent>& pointerEvent, 53 const RefPtr<OHOS::Ace::NG::FrameNode>& node = nullptr, const std::function<void()>& callback = nullptr); 54 static bool DispatchKeyEvent(AceViewOhos* view, const std::shared_ptr<MMI::KeyEvent>& keyEvent); 55 static bool DispatchRotationEvent(AceViewOhos* view, float rotationValue); 56 static void DispatchEventToPerf(const std::shared_ptr<MMI::PointerEvent>& pointerEvent); 57 static void DispatchEventToPerf(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 58 59 60 static uint32_t GetBackgroundColor(); 61 62 void RegisterTouchEventCallback(TouchEventCallback&& callback) override; 63 void RegisterDragEventCallback(DragEventCallBack&& callback) override; 64 void RegisterKeyEventCallback(KeyEventCallback&& callback) override; 65 void RegisterMouseEventCallback(MouseEventCallback&& callback) override; 66 void RegisterAxisEventCallback(AxisEventCallback&& callback) override; 67 void RegisterRotationEventCallback(RotationEventCallBack&& callback) override; RegisterCardViewPositionCallback(CardViewPositionCallBack && callback)68 void RegisterCardViewPositionCallback(CardViewPositionCallBack&& callback) override {} RegisterCardViewAccessibilityParamsCallback(CardViewAccessibilityParamsCallback && callback)69 void RegisterCardViewAccessibilityParamsCallback(CardViewAccessibilityParamsCallback&& callback) override {} 70 71 void Launch() override; 72 73 void ProcessTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, 74 const RefPtr<OHOS::Ace::NG::FrameNode>& node = nullptr, const std::function<void()>& callback = nullptr); 75 76 void ProcessMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, 77 const RefPtr<OHOS::Ace::NG::FrameNode>& node = nullptr); 78 79 void ProcessAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, 80 const RefPtr<OHOS::Ace::NG::FrameNode>& node = nullptr); 81 82 bool ProcessKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 83 84 bool ProcessRotationEvent(float rotationValue); 85 86 void ProcessDragEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent); 87 88 void ProcessDragEvent(int32_t x, int32_t y, const DragEventAction& action); 89 GetInstanceId()90 int32_t GetInstanceId() const override 91 { 92 return instanceId_; 93 } 94 RegisterViewChangeCallback(ViewChangeCallback && callback)95 void RegisterViewChangeCallback(ViewChangeCallback&& callback) override 96 { 97 viewChangeCallback_ = std::move(callback); 98 } 99 RegisterViewPositionChangeCallback(ViewPositionChangeCallback && callback)100 void RegisterViewPositionChangeCallback(ViewPositionChangeCallback&& callback) override 101 { 102 viewPositionChangeCallback_ = std::move(callback); 103 } 104 RegisterDensityChangeCallback(DensityChangeCallback && callback)105 void RegisterDensityChangeCallback(DensityChangeCallback&& callback) override 106 { 107 densityChangeCallback_ = std::move(callback); 108 } 109 RegisterSystemBarHeightChangeCallback(SystemBarHeightChangeCallback && callback)110 void RegisterSystemBarHeightChangeCallback(SystemBarHeightChangeCallback&& callback) override 111 { 112 systemBarHeightChangeCallback_ = std::move(callback); 113 } 114 RegisterSurfaceDestroyCallback(SurfaceDestroyCallback && callback)115 void RegisterSurfaceDestroyCallback(SurfaceDestroyCallback&& callback) override 116 { 117 surfaceDestroyCallback_ = std::move(callback); 118 } 119 RegisterIdleCallback(IdleCallback && callback)120 void RegisterIdleCallback(IdleCallback&& callback) override {} 121 SetPlatformResRegister(const RefPtr<PlatformResRegister> & resRegister)122 void SetPlatformResRegister(const RefPtr<PlatformResRegister>& resRegister) 123 { 124 resRegister_ = resRegister; 125 } 126 GetPlatformResRegister()127 const RefPtr<PlatformResRegister>& GetPlatformResRegister() const override 128 { 129 return resRegister_; 130 } 131 GetViewType()132 ViewType GetViewType() const override 133 { 134 return AceView::ViewType::SURFACE_VIEW; 135 } 136 GetDrawDelegate()137 std::unique_ptr<DrawDelegate> GetDrawDelegate() override 138 { 139 return nullptr; 140 } 141 GetPlatformWindow()142 std::unique_ptr<PlatformWindow> GetPlatformWindow() override 143 { 144 return nullptr; 145 } 146 147 bool Dump(const std::vector<std::string>& params) override; 148 const void* GetNativeWindowById(uint64_t textureId) override; 149 GetThreadModelImpl()150 ThreadModelImpl* GetThreadModelImpl() const 151 { 152 return threadModelImpl_.get(); 153 } 154 155 private: 156 void NotifySurfaceChanged(int width, int height, WindowSizeChangeReason type, 157 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr) 158 { 159 width_ = width; 160 height_ = height; 161 if (viewChangeCallback_) { 162 viewChangeCallback_(width, height, type, rsTransaction); 163 } 164 } 165 NotifySurfacePositionChanged(int32_t posX,int32_t posY)166 void NotifySurfacePositionChanged(int32_t posX, int32_t posY) 167 { 168 if (posX_ == posX && posY_ == posY) { 169 return; 170 } 171 if (viewPositionChangeCallback_) { 172 viewPositionChangeCallback_(posX, posY); 173 } 174 posX_ = posX; 175 posY_ = posY; 176 } 177 NotifyDensityChanged(double density)178 void NotifyDensityChanged(double density) const 179 { 180 if (densityChangeCallback_) { 181 densityChangeCallback_(density); 182 } 183 } 184 NotifySystemBarHeightChanged(double statusBar,double navigationBar)185 void NotifySystemBarHeightChanged(double statusBar, double navigationBar) const 186 { 187 if (systemBarHeightChangeCallback_) { 188 systemBarHeightChangeCallback_(statusBar, navigationBar); 189 } 190 } 191 NotifySurfaceDestroyed()192 void NotifySurfaceDestroyed() const 193 { 194 if (surfaceDestroyCallback_) { 195 surfaceDestroyCallback_(); 196 } 197 } 198 199 TouchEventCallback touchEventCallback_; 200 MouseEventCallback mouseEventCallback_; 201 AxisEventCallback axisEventCallback_; 202 RotationEventCallBack rotationEventCallBack_; 203 ViewChangeCallback viewChangeCallback_; 204 ViewPositionChangeCallback viewPositionChangeCallback_; 205 DensityChangeCallback densityChangeCallback_; 206 SystemBarHeightChangeCallback systemBarHeightChangeCallback_; 207 SurfaceDestroyCallback surfaceDestroyCallback_; 208 DragEventCallBack dragEventCallback_; 209 int32_t instanceId_ = -1; 210 RefPtr<PlatformResRegister> resRegister_; 211 KeyEventCallback keyEventCallback_; 212 KeyEventRecognizer keyEventRecognizer_; 213 // mark the touch event's state, HORIZONTAL_STATE: the event should send to platform, VERTICAL_STATE: should not 214 enum class EventState { INITIAL_STATE, HORIZONTAL_STATE, VERTICAL_STATE }; 215 216 struct TouchPointInfo { 217 Offset offset_; 218 EventState eventState_ = EventState::INITIAL_STATE; 219 220 TouchPointInfo() = default; TouchPointInfoTouchPointInfo221 explicit TouchPointInfo(const Offset& offset) : offset_(offset) {} 222 ~TouchPointInfo() = default; 223 }; 224 std::unordered_map<int32_t, TouchPointInfo> touchPointInfoMap_; 225 226 std::unique_ptr<ThreadModelImpl> threadModelImpl_; 227 228 ACE_DISALLOW_COPY_AND_MOVE(AceViewOhos); 229 }; 230 } // namespace OHOS::Ace::Platform 231 232 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_VIEW_OHOS_H 233