Clipboard
Native clipboard.
Header | #include "nativeui/clipboard.h" |
Namespace | namespace nu |
Type | class |
The Clipboard
class can not be created by user, its instance can only be
recevied by using the factory methods.
// Get the default copy-paste clipboard.
nu::Clipboard* clipboard = nu::Clipboard::Get();
// Changing the content of clipboard with multiple formats of data.
std::vector<nu::Clipboard::Data> objects;
// Add text.
objects.emplace_back(nu::Clipboard::Text, "some text");
// Add HTML.
objects.emplace_back(nu::Clipboard::HTML, "<strong>some text</strong>");
// Add Image.
objects.emplace_back(new nu::Image("..."));
// Add file paths.
objects.emplace_back({
base::FilePath(FILE_PATH_LITERAL("/some/path")),
});
// Change data.
clipboard->SetData(std::move(objects));
// Read data.
Data data = clipboard->GetData(nu::Clipboard::Data::Type::Text);
if (data.type() == nu::Clipboard::Data::Type::Text)
LOG(INFO) << data.str();
Class methods
Clipboard* Get()
Return the default copy-paste clipboard.
The returned clipboard instance is managed by App, there is no need to manually free it.
Return
Clipboard*
Clipboard* FromType(Clipboard::Type type)
Return the clipboard with type
.
The returned clipboard instance is managed by App, there is no need to manually free it.
Parameters
Clipboard::Type
type
Return
Clipboard*
Methods
void Clear()
Clear the clipboard.
void SetText(const std::string& text)
Set text
as clipboard's content.
Parameters
const std::string&
text
std::string GetText() const
Return the content of clipboard as text.
Return
std::string
bool IsDataAvailable(Clipboard::Data::Type type) const
Return whether the data of type
is available.
Parameters
Return
bool
Clipboard::Data GetData(Clipboard::Data::Type type) const
Get the data of type
from clipboard.
You should always check the type of returned data before using it.
Parameters
Return
Clipboard::Data
void SetData(std::vector<Clipboard::Data> objects)
Set clipboard's content.
Parameters
std::vector<Clipboard::Data>
objects - An array ofClipboard::Data
.
void StartWatching()
Start watching clipboard's content.
The on_change
event will be emitted when clipboard's content has
been changed.
On macOS, due to lack of system notifications for clipboard events, this event is implemented by polling every 500ms.
void StopWatching()
Stop watching clipboard's content.
NativeClipboard GetNative() const
Return the native handle of clipboard.
Return
NativeClipboard
Events
void on_change(Clipboard* self)
Emitted when clipboard's content has been changed.
The StartWatching
method must be called first to make this event
work.
Parameters
Clipboard*
self