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