mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-27 13:28:28 +00:00
oops broke text
This commit is contained in:
parent
daea06108f
commit
6faa7f090a
27
imgui-examples/examples/text_input.rs
Normal file
27
imgui-examples/examples/text_input.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -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
|
// SAFETY: this slice is simply a view into the underlying buffer
|
||||||
// of a String. We MAY be holding onto a view of uninitialized memory,
|
// 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
|
// however, since we're holding this as a u8 slice, I think it should be
|
||||||
// alright...
|
// alright...
|
||||||
// additionally, we can go over the bytes directly, rather than char indices,
|
// 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
|
// because NUL will never appear in any UTF8 outside the NUL character (ie, within
|
||||||
// a char).
|
// a char).
|
||||||
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
|
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
|
||||||
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
|
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
|
||||||
// `len` is the position of the first `\0` byte in the String
|
// `len` is the position of the first `\0` byte in the String
|
||||||
unsafe {
|
unsafe {
|
||||||
self.buf.as_mut_vec().set_len(len);
|
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.
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// There is no null terminator, the best we can do is to not
|
||||||
|
// update the string length.
|
||||||
}
|
}
|
||||||
|
|
||||||
o
|
o
|
||||||
@ -468,26 +466,24 @@ impl<'ui, 'p, T: InputTextCallbackHandler, L: AsRef<str>> 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
|
// SAFETY: this slice is simply a view into the underlying buffer
|
||||||
// of a String. We MAY be holding onto a view of uninitialized memory,
|
// 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
|
// however, since we're holding this as a u8 slice, I think it should be
|
||||||
// alright...
|
// alright...
|
||||||
// additionally, we can go over the bytes directly, rather than char indices,
|
// 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
|
// because NUL will never appear in any UTF8 outside the NUL character (ie, within
|
||||||
// a char).
|
// a char).
|
||||||
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
|
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
|
||||||
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
|
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
|
||||||
// `len` is the position of the first `\0` byte in the String
|
// `len` is the position of the first `\0` byte in the String
|
||||||
unsafe {
|
unsafe {
|
||||||
self.buf.as_mut_vec().set_len(len);
|
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.
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// There is no null terminator, the best we can do is to not
|
||||||
|
// update the string length.
|
||||||
}
|
}
|
||||||
|
|
||||||
o
|
o
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user