14
14
15
15
static constexpr char kChannelName [] = " flutter/platform_views" ;
16
16
17
- std::string ExtractStringFromMap (const flutter::EncodableValue& arguments,
18
- const char * key) {
19
- if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
20
- flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
21
- flutter::EncodableValue value = values[flutter::EncodableValue (key)];
22
- if (std::holds_alternative<std::string>(value)) {
23
- return std::get<std::string>(value);
24
- }
25
- }
26
- return std::string ();
27
- }
28
-
29
- int ExtractIntFromMap (const flutter::EncodableValue& arguments,
30
- const char * key) {
31
- if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
32
- flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
33
- flutter::EncodableValue value = values[flutter::EncodableValue (key)];
34
- if (std::holds_alternative<int >(value)) {
35
- return std::get<int >(value);
36
- }
37
- }
38
- return -1 ;
39
- }
40
-
41
- double ExtractDoubleFromMap (const flutter::EncodableValue& arguments,
42
- const char * key) {
43
- if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
44
- flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
45
- flutter::EncodableValue value = values[flutter::EncodableValue (key)];
46
- if (std::holds_alternative<double >(value)) {
47
- return std::get<double >(value);
48
- }
49
- }
50
- return -1 ;
51
- }
52
-
53
- flutter::EncodableMap ExtractMapFromMap (
54
- const flutter::EncodableValue& arguments,
55
- const char * key) {
56
- if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
57
- flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
58
- flutter::EncodableValue value = values[flutter::EncodableValue (key)];
59
- if (std::holds_alternative<flutter::EncodableMap>(value)) {
60
- return std::get<flutter::EncodableMap>(value);
61
- }
62
- }
63
- return flutter::EncodableMap ();
64
- }
65
-
66
- flutter::EncodableList ExtractListFromMap (
67
- const flutter::EncodableValue& arguments,
68
- const char * key) {
69
- if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
70
- flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
71
- flutter::EncodableValue value = values[flutter::EncodableValue (key)];
72
- if (std::holds_alternative<flutter::EncodableList>(value)) {
73
- return std::get<flutter::EncodableList>(value);
17
+ template <typename T>
18
+ bool GetValueFromEncodableMap (const flutter::EncodableValue& arguments,
19
+ std::string key,
20
+ T* out) {
21
+ if (auto pmap = std::get_if<flutter::EncodableMap>(&arguments)) {
22
+ auto iter = pmap->find (flutter::EncodableValue (key));
23
+ if (iter != pmap->end () && !iter->second .IsNull ()) {
24
+ if (auto pval = std::get_if<T>(&iter->second )) {
25
+ *out = *pval;
26
+ return true ;
27
+ }
74
28
}
75
29
}
76
- return flutter::EncodableList () ;
30
+ return false ;
77
31
}
78
32
79
33
PlatformViewChannel::PlatformViewChannel (flutter::BinaryMessenger* messenger,
@@ -96,14 +50,14 @@ PlatformViewChannel::~PlatformViewChannel() {
96
50
97
51
void PlatformViewChannel::Dispose () {
98
52
// Clean-up view_instances_
99
- for (auto const & [viewId, viewInstance ] : view_instances_) {
100
- delete viewInstance ;
53
+ for (auto const & [view_id, view_instance ] : view_instances_) {
54
+ delete view_instance ;
101
55
}
102
56
view_instances_.clear ();
103
57
104
58
// Clean-up view_factories_
105
- for (auto const & [viewType, viewFactory ] : view_factories_) {
106
- viewFactory ->Dispose ();
59
+ for (auto const & [view_type, view_factory ] : view_factories_) {
60
+ view_factory ->Dispose ();
107
61
}
108
62
view_factories_.clear ();
109
63
}
@@ -152,83 +106,99 @@ void PlatformViewChannel::HandleMethodCall(
152
106
const auto method = call.method_name ();
153
107
const auto & arguments = *call.arguments ();
154
108
155
- FT_LOGI (" PlatformViewChannel method: %s" , method.c_str ());
156
109
if (method == " create" ) {
157
- std::string viewType = ExtractStringFromMap (arguments, " viewType" );
158
- int viewId = ExtractIntFromMap (arguments, " id" );
159
- double width = ExtractDoubleFromMap (arguments, " width" );
160
- double height = ExtractDoubleFromMap (arguments, " height" );
110
+ std::string view_type;
111
+ int view_id = -1 ;
112
+ double width = 0.0 , height = 0.0 ;
113
+ if (!GetValueFromEncodableMap (arguments, " viewType" , &view_type) ||
114
+ !GetValueFromEncodableMap (arguments, " id" , &view_id) ||
115
+ !GetValueFromEncodableMap (arguments, " width" , &width) ||
116
+ !GetValueFromEncodableMap (arguments, " height" , &height)) {
117
+ result->Error (" Invalid arguments" );
118
+ return ;
119
+ }
161
120
162
121
FT_LOGI (
163
122
" PlatformViewChannel create viewType: %s id: %d width: %f height: %f " ,
164
- viewType .c_str (), viewId , width, height);
123
+ view_type .c_str (), view_id , width, height);
165
124
166
125
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
167
126
flutter::EncodableValue value = values[flutter::EncodableValue (" params" )];
168
- ByteMessage byteMessage ;
127
+ ByteMessage byte_message ;
169
128
if (std::holds_alternative<ByteMessage>(value)) {
170
- byteMessage = std::get<ByteMessage>(value);
129
+ byte_message = std::get<ByteMessage>(value);
171
130
}
172
- auto it = view_factories_.find (viewType );
131
+ auto it = view_factories_.find (view_type );
173
132
if (it != view_factories_.end ()) {
174
- auto focuesdView = view_instances_.find (CurrentFocusedViewId ());
175
- if (focuesdView != view_instances_.end ()) {
176
- focuesdView ->second ->SetFocus (false );
133
+ auto focused_view = view_instances_.find (CurrentFocusedViewId ());
134
+ if (focused_view != view_instances_.end ()) {
135
+ focused_view ->second ->SetFocus (false );
177
136
}
178
137
179
- auto viewInstance =
180
- it->second ->Create (viewId , width, height, byteMessage );
181
- if (viewInstance ) {
138
+ auto view_instance =
139
+ it->second ->Create (view_id , width, height, byte_message );
140
+ if (view_instance ) {
182
141
view_instances_.insert (
183
- std::pair<int , PlatformView*>(viewId, viewInstance ));
142
+ std::pair<int , PlatformView*>(view_id, view_instance ));
184
143
185
144
if (engine_ && engine_->text_input_channel ) {
186
145
Ecore_IMF_Context* context =
187
146
engine_->text_input_channel ->GetImfContext ();
188
- viewInstance ->SetSoftwareKeyboardContext (context);
147
+ view_instance ->SetSoftwareKeyboardContext (context);
189
148
}
190
- result->Success (flutter::EncodableValue (viewInstance ->GetTextureId ()));
149
+ result->Success (flutter::EncodableValue (view_instance ->GetTextureId ()));
191
150
} else {
192
151
result->Error (" Can't create a webview instance!!" );
193
152
}
194
153
} else {
195
- FT_LOGE (" can't find view type = %s" , viewType .c_str ());
154
+ FT_LOGE (" can't find view type = %s" , view_type .c_str ());
196
155
result->Error (" Can't find view type" );
197
156
}
198
157
} else if (method == " clearFocus" ) {
199
- int viewId = -1 ;
158
+ int view_id = -1 ;
200
159
if (std::holds_alternative<int >(arguments)) {
201
- viewId = std::get<int >(arguments);
160
+ view_id = std::get<int >(arguments);
202
161
};
203
- auto it = view_instances_.find (viewId );
204
- if (viewId >= 0 && it != view_instances_.end ()) {
162
+ auto it = view_instances_.find (view_id );
163
+ if (view_id >= 0 && it != view_instances_.end ()) {
205
164
it->second ->SetFocus (false );
206
165
it->second ->ClearFocus ();
207
166
result->Success ();
208
167
} else {
209
168
result->Error (" Can't find view id" );
210
169
}
211
170
} else {
212
- int viewId = ExtractIntFromMap (arguments, " id" );
213
- auto it = view_instances_.find (viewId);
214
- if (viewId >= 0 && it != view_instances_.end ()) {
171
+ int view_id = -1 ;
172
+ if (!GetValueFromEncodableMap (arguments, " id" , &view_id)) {
173
+ result->Error (" Invalid arguments" );
174
+ return ;
175
+ }
176
+
177
+ auto it = view_instances_.find (view_id);
178
+ if (view_id >= 0 && it != view_instances_.end ()) {
215
179
if (method == " dispose" ) {
216
180
it->second ->Dispose ();
217
181
result->Success ();
218
182
} else if (method == " resize" ) {
219
- double width = ExtractDoubleFromMap (arguments, " width" );
220
- double height = ExtractDoubleFromMap (arguments, " height" );
183
+ double width = 0.0 , height = 0.0 ;
184
+ if (!GetValueFromEncodableMap (arguments, " width" , &width) ||
185
+ !GetValueFromEncodableMap (arguments, " height" , &height)) {
186
+ result->Error (" Invalid arguments" );
187
+ return ;
188
+ }
221
189
it->second ->Resize (width, height);
222
190
result->Success ();
223
191
} else if (method == " touch" ) {
224
- int type, button;
225
- double x, y, dx, dy;
192
+ int type = 0 , button = 0 ;
193
+ double x = 0.0 , y = 0.0 , dx = 0.0 , dy = 0.0 ;
226
194
227
- flutter::EncodableList event = ExtractListFromMap (arguments, " event" );
228
- if (event.size () != 6 ) {
195
+ flutter::EncodableList event;
196
+ if (!GetValueFromEncodableMap (arguments, " event" , &event) ||
197
+ event.size () != 6 ) {
229
198
result->Error (" Invalid Arguments" );
230
199
return ;
231
200
}
201
+
232
202
type = std::get<int >(event[0 ]);
233
203
button = std::get<int >(event[1 ]);
234
204
x = std::get<double >(event[2 ]);
@@ -239,14 +209,14 @@ void PlatformViewChannel::HandleMethodCall(
239
209
it->second ->Touch (type, button, x, y, dx, dy);
240
210
241
211
if (!it->second ->IsFocused ()) {
242
- auto focuesdView = view_instances_.find (CurrentFocusedViewId ());
243
- if (focuesdView != view_instances_.end ()) {
244
- focuesdView ->second ->SetFocus (false );
212
+ auto focused_view = view_instances_.find (CurrentFocusedViewId ());
213
+ if (focused_view != view_instances_.end ()) {
214
+ focused_view ->second ->SetFocus (false );
245
215
}
246
216
247
217
it->second ->SetFocus (true );
248
218
if (channel_ != nullptr ) {
249
- auto id = std::make_unique<flutter::EncodableValue>(viewId );
219
+ auto id = std::make_unique<flutter::EncodableValue>(view_id );
250
220
channel_->InvokeMethod (" viewFocused" , std::move (id));
251
221
}
252
222
}
0 commit comments