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
Notes: sideshowbarker 2024-07-19 02:54:42 +09:00
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);
}));
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_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);
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);
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 data_and_type = GUI::Clipboard::the().data_and_type();
if (data_and_type.type != "text/uri-list") {
dbg() << "Cannot paste clipboard type " << data_and_type.type;
if (data_and_type.mime_type != "text/uri-list") {
dbg() << "Cannot paste clipboard type " << data_and_type.mime_type;
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()) {
dbg() << "No files to paste";
return;
@ -638,7 +638,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
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_back_action->set_enabled(directory_view.path_history_position() > 0);
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);
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);
directory_context_menu->popup(event.screen_position());
} else {
@ -822,7 +822,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
directory_view.open(initial_location);
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();

View file

@ -139,7 +139,7 @@ bool HexEditor::copy_selected_hex_to_clipboard()
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;
}
@ -153,7 +153,7 @@ bool HexEditor::copy_selected_text_to_clipboard()
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;
}
@ -176,7 +176,7 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
}
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;
}

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);

View file

@ -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());

View file

@ -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&) {

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()];
switch (index.column()) {
case Column::Data:
if (data_and_type.type.starts_with("text/"))
return data_and_type.data;
if (data_and_type.mime_type.starts_with("text/"))
return String::copy(data_and_type.data);
return "<...>";
case Column::Type:
return data_and_type.type;
return data_and_type.mime_type;
default:
ASSERT_NOT_REACHED();
}

View file

@ -70,7 +70,7 @@ int main(int argc, char* argv[])
table_view.on_activation = [&](const GUI::ModelIndex& index) {
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();

View file

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

View file

@ -46,19 +46,19 @@ int main(int argc, char* argv[])
auto& clipboard = GUI::Clipboard::the();
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");
return 1;
}
if (!print_type) {
printf("%s", data_and_type.data.characters());
printf("%s", data_and_type.data.data());
// Append a newline to text contents, but
// 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');
} else {
printf("%s\n", data_and_type.type.characters());
printf("%s\n", data_and_type.mime_type.characters());
}
return 0;