C++: expose special key codes constants

This commit is contained in:
Olivier Goffart 2023-07-31 15:11:57 +02:00 committed by GitHub
parent ad84114bae
commit 0a8f9c585a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 164 additions and 10 deletions

View file

@ -44,8 +44,39 @@ fn enums(path: &Path) -> anyhow::Result<()> {
}
}
i_slint_common::for_each_enums!(print_enums);
writeln!(enums_pub, "}}")?;
writeln!(enums_priv, "}}")?;
// Print the key codes constants
// This is not an enum, but fits well in that file
writeln!(
enums_pub,
r#"
/// This namespace contains constants for each special non-printable key.
///
/// Each constant can be converted to SharedString.
/// The constants are meant to be used with the slint::Window::dispatch_key_press_event() and
/// slint::Window::dispatch_key_release_event() functions.
///
/// Example:
/// ```
/// window.dispatch_key_press_event(slint::platform::key_codes::Tab);
/// ```
namespace slint::platform::key_codes {{
"#
)?;
macro_rules! print_key_codes {
($($char:literal # $name:ident # $($qt:ident)|* # $($winit:ident)|* ;)*) => {
$(
writeln!(enums_pub, "/// A constant that represents the key code to be used in slint::Window::dispatch_key_press_event()")?;
writeln!(enums_pub, r#"constexpr std::u8string_view {} = u8"\u{:04x}";"#, stringify!($name), $char as u32)?;
)*
};
}
i_slint_common::for_each_special_keys!(print_key_codes);
writeln!(enums_pub, "}}")?;
Ok(())
}

View file

@ -33,19 +33,15 @@ struct SharedString
/// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
/// string data is copied. It is assumed that the string is UTF-8 encoded.
SharedString(const char *s) : SharedString(std::string_view(s)) { }
#if defined(__cpp_char8_t) || __cplusplus >= 202002L
/// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
/// string data is copied.
SharedString(const char8_t *s) : SharedString(reinterpret_cast<const char *>(s)) { }
#endif
#ifdef __cpp_lib_char8_t
/// Creates a new SharedString from the string view \a s. The underlying string data is copied.
SharedString(std::u8string_view s)
{
cbindgen_private::slint_shared_string_from_bytes(
this, reinterpret_cast<const char *>(s.data()), s.size());
}
#endif
/// Creates a new SharedString from \a other.
SharedString(const SharedString &other)
{