Struct imgui::TextCallbackData
source · pub struct TextCallbackData(/* private fields */);
Expand description
This struct provides methods to edit the underlying text buffer that Dear ImGui manipulates. Primarily, it gives remove_chars, insert_chars, and mutable access to what text is selected.
Implementations§
source§impl TextCallbackData
impl TextCallbackData
sourcepub unsafe fn str_as_bytes_mut(&mut self) -> &mut [u8] ⓘ
pub unsafe fn str_as_bytes_mut(&mut self) -> &mut [u8] ⓘ
Gives access to the underlying byte array MUTABLY.
§Safety
This is very unsafe, and the following invariants must be upheld:
- Keep the data utf8 valid.
- After editing the string, call set_dirty.
To truncate the string, please use remove_chars. To extend the string, please use insert_chars and push_str.
This function should have highly limited usage, but could be for editing certain characters in the buffer based on some external condition.
sourcepub fn set_dirty(&mut self)
pub fn set_dirty(&mut self)
Sets the dirty flag on the text to imgui, indicating that it should reapply this string to its internal state.
NB: You only need to use this method if you’re using [str_as_bytes_mut]
.
If you use the helper methods remove_chars and insert_chars,
this will be set for you. However, this is no downside to setting
the dirty flag spuriously except the minor CPU time imgui will spend.
sourcepub fn selection(&self) -> Range<usize>
pub fn selection(&self) -> Range<usize>
Gets a range of the selected text. See selection_start_mut and selection_end_mut to mutably edit these values.
This Range is given in usize
so that it might be used in indexing
operations more easily. To quickly grab the selected text, use selected.
sourcepub fn selected(&self) -> &str
pub fn selected(&self) -> &str
Returns the selected text directly. Note that if no text is selected, an empty str slice will be returned.
sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
Sets the cursor to select all.
sourcepub fn clear_selection(&mut self)
pub fn clear_selection(&mut self)
Clears the selection.
sourcepub fn has_selection(&self) -> bool
pub fn has_selection(&self) -> bool
Checks if there is a selection within the text.
sourcepub fn push_str(&mut self, s: &str)
pub fn push_str(&mut self, s: &str)
Pushes the given str to the end of this buffer. If this would require the String to resize, it will be resized. This is automatically handled.
sourcepub fn insert_chars(&mut self, pos: usize, s: &str)
pub fn insert_chars(&mut self, pos: usize, s: &str)
Inserts the given string at the given position. If this would require the String to resize, it will be resized automatically.
§Panics
Panics if the pos
is not a char_boundary.
sourcepub unsafe fn insert_chars_unsafe(&mut self, pos: usize, s: &str)
pub unsafe fn insert_chars_unsafe(&mut self, pos: usize, s: &str)
Inserts the given string at the given position, unsafely. If this would require the String to resize, it will be resized automatically.
§Safety
It is up to the caller to confirm that the pos
is a valid byte
position, or use insert_chars which will panic
if it isn’t.
sourcepub fn remove_chars(&mut self, pos: usize, char_count: usize)
pub fn remove_chars(&mut self, pos: usize, char_count: usize)
Removes the given number of characters from the string starting at some byte pos.
§Panics
Panics if the pos
is not a char boundary.
sourcepub unsafe fn remove_chars_unchecked(&mut self, pos: usize, byte_count: usize)
pub unsafe fn remove_chars_unchecked(&mut self, pos: usize, byte_count: usize)
Removes the given number of bytes from the string starting at some byte pos, without checking for utf8 validity. Use remove_chars for a safe variant.
§Safety
It is up to the caller to ensure that the position is at a valid utf8 char_boundary and that there are enough bytes within the string remaining.
sourcepub fn cursor_pos(&self) -> usize
pub fn cursor_pos(&self) -> usize
Get a reference to the text callback buffer’s cursor pos.
sourcepub fn set_cursor_pos(&mut self, cursor_pos: usize)
pub fn set_cursor_pos(&mut self, cursor_pos: usize)
Set the text callback buffer’s cursor pos.
sourcepub fn selection_start_mut(&mut self) -> &mut i32
pub fn selection_start_mut(&mut self) -> &mut i32
Get a mutable reference to the text callback buffer’s selection start.
sourcepub fn selection_end_mut(&mut self) -> &mut i32
pub fn selection_end_mut(&mut self) -> &mut i32
Get a mutable reference to the text callback buffer’s selection end..