From 9a7b556f915e0f5a846218c4e0461b111db58169 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Tue, 6 Oct 2020 16:29:12 -0700 Subject: [PATCH] hasStrings Linux (#21388) hasStrings method for clipboard status, Linux --- shell/platform/linux/fl_platform_plugin.cc | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/shell/platform/linux/fl_platform_plugin.cc b/shell/platform/linux/fl_platform_plugin.cc index 55aa6b002..314b32939 100644 --- a/shell/platform/linux/fl_platform_plugin.cc +++ b/shell/platform/linux/fl_platform_plugin.cc @@ -17,8 +17,10 @@ static constexpr char kUnknownClipboardFormatError[] = static constexpr char kFailedError[] = "Failed"; static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"; static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; +static constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings"; static constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop"; static constexpr char kTextKey[] = "text"; +static constexpr char kValueKey[] = "value"; static constexpr char kTextPlainFormat[] = "text/plain"; @@ -56,6 +58,22 @@ static void clipboard_text_cb(GtkClipboard* clipboard, send_response(method_call, response); } +// Called when clipboard text received during has_strings. +static void clipboard_text_has_strings_cb(GtkClipboard* clipboard, + const gchar* text, + gpointer user_data) { + g_autoptr(FlMethodCall) method_call = FL_METHOD_CALL(user_data); + + g_autoptr(FlValue) result = fl_value_new_map(); + fl_value_set_string_take( + result, kValueKey, + fl_value_new_bool(text != nullptr && strlen(text) > 0)); + + g_autoptr(FlMethodResponse) response = + FL_METHOD_RESPONSE(fl_method_success_response_new(result)); + send_response(method_call, response); +} + // Called when Flutter wants to copy to the clipboard. static FlMethodResponse* clipboard_set_data(FlPlatformPlugin* self, FlValue* args) { @@ -100,7 +118,21 @@ static FlMethodResponse* clipboard_get_data_async(FlPlatformPlugin* self, gtk_clipboard_request_text(clipboard, clipboard_text_cb, g_object_ref(method_call)); - // Will response later. + // Will respond later. + return nullptr; +} + +// Called when Flutter wants to know if the content of the clipboard is able to +// be pasted, without actually accessing the clipboard content itself. +static FlMethodResponse* clipboard_has_strings_async( + FlPlatformPlugin* self, + FlMethodCall* method_call) { + GtkClipboard* clipboard = + gtk_clipboard_get_default(gdk_display_get_default()); + gtk_clipboard_request_text(clipboard, clipboard_text_has_strings_cb, + g_object_ref(method_call)); + + // Will respond later. return nullptr; } @@ -131,6 +163,8 @@ static void method_call_cb(FlMethodChannel* channel, response = clipboard_set_data(self, args); } else if (strcmp(method, kGetClipboardDataMethod) == 0) { response = clipboard_get_data_async(self, method_call); + } else if (strcmp(method, kClipboardHasStringsMethod) == 0) { + response = clipboard_has_strings_async(self, method_call); } else if (strcmp(method, kSystemNavigatorPopMethod) == 0) { response = system_navigator_pop(self); } else { -- GitLab