Expose clipboard_text accessors to C++ (#3265)

This commit is contained in:
Ian McFarlane 2023-08-28 05:26:40 -05:00 committed by GitHub
parent 93a8f604a8
commit 1de7b1512c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 3 deletions

View file

@ -267,6 +267,7 @@ public:
class Platform
{
public:
using Clipboard = cbindgen_private::Clipboard;
virtual ~Platform() = default;
Platform(const Platform &) = delete;
Platform &operator=(const Platform &) = delete;
@ -286,6 +287,19 @@ public:
}
#endif
/// Sends the given text into the system clipboard.
///
/// If the platform doesn't support the specified clipboard, this function should do nothing
virtual void set_clipboard_text(const SharedString &, Clipboard) { }
/// Returns a copy of text stored in the system clipboard, if any.
///
/// If the platform doesn't support the specified clipboard, the function should return nullopt
virtual std::optional<SharedString> clipboard_text(Clipboard)
{
return {};
}
/// Spins an event loop and renders the visible windows.
virtual void run_event_loop() { }
@ -364,6 +378,19 @@ inline void set_platform(std::unique_ptr<Platform> platform)
return reinterpret_cast<const Platform *>(p)->duration_since_start().count();
#endif
},
[](void *p, const SharedString *text, uint8_t clipboard) {
reinterpret_cast<Platform *>(p)->set_clipboard_text(*text,
Platform::Clipboard(clipboard));
},
[](void *p, SharedString *out_text, uint8_t clipboard) -> bool {
auto maybe_clipboard = reinterpret_cast<Platform *>(p)->clipboard_text(
Platform::Clipboard(clipboard));
bool status = maybe_clipboard.has_value();
if (status)
*out_text = maybe_clipboard.value();
return status;
},
[](void *p) { return reinterpret_cast<Platform *>(p)->run_event_loop(); },
[](void *p) { return reinterpret_cast<Platform *>(p)->quit_event_loop(); },
[](void *p, cbindgen_private::PlatformTaskOpaque event) {