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