mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 18:58:36 +00:00
Expose clipboard_text accessors to C++ (#3265)
This commit is contained in:
parent
93a8f604a8
commit
1de7b1512c
4 changed files with 60 additions and 3 deletions
|
@ -258,6 +258,7 @@ fn gen_corelib(
|
|||
];
|
||||
|
||||
config.export.include = [
|
||||
"Clipboard",
|
||||
"ComponentVTable",
|
||||
"Slice",
|
||||
"WindowAdapterRcOpaque",
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -6,7 +6,7 @@ use alloc::rc::Rc;
|
|||
use core::ffi::c_void;
|
||||
use i_slint_core::api::{LogicalSize, PhysicalSize, Window};
|
||||
use i_slint_core::graphics::IntSize;
|
||||
use i_slint_core::platform::{Platform, PlatformError};
|
||||
use i_slint_core::platform::{Clipboard, Platform, PlatformError};
|
||||
use i_slint_core::renderer::Renderer;
|
||||
use i_slint_core::window::ffi::WindowAdapterRcOpaque;
|
||||
use i_slint_core::window::{WindowAdapter, WindowProperties};
|
||||
|
@ -141,6 +141,9 @@ struct CppPlatform {
|
|||
window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque),
|
||||
#[cfg(not(feature = "std"))]
|
||||
duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
|
||||
set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, _clipboard: u8),
|
||||
clipboard_text:
|
||||
unsafe extern "C" fn(PlatformUserData, &mut SharedString, _clipboard: u8) -> bool,
|
||||
run_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),
|
||||
|
@ -181,6 +184,23 @@ impl Platform for CppPlatform {
|
|||
invoke_from_event_loop: self.invoke_from_event_loop,
|
||||
}))
|
||||
}
|
||||
|
||||
fn set_clipboard_text(&self, _text: &str, _clipboard: Clipboard) {
|
||||
let shared_text = SharedString::from(_text);
|
||||
unsafe { (self.set_clipboard_text)(self.user_data, &shared_text, _clipboard as u8) }
|
||||
}
|
||||
|
||||
fn clipboard_text(&self, _clipboard: Clipboard) -> Option<String> {
|
||||
let mut out_text = SharedString::new();
|
||||
let status =
|
||||
unsafe { (self.clipboard_text)(self.user_data, &mut out_text, _clipboard as u8) };
|
||||
|
||||
if !status {
|
||||
None
|
||||
} else {
|
||||
String::from(out_text).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CppEventLoopProxy {
|
||||
|
@ -218,6 +238,12 @@ pub unsafe extern "C" fn slint_platform_register(
|
|||
drop: unsafe extern "C" fn(PlatformUserData),
|
||||
window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque),
|
||||
#[allow(unused)] duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
|
||||
set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, _clipboard: u8),
|
||||
clipboard_text: unsafe extern "C" fn(
|
||||
PlatformUserData,
|
||||
&mut SharedString,
|
||||
_clipboard: u8,
|
||||
) -> bool,
|
||||
run_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),
|
||||
|
@ -228,6 +254,8 @@ pub unsafe extern "C" fn slint_platform_register(
|
|||
window_factory,
|
||||
#[cfg(not(feature = "std"))]
|
||||
duration_since_start,
|
||||
set_clipboard_text,
|
||||
clipboard_text,
|
||||
run_event_loop,
|
||||
quit_event_loop,
|
||||
invoke_from_event_loop,
|
||||
|
|
|
@ -98,18 +98,19 @@ pub trait Platform {
|
|||
}
|
||||
|
||||
/// The clip board, used in [`Platform::clipboard_text`] and [Platform::set_clipboard_text`]
|
||||
#[repr(u8)]
|
||||
#[non_exhaustive]
|
||||
#[derive(PartialEq, Clone, Default)]
|
||||
pub enum Clipboard {
|
||||
/// This is the default clipboard used for text action for Ctrl+V, Ctrl+C.
|
||||
/// Corresponds to the secondary clipboard on X11.
|
||||
#[default]
|
||||
DefaultClipboard,
|
||||
DefaultClipboard = 0,
|
||||
|
||||
/// This is the clipboard that is used when text is selected
|
||||
/// Corresponds to the primary clipboard on X11.
|
||||
/// The Platform implementation should do nothing if copy on select is not supported on that platform.
|
||||
SelectionClipboard,
|
||||
SelectionClipboard = 1,
|
||||
}
|
||||
|
||||
/// Trait that is returned by the [`Platform::new_event_loop_proxy`]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue