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