Skip to content

Commit c42525b

Browse files
authored
Refactor PlatformViewChannel (#256)
* Clean up platform_view_channel.cc * Replace CurrentFocusedViewId with FocusedViewInstance
1 parent f493869 commit c42525b

File tree

2 files changed

+103
-100
lines changed

2 files changed

+103
-100
lines changed

shell/platform/tizen/channels/platform_view_channel.cc

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h"
88
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
99
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
10-
#include "flutter/shell/platform/common/json_method_codec.h"
1110
#include "flutter/shell/platform/tizen/channels/encodable_value_holder.h"
12-
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
1311
#include "flutter/shell/platform/tizen/logger.h"
1412
#include "flutter/shell/platform/tizen/public/flutter_platform_view.h"
1513

1614
namespace flutter {
1715

1816
namespace {
17+
1918
constexpr char kChannelName[] = "flutter/platform_views";
19+
2020
} // namespace
2121

2222
PlatformViewChannel::PlatformViewChannel(BinaryMessenger* messenger)
@@ -36,63 +36,67 @@ PlatformViewChannel::~PlatformViewChannel() {
3636
}
3737

3838
void PlatformViewChannel::Dispose() {
39-
ClearViewInstances();
39+
ClearViews();
4040
ClearViewFactories();
4141
}
4242

43-
void PlatformViewChannel::RemoveViewInstanceIfNeeded(int view_id) {
44-
auto it = view_instances_.find(view_id);
45-
if (view_id >= 0 && it != view_instances_.end()) {
46-
auto view_instance = it->second;
47-
view_instance->Dispose();
48-
delete view_instance;
49-
view_instances_.erase(it);
43+
PlatformView* PlatformViewChannel::FindViewById(int view_id) {
44+
auto it = views_.find(view_id);
45+
if (it != views_.end()) {
46+
return it->second;
47+
}
48+
return nullptr;
49+
}
50+
51+
PlatformView* PlatformViewChannel::FindFocusedView() {
52+
for (const auto& [view_id, view] : views_) {
53+
if (view->IsFocused()) {
54+
return view;
55+
}
56+
}
57+
return nullptr;
58+
}
59+
60+
void PlatformViewChannel::RemoveViewIfExists(int view_id) {
61+
PlatformView* view = FindViewById(view_id);
62+
if (view) {
63+
view->Dispose();
64+
delete view;
65+
views_.erase(view_id);
5066
}
5167
}
5268

53-
void PlatformViewChannel::ClearViewInstances() {
54-
// Clean-up view_instances_
55-
for (auto const& [view_id, view_instance] : view_instances_) {
56-
view_instance->Dispose();
57-
delete view_instance;
69+
void PlatformViewChannel::ClearViews() {
70+
for (const auto& [view_id, view] : views_) {
71+
view->Dispose();
72+
delete view;
5873
}
59-
view_instances_.clear();
74+
views_.clear();
6075
}
6176

6277
void PlatformViewChannel::ClearViewFactories() {
63-
// Clean-up view_factories_
64-
for (auto const& [view_type, view_factory] : view_factories_) {
78+
for (const auto& [view_type, view_factory] : view_factories_) {
6579
view_factory->Dispose();
6680
}
6781
view_factories_.clear();
6882
}
6983

70-
void PlatformViewChannel::SendKeyEvent(Ecore_Event_Key* key, bool is_down) {
71-
auto instances = ViewInstances();
72-
auto it = instances.find(CurrentFocusedViewId());
73-
if (it != instances.end()) {
84+
void PlatformViewChannel::SendKeyEvent(Ecore_Event_Key* event, bool is_down) {
85+
PlatformView* view = FindFocusedView();
86+
if (view) {
7487
if (is_down) {
75-
it->second->DispatchKeyDownEvent(key);
88+
view->DispatchKeyDownEvent(event);
7689
} else {
77-
it->second->DispatchKeyUpEvent(key);
90+
view->DispatchKeyUpEvent(event);
7891
}
7992
}
8093
}
8194

82-
int PlatformViewChannel::CurrentFocusedViewId() {
83-
for (auto it = view_instances_.begin(); it != view_instances_.end(); it++) {
84-
if (it->second->IsFocused()) {
85-
return it->second->GetViewId();
86-
}
87-
}
88-
return -1;
89-
}
90-
9195
void PlatformViewChannel::HandleMethodCall(
9296
const MethodCall<EncodableValue>& call,
9397
std::unique_ptr<MethodResult<EncodableValue>> result) {
94-
const auto method = call.method_name();
95-
const auto arguments = call.arguments();
98+
const std::string& method = call.method_name();
99+
const EncodableValue* arguments = call.arguments();
96100

97101
if (method == "create") {
98102
OnCreate(arguments, std::move(result));
@@ -113,44 +117,43 @@ void PlatformViewChannel::HandleMethodCall(
113117
void PlatformViewChannel::OnCreate(
114118
const EncodableValue* arguments,
115119
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
116-
auto map_ptr = std::get_if<EncodableMap>(arguments);
117-
if (!map_ptr) {
120+
auto* map = std::get_if<EncodableMap>(arguments);
121+
if (!map) {
118122
result->Error("Invalid arguments");
119123
return;
120124
}
121125

122-
EncodableValueHolder<std::string> view_type(map_ptr, "viewType");
123-
EncodableValueHolder<int> view_id(map_ptr, "id");
124-
EncodableValueHolder<double> width(map_ptr, "width");
125-
EncodableValueHolder<double> height(map_ptr, "height");
126+
EncodableValueHolder<std::string> view_type(map, "viewType");
127+
EncodableValueHolder<int> view_id(map, "id");
128+
EncodableValueHolder<double> width(map, "width");
129+
EncodableValueHolder<double> height(map, "height");
126130

127131
if (!view_type || !view_id || !width || !height) {
128132
result->Error("Invalid arguments");
129133
return;
130134
}
131135

132-
FT_LOG(Info) << "Creating a platform view: " << *view_type.value;
133-
RemoveViewInstanceIfNeeded(*view_id);
136+
FT_LOG(Info) << "Creating a platform view: " << view_type.value;
137+
RemoveViewIfExists(*view_id);
134138

135-
EncodableValueHolder<ByteMessage> params(map_ptr, "params");
139+
EncodableValueHolder<ByteMessage> params(map, "params");
136140
ByteMessage byte_message;
137141
if (params) {
138142
byte_message = *params;
139143
}
140144
auto it = view_factories_.find(*view_type);
141145
if (it != view_factories_.end()) {
142-
auto focused_view = view_instances_.find(CurrentFocusedViewId());
143-
if (focused_view != view_instances_.end()) {
144-
focused_view->second->SetFocus(false);
146+
PlatformView* focused_view = FindFocusedView();
147+
if (focused_view) {
148+
focused_view->SetFocus(false);
145149
}
146-
auto view_instance =
150+
PlatformView* view =
147151
it->second->Create(*view_id, *width, *height, byte_message);
148-
if (view_instance) {
149-
view_instances_.insert(
150-
std::pair<int, PlatformView*>(*view_id, view_instance));
151-
result->Success(EncodableValue(view_instance->GetTextureId()));
152+
if (view) {
153+
views_[*view_id] = view;
154+
result->Success(EncodableValue(view->GetTextureId()));
152155
} else {
153-
result->Error("Can't create a webview instance!!");
156+
result->Error("Can't create view instance");
154157
}
155158
} else {
156159
FT_LOG(Error) << "Can't find view type: " << *view_type;
@@ -161,99 +164,93 @@ void PlatformViewChannel::OnCreate(
161164
void PlatformViewChannel::OnClearFocus(
162165
const EncodableValue* arguments,
163166
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
164-
auto view_id_ptr = std::get_if<int>(arguments);
165-
if (!view_id_ptr) {
167+
const int* view_id = std::get_if<int>(arguments);
168+
if (!view_id) {
166169
result->Error("Invalid arguments");
167170
return;
168171
}
169172

170-
auto it = view_instances_.find(*view_id_ptr);
171-
if (it == view_instances_.end()) {
173+
PlatformView* view = FindViewById(*view_id);
174+
if (!view) {
172175
result->Error("Can't find view id");
173176
return;
174177
}
178+
view->SetFocus(false);
179+
view->ClearFocus();
175180

176-
it->second->SetFocus(false);
177-
it->second->ClearFocus();
178181
result->Success();
179182
}
180183

181184
void PlatformViewChannel::OnDispose(
182185
const EncodableValue* arguments,
183186
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
184-
auto map_ptr = std::get_if<EncodableMap>(arguments);
185-
if (!map_ptr) {
187+
auto* map = std::get_if<EncodableMap>(arguments);
188+
if (!map) {
186189
result->Error("Invalid arguments");
187190
return;
188191
}
189192

190-
EncodableValueHolder<int> view_id(map_ptr, "id");
191-
193+
EncodableValueHolder<int> view_id(map, "id");
192194
if (!view_id) {
193195
result->Error("Invalid arguments");
194196
return;
195197
}
196198

197-
if (view_instances_.find(*view_id) == view_instances_.end()) {
199+
PlatformView* view = FindViewById(*view_id);
200+
if (!view) {
198201
result->Error("Can't find view id");
199202
return;
200203
}
204+
RemoveViewIfExists(*view_id);
201205

202-
RemoveViewInstanceIfNeeded(*view_id);
203206
result->Success();
204207
}
205208

206209
void PlatformViewChannel::OnResize(
207210
const EncodableValue* arguments,
208211
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
209-
auto map_ptr = std::get_if<EncodableMap>(arguments);
210-
if (!map_ptr) {
212+
auto* map = std::get_if<EncodableMap>(arguments);
213+
if (!map) {
211214
result->Error("Invalid arguments");
212215
return;
213216
}
214217

215-
EncodableValueHolder<int> view_id(map_ptr, "id");
216-
EncodableValueHolder<double> width(map_ptr, "width");
217-
EncodableValueHolder<double> height(map_ptr, "height");
218+
EncodableValueHolder<int> view_id(map, "id");
219+
EncodableValueHolder<double> width(map, "width");
220+
EncodableValueHolder<double> height(map, "height");
218221

219222
if (!view_id || !width || !height) {
220223
result->Error("Invalid arguments");
221224
return;
222225
}
223226

224-
auto it = view_instances_.find(*view_id);
225-
if (it == view_instances_.end()) {
227+
PlatformView* view = FindViewById(*view_id);
228+
if (!view) {
226229
result->Error("Can't find view id");
227230
return;
228231
}
232+
view->Resize(*width, *height);
229233

230-
it->second->Resize(*width, *height);
231234
result->Success();
232235
}
233236

234237
void PlatformViewChannel::OnTouch(
235238
const EncodableValue* arguments,
236239
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
237-
auto map_ptr = std::get_if<EncodableMap>(arguments);
238-
if (!map_ptr) {
240+
auto* map = std::get_if<EncodableMap>(arguments);
241+
if (!map) {
239242
result->Error("Invalid arguments");
240243
return;
241244
}
242245

243246
int type = 0, button = 0;
244247
double x = 0.0, y = 0.0, dx = 0.0, dy = 0.0;
245248

246-
EncodableValueHolder<EncodableList> event(map_ptr, "event");
247-
EncodableValueHolder<int> view_id(map_ptr, "id");
249+
EncodableValueHolder<EncodableList> event(map, "event");
250+
EncodableValueHolder<int> view_id(map, "id");
248251

249252
if (!view_id || !event || event->size() != 6) {
250-
result->Error("Invalid Arguments");
251-
return;
252-
}
253-
254-
auto it = view_instances_.find(*view_id);
255-
if (it == view_instances_.end()) {
256-
result->Error("Can't find view id");
253+
result->Error("Invalid arguments");
257254
return;
258255
}
259256

@@ -264,18 +261,23 @@ void PlatformViewChannel::OnTouch(
264261
dx = std::get<double>(event->at(4));
265262
dy = std::get<double>(event->at(5));
266263

267-
it->second->Touch(type, button, x, y, dx, dy);
264+
PlatformView* view = FindViewById(*view_id);
265+
if (!view) {
266+
result->Error("Can't find view id");
267+
return;
268+
}
269+
view->Touch(type, button, x, y, dx, dy);
268270

269-
if (!it->second->IsFocused()) {
270-
auto focused_view = view_instances_.find(CurrentFocusedViewId());
271-
if (focused_view != view_instances_.end()) {
272-
focused_view->second->SetFocus(false);
271+
if (!view->IsFocused()) {
272+
PlatformView* focused_view = FindFocusedView();
273+
if (focused_view) {
274+
focused_view->SetFocus(false);
273275
}
274276

275-
it->second->SetFocus(true);
277+
view->SetFocus(true);
276278
if (channel_ != nullptr) {
277-
auto id = std::make_unique<EncodableValue>(*view_id);
278-
channel_->InvokeMethod("viewFocused", std::move(id));
279+
auto args = std::make_unique<EncodableValue>(*view_id);
280+
channel_->InvokeMethod("viewFocused", std::move(args));
279281
}
280282
}
281283

shell/platform/tizen/channels/platform_view_channel.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
1515
#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h"
16-
#include "rapidjson/document.h"
1716

1817
class PlatformView;
1918
class PlatformViewFactory;
@@ -26,19 +25,21 @@ class PlatformViewChannel {
2625
virtual ~PlatformViewChannel();
2726

2827
void Dispose();
29-
void RemoveViewInstanceIfNeeded(int view_id);
30-
void ClearViewInstances();
31-
void ClearViewFactories();
3228

3329
std::map<std::string, std::unique_ptr<PlatformViewFactory>>& ViewFactories() {
3430
return view_factories_;
3531
}
36-
std::map<int, PlatformView*>& ViewInstances() { return view_instances_; }
3732

38-
void SendKeyEvent(Ecore_Event_Key* key, bool is_down);
39-
int CurrentFocusedViewId();
33+
void SendKeyEvent(Ecore_Event_Key* event, bool is_down);
4034

4135
private:
36+
PlatformView* FindViewById(int view_id);
37+
PlatformView* FindFocusedView();
38+
39+
void RemoveViewIfExists(int view_id);
40+
void ClearViews();
41+
void ClearViewFactories();
42+
4243
void HandleMethodCall(const MethodCall<EncodableValue>& call,
4344
std::unique_ptr<MethodResult<EncodableValue>> result);
4445

@@ -55,7 +56,7 @@ class PlatformViewChannel {
5556

5657
std::unique_ptr<MethodChannel<EncodableValue>> channel_;
5758
std::map<std::string, std::unique_ptr<PlatformViewFactory>> view_factories_;
58-
std::map<int, PlatformView*> view_instances_;
59+
std::map<int, PlatformView*> views_;
5960
};
6061

6162
} // namespace flutter

0 commit comments

Comments
 (0)