pub struct TextEditState {
pub caret_byte: usize,
pub selection: Option<TextSelection>,
pub preedit: Option<ImePreedit>,
pub desired_x: Option<f32>,
pub scroll_x: f32,
pub scroll_y: f32,
pub drag_anchor: Option<usize>,
/* private fields */
}Expand description
Mutable edit session: caret, selection, IME preedit, scroll, undo stacks.
Byte positions are public for widget integration but should be kept on UTF-8
boundaries; Self::apply clamps them before and after every action. Undo
and redo snapshots copy the whole buffer and retain at most 256 undo entries.
Scroll positions are logical pixels and are not clamped or interpreted by
this engine. Cloning duplicates all snapshot strings.
§Examples
use ailloli_ui_text::TextEditState;
let state = TextEditState::new();
assert_eq!(state.caret_byte, 0);
assert_eq!(state.selection, None);
assert_eq!((state.scroll_x, state.scroll_y), (0.0, 0.0));Fields§
§caret_byte: usizeActive caret as a UTF-8 byte index.
selection: Option<TextSelection>Optional anchor/caret pair; Some(collapsed) remains distinct from None.
preedit: Option<ImePreedit>Active IME composition, kept outside the committed TextBuffer.
desired_x: Option<f32>Reserved horizontal target in logical pixels; current movement clears but does not use it.
scroll_x: f32Widget-managed horizontal scroll offset in logical pixels.
scroll_y: f32Widget-managed vertical scroll offset in logical pixels.
drag_anchor: Option<usize>Widget-managed pointer-drag anchor byte index.
Implementations§
Source§impl TextEditState
impl TextEditState
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a session at byte zero with no selection, IME, history, or scroll.
§Examples
use ailloli_ui_text::TextEditState;
let state = TextEditState::new();
assert_eq!(state, TextEditState::default());
assert_eq!(state.desired_x, None);Sourcepub fn clamp_to_buffer(&mut self, buffer: &TextBuffer)
pub fn clamp_to_buffer(&mut self, buffer: &TextBuffer)
Clamps caret and selection endpoints backward to buffer UTF-8 boundaries.
Values beyond the end become buffer.len_bytes(). Values inside a
multibyte scalar move to its starting byte. The method does not force the
state’s caret to equal selection.caret, remove collapsed selections,
or modify IME, scroll, drag, and history fields.
§Examples
use ailloli_ui_text::{TextBuffer, TextEditState, TextSelection};
let buffer = TextBuffer::from_string("é");
let mut state = TextEditState::new();
state.caret_byte = 1;
state.selection = Some(TextSelection { anchor: 1, caret: 99 });
state.clamp_to_buffer(&buffer);
assert_eq!(state.caret_byte, 0);
assert_eq!(state.selection, Some(TextSelection { anchor: 0, caret: 2 }));Sourcepub fn selected_text(&self, buffer: &TextBuffer) -> Option<String>
pub fn selected_text(&self, buffer: &TextBuffer) -> Option<String>
Copies the current non-collapsed selection from buffer.
Reversed selections are normalized. None means no selection or a
collapsed one. Endpoints beyond the buffer are clamped to its end.
§Panics
This method can panic if publicly mutated selection endpoints lie inside
a multibyte scalar. Call Self::clamp_to_buffer first when state did not
come through Self::apply.
§Examples
use ailloli_ui_text::{TextBuffer, TextEditState, TextSelection};
let buffer = TextBuffer::from_string("hello");
let mut state = TextEditState::new();
state.selection = Some(TextSelection { anchor: 5, caret: 1 });
assert_eq!(state.selected_text(&buffer).as_deref(), Some("ello"));Sourcepub fn set_caret(
&mut self,
buffer: &TextBuffer,
caret_byte: usize,
extend: bool,
) -> bool
pub fn set_caret( &mut self, buffer: &TextBuffer, caret_byte: usize, extend: bool, ) -> bool
Moves the caret, optionally extending the current selection.
caret_byte is clamped backward to a UTF-8 boundary. With extend, an
existing anchor is preserved; otherwise the current caret becomes the
anchor. Without extend, selection is cleared. desired_x is always
cleared. The boolean reports whether caret, selection, or desired_x
changed; it does not report a text mutation.
Existing public state is not pre-clamped by this method, so callers with
externally modified indices should call Self::clamp_to_buffer first.
§Examples
use ailloli_ui_text::{TextBuffer, TextEditState, TextSelection};
let buffer = TextBuffer::from_string("abc");
let mut state = TextEditState::new();
assert!(state.set_caret(&buffer, 2, true));
assert_eq!(state.selection, Some(TextSelection { anchor: 0, caret: 2 }));
assert_eq!(state.caret_byte, 2);Sourcepub fn apply(
&mut self,
buffer: &mut TextBuffer,
action: TextEditAction,
) -> TextEditOutcome
pub fn apply( &mut self, buffer: &mut TextBuffer, action: TextEditAction, ) -> TextEditOutcome
Applies one editing command and returns requested side effects.
State indices are clamped before and after dispatch. Nonempty committed
text mutations take a whole-buffer undo snapshot, clear redo, update the
buffer revision, clear selection/desired_x, and cap undo at 256 entries.
InsertText replaces each carriage return with a newline; Paste and
ImeCommit preserve their strings exactly. Empty inserts and unavailable
undo/redo operations are no-ops.
text_changed means the action executed a text mutation, not that old and
new strings were compared for inequality. state_changed excludes pure
clipboard requests: Copy can set clipboard_write and RequestPaste sets
clipboard_read while both flags remain false. IME preedit is not part of
the buffer and is cleared only by commit, end, undo, or redo.
§Examples
use ailloli_ui_text::{TextBuffer, TextEditAction, TextEditState};
let mut buffer = TextBuffer::from_string("ab");
let mut state = TextEditState::new();
state.caret_byte = 2;
let outcome = state.apply(&mut buffer, TextEditAction::InsertText { text: "\rc".into() });
assert_eq!(buffer.as_str(), "ab\nc");
assert!(outcome.text_changed && outcome.state_changed);Trait Implementations§
Source§impl Clone for TextEditState
impl Clone for TextEditState
Source§fn clone(&self) -> TextEditState
fn clone(&self) -> TextEditState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more