mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:07:45 +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:
parent
802f541184
commit
51146e3075
12 changed files with 43 additions and 38 deletions
|
@ -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)
|
||||
|
|
|
@ -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>);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -550,7 +550,7 @@ void TerminalWidget::paste()
|
|||
auto text = GUI::Clipboard::the().data();
|
||||
if (text.is_empty())
|
||||
return;
|
||||
int nwritten = write(m_ptm_fd, text.characters(), text.length());
|
||||
int nwritten = write(m_ptm_fd, text.data(), text.size());
|
||||
if (nwritten < 0) {
|
||||
perror("write");
|
||||
ASSERT_NOT_REACHED();
|
||||
|
@ -560,7 +560,7 @@ void TerminalWidget::paste()
|
|||
void TerminalWidget::copy()
|
||||
{
|
||||
if (has_selection())
|
||||
GUI::Clipboard::the().set_data(selected_text());
|
||||
GUI::Clipboard::the().set_plain_text(selected_text());
|
||||
}
|
||||
|
||||
void TerminalWidget::mouseup_event(GUI::MouseEvent& event)
|
||||
|
@ -829,7 +829,7 @@ void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event)
|
|||
m_context_menu_for_hyperlink->add_action(action);
|
||||
}
|
||||
m_context_menu_for_hyperlink->add_action(GUI::Action::create("Copy URL", [this](auto&) {
|
||||
GUI::Clipboard::the().set_data(m_context_menu_href);
|
||||
GUI::Clipboard::the().set_plain_text(m_context_menu_href);
|
||||
}));
|
||||
m_context_menu_for_hyperlink->add_separator();
|
||||
m_context_menu_for_hyperlink->add_action(copy_action());
|
||||
|
|
|
@ -66,7 +66,7 @@ InProcessWebView::InProcessWebView()
|
|||
set_background_role(ColorRole::Base);
|
||||
|
||||
m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
|
||||
GUI::Clipboard::the().set_data(selected_text(), "text/plain");
|
||||
GUI::Clipboard::the().set_plain_text(selected_text());
|
||||
});
|
||||
|
||||
m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue