1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

WindowServer+LibGUI: Store a "data type" with the clipboard content

This will allow us to distinguish between different types of data
stored on the clipboard.
This commit is contained in:
Andreas Kling 2019-09-14 09:19:05 +02:00
parent 9d2c4d223a
commit c543ee5c5b
10 changed files with 94 additions and 15 deletions

View file

@ -15,7 +15,7 @@ GClipboard::GClipboard()
{
}
String GClipboard::data() const
GClipboard::DataAndType GClipboard::data_and_type() const
{
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::GetClipboardContents;
@ -31,10 +31,12 @@ String GClipboard::data() const
dbgprintf("GClipboard::data() clipping contents size is greater than shared buffer size\n");
return {};
}
return String((const char*)shared_buffer->data(), response.clipboard.contents_size);
auto data = String((const char*)shared_buffer->data(), response.clipboard.contents_size);
auto type = String(response.text, response.text_length);
return { data, type };
}
void GClipboard::set_data(const StringView& data)
void GClipboard::set_data(const StringView& data, const String& type)
{
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::SetClipboardContents;
@ -51,6 +53,18 @@ void GClipboard::set_data(const StringView& data)
shared_buffer->share_with(GWindowServerConnection::the().server_pid());
request.clipboard.shared_buffer_id = shared_buffer->shared_buffer_id();
request.clipboard.contents_size = data.length();
ASSERT(type.length() < (ssize_t)sizeof(request.text));
if (!type.is_null())
strcpy(request.text, type.characters());
request.text_length = type.length();
auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidSetClipboardContents);
ASSERT(response.clipboard.shared_buffer_id == shared_buffer->shared_buffer_id());
}
void GClipboard::did_receive_clipboard_contents_changed(Badge<GWindowServerConnection>, const String& data_type)
{
if (on_content_change)
on_content_change(data_type);
}

View file

@ -1,13 +1,29 @@
#pragma once
#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/String.h>
class GWindowServerConnection;
class GClipboard {
public:
static GClipboard& the();
String data() const;
void set_data(const StringView&);
String data() const { return data_and_type().data; }
String type() const { return data_and_type().type; }
void set_data(const StringView&, const String& data_type = "text");
struct DataAndType {
String data;
String type;
};
DataAndType data_and_type() const;
void did_receive_clipboard_contents_changed(Badge<GWindowServerConnection>, const String& data_type);
Function<void(const String& data_type)> on_content_change;
private:
GClipboard();

View file

@ -15,6 +15,7 @@
#include <LibCore/CObject.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GClipboard.h>
#include <LibGUI/GDesktop.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GWidget.h>
@ -244,6 +245,11 @@ void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>&
continue;
}
if (event.type == WSAPI_ServerMessage::Type::ClipboardContentsChanged) {
GClipboard::the().did_receive_clipboard_contents_changed({}, String(event.text, event.text_length));
continue;
}
if (event.type == WSAPI_ServerMessage::Error) {
dbgprintf("GEventLoop got error message from server\n");
dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());