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