Skip to content

Commit c63f70d

Browse files
tcl3linusg
authored andcommitted
LibGUI: Allow clipboard items to have no associated data
1 parent 76891ae commit c63f70d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Userland/Libraries/LibGUI/Clipboard.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ Clipboard& Clipboard::the()
5757
Clipboard::DataAndType Clipboard::fetch_data_and_type() const
5858
{
5959
auto response = connection().get_clipboard_data();
60+
auto type = response.mime_type();
61+
auto metadata = response.metadata().entries();
6062
if (!response.data().is_valid())
61-
return {};
63+
return { {}, type, metadata };
6264
auto data = ByteBuffer::copy(response.data().data<void>(), response.data().size());
6365
if (data.is_error())
6466
return {};
6567

66-
auto type = response.mime_type();
67-
auto metadata = response.metadata().entries();
6868
return { data.release_value(), type, metadata };
6969
}
7070

@@ -125,15 +125,18 @@ RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
125125

126126
void Clipboard::set_data(ReadonlyBytes data, DeprecatedString const& type, HashMap<DeprecatedString, DeprecatedString> const& metadata)
127127
{
128+
if (data.is_empty()) {
129+
connection().async_set_clipboard_data({}, type, metadata);
130+
return;
131+
}
132+
128133
auto buffer_or_error = Core::AnonymousBuffer::create_with_size(data.size());
129134
if (buffer_or_error.is_error()) {
130135
dbgln("GUI::Clipboard::set_data() failed to create a buffer");
131136
return;
132137
}
133138
auto buffer = buffer_or_error.release_value();
134-
if (!data.is_empty())
135-
memcpy(buffer.data<void>(), data.data(), data.size());
136-
139+
memcpy(buffer.data<void>(), data.data(), data.size());
137140
connection().async_set_clipboard_data(move(buffer), type, metadata);
138141
}
139142

0 commit comments

Comments
 (0)