Struct TextEditState

Source
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: usize

Active 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: f32

Widget-managed horizontal scroll offset in logical pixels.

§scroll_y: f32

Widget-managed vertical scroll offset in logical pixels.

§drag_anchor: Option<usize>

Widget-managed pointer-drag anchor byte index.

Implementations§

Source§

impl TextEditState

Source

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);
Source

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 }));
Source

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"));
Source

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);
Source

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

Source§

fn clone(&self) -> TextEditState

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextEditState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TextEditState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for TextEditState

Source§

fn eq(&self, other: &TextEditState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TextEditState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Brush for T
where T: Clone + PartialEq + Default + Debug,

§

impl<T> ErasedDestructor for T
where T: 'static,