Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

hasStrings Linux #21388

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion shell/platform/linux/fl_platform_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robert-ancell Is there a reason to use the *_request_* APIs over the *_wait_* APIs? Otherwise this could all just be a call to gtk_clipboard_wait_is_text_available ()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm merging this PR, but feel free to reply if anyone thinks this doesn't seem right and I'm happy to take a look at changing this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The *_wait_* methods would block and cause no other Flutter method calls etc to be handled. So the implemented method is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good to know, thanks for following up @robert-ancell !

g_object_ref(method_call));

// Will respond later.
return nullptr;
}

Expand Down Expand Up @@ -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 {
Expand Down