2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-07-27 03:19:56 +08:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-08 13:27:19 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-23 13:21:18 +02:00
|
|
|
#include <AK/ByteBuffer.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2019-09-14 09:19:05 +02:00
|
|
|
#include <AK/Function.h>
|
2020-09-05 16:26:22 +02:00
|
|
|
#include <AK/HashMap.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGUI/Forward.h>
|
2020-09-05 16:26:22 +02:00
|
|
|
#include <LibGfx/Forward.h>
|
2019-03-08 13:27:19 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2022-02-25 13:13:30 +02:00
|
|
|
class ConnectionToClipboardServer;
|
2021-07-27 03:19:56 +08:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class Clipboard {
|
2019-03-08 13:27:19 +01:00
|
|
|
public:
|
2021-07-27 03:19:56 +08:00
|
|
|
class ClipboardClient {
|
|
|
|
public:
|
|
|
|
ClipboardClient();
|
|
|
|
virtual ~ClipboardClient();
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual void clipboard_content_did_change(DeprecatedString const& mime_type) = 0;
|
2021-07-27 03:19:56 +08:00
|
|
|
};
|
2020-09-05 16:26:22 +02:00
|
|
|
|
2019-09-14 09:19:05 +02:00
|
|
|
struct DataAndType {
|
2020-09-05 16:16:01 +02:00
|
|
|
ByteBuffer data;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString mime_type;
|
|
|
|
HashMap<DeprecatedString, DeprecatedString> metadata;
|
2021-11-20 13:04:38 +01:00
|
|
|
|
|
|
|
RefPtr<Gfx::Bitmap> as_bitmap() const;
|
2019-09-14 09:19:05 +02:00
|
|
|
};
|
|
|
|
|
2021-07-27 03:19:56 +08:00
|
|
|
static void initialize(Badge<Application>);
|
|
|
|
static Clipboard& the();
|
|
|
|
|
2021-11-20 15:22:01 +01:00
|
|
|
DataAndType fetch_data_and_type() const;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString fetch_mime_type() const { return fetch_data_and_type().mime_type; }
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void set_data(ReadonlyBytes data, DeprecatedString const& mime_type = "text/plain", HashMap<DeprecatedString, DeprecatedString> const& metadata = {});
|
|
|
|
void set_plain_text(DeprecatedString const& text) { set_data(text.bytes()); }
|
2021-07-27 03:19:56 +08:00
|
|
|
void set_bitmap(Gfx::Bitmap const&);
|
|
|
|
void clear();
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void clipboard_data_changed(Badge<ConnectionToClipboardServer>, DeprecatedString const& mime_type);
|
2021-07-27 03:19:56 +08:00
|
|
|
|
|
|
|
void register_client(Badge<ClipboardClient>, ClipboardClient& client) { m_clients.set(&client); }
|
|
|
|
void unregister_client(Badge<ClipboardClient>, ClipboardClient& client) { m_clients.remove(&client); }
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Function<void(DeprecatedString const& mime_type)> on_change;
|
2019-04-05 05:10:18 +02:00
|
|
|
|
|
|
|
private:
|
2021-07-27 03:19:56 +08:00
|
|
|
Clipboard() = default;
|
|
|
|
|
|
|
|
HashTable<ClipboardClient*> m_clients;
|
2019-03-08 13:27:19 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
}
|