1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:17:44 +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

@ -160,7 +160,7 @@ Tab::Tab(Type type)
hooks().on_link_click(m_link_context_menu_url, "_blank", 0); hooks().on_link_click(m_link_context_menu_url, "_blank", 0);
})); }));
m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) { m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) {
GUI::Clipboard::the().set_data(m_link_context_menu_url.to_string()); GUI::Clipboard::the().set_plain_text(m_link_context_menu_url.to_string());
})); }));
m_link_context_menu->add_separator(); m_link_context_menu->add_separator();
m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) { m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) {

View file

@ -368,7 +368,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto url = URL::create_with_file_protocol(path); auto url = URL::create_with_file_protocol(path);
copy_text.appendf("%s\n", url.to_string().characters()); copy_text.appendf("%s\n", url.to_string().characters());
} }
GUI::Clipboard::the().set_data(copy_text.build(), "text/uri-list"); GUI::Clipboard::the().set_data(copy_text.build().bytes(), "text/uri-list");
}, },
window); window);
copy_action->set_enabled(false); copy_action->set_enabled(false);
@ -407,11 +407,11 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto do_paste = [&](const GUI::Action& action) { auto do_paste = [&](const GUI::Action& action) {
auto data_and_type = GUI::Clipboard::the().data_and_type(); auto data_and_type = GUI::Clipboard::the().data_and_type();
if (data_and_type.type != "text/uri-list") { if (data_and_type.mime_type != "text/uri-list") {
dbg() << "Cannot paste clipboard type " << data_and_type.type; dbg() << "Cannot paste clipboard type " << data_and_type.mime_type;
return; return;
} }
auto copied_lines = data_and_type.data.split('\n'); auto copied_lines = String::copy(data_and_type.data).split('\n');
if (copied_lines.is_empty()) { if (copied_lines.is_empty()) {
dbg() << "No files to paste"; dbg() << "No files to paste";
return; return;
@ -638,7 +638,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
return; return;
} }
paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list"); paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().mime_type() == "text/uri-list");
go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1); go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1);
go_back_action->set_enabled(directory_view.path_history_position() > 0); go_back_action->set_enabled(directory_view.path_history_position() > 0);
open_parent_directory_action->set_enabled(new_path != "/"); open_parent_directory_action->set_enabled(new_path != "/");
@ -705,7 +705,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto& node = directory_view.node(index); auto& node = directory_view.node(index);
if (node.is_directory()) { if (node.is_directory()) {
auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().type() == "text/uri-list"; auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().mime_type() == "text/uri-list";
folder_specific_paste_action->set_enabled(should_get_enabled); folder_specific_paste_action->set_enabled(should_get_enabled);
directory_context_menu->popup(event.screen_position()); directory_context_menu->popup(event.screen_position());
} else { } else {
@ -822,7 +822,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
directory_view.open(initial_location); directory_view.open(initial_location);
directory_view.set_focus(true); directory_view.set_focus(true);
paste_action->set_enabled(GUI::Clipboard::the().type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0); paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0);
window->show(); window->show();

View file

@ -139,7 +139,7 @@ bool HexEditor::copy_selected_hex_to_clipboard()
output_string_builder.appendf("%02X ", m_buffer.data()[i]); output_string_builder.appendf("%02X ", m_buffer.data()[i]);
} }
GUI::Clipboard::the().set_data(output_string_builder.to_string()); GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
return true; return true;
} }
@ -153,7 +153,7 @@ bool HexEditor::copy_selected_text_to_clipboard()
output_string_builder.appendf("%c", isprint(m_buffer.data()[i]) ? m_buffer[i] : '.'); output_string_builder.appendf("%c", isprint(m_buffer.data()[i]) ? m_buffer[i] : '.');
} }
GUI::Clipboard::the().set_data(output_string_builder.to_string()); GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
return true; return true;
} }
@ -176,7 +176,7 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
} }
output_string_builder.append("\n};\n"); output_string_builder.append("\n};\n");
GUI::Clipboard::the().set_data(output_string_builder.to_string()); GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
return true; return true;
} }

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"); dbgprintf("GUI::Clipboard::data() clipping contents size is greater than shared buffer size\n");
return {}; 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(); auto type = response->mime_type();
return { data, 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) { if (!shared_buffer) {
dbgprintf("GUI::Clipboard::set_data() failed to create a shared buffer\n"); dbgprintf("GUI::Clipboard::set_data() failed to create a shared buffer\n");
return; return;
} }
if (!data.is_empty()) 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 else
((u8*)shared_buffer->data())[0] = '\0'; ((u8*)shared_buffer->data())[0] = '\0';
shared_buffer->seal(); shared_buffer->seal();
shared_buffer->share_with(connection().server_pid()); 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) void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message)

View file

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

View file

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

View file

@ -550,7 +550,7 @@ void TerminalWidget::paste()
auto text = GUI::Clipboard::the().data(); auto text = GUI::Clipboard::the().data();
if (text.is_empty()) if (text.is_empty())
return; return;
int nwritten = write(m_ptm_fd, text.characters(), text.length()); int nwritten = write(m_ptm_fd, text.data(), text.size());
if (nwritten < 0) { if (nwritten < 0) {
perror("write"); perror("write");
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
@ -560,7 +560,7 @@ void TerminalWidget::paste()
void TerminalWidget::copy() void TerminalWidget::copy()
{ {
if (has_selection()) 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) 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(action);
} }
m_context_menu_for_hyperlink->add_action(GUI::Action::create("Copy URL", [this](auto&) { 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_separator();
m_context_menu_for_hyperlink->add_action(copy_action()); m_context_menu_for_hyperlink->add_action(copy_action());

View file

@ -66,7 +66,7 @@ InProcessWebView::InProcessWebView()
set_background_role(ColorRole::Base); set_background_role(ColorRole::Base);
m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) { 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&) { m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {

View file

@ -54,11 +54,11 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode
auto& data_and_type = m_history_items[index.row()]; auto& data_and_type = m_history_items[index.row()];
switch (index.column()) { switch (index.column()) {
case Column::Data: case Column::Data:
if (data_and_type.type.starts_with("text/")) if (data_and_type.mime_type.starts_with("text/"))
return data_and_type.data; return String::copy(data_and_type.data);
return "<...>"; return "<...>";
case Column::Type: case Column::Type:
return data_and_type.type; return data_and_type.mime_type;
default: default:
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }

View file

@ -70,7 +70,7 @@ int main(int argc, char* argv[])
table_view.on_activation = [&](const GUI::ModelIndex& index) { table_view.on_activation = [&](const GUI::ModelIndex& index) {
auto& data_and_type = model->item_at(index.row()); auto& data_and_type = model->item_at(index.row());
GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.type); GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.mime_type);
}; };
auto applet_window = GUI::Window::construct(); auto applet_window = GUI::Window::construct();

View file

@ -80,7 +80,7 @@ int main(int argc, char* argv[])
Options options = parse_options(argc, argv); Options options = parse_options(argc, argv);
auto& clipboard = GUI::Clipboard::the(); auto& clipboard = GUI::Clipboard::the();
clipboard.set_data(options.data, options.type); clipboard.set_data(options.data.bytes(), options.type);
return 0; return 0;
} }

View file

@ -46,19 +46,19 @@ int main(int argc, char* argv[])
auto& clipboard = GUI::Clipboard::the(); auto& clipboard = GUI::Clipboard::the();
auto data_and_type = clipboard.data_and_type(); auto data_and_type = clipboard.data_and_type();
if (data_and_type.type.is_null()) { if (data_and_type.mime_type.is_null()) {
fprintf(stderr, "Nothing copied\n"); fprintf(stderr, "Nothing copied\n");
return 1; return 1;
} }
if (!print_type) { if (!print_type) {
printf("%s", data_and_type.data.characters()); printf("%s", data_and_type.data.data());
// Append a newline to text contents, but // Append a newline to text contents, but
// only if we're not asked not to do this. // only if we're not asked not to do this.
if (data_and_type.type.starts_with("text/") && !no_newline) if (data_and_type.mime_type.starts_with("text/") && !no_newline)
putchar('\n'); putchar('\n');
} else { } else {
printf("%s\n", data_and_type.type.characters()); printf("%s\n", data_and_type.mime_type.characters());
} }
return 0; return 0;