#[repr(transparent)]
#[derive(Debug, PartialEq, Clone, Default)]
pub struct StyledText {
pub(crate) paragraphs: crate::SharedVector<i_slint_common::styled_text::StyledTextParagraph>,
}
#[cfg(feature = "std")]
impl StyledText {
pub fn parse_interpolated<S: AsRef<[i_slint_common::styled_text::StyledTextParagraph]>>(
format_string: &str,
args: &[S],
) -> Result<Self, i_slint_common::styled_text::StyledTextError<'static>> {
Ok(Self {
paragraphs: i_slint_common::styled_text::parse_interpolated(format_string, args)
.collect::<Result<crate::SharedVector<_>, _>>()?,
})
}
}
impl AsRef<[i_slint_common::styled_text::StyledTextParagraph]> for StyledText {
fn as_ref(&self) -> &[i_slint_common::styled_text::StyledTextParagraph] {
&self.paragraphs
}
}
pub fn get_raw_text(styled_text: &StyledText) -> alloc::borrow::Cow<'_, str> {
match styled_text.paragraphs.as_slice() {
[] => "".into(),
[paragraph] => paragraph.text.as_str().into(),
_ => {
let mut result = alloc::string::String::new();
for paragraph in styled_text.paragraphs.iter() {
if !result.is_empty() {
result.push('\n');
}
result.push_str(paragraph.text.as_str());
}
result.into()
}
}
}
#[cfg(feature = "ffi")]
pub mod ffi {
#![allow(unsafe_code)]
use super::*;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn slint_styled_text_new(out: *mut StyledText) {
unsafe {
core::ptr::write(out, Default::default());
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn slint_styled_text_drop(text: *const StyledText) {
unsafe {
core::ptr::read(text);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn slint_styled_text_eq(a: &StyledText, b: &StyledText) -> bool {
a == b
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn slint_styled_text_clone(out: *mut StyledText, ss: &StyledText) {
unsafe { core::ptr::write(out, ss.clone()) }
}
}
pub fn parse_markdown<S: AsRef<[i_slint_common::styled_text::StyledTextParagraph]>>(
_format_string: &str,
_args: &[S],
) -> StyledText {
#[cfg(feature = "std")]
{
StyledText::parse_interpolated(_format_string, _args).unwrap()
}
#[cfg(not(feature = "std"))]
Default::default()
}
pub fn string_to_styled_text(_string: alloc::string::String) -> StyledText {
#[cfg(feature = "std")]
{
if _string.is_empty() {
return Default::default();
}
StyledText {
paragraphs: [i_slint_common::styled_text::paragraph_from_plain_text(_string)].into(),
}
}
#[cfg(not(feature = "std"))]
Default::default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn string_to_styled_text() {
assert_eq!(super::string_to_styled_text(Default::default()), StyledText::default());
}
}