Skip to content

Commit cc191bc

Browse files
justinmcgspencergoog
authored andcommitted
hasStrings Linux (flutter#21388)
hasStrings method for clipboard status, Linux
1 parent afa0ffe commit cc191bc

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

shell/platform/linux/fl_platform_plugin.cc

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ static constexpr char kUnknownClipboardFormatError[] =
1717
static constexpr char kFailedError[] = "Failed";
1818
static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
1919
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
20+
static constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings";
2021
static constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
2122
static constexpr char kTextKey[] = "text";
23+
static constexpr char kValueKey[] = "value";
2224

2325
static constexpr char kTextPlainFormat[] = "text/plain";
2426

@@ -56,6 +58,22 @@ static void clipboard_text_cb(GtkClipboard* clipboard,
5658
send_response(method_call, response);
5759
}
5860

61+
// Called when clipboard text received during has_strings.
62+
static void clipboard_text_has_strings_cb(GtkClipboard* clipboard,
63+
const gchar* text,
64+
gpointer user_data) {
65+
g_autoptr(FlMethodCall) method_call = FL_METHOD_CALL(user_data);
66+
67+
g_autoptr(FlValue) result = fl_value_new_map();
68+
fl_value_set_string_take(
69+
result, kValueKey,
70+
fl_value_new_bool(text != nullptr && strlen(text) > 0));
71+
72+
g_autoptr(FlMethodResponse) response =
73+
FL_METHOD_RESPONSE(fl_method_success_response_new(result));
74+
send_response(method_call, response);
75+
}
76+
5977
// Called when Flutter wants to copy to the clipboard.
6078
static FlMethodResponse* clipboard_set_data(FlPlatformPlugin* self,
6179
FlValue* args) {
@@ -100,7 +118,21 @@ static FlMethodResponse* clipboard_get_data_async(FlPlatformPlugin* self,
100118
gtk_clipboard_request_text(clipboard, clipboard_text_cb,
101119
g_object_ref(method_call));
102120

103-
// Will response later.
121+
// Will respond later.
122+
return nullptr;
123+
}
124+
125+
// Called when Flutter wants to know if the content of the clipboard is able to
126+
// be pasted, without actually accessing the clipboard content itself.
127+
static FlMethodResponse* clipboard_has_strings_async(
128+
FlPlatformPlugin* self,
129+
FlMethodCall* method_call) {
130+
GtkClipboard* clipboard =
131+
gtk_clipboard_get_default(gdk_display_get_default());
132+
gtk_clipboard_request_text(clipboard, clipboard_text_has_strings_cb,
133+
g_object_ref(method_call));
134+
135+
// Will respond later.
104136
return nullptr;
105137
}
106138

@@ -131,6 +163,8 @@ static void method_call_cb(FlMethodChannel* channel,
131163
response = clipboard_set_data(self, args);
132164
} else if (strcmp(method, kGetClipboardDataMethod) == 0) {
133165
response = clipboard_get_data_async(self, method_call);
166+
} else if (strcmp(method, kClipboardHasStringsMethod) == 0) {
167+
response = clipboard_has_strings_async(self, method_call);
134168
} else if (strcmp(method, kSystemNavigatorPopMethod) == 0) {
135169
response = system_navigator_pop(self);
136170
} else {

0 commit comments

Comments
 (0)