C++: polish the clipboard patch

- make sure the Platform::Clipboard type is properly documented
 - Use the actual enum in the interface (even though it is
   #[non_exhaustive] so we need to slience a warning)

CC: #3265
This commit is contained in:
Olivier Goffart 2023-08-28 13:11:25 +02:00 committed by Olivier Goffart
parent 67423b288f
commit ded66231d1
2 changed files with 33 additions and 28 deletions

View file

@ -281,7 +281,6 @@ public:
class Platform class Platform
{ {
public: public:
using Clipboard = cbindgen_private::Clipboard;
virtual ~Platform() = default; virtual ~Platform() = default;
Platform(const Platform &) = delete; Platform(const Platform &) = delete;
Platform &operator=(const Platform &) = delete; Platform &operator=(const Platform &) = delete;
@ -290,7 +289,7 @@ public:
/// Returns a new WindowAdapter /// Returns a new WindowAdapter
virtual std::unique_ptr<WindowAdapter> create_window_adapter() = 0; virtual std::unique_ptr<WindowAdapter> create_window_adapter() = 0;
#ifdef SLINT_FEATURE_FREESTANDING #if defined(SLINT_FEATURE_FREESTANDING) || defined(DOXYGEN)
/// Returns the amount of milliseconds since start of the application. /// Returns the amount of milliseconds since start of the application.
/// ///
/// This function should only be implemented if the runtime is compiled with /// This function should only be implemented if the runtime is compiled with
@ -301,6 +300,18 @@ public:
} }
#endif #endif
/// The type of clipboard used in Platform::clipboard_text and PLatform::set_clipboard_text.
enum class Clipboard {
/// This is the default clipboard used for text action for Ctrl+V, Ctrl+C.
/// Corresponds to the secondary selection on X11.
DefaultClipboard = static_cast<uint8_t>(cbindgen_private::Clipboard::DefaultClipboard),
/// This is the clipboard that is used when text is selected
/// Corresponds to the primary selection on X11.
/// The Platform implementation should do nothing if copy on select is not supported on that
/// platform.
SelectionClipboard = static_cast<uint8_t>(cbindgen_private::Clipboard::SelectionClipboard),
};
/// Sends the given text into the system clipboard. /// Sends the given text into the system clipboard.
/// ///
/// If the platform doesn't support the specified clipboard, this function should do nothing /// If the platform doesn't support the specified clipboard, this function should do nothing
@ -392,13 +403,13 @@ inline void set_platform(std::unique_ptr<Platform> platform)
return reinterpret_cast<const Platform *>(p)->duration_since_start().count(); return reinterpret_cast<const Platform *>(p)->duration_since_start().count();
#endif #endif
}, },
[](void *p, const SharedString *text, uint8_t clipboard) { [](void *p, const SharedString *text, cbindgen_private::Clipboard clipboard) {
reinterpret_cast<Platform *>(p)->set_clipboard_text(*text, reinterpret_cast<Platform *>(p)->set_clipboard_text(
Platform::Clipboard(clipboard)); *text, static_cast<Platform::Clipboard>(clipboard));
}, },
[](void *p, SharedString *out_text, uint8_t clipboard) -> bool { [](void *p, SharedString *out_text, cbindgen_private::Clipboard clipboard) -> bool {
auto maybe_clipboard = reinterpret_cast<Platform *>(p)->clipboard_text( auto maybe_clipboard = reinterpret_cast<Platform *>(p)->clipboard_text(
Platform::Clipboard(clipboard)); static_cast<Platform::Clipboard>(clipboard));
bool status = maybe_clipboard.has_value(); bool status = maybe_clipboard.has_value();
if (status) if (status)

View file

@ -141,9 +141,11 @@ struct CppPlatform {
window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque), window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque),
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64, duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, _clipboard: u8), // silent the warning depite `Clipboard` is a `#[non_exhaustive]` enum from another crate.
clipboard_text: #[allow(improper_ctypes_definitions)]
unsafe extern "C" fn(PlatformUserData, &mut SharedString, _clipboard: u8) -> bool, set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, Clipboard),
#[allow(improper_ctypes_definitions)]
clipboard_text: unsafe extern "C" fn(PlatformUserData, &mut SharedString, Clipboard) -> bool,
run_event_loop: unsafe extern "C" fn(PlatformUserData), run_event_loop: unsafe extern "C" fn(PlatformUserData),
quit_event_loop: unsafe extern "C" fn(PlatformUserData), quit_event_loop: unsafe extern "C" fn(PlatformUserData),
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque), invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
@ -185,21 +187,15 @@ impl Platform for CppPlatform {
})) }))
} }
fn set_clipboard_text(&self, _text: &str, _clipboard: Clipboard) { fn set_clipboard_text(&self, text: &str, clipboard: Clipboard) {
let shared_text = SharedString::from(_text); let shared_text = SharedString::from(text);
unsafe { (self.set_clipboard_text)(self.user_data, &shared_text, _clipboard as u8) } unsafe { (self.set_clipboard_text)(self.user_data, &shared_text, clipboard) }
} }
fn clipboard_text(&self, _clipboard: Clipboard) -> Option<String> { fn clipboard_text(&self, clipboard: Clipboard) -> Option<String> {
let mut out_text = SharedString::new(); let mut out_text = SharedString::new();
let status = let status = unsafe { (self.clipboard_text)(self.user_data, &mut out_text, clipboard) };
unsafe { (self.clipboard_text)(self.user_data, &mut out_text, _clipboard as u8) }; status.then(|| out_text.into())
if !status {
None
} else {
String::from(out_text).into()
}
} }
} }
@ -232,18 +228,16 @@ impl i_slint_core::platform::EventLoopProxy for CppEventLoopProxy {
unsafe impl Send for CppEventLoopProxy {} unsafe impl Send for CppEventLoopProxy {}
unsafe impl Sync for CppEventLoopProxy {} unsafe impl Sync for CppEventLoopProxy {}
// silent the warning depite `Clipboard` is a `#[non_exhaustive]` enum from another crate.
#[allow(improper_ctypes_definitions)]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn slint_platform_register( pub unsafe extern "C" fn slint_platform_register(
user_data: PlatformUserData, user_data: PlatformUserData,
drop: unsafe extern "C" fn(PlatformUserData), drop: unsafe extern "C" fn(PlatformUserData),
window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque), window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque),
#[allow(unused)] duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64, #[allow(unused)] duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, _clipboard: u8), set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, Clipboard),
clipboard_text: unsafe extern "C" fn( clipboard_text: unsafe extern "C" fn(PlatformUserData, &mut SharedString, Clipboard) -> bool,
PlatformUserData,
&mut SharedString,
_clipboard: u8,
) -> bool,
run_event_loop: unsafe extern "C" fn(PlatformUserData), run_event_loop: unsafe extern "C" fn(PlatformUserData),
quit_event_loop: unsafe extern "C" fn(PlatformUserData), quit_event_loop: unsafe extern "C" fn(PlatformUserData),
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque), invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),