dear_imgui_rs/widget/text.rs
1//! Text helpers
2//!
3//! Convenience functions for colored text, wrapped text, disabled text and
4//! label helpers.
5//!
6//! Quick examples:
7//! ```no_run
8//! # use dear_imgui_rs::*;
9//! # let mut ctx = Context::create();
10//! # let ui = ctx.frame();
11//! ui.text("normal");
12//! ui.text_colored([1.0, 0.5, 0.0, 1.0], "warning");
13//! ui.text_disabled("disabled");
14//! ui.text_wrapped("very long text that will wrap when needed...");
15//! ```
16//!
17use crate::Ui;
18use crate::style::StyleColor;
19use crate::sys;
20
21impl Ui {
22 /// Display colored text
23 ///
24 /// This implementation uses zero-copy optimization with `igTextEx`,
25 /// avoiding string allocation and null-termination overhead.
26 ///
27 /// # Example
28 /// ```no_run
29 /// # use dear_imgui_rs::*;
30 /// # let mut ctx = Context::create();
31 /// # let ui = ctx.frame();
32 /// ui.text_colored([1.0, 0.0, 0.0, 1.0], "Red text");
33 /// ui.text_colored([0.0, 1.0, 0.0, 1.0], "Green text");
34 /// ```
35 #[doc(alias = "TextColored")]
36 pub fn text_colored(&self, color: [f32; 4], text: impl AsRef<str>) {
37 let s = text.as_ref();
38
39 // Temporarily set the text color
40 let _token = self.push_style_color(StyleColor::Text, color);
41
42 // Use igTextEx with zero-copy (begin/end pointers)
43 unsafe {
44 let begin = s.as_ptr() as *const std::os::raw::c_char;
45 let end = begin.add(s.len());
46 sys::igTextEx(begin, end, 0); // ImGuiTextFlags_None = 0
47 }
48 }
49
50 /// Display disabled (grayed out) text
51 ///
52 /// This implementation uses zero-copy optimization with `igTextEx`,
53 /// avoiding string allocation and null-termination overhead.
54 ///
55 /// # Example
56 /// ```no_run
57 /// # use dear_imgui_rs::*;
58 /// # let mut ctx = Context::create();
59 /// # let ui = ctx.frame();
60 /// ui.text_disabled("This option is not available");
61 /// ```
62 #[doc(alias = "TextDisabled")]
63 pub fn text_disabled(&self, text: impl AsRef<str>) {
64 let s = text.as_ref();
65
66 // Get the disabled color from the current style
67 let disabled_color = self.style_color(StyleColor::TextDisabled);
68
69 // Temporarily set the text color to disabled color
70 let _token = self.push_style_color(StyleColor::Text, disabled_color);
71
72 // Use igTextEx with zero-copy (begin/end pointers)
73 unsafe {
74 let begin = s.as_ptr() as *const std::os::raw::c_char;
75 let end = begin.add(s.len());
76 sys::igTextEx(begin, end, 0); // ImGuiTextFlags_None = 0
77 }
78 }
79
80 /// Display text wrapped to fit the current item width
81 ///
82 /// # Note
83 ///
84 /// This function currently uses the scratch buffer implementation.
85 /// Optimization for this function requires additional investigation.
86 #[doc(alias = "TextWrapped")]
87 pub fn text_wrapped(&self, text: impl AsRef<str>) {
88 let text_ptr = self.scratch_txt(text);
89 unsafe {
90 sys::igTextWrapped(text_ptr);
91 }
92 }
93
94 /// Display a label and text on the same line
95 #[doc(alias = "LabelText")]
96 pub fn label_text(&self, label: impl AsRef<str>, text: impl AsRef<str>) {
97 let (label_ptr, text_ptr) = self.scratch_txt_two(label, text);
98 unsafe {
99 sys::igLabelText(label_ptr, text_ptr);
100 }
101 }
102
103 /// Render a hyperlink-style text button. Returns true when clicked.
104 #[doc(alias = "TextLink")]
105 pub fn text_link(&self, label: impl AsRef<str>) -> bool {
106 unsafe { sys::igTextLink(self.scratch_txt(label)) }
107 }
108
109 /// Render a hyperlink-style text button, and open the given URL when clicked.
110 /// Returns true when clicked.
111 #[doc(alias = "TextLinkOpenURL")]
112 pub fn text_link_open_url(&self, label: impl AsRef<str>, url: impl AsRef<str>) -> bool {
113 let (label_ptr, url_ptr) = self.scratch_txt_two(label, url);
114 unsafe { sys::igTextLinkOpenURL(label_ptr, url_ptr) }
115 }
116}