i_slint_core/
styled_text.rs1#[repr(transparent)]
6#[derive(Debug, PartialEq, Clone, Default)]
7pub struct StyledText {
8 pub(crate) paragraphs: crate::SharedVector<i_slint_common::styled_text::StyledTextParagraph>,
10}
11
12#[cfg(feature = "std")]
13impl StyledText {
14 pub fn parse_interpolated<S: AsRef<[i_slint_common::styled_text::StyledTextParagraph]>>(
15 format_string: &str,
16 args: &[S],
17 ) -> Result<Self, i_slint_common::styled_text::StyledTextError<'static>> {
18 Ok(Self {
19 paragraphs: i_slint_common::styled_text::parse_interpolated(format_string, args)
20 .collect::<Result<crate::SharedVector<_>, _>>()?,
21 })
22 }
23}
24
25impl AsRef<[i_slint_common::styled_text::StyledTextParagraph]> for StyledText {
26 fn as_ref(&self) -> &[i_slint_common::styled_text::StyledTextParagraph] {
27 &self.paragraphs
28 }
29}
30
31pub fn get_raw_text(styled_text: &StyledText) -> alloc::borrow::Cow<'_, str> {
32 match styled_text.paragraphs.as_slice() {
33 [] => "".into(),
34 [paragraph] => paragraph.text.as_str().into(),
35 _ => {
36 let mut result = alloc::string::String::new();
37 for paragraph in styled_text.paragraphs.iter() {
38 if !result.is_empty() {
39 result.push('\n');
40 }
41 result.push_str(paragraph.text.as_str());
42 }
43 result.into()
44 }
45 }
46}
47
48#[cfg(feature = "ffi")]
50pub mod ffi {
51 #![allow(unsafe_code)]
52
53 use super::*;
54
55 #[unsafe(no_mangle)]
56 pub unsafe extern "C" fn slint_styled_text_new(out: *mut StyledText) {
58 unsafe {
59 core::ptr::write(out, Default::default());
60 }
61 }
62
63 #[unsafe(no_mangle)]
64 pub unsafe extern "C" fn slint_styled_text_drop(text: *const StyledText) {
66 unsafe {
67 core::ptr::read(text);
68 }
69 }
70
71 #[unsafe(no_mangle)]
72 pub extern "C" fn slint_styled_text_eq(a: &StyledText, b: &StyledText) -> bool {
74 a == b
75 }
76
77 #[unsafe(no_mangle)]
78 pub unsafe extern "C" fn slint_styled_text_clone(out: *mut StyledText, ss: &StyledText) {
80 unsafe { core::ptr::write(out, ss.clone()) }
81 }
82}
83
84pub fn parse_markdown<S: AsRef<[i_slint_common::styled_text::StyledTextParagraph]>>(
85 _format_string: &str,
86 _args: &[S],
87) -> StyledText {
88 #[cfg(feature = "std")]
89 {
90 StyledText::parse_interpolated(_format_string, _args).unwrap()
91 }
92 #[cfg(not(feature = "std"))]
93 Default::default()
94}
95
96pub fn string_to_styled_text(_string: alloc::string::String) -> StyledText {
97 #[cfg(feature = "std")]
98 {
99 StyledText {
100 paragraphs: [i_slint_common::styled_text::paragraph_from_plain_text(_string)].into(),
101 }
102 }
103 #[cfg(not(feature = "std"))]
104 Default::default()
105}