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