• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 SetViewportMetrics(FlutterAceView* view, const flutter::ViewportMetrics& metrics);
47 
48     static void DispatchTouchEvent(FlutterAceView* view, const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
49     static bool DispatchKeyEvent(FlutterAceView* view, const std::shared_ptr<MMI::KeyEvent>& keyEvent);
50     static bool DispatchRotationEvent(FlutterAceView* view, float rotationValue);
51 
52     static void InitCacheFilePath(const std::string& path);
53     static uint32_t GetBackgroundColor();
54 
55     void RegisterTouchEventCallback(TouchEventCallback&& callback) override;
56     void RegisterDragEventCallback(DragEventCallBack&& callback) override;
57     void RegisterKeyEventCallback(KeyEventCallback&& callback) override;
58     void RegisterMouseEventCallback(MouseEventCallback&& callback) override;
59     void RegisterAxisEventCallback(AxisEventCallback&& callback) override;
60     void RegisterRotationEventCallback(RotationEventCallBack&& callback) override;
RegisterCardViewPositionCallback(CardViewPositionCallBack && callback)61     void RegisterCardViewPositionCallback(CardViewPositionCallBack&& callback) override {}
RegisterCardViewAccessibilityParamsCallback(CardViewAccessibilityParamsCallback && callback)62     void RegisterCardViewAccessibilityParamsCallback(CardViewAccessibilityParamsCallback&& callback) override {}
63 
64     void Launch() override;
65     void SetShellHolder(std::unique_ptr<flutter::OhosShellHolder> holder);
GetShellHolder()66     flutter::OhosShellHolder* GetShellHolder() const
67     {
68         return shell_holder_.get();
69     }
70 
71     void ProcessTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
72 
73     void ProcessMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
74 
75     void ProcessAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
76 
77     bool ProcessKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent);
78 
79     bool ProcessRotationEvent(float rotationValue);
80 
GetInstanceId()81     int32_t GetInstanceId() const override
82     {
83         return instanceId_;
84     }
85 
RegisterViewChangeCallback(ViewChangeCallback && callback)86     void RegisterViewChangeCallback(ViewChangeCallback&& callback) override
87     {
88         viewChangeCallback_ = std::move(callback);
89     }
90 
RegisterDensityChangeCallback(DensityChangeCallback && callback)91     void RegisterDensityChangeCallback(DensityChangeCallback&& callback) override
92     {
93         densityChangeCallback_ = std::move(callback);
94     }
95 
RegisterSystemBarHeightChangeCallback(SystemBarHeightChangeCallbak && callback)96     void RegisterSystemBarHeightChangeCallback(SystemBarHeightChangeCallbak&& callback) override
97     {
98         systemBarHeightChangeCallback_ = std::move(callback);
99     }
100 
RegisterSurfaceDestroyCallback(SurfaceDestroyCallback && callback)101     void RegisterSurfaceDestroyCallback(SurfaceDestroyCallback&& callback) override
102     {
103         surfaceDestroyCallback_ = std::move(callback);
104     }
105 
RegisterIdleCallback(IdleCallback && callback)106     void RegisterIdleCallback(IdleCallback&& callback) override {}
107 
SetPlatformResRegister(const RefPtr<PlatformResRegister> & resRegister)108     void SetPlatformResRegister(const RefPtr<PlatformResRegister>& resRegister)
109     {
110         resRegister_ = resRegister;
111     }
112 
GetPlatformResRegister()113     const RefPtr<PlatformResRegister>& GetPlatformResRegister() const override
114     {
115         return resRegister_;
116     }
117 
GetViewType()118     ViewType GetViewType() const override
119     {
120         return AceView::ViewType::SURFACE_VIEW;
121     }
122 
123     void ProcessDragEvent(int32_t x, int32_t y, const DragEventAction& action);
124     std::unique_ptr<DrawDelegate> GetDrawDelegate() override;
125     std::unique_ptr<PlatformWindow> GetPlatformWindow() override;
126     bool Dump(const std::vector<std::string>& params) override;
127     const void* GetNativeWindowById(uint64_t textureId) override;
128 
129 private:
NotifySurfaceChanged(int width,int height,WindowSizeChangeReason type)130     void NotifySurfaceChanged(int width, int height, WindowSizeChangeReason type)
131     {
132         if (viewChangeCallback_) {
133             viewChangeCallback_(width, height, type);
134         }
135         width_ = width;
136         height_ = height;
137     }
138 
NotifyDensityChanged(double density)139     void NotifyDensityChanged(double density) const
140     {
141         if (densityChangeCallback_) {
142             densityChangeCallback_(density);
143         }
144     }
145 
NotifySystemBarHeightChanged(double statusBar,double navigationBar)146     void NotifySystemBarHeightChanged(double statusBar, double navigationBar) const
147     {
148         if (systemBarHeightChangeCallback_) {
149             systemBarHeightChangeCallback_(statusBar, navigationBar);
150         }
151     }
152 
NotifySurfaceDestroyed()153     void NotifySurfaceDestroyed() const
154     {
155         if (surfaceDestroyCallback_) {
156             surfaceDestroyCallback_();
157         }
158     }
159 
160     bool IsLastPage() const;
161     bool IsNeedForbidToPlatform(TouchEvent point);
162 
163     std::unique_ptr<flutter::OhosShellHolder> shell_holder_;
164     TouchEventCallback touchEventCallback_;
165     MouseEventCallback mouseEventCallback_;
166     AxisEventCallback axisEventCallback_;
167     RotationEventCallBack rotationEventCallBack_;
168     ViewChangeCallback viewChangeCallback_;
169     DensityChangeCallback densityChangeCallback_;
170     SystemBarHeightChangeCallbak systemBarHeightChangeCallback_;
171     SurfaceDestroyCallback surfaceDestroyCallback_;
172     DragEventCallBack dragEventCallback_;
173     int32_t instanceId_ = 0;
174     bool viewLaunched_ = false;
175     RefPtr<PlatformResRegister> resRegister_;
176     KeyEventCallback keyEventCallback_;
177     KeyEventRecognizer keyEventRecognizer_;
178     // mark the touch event's state, HORIZONTAL_STATE: the event should send to platform, VERTICAL_STATE: should not
179     enum class EventState { INITIAL_STATE, HORIZONTAL_STATE, VERTICAL_STATE };
180 
181     struct TouchPointInfo {
182         Offset offset_;
183         EventState eventState_ = EventState::INITIAL_STATE;
184 
185         TouchPointInfo() = default;
TouchPointInfoTouchPointInfo186         explicit TouchPointInfo(const Offset& offset) : offset_(offset) {}
187         ~TouchPointInfo() = default;
188     };
189     std::unordered_map<int32_t, TouchPointInfo> touchPointInfoMap_;
190 
191     ACE_DISALLOW_COPY_AND_MOVE(FlutterAceView);
192 };
193 } // namespace OHOS::Ace::Platform
194 
195 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_FLUTTER_ACE_VIEW_H
196