pub struct EditCore {
pub bounds: Rect,
pub buffer: String,
pub caret: usize,
pub active: bool,
pub multi_line: bool,
pub max_len: Option<usize>,
pub accept: Option<AcceptFn>,
pub on_change: Option<ChangeCallback>,
pub char_width: i32,
pub line_height: i32,
}Expand description
Shared edit-state machine for single-line and multi-line text fields (WID-00 §5; LPAR-14 §5.C).
EditCore owns the edit buffer, caret position, and the optional
mutation gates (max_len, accept, on_change). Rendering helpers
that need glyph geometry or a [Label] remain in the widget wrappers
that embed this struct.
§Buffer and caret invariants
- The buffer may contain arbitrary Unicode inserted via
set_text. - Interactive insertion via
try_insertis limited to printable ASCII (0x20..=0x7E) plus'\n'on multi-line fields (WID-00 §5.2). caretis a char index (0..=char_count()).byte_indexconverts it to a byte offset for safeStringmutation.
Fields§
§bounds: RectText bounds used for caret geometry and derived rendering.
Widgets that embed EditCore expose set_bounds on themselves and
forward the update here.
buffer: StringEdit buffer.
ASCII-bounded for keyboard input so byte index == char index in
practice; stored as String so a programmatic set_text can hold
arbitrary Unicode without corrupting subsequent edits.
caret: usizeCaret position as a char index in 0..=char_count().
active: boolWhether this field is currently consuming keyboard events.
multi_line: boolWhether Enter inserts a newline (true) or signals submit (false).
max_len: Option<usize>Optional cap on the number of characters in the buffer.
accept: Option<AcceptFn>Optional accepted-charset predicate (applied after the ASCII gate).
on_change: Option<ChangeCallback>Optional change callback invoked after every committed edit.
char_width: i32Nominal character advance used for caret geometry (pixels).
line_height: i32Nominal line height used for caret geometry and line pitch (pixels).
Implementations§
Source§impl EditCore
impl EditCore
Sourcepub fn new(text: &str, bounds: Rect, multi_line: bool) -> Self
pub fn new(text: &str, bounds: Rect, multi_line: bool) -> Self
Create an EditCore with text pre-loaded, caret at the end.
multi_line controls whether Enter inserts a newline.
Sourcepub fn set_text(&mut self, text: &str)
pub fn set_text(&mut self, text: &str)
Replace the buffer with text, clamp the caret, fire on_change.
This is the programmatic path; it bypasses the ASCII gate and
accept predicate.
Sourcepub fn committed(&mut self)
pub fn committed(&mut self)
Sync the label/display state and fire on_change after a committed
edit (WID-00 §5.1).
Called internally by try_insert and try_backspace; also
available for widget wrappers that perform their own buffer mutations.
Sourcepub fn byte_index(&self, char_index: usize) -> usize
pub fn byte_index(&self, char_index: usize) -> usize
Convert a char index to a byte offset in Self::buffer.
Safe for any char index in 0..=char_count(); returns
buffer.len() for out-of-range values.
Sourcepub fn char_count(&self) -> usize
pub fn char_count(&self) -> usize
Return the number of chars currently in the buffer.
Sourcepub fn try_insert(&mut self, c: char) -> bool
pub fn try_insert(&mut self, c: char) -> bool
Attempt to insert c at the caret.
Returns true when the edit was applied. Failed insertions leave
the buffer and caret untouched and do not fire on_change
(WID-00 §5.1).
Gate order:
- ASCII printable bound (
0x20..=0x7E) plus'\n'on multi-line. acceptpredicate (skipped for'\n').max_lencap.
Sourcepub fn try_backspace(&mut self) -> bool
pub fn try_backspace(&mut self) -> bool
Delete the character before the caret.
Returns true if a character was removed. No-ops silently at
position 0 (WID-00 §5.1).
Sourcepub fn handle_key(&mut self, key: &Key) -> bool
pub fn handle_key(&mut self, key: &Key) -> bool
Handle a raw key event while the field is active.
Returns true when the key was consumed. Enter is not
handled here — its semantics differ between single-line (Input
fires submit) and multi-line (Textarea inserts newline), so the
wrapper is responsible (WID-00 §5.3).
Sourcepub fn caret_row_col(&self) -> (i32, i32)
pub fn caret_row_col(&self) -> (i32, i32)
Return the (row, col) of the caret in '\n'-split line space.
Row 0 is the first line; col 0 is the start of a line.