Skip to main content

dear_imgui_rs/widget/input/
entry.rs

1use super::multiline::{InputTextMultiline, InputTextMultilineImStr};
2use super::numeric::{
3    InputDouble, InputFloat, InputFloat2, InputFloat3, InputFloat4, InputInt, InputInt2, InputInt3,
4    InputInt4, InputScalar, InputScalarN,
5};
6use super::single_line::{InputText, InputTextImStr};
7use crate::internal::DataTypeKind;
8use crate::string::ImString;
9use crate::ui::Ui;
10use std::borrow::Cow;
11
12/// # Input Widgets
13impl Ui {
14    /// Creates a single-line text input widget builder.
15    ///
16    /// # Examples
17    ///
18    /// ```no_run
19    /// # use dear_imgui_rs::*;
20    /// # let mut ctx = Context::create();
21    /// # let ui = ctx.frame();
22    /// let mut text = String::new();
23    /// if ui.input_text("Label", &mut text).build() {
24    ///     println!("Text changed: {}", text);
25    /// }
26    /// ```
27    #[doc(alias = "InputText", alias = "InputTextWithHint")]
28    pub fn input_text<'ui, 'p>(
29        &'ui self,
30        label: impl Into<Cow<'ui, str>>,
31        buf: &'p mut String,
32    ) -> InputText<'ui, 'p> {
33        InputText::new(self, label, buf)
34    }
35
36    /// Creates a single-line text input backed by ImString (zero-copy)
37    pub fn input_text_imstr<'ui, 'p>(
38        &'ui self,
39        label: impl Into<Cow<'ui, str>>,
40        buf: &'p mut ImString,
41    ) -> InputTextImStr<'ui, 'p> {
42        InputTextImStr::new(self, label, buf)
43    }
44
45    /// Creates a multi-line text input widget builder.
46    ///
47    /// # Examples
48    ///
49    /// ```no_run
50    /// # use dear_imgui_rs::*;
51    /// # let mut ctx = Context::create();
52    /// # let ui = ctx.frame();
53    /// let mut text = String::new();
54    /// if ui.input_text_multiline("Label", &mut text, [200.0, 100.0]).build() {
55    ///     println!("Text changed: {}", text);
56    /// }
57    /// ```
58    #[doc(alias = "InputTextMultiline")]
59    pub fn input_text_multiline<'ui, 'p>(
60        &'ui self,
61        label: impl Into<Cow<'ui, str>>,
62        buf: &'p mut String,
63        size: impl Into<[f32; 2]>,
64    ) -> InputTextMultiline<'ui, 'p> {
65        InputTextMultiline::new(self, label, buf, size)
66    }
67
68    /// Creates a multi-line text input backed by ImString (zero-copy)
69    pub fn input_text_multiline_imstr<'ui, 'p>(
70        &'ui self,
71        label: impl Into<Cow<'ui, str>>,
72        buf: &'p mut ImString,
73        size: impl Into<[f32; 2]>,
74    ) -> InputTextMultilineImStr<'ui, 'p> {
75        InputTextMultilineImStr::new(self, label, buf, size)
76    }
77
78    /// Creates an integer input widget.
79    ///
80    /// Returns true if the value was edited.
81    #[doc(alias = "InputInt")]
82    pub fn input_int(&self, label: impl AsRef<str>, value: &mut i32) -> bool {
83        self.input_int_config(label.as_ref()).build(value)
84    }
85
86    /// Creates a float input widget.
87    ///
88    /// Returns true if the value was edited.
89    #[doc(alias = "InputFloat")]
90    pub fn input_float(&self, label: impl AsRef<str>, value: &mut f32) -> bool {
91        self.input_float_config(label.as_ref()).build(value)
92    }
93
94    /// Creates a double input widget.
95    ///
96    /// Returns true if the value was edited.
97    #[doc(alias = "InputDouble")]
98    pub fn input_double(&self, label: impl AsRef<str>, value: &mut f64) -> bool {
99        self.input_double_config(label.as_ref()).build(value)
100    }
101
102    /// Creates an integer input builder
103    pub fn input_int_config<'ui>(&'ui self, label: impl Into<Cow<'ui, str>>) -> InputInt<'ui> {
104        InputInt::new(self, label)
105    }
106
107    /// Creates a float input builder
108    pub fn input_float_config<'ui>(&'ui self, label: impl Into<Cow<'ui, str>>) -> InputFloat<'ui> {
109        InputFloat::new(self, label)
110    }
111
112    /// Creates a double input builder
113    pub fn input_double_config<'ui>(
114        &'ui self,
115        label: impl Into<Cow<'ui, str>>,
116    ) -> InputDouble<'ui> {
117        InputDouble::new(self, label)
118    }
119
120    /// Shows an input field for a scalar value. This is not limited to `f32` and `i32` and can be used with
121    /// any primitive scalar type e.g. `u8` and `f64`.
122    #[doc(alias = "InputScalar")]
123    pub fn input_scalar<'p, L, T>(&self, label: L, value: &'p mut T) -> InputScalar<'_, 'p, T, L>
124    where
125        L: AsRef<str>,
126        T: DataTypeKind,
127    {
128        InputScalar::new(self, label, value)
129    }
130
131    /// Shows a horizontal array of scalar value input fields. See [`input_scalar`].
132    ///
133    /// [`input_scalar`]: Self::input_scalar
134    #[doc(alias = "InputScalarN")]
135    pub fn input_scalar_n<'p, L, T>(
136        &self,
137        label: L,
138        values: &'p mut [T],
139    ) -> InputScalarN<'_, 'p, T, L>
140    where
141        L: AsRef<str>,
142        T: DataTypeKind,
143    {
144        InputScalarN::new(self, label, values)
145    }
146
147    /// Widget to edit two floats
148    #[doc(alias = "InputFloat2")]
149    pub fn input_float2<'p, L>(&self, label: L, value: &'p mut [f32; 2]) -> InputFloat2<'_, 'p, L>
150    where
151        L: AsRef<str>,
152    {
153        InputFloat2::new(self, label, value)
154    }
155
156    /// Widget to edit three floats
157    #[doc(alias = "InputFloat3")]
158    pub fn input_float3<'p, L>(&self, label: L, value: &'p mut [f32; 3]) -> InputFloat3<'_, 'p, L>
159    where
160        L: AsRef<str>,
161    {
162        InputFloat3::new(self, label, value)
163    }
164
165    /// Widget to edit four floats
166    #[doc(alias = "InputFloat4")]
167    pub fn input_float4<'p, L>(&self, label: L, value: &'p mut [f32; 4]) -> InputFloat4<'_, 'p, L>
168    where
169        L: AsRef<str>,
170    {
171        InputFloat4::new(self, label, value)
172    }
173
174    /// Widget to edit two integers
175    #[doc(alias = "InputInt2")]
176    pub fn input_int2<'p, L>(&self, label: L, value: &'p mut [i32; 2]) -> InputInt2<'_, 'p, L>
177    where
178        L: AsRef<str>,
179    {
180        InputInt2::new(self, label, value)
181    }
182
183    /// Widget to edit three integers
184    #[doc(alias = "InputInt3")]
185    pub fn input_int3<'p, L>(&self, label: L, value: &'p mut [i32; 3]) -> InputInt3<'_, 'p, L>
186    where
187        L: AsRef<str>,
188    {
189        InputInt3::new(self, label, value)
190    }
191
192    /// Widget to edit four integers
193    #[doc(alias = "InputInt4")]
194    pub fn input_int4<'p, L>(&self, label: L, value: &'p mut [i32; 4]) -> InputInt4<'_, 'p, L>
195    where
196        L: AsRef<str>,
197    {
198        InputInt4::new(self, label, value)
199    }
200}