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 #include "adapter/preview/entrance/flutter_ace_view.h"
17
18 #include "base/log/dump_log.h"
19 #include "base/log/event_report.h"
20 #include "base/log/log.h"
21 #include "base/utils/macros.h"
22 #include "base/utils/system_properties.h"
23 #include "base/utils/utils.h"
24 #include "core/common/ace_engine.h"
25 #include "core/components/theme/theme_manager.h"
26 #include "core/event/mouse_event.h"
27 #include "core/event/touch_event.h"
28 #include "core/image/image_cache.h"
29 #include "core/pipeline/layers/flutter_scene_builder.h"
30
31 namespace OHOS::Ace::Platform {
32
RegisterTouchEventCallback(TouchEventCallback && callback)33 void FlutterAceView::RegisterTouchEventCallback(TouchEventCallback&& callback)
34 {
35 ACE_DCHECK(callback);
36 touchEventCallback_ = std::move(callback);
37 }
38
RegisterKeyEventCallback(KeyEventCallback && callback)39 void FlutterAceView::RegisterKeyEventCallback(KeyEventCallback&& callback)
40 {
41 ACE_DCHECK(callback);
42 keyEventCallback_ = std::move(callback);
43 }
44
RegisterMouseEventCallback(MouseEventCallback && callback)45 void FlutterAceView::RegisterMouseEventCallback(MouseEventCallback&& callback)
46 {
47 ACE_DCHECK(callback);
48 mouseEventCallback_ = std::move(callback);
49 }
50
RegisterAxisEventCallback(AxisEventCallback && callback)51 void FlutterAceView::RegisterAxisEventCallback(AxisEventCallback&& callback)
52 {
53 ACE_DCHECK(callback);
54 axisEventCallback_ = std::move(callback);
55 }
56
RegisterRotationEventCallback(RotationEventCallBack && callback)57 void FlutterAceView::RegisterRotationEventCallback(RotationEventCallBack&& callback)
58 {
59 ACE_DCHECK(callback);
60 rotationEventCallBack_ = std::move(callback);
61 }
62
Launch()63 void FlutterAceView::Launch()
64 {
65 }
66
Dump(const std::vector<std::string> & params)67 bool FlutterAceView::Dump(const std::vector<std::string>& params)
68 {
69 return false;
70 }
71
ProcessIdleEvent(int64_t deadline)72 void FlutterAceView::ProcessIdleEvent(int64_t deadline)
73 {
74 if (idleCallback_) {
75 idleCallback_(deadline);
76 }
77 }
78
HandleTouchEvent(const TouchEvent & touchEvent)79 bool FlutterAceView::HandleTouchEvent(const TouchEvent& touchEvent)
80 {
81 if (touchEvent.type == TouchType::UNKNOWN) {
82 LOGW("Unknown event.");
83 return false;
84 }
85
86 LOGD("HandleTouchEvent touchEvent.x: %lf, touchEvent.y: %lf, touchEvent.size: %lf",
87 touchEvent.x, touchEvent.y, touchEvent.size);
88 auto event = touchEvent.UpdatePointers();
89 if (touchEventCallback_) {
90 touchEventCallback_(event, nullptr);
91 }
92
93 return true;
94 }
95
HandleKeyEvent(const KeyEvent & keyEvent)96 bool FlutterAceView::HandleKeyEvent(const KeyEvent& keyEvent)
97 {
98 if (!keyEventCallback_) {
99 return false;
100 }
101
102 return keyEventCallback_(keyEvent);
103 }
104
GetDrawDelegate()105 std::unique_ptr<DrawDelegate> FlutterAceView::GetDrawDelegate()
106 {
107 auto drawDelegate = std::make_unique<DrawDelegate>();
108
109 drawDelegate->SetDrawFrameCallback([this](RefPtr<Flutter::Layer>& layer, const Rect& dirty) {
110 if (!layer) {
111 return;
112 }
113 RefPtr<Flutter::FlutterSceneBuilder> flutterSceneBuilder = AceType::MakeRefPtr<Flutter::FlutterSceneBuilder>();
114 layer->AddToScene(*flutterSceneBuilder, 0.0, 0.0);
115 auto scene_ = flutterSceneBuilder->Build();
116 if (!flutter::UIDartState::Current()) {
117 LOGE("uiDartState is nullptr");
118 return;
119 }
120 auto window = flutter::UIDartState::Current()->window();
121 if (window != nullptr && window->client() != nullptr) {
122 window->client()->Render(scene_.get());
123 }
124 });
125
126 return drawDelegate;
127 }
128
GetPlatformWindow()129 std::unique_ptr<PlatformWindow> FlutterAceView::GetPlatformWindow()
130 {
131 return nullptr;
132 }
133
GetNativeWindowById(uint64_t textureId)134 const void* FlutterAceView::GetNativeWindowById(uint64_t textureId)
135 {
136 return nullptr;
137 }
138 } // namespace OHOS::Ace::Platform
139