diff --git a/imgui-examples/examples/text_input.rs b/imgui-examples/examples/text_input.rs new file mode 100644 index 0000000..c052de2 --- /dev/null +++ b/imgui-examples/examples/text_input.rs @@ -0,0 +1,27 @@ +use imgui::*; + +mod support; + +fn main() { + let system = support::init(file!()); + let mut stable_str = String::new(); + + system.main_loop(move |_, ui| { + if let Some(_window) = ui + .window("Input text callbacks") + .size([500.0, 300.0], Condition::FirstUseEver) + .begin() + { + if ui.input_text("input stable", &mut stable_str).build() { + dbg!(&stable_str); + } + + let mut per_frame_buf = String::new(); + ui.input_text("input per frame", &mut per_frame_buf).build(); + + if ui.is_item_deactivated_after_edit() { + dbg!(&per_frame_buf); + } + } + }); +} diff --git a/imgui/src/input_widget.rs b/imgui/src/input_widget.rs index 43df338..7865446 100644 --- a/imgui/src/input_widget.rs +++ b/imgui/src/input_widget.rs @@ -320,26 +320,24 @@ where } }; - if o { - let cap = self.buf.capacity(); + let cap = self.buf.capacity(); - // SAFETY: this slice is simply a view into the underlying buffer - // of a String. We MAY be holding onto a view of uninitialized memory, - // however, since we're holding this as a u8 slice, I think it should be - // alright... - // additionally, we can go over the bytes directly, rather than char indices, - // because NUL will never appear in any UTF8 outside the NUL character (ie, within - // a char). - let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) }; - if let Some(len) = buf.iter().position(|x| *x == b'\0') { - // `len` is the position of the first `\0` byte in the String - unsafe { - self.buf.as_mut_vec().set_len(len); - } - } else { - // There is no null terminator, the best we can do is to not - // update the string length. + // SAFETY: this slice is simply a view into the underlying buffer + // of a String. We MAY be holding onto a view of uninitialized memory, + // however, since we're holding this as a u8 slice, I think it should be + // alright... + // additionally, we can go over the bytes directly, rather than char indices, + // because NUL will never appear in any UTF8 outside the NUL character (ie, within + // a char). + let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) }; + if let Some(len) = buf.iter().position(|x| *x == b'\0') { + // `len` is the position of the first `\0` byte in the String + unsafe { + self.buf.as_mut_vec().set_len(len); } + } else { + // There is no null terminator, the best we can do is to not + // update the string length. } o @@ -468,26 +466,24 @@ impl<'ui, 'p, T: InputTextCallbackHandler, L: AsRef> InputTextMultiline<'ui ) }; - if o { - let cap = self.buf.capacity(); + let cap = self.buf.capacity(); - // SAFETY: this slice is simply a view into the underlying buffer - // of a String. We MAY be holding onto a view of uninitialized memory, - // however, since we're holding this as a u8 slice, I think it should be - // alright... - // additionally, we can go over the bytes directly, rather than char indices, - // because NUL will never appear in any UTF8 outside the NUL character (ie, within - // a char). - let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) }; - if let Some(len) = buf.iter().position(|x| *x == b'\0') { - // `len` is the position of the first `\0` byte in the String - unsafe { - self.buf.as_mut_vec().set_len(len); - } - } else { - // There is no null terminator, the best we can do is to not - // update the string length. + // SAFETY: this slice is simply a view into the underlying buffer + // of a String. We MAY be holding onto a view of uninitialized memory, + // however, since we're holding this as a u8 slice, I think it should be + // alright... + // additionally, we can go over the bytes directly, rather than char indices, + // because NUL will never appear in any UTF8 outside the NUL character (ie, within + // a char). + let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) }; + if let Some(len) = buf.iter().position(|x| *x == b'\0') { + // `len` is the position of the first `\0` byte in the String + unsafe { + self.buf.as_mut_vec().set_len(len); } + } else { + // There is no null terminator, the best we can do is to not + // update the string length. } o