mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:47:35 +00:00
SpiceAgent: Add support for copying images to the clipboard
This commit is contained in:
parent
50a8db3922
commit
9f92e52464
4 changed files with 63 additions and 11 deletions
|
@ -8,6 +8,7 @@
|
|||
#include "SpiceAgent.h"
|
||||
#include <AK/Debug.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
#include <LibGfx/ImageFormats/ImageDecoder.h>
|
||||
|
||||
namespace SpiceAgent {
|
||||
|
||||
|
@ -111,11 +112,33 @@ ErrorOr<void> SpiceAgent::did_receive_clipboard_message(ClipboardMessage& messag
|
|||
case ClipboardDataType::Text: {
|
||||
// The default mime_type for set_data is `text/plain`.
|
||||
GUI::Clipboard::the().set_data(message.contents());
|
||||
return {};
|
||||
break;
|
||||
}
|
||||
|
||||
// For the image formats, let's try to find a decoder from LibGfx.
|
||||
case ClipboardDataType::PNG:
|
||||
case ClipboardDataType::BMP:
|
||||
case ClipboardDataType::JPG:
|
||||
case ClipboardDataType::TIFF: {
|
||||
auto mime_type = TRY(clipboard_data_type_to_mime_type(message.data_type()));
|
||||
|
||||
// FIXME: It should be trivial to make `try_create_for_raw_bytes` take a `StringView` instead of a direct `DeprecatedString`.
|
||||
auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(message.contents(), mime_type.to_deprecated_string());
|
||||
if (!decoder || (decoder->frame_count() == 0)) {
|
||||
return Error::from_string_literal("Failed to find a suitable decoder for a pasted image!");
|
||||
}
|
||||
|
||||
auto frame = TRY(decoder->frame(0));
|
||||
GUI::Clipboard::the().set_bitmap(*frame.image);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
return Error::from_string_literal("Unsupported clipboard data type!");
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<ByteBuffer> SpiceAgent::read_message_buffer()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue