From 3d3097e222f8d351cb8ca5ee63a440289fe344fa Mon Sep 17 00:00:00 2001 From: Lukasz Wiklendt Date: Fri, 19 Feb 2021 09:24:17 +1030 Subject: [PATCH] add input_text hinting --- imgui-examples/examples/test_window_impl.rs | 6 ++++ imgui/src/input_widget.rs | 37 ++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/imgui-examples/examples/test_window_impl.rs b/imgui-examples/examples/test_window_impl.rs index 46bc3fc..af04548 100644 --- a/imgui-examples/examples/test_window_impl.rs +++ b/imgui-examples/examples/test_window_impl.rs @@ -30,6 +30,7 @@ struct State { item2: usize, item3: i32, text: ImString, + text_with_hint: ImString, text_multiline: ImString, i0: i32, f0: f32, @@ -60,6 +61,7 @@ impl Default for State { buf.push_str("日本語"); let mut text = ImString::with_capacity(128); text.push_str("Hello, world!"); + let text_with_hint = ImString::with_capacity(128); let mut text_multiline = ImString::with_capacity(128); text_multiline.push_str("Hello, world!\nMultiline"); State { @@ -90,6 +92,7 @@ impl Default for State { item2: 0, item3: 0, text, + text_with_hint, text_multiline, i0: 123, f0: 0.001, @@ -533,6 +536,9 @@ fn show_test_window(ui: &Ui, state: &mut State, opened: &mut bool) { ui.input_text(im_str!("input text"), &mut state.text) .build(); + ui.input_text(im_str!("input text with hint"), &mut state.text_with_hint) + .hint(im_str!("enter text here")) + .build(); ui.input_int(im_str!("input int"), &mut state.i0).build(); Drag::new(im_str!("drag int")).build(ui, &mut state.i0); ui.input_float(im_str!("input float"), &mut state.f0) diff --git a/imgui/src/input_widget.rs b/imgui/src/input_widget.rs index d39d350..a5367ce 100644 --- a/imgui/src/input_widget.rs +++ b/imgui/src/input_widget.rs @@ -159,6 +159,7 @@ extern "C" fn resize_callback(data: *mut sys::ImGuiInputTextCallbackData) -> c_i #[must_use] pub struct InputText<'ui, 'p> { label: &'p ImStr, + hint: Option<&'p ImStr>, buf: &'p mut ImString, flags: ImGuiInputTextFlags, _phantom: PhantomData<&'ui Ui<'ui>>, @@ -168,12 +169,20 @@ impl<'ui, 'p> InputText<'ui, 'p> { pub fn new(_: &Ui<'ui>, label: &'p ImStr, buf: &'p mut ImString) -> Self { InputText { label, + hint: None, buf, flags: ImGuiInputTextFlags::empty(), _phantom: PhantomData, } } + /// Sets the hint displayed in the input text background. + #[inline] + pub fn hint(mut self, hint: &'p ImStr) -> Self { + self.hint = Some(hint); + self + } + impl_text_flags!(InputText); // TODO: boxed closure...? @@ -190,14 +199,26 @@ impl<'ui, 'p> InputText<'ui, 'p> { }; unsafe { - let result = sys::igInputText( - self.label.as_ptr(), - ptr, - capacity, - self.flags.bits(), - callback, - data, - ); + let result = if let Some(hint) = self.hint { + sys::igInputTextWithHint( + self.label.as_ptr(), + hint.as_ptr(), + ptr, + capacity, + self.flags.bits(), + callback, + data, + ) + } else { + sys::igInputText( + self.label.as_ptr(), + ptr, + capacity, + self.flags.bits(), + callback, + data, + ) + }; self.buf.refresh_len(); result }