1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:17:34 +00:00

LibGUI: Make the Clipboard API deal in raw byte buffers a bit more

To open up for putting not just text/plain content on the clipboard,
let's make the GUI::Clipboard API a bit more raw-data-friendly. :^)
This commit is contained in:
Andreas Kling 2020-09-05 16:16:01 +02:00
parent 802f541184
commit 51146e3075
12 changed files with 43 additions and 38 deletions

View file

@ -90,26 +90,26 @@ Clipboard::DataAndType Clipboard::data_and_type() const
dbgprintf("GUI::Clipboard::data() clipping contents size is greater than shared buffer size\n");
return {};
}
auto data = String((const char*)shared_buffer->data(), response->data_size());
auto data = ByteBuffer::copy(shared_buffer->data(), response->data_size());
auto type = response->mime_type();
return { data, type };
}
void Clipboard::set_data(const StringView& data, const String& type)
void Clipboard::set_data(ReadonlyBytes data, const String& type)
{
auto shared_buffer = SharedBuffer::create_with_size(data.length() + 1);
auto shared_buffer = SharedBuffer::create_with_size(data.size() + 1);
if (!shared_buffer) {
dbgprintf("GUI::Clipboard::set_data() failed to create a shared buffer\n");
return;
}
if (!data.is_empty())
memcpy(shared_buffer->data(), data.characters_without_null_termination(), data.length() + 1);
memcpy(shared_buffer->data(), data.data(), data.size() + 1);
else
((u8*)shared_buffer->data())[0] = '\0';
shared_buffer->seal();
shared_buffer->share_with(connection().server_pid());
connection().send_sync<Messages::ClipboardServer::SetClipboardData>(shared_buffer->shbuf_id(), data.length(), type);
connection().send_sync<Messages::ClipboardServer::SetClipboardData>(shared_buffer->shbuf_id(), data.size(), type);
}
void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message)

View file

@ -36,18 +36,23 @@ class Clipboard {
public:
static Clipboard& the();
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/plain");
ByteBuffer data() const { return data_and_type().data; }
String mime_type() const { return data_and_type().mime_type; }
void set_data(ReadonlyBytes, const String& mime_type = "text/plain");
void set_plain_text(const String& text)
{
set_data(text.bytes());
}
struct DataAndType {
String data;
String type;
ByteBuffer data;
String mime_type;
};
DataAndType data_and_type() const;
Function<void(const String& data_type)> on_change;
Function<void(const String& mime_type)> on_change;
static void initialize(Badge<Application>);

View file

@ -1300,7 +1300,7 @@ void TextEditor::cut()
return;
auto selected_text = this->selected_text();
printf("Cut: \"%s\"\n", selected_text.characters());
Clipboard::the().set_data(selected_text);
Clipboard::the().set_plain_text(selected_text);
delete_selection();
}
@ -1308,7 +1308,7 @@ void TextEditor::copy()
{
auto selected_text = this->selected_text();
printf("Copy: \"%s\"\n", selected_text.characters());
Clipboard::the().set_data(selected_text);
Clipboard::the().set_plain_text(selected_text);
}
void TextEditor::paste()
@ -1317,7 +1317,7 @@ void TextEditor::paste()
return;
auto paste_text = Clipboard::the().data();
printf("Paste: \"%s\"\n", paste_text.characters());
printf("Paste: \"%s\"\n", String::copy(paste_text).characters());
TemporaryChange change(m_automatic_indentation_enabled, false);
insert_at_cursor_or_replace_selection(paste_text);