Skip to content

Commit

Permalink
add input_text hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
lwiklendt authored and thomcc committed Feb 18, 2021
1 parent b3f5558 commit 3d3097e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions imgui-examples/examples/test_window_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct State {
item2: usize,
item3: i32,
text: ImString,
text_with_hint: ImString,
text_multiline: ImString,
i0: i32,
f0: f32,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -90,6 +92,7 @@ impl Default for State {
item2: 0,
item3: 0,
text,
text_with_hint,
text_multiline,
i0: 123,
f0: 0.001,
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 29 additions & 8 deletions imgui/src/input_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand All @@ -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...?
Expand All @@ -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
}
Expand Down

0 comments on commit 3d3097e

Please sign in to comment.