Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fl_text_input_plugin.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/linux/fl_text_input_plugin.h"
6
7#include <gtk/gtk.h>
8
9#include "flutter/shell/platform/common/text_editing_delta.h"
10#include "flutter/shell/platform/common/text_input_model.h"
11#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
12#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
13
14static constexpr char kChannelName[] = "flutter/textinput";
15
16static constexpr char kBadArgumentsError[] = "Bad Arguments";
17
18static constexpr char kSetClientMethod[] = "TextInput.setClient";
19static constexpr char kShowMethod[] = "TextInput.show";
20static constexpr char kSetEditingStateMethod[] = "TextInput.setEditingState";
21static constexpr char kClearClientMethod[] = "TextInput.clearClient";
22static constexpr char kHideMethod[] = "TextInput.hide";
23static constexpr char kUpdateEditingStateMethod[] =
24 "TextInputClient.updateEditingState";
25static constexpr char kUpdateEditingStateWithDeltasMethod[] =
26 "TextInputClient.updateEditingStateWithDeltas";
27static constexpr char kPerformActionMethod[] = "TextInputClient.performAction";
28static constexpr char kSetEditableSizeAndTransform[] =
29 "TextInput.setEditableSizeAndTransform";
30static constexpr char kSetMarkedTextRect[] = "TextInput.setMarkedTextRect";
31
32static constexpr char kInputActionKey[] = "inputAction";
33static constexpr char kTextInputTypeKey[] = "inputType";
34static constexpr char kEnableDeltaModel[] = "enableDeltaModel";
35static constexpr char kTextInputTypeNameKey[] = "name";
36static constexpr char kTextKey[] = "text";
37static constexpr char kSelectionBaseKey[] = "selectionBase";
38static constexpr char kSelectionExtentKey[] = "selectionExtent";
39static constexpr char kSelectionAffinityKey[] = "selectionAffinity";
40static constexpr char kSelectionIsDirectionalKey[] = "selectionIsDirectional";
41static constexpr char kComposingBaseKey[] = "composingBase";
42static constexpr char kComposingExtentKey[] = "composingExtent";
43
44static constexpr char kTransform[] = "transform";
45
46static constexpr char kTextAffinityDownstream[] = "TextAffinity.downstream";
47static constexpr char kMultilineInputType[] = "TextInputType.multiline";
48static constexpr char kNoneInputType[] = "TextInputType.none";
49
50static constexpr char kNewlineInputAction[] = "TextInputAction.newline";
51
52static constexpr int64_t kClientIdUnset = -1;
53
54typedef enum {
56 // Send newline when multi-line and enter is pressed.
58 // The input method is not shown at all.
61
64
65 FlMethodChannel* channel;
66
67 // Client ID provided by Flutter to report events with.
68 int64_t client_id;
69
70 // Input action to perform when enter pressed.
72
73 // The type of the input method.
75
76 // Whether to enable that the engine sends text input updates to the framework
77 // as TextEditingDeltas or as one TextEditingValue.
78 // For more information on the delta model, see:
79 // https://master-api.flutter.dev/flutter/services/TextInputConfiguration/enableDeltaModel.html
81
82 // Input method.
83 GtkIMContext* im_context;
84
85 FlTextInputViewDelegate* view_delegate;
86
88
89 // A 4x4 matrix that maps from `EditableText` local coordinates to the
90 // coordinate system of `PipelineOwner.rootNode`.
92
93 // The smallest rect, in local coordinates, of the text in the composing
94 // range, or of the caret in the case where there is no current composing
95 // range. This value is updated via `TextInput.setMarkedTextRect` messages
96 // over the text input channel.
97 GdkRectangle composing_rect;
98};
99
101 fl_text_input_plugin,
102 G_TYPE_OBJECT)
103
104// Completes method call and returns TRUE if the call was successful.
105static gboolean finish_method(GObject* object,
106 GAsyncResult* result,
107 GError** error) {
108 g_autoptr(FlMethodResponse) response = fl_method_channel_invoke_method_finish(
109 FL_METHOD_CHANNEL(object), result, error);
110 if (response == nullptr) {
111 return FALSE;
112 }
113 return fl_method_response_get_result(response, error) != nullptr;
114}
115
116// Called when a response is received from TextInputClient.updateEditingState()
117static void update_editing_state_response_cb(GObject* object,
118 GAsyncResult* result,
119 gpointer user_data) {
120 g_autoptr(GError) error = nullptr;
121 if (!finish_method(object, result, &error)) {
122 g_warning("Failed to call %s: %s", kUpdateEditingStateMethod,
123 error->message);
124 }
125}
126
127// Informs Flutter of text input changes.
128static void update_editing_state(FlTextInputPlugin* self) {
130 fl_text_input_plugin_get_instance_private(self));
131
132 g_autoptr(FlValue) args = fl_value_new_list();
134 g_autoptr(FlValue) value = fl_value_new_map();
135
136 flutter::TextRange selection = priv->text_model->selection();
139 fl_value_new_string(priv->text_model->GetText().c_str()));
141 fl_value_new_int(selection.base()));
143 fl_value_new_int(selection.extent()));
144
145 int composing_base = -1;
146 int composing_extent = -1;
147 if (!priv->text_model->composing_range().collapsed()) {
148 composing_base = priv->text_model->composing_range().base();
149 composing_extent = priv->text_model->composing_range().extent();
150 }
152 fl_value_new_int(composing_base));
154 fl_value_new_int(composing_extent));
155
156 // The following keys are not implemented and set to default values.
161
163
165 args, nullptr,
167}
168
169// Informs Flutter of text input changes by passing just the delta.
170static void update_editing_state_with_delta(FlTextInputPlugin* self,
173 fl_text_input_plugin_get_instance_private(self));
174
175 g_autoptr(FlValue) args = fl_value_new_list();
177
178 g_autoptr(FlValue) deltaValue = fl_value_new_map();
179 fl_value_set_string_take(deltaValue, "oldText",
180 fl_value_new_string(delta->old_text().c_str()));
181
182 fl_value_set_string_take(deltaValue, "deltaText",
183 fl_value_new_string(delta->delta_text().c_str()));
184
185 fl_value_set_string_take(deltaValue, "deltaStart",
186 fl_value_new_int(delta->delta_start()));
187
188 fl_value_set_string_take(deltaValue, "deltaEnd",
189 fl_value_new_int(delta->delta_end()));
190
191 flutter::TextRange selection = priv->text_model->selection();
192 fl_value_set_string_take(deltaValue, "selectionBase",
193 fl_value_new_int(selection.base()));
194
195 fl_value_set_string_take(deltaValue, "selectionExtent",
196 fl_value_new_int(selection.extent()));
197
198 fl_value_set_string_take(deltaValue, "selectionAffinity",
200
201 fl_value_set_string_take(deltaValue, "selectionIsDirectional",
203
204 int composing_base = -1;
205 int composing_extent = -1;
206 if (!priv->text_model->composing_range().collapsed()) {
207 composing_base = priv->text_model->composing_range().base();
208 composing_extent = priv->text_model->composing_range().extent();
209 }
210 fl_value_set_string_take(deltaValue, "composingBase",
211 fl_value_new_int(composing_base));
212 fl_value_set_string_take(deltaValue, "composingExtent",
213 fl_value_new_int(composing_extent));
214
215 g_autoptr(FlValue) deltas = fl_value_new_list();
216 fl_value_append(deltas, deltaValue);
217 g_autoptr(FlValue) value = fl_value_new_map();
218 fl_value_set_string(value, "deltas", deltas);
219
221
225}
226
227// Called when a response is received from TextInputClient.performAction()
228static void perform_action_response_cb(GObject* object,
229 GAsyncResult* result,
230 gpointer user_data) {
231 g_autoptr(GError) error = nullptr;
232 if (!finish_method(object, result, &error)) {
233 g_warning("Failed to call %s: %s", kPerformActionMethod, error->message);
234 }
235}
236
237// Inform Flutter that the input has been activated.
238static void perform_action(FlTextInputPlugin* self) {
240 fl_text_input_plugin_get_instance_private(self));
241
242 g_return_if_fail(FL_IS_TEXT_INPUT_PLUGIN(self));
243 g_return_if_fail(priv->client_id != 0);
244 g_return_if_fail(priv->input_action != nullptr);
245
246 g_autoptr(FlValue) args = fl_value_new_list();
249
252}
253
254// Signal handler for GtkIMContext::preedit-start
255static void im_preedit_start_cb(FlTextInputPlugin* self) {
257 fl_text_input_plugin_get_instance_private(self));
258 priv->text_model->BeginComposing();
259}
260
261// Signal handler for GtkIMContext::preedit-changed
262static void im_preedit_changed_cb(FlTextInputPlugin* self) {
264 fl_text_input_plugin_get_instance_private(self));
265 std::string text_before_change = priv->text_model->GetText();
266 flutter::TextRange composing_before_change =
267 priv->text_model->composing_range();
268 g_autofree gchar* buf = nullptr;
269 gint cursor_offset = 0;
270 gtk_im_context_get_preedit_string(priv->im_context, &buf, nullptr,
271 &cursor_offset);
272 if (priv->text_model->composing()) {
273 cursor_offset += priv->text_model->composing_range().start();
274 } else {
275 cursor_offset += priv->text_model->selection().start();
276 }
277 priv->text_model->UpdateComposingText(buf);
278 priv->text_model->SetSelection(flutter::TextRange(cursor_offset));
279
280 if (priv->enable_delta_model) {
281 std::string text(buf);
283 text_before_change, composing_before_change, text);
285 } else {
287 }
288}
289
290// Signal handler for GtkIMContext::commit
291static void im_commit_cb(FlTextInputPlugin* self, const gchar* text) {
293 fl_text_input_plugin_get_instance_private(self));
294 std::string text_before_change = priv->text_model->GetText();
295 flutter::TextRange composing_before_change =
296 priv->text_model->composing_range();
297 flutter::TextRange selection_before_change = priv->text_model->selection();
298 gboolean was_composing = priv->text_model->composing();
299
300 priv->text_model->AddText(text);
301 if (priv->text_model->composing()) {
302 priv->text_model->CommitComposing();
303 }
304
305 if (priv->enable_delta_model) {
306 flutter::TextRange replace_range =
307 was_composing ? composing_before_change : selection_before_change;
308 std::unique_ptr<flutter::TextEditingDelta> delta =
309 std::make_unique<flutter::TextEditingDelta>(text_before_change,
310 replace_range, text);
312 } else {
314 }
315}
316
317// Signal handler for GtkIMContext::preedit-end
318static void im_preedit_end_cb(FlTextInputPlugin* self) {
320 fl_text_input_plugin_get_instance_private(self));
321 priv->text_model->EndComposing();
322 if (priv->enable_delta_model) {
324 flutter::TextEditingDelta(priv->text_model->GetText());
326 } else {
328 }
329}
330
331// Signal handler for GtkIMContext::retrieve-surrounding
332static gboolean im_retrieve_surrounding_cb(FlTextInputPlugin* self) {
334 fl_text_input_plugin_get_instance_private(self));
335 auto text = priv->text_model->GetText();
336 size_t cursor_offset = priv->text_model->GetCursorOffset();
337 gtk_im_context_set_surrounding(priv->im_context, text.c_str(), -1,
338 cursor_offset);
339 return TRUE;
340}
341
342// Signal handler for GtkIMContext::delete-surrounding
343static gboolean im_delete_surrounding_cb(FlTextInputPlugin* self,
344 gint offset,
345 gint n_chars) {
347 fl_text_input_plugin_get_instance_private(self));
348
349 std::string text_before_change = priv->text_model->GetText();
350 if (priv->text_model->DeleteSurrounding(offset, n_chars)) {
351 if (priv->enable_delta_model) {
353 text_before_change, priv->text_model->composing_range(),
354 priv->text_model->GetText());
356 } else {
358 }
359 }
360 return TRUE;
361}
362
363// Called when the input method client is set up.
364static FlMethodResponse* set_client(FlTextInputPlugin* self, FlValue* args) {
367 return FL_METHOD_RESPONSE(fl_method_error_response_new(
368 kBadArgumentsError, "Expected 2-element list", nullptr));
369 }
371 fl_text_input_plugin_get_instance_private(self));
372
374 FlValue* config_value = fl_value_get_list_value(args, 1);
375 g_free(priv->input_action);
376 FlValue* input_action_value =
378 if (fl_value_get_type(input_action_value) == FL_VALUE_TYPE_STRING) {
379 priv->input_action = g_strdup(fl_value_get_string(input_action_value));
380 }
381
382 FlValue* enable_delta_model_value =
384 gboolean enable_delta_model = fl_value_get_bool(enable_delta_model_value);
385 priv->enable_delta_model = enable_delta_model;
386
387 // Reset the input type, then set only if appropriate.
388 priv->input_type = kFlTextInputTypeText;
389 FlValue* input_type_value =
391 if (fl_value_get_type(input_type_value) == FL_VALUE_TYPE_MAP) {
392 FlValue* input_type_name =
394 if (fl_value_get_type(input_type_name) == FL_VALUE_TYPE_STRING) {
395 const gchar* input_type = fl_value_get_string(input_type_name);
396 if (g_strcmp0(input_type, kMultilineInputType) == 0) {
397 priv->input_type = kFlTextInputTypeMultiline;
398 } else if (g_strcmp0(input_type, kNoneInputType) == 0) {
399 priv->input_type = kFlTextInputTypeNone;
400 }
401 }
402 }
403
404 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
405}
406
407// Hides the input method.
408static FlMethodResponse* hide(FlTextInputPlugin* self) {
410 fl_text_input_plugin_get_instance_private(self));
411 gtk_im_context_focus_out(priv->im_context);
412
413 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
414}
415
416// Shows the input method.
417static FlMethodResponse* show(FlTextInputPlugin* self) {
419 fl_text_input_plugin_get_instance_private(self));
420 if (priv->input_type == kFlTextInputTypeNone) {
421 return hide(self);
422 }
423
424 gtk_im_context_focus_in(priv->im_context);
425
426 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
427}
428
429// Updates the editing state from Flutter.
430static FlMethodResponse* set_editing_state(FlTextInputPlugin* self,
431 FlValue* args) {
433 fl_text_input_plugin_get_instance_private(self));
434 const gchar* text =
436 priv->text_model->SetText(text);
437
438 int64_t selection_base =
440 int64_t selection_extent =
442 // Flutter uses -1/-1 for invalid; translate that to 0/0 for the model.
443 if (selection_base == -1 && selection_extent == -1) {
444 selection_base = selection_extent = 0;
445 }
446
447 priv->text_model->SetText(text);
448 priv->text_model->SetSelection(
449 flutter::TextRange(selection_base, selection_extent));
450
451 int64_t composing_base =
453 int64_t composing_extent =
455 if (composing_base == -1 && composing_extent == -1) {
456 priv->text_model->EndComposing();
457 } else {
458 size_t composing_start = std::min(composing_base, composing_extent);
459 size_t cursor_offset = selection_base - composing_start;
460 priv->text_model->SetComposingRange(
461 flutter::TextRange(composing_base, composing_extent), cursor_offset);
462 }
463
464 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
465}
466
467// Called when the input method client is complete.
468static FlMethodResponse* clear_client(FlTextInputPlugin* self) {
470 fl_text_input_plugin_get_instance_private(self));
471 priv->client_id = kClientIdUnset;
472
473 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
474}
475
476// Update the IM cursor position.
477//
478// As text is input by the user, the framework sends two streams of updates
479// over the text input channel: updates to the composing rect (cursor rect when
480// not in IME composing mode) and updates to the matrix transform from local
481// coordinates to Flutter root coordinates. This function is called after each
482// of these updates. It transforms the composing rect to GDK window coordinates
483// and notifies GTK of the updated cursor position.
484static void update_im_cursor_position(FlTextInputPlugin* self) {
486 fl_text_input_plugin_get_instance_private(self));
487
488 // Skip update if not composing to avoid setting to position 0.
489 if (!priv->text_model->composing()) {
490 return;
491 }
492
493 // Transform the x, y positions of the cursor from local coordinates to
494 // Flutter view coordinates.
495 gint x = priv->composing_rect.x * priv->editabletext_transform[0][0] +
496 priv->composing_rect.y * priv->editabletext_transform[1][0] +
497 priv->editabletext_transform[3][0] + priv->composing_rect.width;
498 gint y = priv->composing_rect.x * priv->editabletext_transform[0][1] +
499 priv->composing_rect.y * priv->editabletext_transform[1][1] +
500 priv->editabletext_transform[3][1] + priv->composing_rect.height;
501
502 // Transform from Flutter view coordinates to GTK window coordinates.
503 GdkRectangle preedit_rect = {};
505 priv->view_delegate, x, y, &preedit_rect.x, &preedit_rect.y);
506
507 // Set the cursor location in window coordinates so that GTK can position any
508 // system input method windows.
509 gtk_im_context_set_cursor_location(priv->im_context, &preedit_rect);
510}
511
512// Handles updates to the EditableText size and position from the framework.
513//
514// On changes to the size or position of the RenderObject underlying the
515// EditableText, this update may be triggered. It provides an updated size and
516// transform from the local coordinate system of the EditableText to root
517// Flutter coordinate system.
518static FlMethodResponse* set_editable_size_and_transform(
519 FlTextInputPlugin* self,
520 FlValue* args) {
522 size_t transform_len = fl_value_get_length(transform);
523 g_warn_if_fail(transform_len == 16);
524
525 for (size_t i = 0; i < transform_len; ++i) {
528 fl_text_input_plugin_get_instance_private(self));
529 priv->editabletext_transform[i / 4][i % 4] = val;
530 }
532
533 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
534}
535
536// Handles updates to the composing rect from the framework.
537//
538// On changes to the state of the EditableText in the framework, this update
539// may be triggered. It provides an updated rect for the composing region in
540// local coordinates of the EditableText. In the case where there is no
541// composing region, the cursor rect is sent.
542static FlMethodResponse* set_marked_text_rect(FlTextInputPlugin* self,
543 FlValue* args) {
545 fl_text_input_plugin_get_instance_private(self));
546 priv->composing_rect.x =
548 priv->composing_rect.y =
550 priv->composing_rect.width =
552 priv->composing_rect.height =
555
556 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
557}
558
559// Called when a method call is received from Flutter.
560static void method_call_cb(FlMethodChannel* channel,
561 FlMethodCall* method_call,
562 gpointer user_data) {
563 FlTextInputPlugin* self = FL_TEXT_INPUT_PLUGIN(user_data);
564
565 const gchar* method = fl_method_call_get_name(method_call);
567
568 g_autoptr(FlMethodResponse) response = nullptr;
569 if (strcmp(method, kSetClientMethod) == 0) {
570 response = set_client(self, args);
571 } else if (strcmp(method, kShowMethod) == 0) {
572 response = show(self);
573 } else if (strcmp(method, kSetEditingStateMethod) == 0) {
574 response = set_editing_state(self, args);
575 } else if (strcmp(method, kClearClientMethod) == 0) {
576 response = clear_client(self);
577 } else if (strcmp(method, kHideMethod) == 0) {
578 response = hide(self);
579 } else if (strcmp(method, kSetEditableSizeAndTransform) == 0) {
581 } else if (strcmp(method, kSetMarkedTextRect) == 0) {
582 response = set_marked_text_rect(self, args);
583 } else {
584 response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
585 }
586
587 g_autoptr(GError) error = nullptr;
588 if (!fl_method_call_respond(method_call, response, &error)) {
589 g_warning("Failed to send method call response: %s", error->message);
590 }
591}
592
593// Disposes of an FlTextInputPlugin.
594static void fl_text_input_plugin_dispose(GObject* object) {
595 FlTextInputPlugin* self = FL_TEXT_INPUT_PLUGIN(object);
597 fl_text_input_plugin_get_instance_private(self));
598
599 g_clear_object(&priv->channel);
600 g_clear_pointer(&priv->input_action, g_free);
601 g_clear_object(&priv->im_context);
602 if (priv->text_model != nullptr) {
603 delete priv->text_model;
604 priv->text_model = nullptr;
605 }
606 if (priv->view_delegate != nullptr) {
607 g_object_remove_weak_pointer(
608 G_OBJECT(priv->view_delegate),
609 reinterpret_cast<gpointer*>(&(priv->view_delegate)));
610 priv->view_delegate = nullptr;
611 }
612
613 G_OBJECT_CLASS(fl_text_input_plugin_parent_class)->dispose(object);
614}
615
616// Implements FlTextInputPlugin::filter_keypress.
618 FlTextInputPlugin* self,
619 FlKeyEvent* event) {
620 g_return_val_if_fail(FL_IS_TEXT_INPUT_PLUGIN(self), false);
621
623 fl_text_input_plugin_get_instance_private(self));
624
625 if (priv->client_id == kClientIdUnset) {
626 return FALSE;
627 }
628
629 if (gtk_im_context_filter_keypress(
630 priv->im_context, reinterpret_cast<GdkEventKey*>(event->origin))) {
631 return TRUE;
632 }
633
634 std::string text_before_change = priv->text_model->GetText();
635 flutter::TextRange selection_before_change = priv->text_model->selection();
636 std::string text = priv->text_model->GetText();
637
638 // Handle the enter/return key.
639 gboolean do_action = FALSE;
640 // Handle navigation keys.
641 gboolean changed = FALSE;
642 if (event->is_press) {
643 switch (event->keyval) {
644 case GDK_KEY_End:
645 case GDK_KEY_KP_End:
646 if (event->state & GDK_SHIFT_MASK) {
647 changed = priv->text_model->SelectToEnd();
648 } else {
649 changed = priv->text_model->MoveCursorToEnd();
650 }
651 break;
652 case GDK_KEY_Return:
653 case GDK_KEY_KP_Enter:
654 case GDK_KEY_ISO_Enter:
655 if (priv->input_type == kFlTextInputTypeMultiline &&
656 strcmp(priv->input_action, kNewlineInputAction) == 0) {
657 priv->text_model->AddCodePoint('\n');
658 text = "\n";
659 changed = TRUE;
660 }
661 do_action = TRUE;
662 break;
663 case GDK_KEY_Home:
664 case GDK_KEY_KP_Home:
665 if (event->state & GDK_SHIFT_MASK) {
666 changed = priv->text_model->SelectToBeginning();
667 } else {
668 changed = priv->text_model->MoveCursorToBeginning();
669 }
670 break;
671 case GDK_KEY_BackSpace:
672 case GDK_KEY_Delete:
673 case GDK_KEY_KP_Delete:
674 case GDK_KEY_Left:
675 case GDK_KEY_KP_Left:
676 case GDK_KEY_Right:
677 case GDK_KEY_KP_Right:
678 // Already handled inside the framework in RenderEditable.
679 break;
680 }
681 }
682
683 if (changed) {
684 if (priv->enable_delta_model) {
686 text_before_change, selection_before_change, text);
688 } else {
690 }
691 }
692 if (do_action) {
694 }
695
696 return changed;
697}
698
699// Initializes the FlTextInputPlugin class.
700static void fl_text_input_plugin_class_init(FlTextInputPluginClass* klass) {
701 G_OBJECT_CLASS(klass)->dispose = fl_text_input_plugin_dispose;
702 FL_TEXT_INPUT_PLUGIN_CLASS(klass)->filter_keypress =
704}
705
706// Initializes an instance of the FlTextInputPlugin class.
707static void fl_text_input_plugin_init(FlTextInputPlugin* self) {
709 fl_text_input_plugin_get_instance_private(self));
710
711 priv->client_id = kClientIdUnset;
712 priv->input_type = kFlTextInputTypeText;
713 priv->text_model = new flutter::TextInputModel();
714}
715
716static void init_im_context(FlTextInputPlugin* self, GtkIMContext* im_context) {
718 fl_text_input_plugin_get_instance_private(self));
719 priv->im_context = GTK_IM_CONTEXT(g_object_ref(im_context));
720
721 // On Wayland, this call sets up the input method so it can be enabled
722 // immediately when required. Without it, on-screen keyboard's don't come up
723 // the first time a text field is focused.
724 gtk_im_context_focus_out(priv->im_context);
725
726 g_signal_connect_object(priv->im_context, "preedit-start",
727 G_CALLBACK(im_preedit_start_cb), self,
728 G_CONNECT_SWAPPED);
729 g_signal_connect_object(priv->im_context, "preedit-end",
730 G_CALLBACK(im_preedit_end_cb), self,
731 G_CONNECT_SWAPPED);
732 g_signal_connect_object(priv->im_context, "preedit-changed",
733 G_CALLBACK(im_preedit_changed_cb), self,
734 G_CONNECT_SWAPPED);
735 g_signal_connect_object(priv->im_context, "commit", G_CALLBACK(im_commit_cb),
736 self, G_CONNECT_SWAPPED);
737 g_signal_connect_object(priv->im_context, "retrieve-surrounding",
738 G_CALLBACK(im_retrieve_surrounding_cb), self,
739 G_CONNECT_SWAPPED);
740 g_signal_connect_object(priv->im_context, "delete-surrounding",
741 G_CALLBACK(im_delete_surrounding_cb), self,
742 G_CONNECT_SWAPPED);
743}
744
745FlTextInputPlugin* fl_text_input_plugin_new(
746 FlBinaryMessenger* messenger,
747 GtkIMContext* im_context,
748 FlTextInputViewDelegate* view_delegate) {
749 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
750 g_return_val_if_fail(GTK_IS_IM_CONTEXT(im_context), nullptr);
751 g_return_val_if_fail(FL_IS_TEXT_INPUT_VIEW_DELEGATE(view_delegate), nullptr);
752
753 FlTextInputPlugin* self = FL_TEXT_INPUT_PLUGIN(
754 g_object_new(fl_text_input_plugin_get_type(), nullptr));
755
756 g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
758 fl_text_input_plugin_get_instance_private(self));
759 priv->channel =
760 fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
762 nullptr);
763
764 init_im_context(self, im_context);
765
766 priv->view_delegate = view_delegate;
768 G_OBJECT(view_delegate),
769 reinterpret_cast<gpointer*>(&(priv->view_delegate)));
770
771 return self;
772}
773
774// Filters the a keypress given to the plugin through the plugin's
775// filter_keypress callback.
776gboolean fl_text_input_plugin_filter_keypress(FlTextInputPlugin* self,
777 FlKeyEvent* event) {
778 g_return_val_if_fail(FL_IS_TEXT_INPUT_PLUGIN(self), FALSE);
779 if (FL_TEXT_INPUT_PLUGIN_GET_CLASS(self)->filter_keypress) {
780 return FL_TEXT_INPUT_PLUGIN_GET_CLASS(self)->filter_keypress(self, event);
781 }
782 return FALSE;
783}
size_t base() const
Definition text_range.h:30
size_t extent() const
Definition text_range.h:36
static gboolean finish_method(GObject *object, GAsyncResult *result, GError **error)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
FlKeyEvent * event
G_MODULE_EXPORT const gchar * fl_method_call_get_name(FlMethodCall *self)
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
G_MODULE_EXPORT FlMethodResponse * fl_method_channel_invoke_method_finish(FlMethodChannel *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
G_MODULE_EXPORT void fl_method_channel_invoke_method(FlMethodChannel *self, const gchar *method, FlValue *args, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
FlPixelBufferTexturePrivate * priv
uint8_t value
static constexpr char kClearClientMethod[]
static constexpr char kSetEditableSizeAndTransform[]
static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)
static constexpr char kSetMarkedTextRect[]
static constexpr char kEnableDeltaModel[]
GAsyncResult * result
static constexpr char kNoneInputType[]
static FlMethodResponse * clear_client(FlTextInputPlugin *self)
return fl_method_response_get_result(response, error) !
static void init_im_context(FlTextInputPlugin *self, GtkIMContext *im_context)
static void im_preedit_changed_cb(FlTextInputPlugin *self)
static constexpr char kTransform[]
static constexpr char kSelectionIsDirectionalKey[]
static constexpr char kHideMethod[]
static constexpr char kMultilineInputType[]
static FlMethodResponse * set_client(FlTextInputPlugin *self, FlValue *args)
static void update_editing_state_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static constexpr char kSetEditingStateMethod[]
static constexpr char kSelectionExtentKey[]
static FlMethodResponse * show(FlTextInputPlugin *self)
static void perform_action_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static constexpr char kTextInputTypeNameKey[]
static FlMethodResponse * set_marked_text_rect(FlTextInputPlugin *self, FlValue *args)
static FlMethodResponse * set_editable_size_and_transform(FlTextInputPlugin *self, FlValue *args)
static constexpr char kNewlineInputAction[]
static void update_editing_state(FlTextInputPlugin *self)
static constexpr char kChannelName[]
static void im_preedit_end_cb(FlTextInputPlugin *self)
static void perform_action(FlTextInputPlugin *self)
G_DEFINE_TYPE_WITH_PRIVATE(FlTextInputPlugin, fl_text_input_plugin, G_TYPE_OBJECT) static gboolean finish_method(GObject *object
static constexpr char kSetClientMethod[]
static gboolean im_delete_surrounding_cb(FlTextInputPlugin *self, gint offset, gint n_chars)
static void fl_text_input_plugin_dispose(GObject *object)
static constexpr char kUpdateEditingStateMethod[]
gboolean fl_text_input_plugin_filter_keypress(FlTextInputPlugin *self, FlKeyEvent *event)
static constexpr char kTextInputTypeKey[]
static void update_editing_state_with_delta(FlTextInputPlugin *self, flutter::TextEditingDelta *delta)
static constexpr char kBadArgumentsError[]
static constexpr char kPerformActionMethod[]
static constexpr char kShowMethod[]
static void update_im_cursor_position(FlTextInputPlugin *self)
static gboolean fl_text_input_plugin_filter_keypress_default(FlTextInputPlugin *self, FlKeyEvent *event)
static FlMethodResponse * hide(FlTextInputPlugin *self)
static constexpr char kComposingExtentKey[]
static constexpr char kTextAffinityDownstream[]
static void im_preedit_start_cb(FlTextInputPlugin *self)
static void im_commit_cb(FlTextInputPlugin *self, const gchar *text)
GAsyncResult GError ** error
static constexpr char kInputActionKey[]
static constexpr char kComposingBaseKey[]
static constexpr char kUpdateEditingStateWithDeltasMethod[]
static FlMethodResponse * set_editing_state(FlTextInputPlugin *self, FlValue *args)
FlTextInputPlugin * fl_text_input_plugin_new(FlBinaryMessenger *messenger, GtkIMContext *im_context, FlTextInputViewDelegate *view_delegate)
static gboolean im_retrieve_surrounding_cb(FlTextInputPlugin *self)
static void fl_text_input_plugin_init(FlTextInputPlugin *self)
@ kFlTextInputTypeNone
@ kFlTextInputTypeMultiline
@ kFlTextInputTypeText
static constexpr int64_t kClientIdUnset
static constexpr char kSelectionAffinityKey[]
static constexpr char kTextKey[]
static void fl_text_input_plugin_class_init(FlTextInputPluginClass *klass)
static constexpr char kSelectionBaseKey[]
void fl_text_input_view_delegate_translate_coordinates(FlTextInputViewDelegate *self, gint view_x, gint view_y, gint *window_x, gint *window_y)
G_MODULE_EXPORT void fl_value_set_string(FlValue *self, const gchar *key, FlValue *value)
Definition fl_value.cc:638
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition fl_value.cc:366
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition fl_value.cc:649
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition fl_value.cc:810
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition fl_value.cc:667
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition fl_value.cc:465
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition fl_value.cc:276
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition fl_value.cc:591
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition fl_value.cc:255
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition fl_value.cc:681
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition fl_value.cc:262
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition fl_value.cc:599
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition fl_value.cc:775
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition fl_value.cc:660
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition fl_value.cc:349
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition fl_value.cc:674
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition fl_value.cc:723
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition fl_value.h:42
@ FL_VALUE_TYPE_STRING
Definition fl_value.h:69
@ FL_VALUE_TYPE_LIST
Definition fl_value.h:74
@ FL_VALUE_TYPE_MAP
Definition fl_value.h:75
g_object_add_weak_pointer(G_OBJECT(self), reinterpret_cast< gpointer * >(&self->engine))
std::u16string text
return FALSE
double y
double x
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
Point offset
flutter::TextInputModel * text_model
FlTextInputViewDelegate * view_delegate
GdkEvent * origin
GdkModifierType state
A change in the state of an input field.