pub struct TextAreaState { /* private fields */ }Expand description
State for a TextArea component.
Implementations§
Source§impl TextAreaState
impl TextAreaState
Sourcepub fn search_query(&self) -> Option<&str>
pub fn search_query(&self) -> Option<&str>
Returns the current search query, if any.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("hello world");
assert_eq!(state.search_query(), None);
TextArea::update(&mut state, TextAreaMessage::SetSearchQuery("hello".into()));
assert_eq!(state.search_query(), Some("hello"));Sourcepub fn search_matches(&self) -> &[(usize, usize)]
pub fn search_matches(&self) -> &[(usize, usize)]
Returns the list of search matches as (line, byte_col) pairs.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("foo bar foo");
TextArea::update(&mut state, TextAreaMessage::SetSearchQuery("foo".into()));
assert_eq!(state.search_matches().len(), 2);Sourcepub fn current_match_index(&self) -> usize
pub fn current_match_index(&self) -> usize
Returns the index of the current match within the match list.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("aaa");
TextArea::update(&mut state, TextAreaMessage::SetSearchQuery("a".into()));
assert_eq!(state.current_match_index(), 0);Sourcepub fn current_match_position(&self) -> Option<(usize, usize)>
pub fn current_match_position(&self) -> Option<(usize, usize)>
Returns the current match as (line, byte_col), if any.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("hello");
TextArea::update(&mut state, TextAreaMessage::SetSearchQuery("hello".into()));
assert_eq!(state.current_match_position(), Some((0, 0)));Sourcepub fn is_searching(&self) -> bool
pub fn is_searching(&self) -> bool
Returns true if search mode is active.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new();
assert!(!state.is_searching());
TextArea::update(&mut state, TextAreaMessage::StartSearch);
assert!(state.is_searching());Source§impl TextAreaState
impl TextAreaState
Sourcepub fn has_selection(&self) -> bool
pub fn has_selection(&self) -> bool
Returns true if there is an active text selection.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("hello");
assert!(!state.has_selection());
TextArea::update(&mut state, TextAreaMessage::SelectAll);
assert!(state.has_selection());Sourcepub fn selection_positions(&self) -> Option<((usize, usize), (usize, usize))>
pub fn selection_positions(&self) -> Option<((usize, usize), (usize, usize))>
Returns the ordered selection positions as ((start_row, start_col), (end_row, end_col)).
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("hello");
TextArea::update(&mut state, TextAreaMessage::SelectAll);
let positions = state.selection_positions();
assert_eq!(positions, Some(((0, 0), (0, 5))));Sourcepub fn selected_text(&self) -> Option<String>
pub fn selected_text(&self) -> Option<String>
Returns the selected text, or None if no selection.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("hello");
assert_eq!(state.selected_text(), None);
TextArea::update(&mut state, TextAreaMessage::SelectAll);
assert_eq!(state.selected_text(), Some("hello".to_string()));Sourcepub fn clipboard(&self) -> &str
pub fn clipboard(&self) -> &str
Returns the internal clipboard contents.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("hello");
TextArea::update(&mut state, TextAreaMessage::SelectAll);
TextArea::update(&mut state, TextAreaMessage::Copy);
assert_eq!(state.clipboard(), "hello");Source§impl TextAreaState
impl TextAreaState
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty textarea.
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new();
assert!(state.is_empty());
assert_eq!(state.line_count(), 1);Sourcepub fn with_value(self, value: impl Into<String>) -> Self
pub fn with_value(self, value: impl Into<String>) -> Self
Creates a textarea with initial content, split on newlines. Sets the content and places cursor at the end (builder pattern).
§Examples
use envision::prelude::*;
let state = TextAreaState::new().with_value("Hello\nWorld");
assert_eq!(state.value(), "Hello\nWorld");
assert_eq!(state.line_count(), 2);Sourcepub fn with_placeholder(self, placeholder: impl Into<String>) -> Self
pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self
Sets the placeholder text (builder pattern).
§Examples
use envision::prelude::*;
let state = TextAreaState::new().with_placeholder("Enter text...");
assert_eq!(state.placeholder(), "Enter text...");
assert!(state.is_empty());Sourcepub fn value(&self) -> String
pub fn value(&self) -> String
Returns the full text content (lines joined with \n).
§Examples
use envision::prelude::*;
let state = TextAreaState::new().with_value("line1\nline2");
assert_eq!(state.value(), "line1\nline2");Sourcepub fn set_value(&mut self, value: impl Into<String>)
pub fn set_value(&mut self, value: impl Into<String>)
Sets the content from a string (splits on \n). Cursor moves to end.
§Examples
use envision::prelude::*;
let mut state = TextAreaState::new();
state.set_value("Hello\nWorld");
assert_eq!(state.value(), "Hello\nWorld");
assert_eq!(state.cursor_position(), (1, 5));Sourcepub fn cursor_position(&self) -> (usize, usize)
pub fn cursor_position(&self) -> (usize, usize)
Returns the cursor position as (row, char_column).
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new();
TextArea::update(&mut state, TextAreaMessage::Insert('H'));
TextArea::update(&mut state, TextAreaMessage::Insert('i'));
assert_eq!(state.cursor_position(), (0, 2));Sourcepub fn cursor_display_position(&self) -> (usize, usize)
pub fn cursor_display_position(&self) -> (usize, usize)
Returns the cursor display position as (row, terminal_column_width).
Unlike cursor_position() which returns the
character count for the column, this returns the display width
accounting for wide characters (emoji, CJK) that occupy 2 terminal columns.
§Example
use envision::component::{TextArea, TextAreaState, TextAreaMessage, Component};
let mut state = TextArea::init();
TextArea::update(&mut state, TextAreaMessage::Insert('A'));
TextArea::update(&mut state, TextAreaMessage::Insert('\u{1F600}')); // emoji
// Character count is 2 (two characters)
assert_eq!(state.cursor_position(), (0, 2));
// Display width is 3 (A=1 + 😀=2)
assert_eq!(state.cursor_display_position(), (0, 3));Sourcepub fn cursor_row(&self) -> usize
pub fn cursor_row(&self) -> usize
Returns the cursor row.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new().with_value("Line 1\nLine 2");
assert_eq!(state.cursor_row(), 1);Sourcepub fn cursor_col(&self) -> usize
pub fn cursor_col(&self) -> usize
Returns the cursor column (byte offset).
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let state = TextAreaState::new().with_value("Hello");
assert_eq!(state.cursor_col(), 5);Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Returns the number of lines.
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new().with_value("a\nb\nc");
assert_eq!(state.line_count(), 3);Sourcepub fn line(&self, index: usize) -> Option<&str>
pub fn line(&self, index: usize) -> Option<&str>
Returns a specific line by index.
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new().with_value("first\nsecond\nthird");
assert_eq!(state.line(0), Some("first"));
assert_eq!(state.line(1), Some("second"));
assert_eq!(state.line(99), None);Sourcepub fn current_line(&self) -> &str
pub fn current_line(&self) -> &str
Returns the current line (at cursor row).
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new().with_value("first\nsecond");
assert_eq!(state.current_line(), "second");Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the textarea is empty.
A textarea is empty if it contains only a single empty line.
§Examples
use envision::prelude::*;
assert!(TextAreaState::new().is_empty());
assert!(!TextAreaState::new().with_value("hi").is_empty());Sourcepub fn placeholder(&self) -> &str
pub fn placeholder(&self) -> &str
Returns the placeholder text.
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new().with_placeholder("Type here...");
assert_eq!(state.placeholder(), "Type here...");Sourcepub fn set_placeholder(&mut self, placeholder: impl Into<String>)
pub fn set_placeholder(&mut self, placeholder: impl Into<String>)
Sets the placeholder text.
§Example
use envision::component::TextAreaState;
let mut state = TextAreaState::new();
state.set_placeholder("Enter your text...");
assert_eq!(state.placeholder(), "Enter your text...");Sourcepub fn scroll_offset(&self) -> usize
pub fn scroll_offset(&self) -> usize
Returns the scroll offset.
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new();
assert_eq!(state.scroll_offset(), 0);Sourcepub fn set_cursor_position(&mut self, row: usize, col: usize)
pub fn set_cursor_position(&mut self, row: usize, col: usize)
Sets the cursor position (row, char_column).
Both row and column are clamped to valid ranges.
§Example
use envision::component::TextAreaState;
let mut state = TextAreaState::new().with_value("Hello\nWorld");
state.set_cursor_position(0, 3);
assert_eq!(state.cursor_position(), (0, 3));Sourcepub fn ensure_cursor_visible(&mut self, visible_lines: usize)
pub fn ensure_cursor_visible(&mut self, visible_lines: usize)
Ensures the cursor is visible within the viewport.
Adjusts scroll_offset so that the cursor row is within the range
[scroll_offset, scroll_offset + visible_lines).
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new();
for _ in 0..10 {
TextArea::update(&mut state, TextAreaMessage::NewLine);
}
state.ensure_cursor_visible(5);
assert!(state.scroll_offset() + 5 > state.cursor_position().0);Sourcepub fn can_undo(&self) -> bool
pub fn can_undo(&self) -> bool
Returns true if there are edits that can be undone.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new();
assert!(!state.can_undo());
TextArea::update(&mut state, TextAreaMessage::Insert('a'));
assert!(state.can_undo());Sourcepub fn can_redo(&self) -> bool
pub fn can_redo(&self) -> bool
Returns true if there are edits that can be redone.
§Example
use envision::component::{TextArea, TextAreaMessage, TextAreaState, Component};
let mut state = TextAreaState::new();
TextArea::update(&mut state, TextAreaMessage::Insert('a'));
TextArea::update(&mut state, TextAreaMessage::Undo);
assert!(state.can_redo());Sourcepub fn with_line_numbers(self, show: bool) -> Self
pub fn with_line_numbers(self, show: bool) -> Self
Sets whether line numbers are shown (builder pattern).
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new().with_line_numbers(true);
assert!(state.show_line_numbers());Sourcepub fn show_line_numbers(&self) -> bool
pub fn show_line_numbers(&self) -> bool
Returns whether line numbers are shown.
§Example
use envision::component::TextAreaState;
let state = TextAreaState::new().with_line_numbers(true);
assert!(state.show_line_numbers());Sourcepub fn set_show_line_numbers(&mut self, show: bool)
pub fn set_show_line_numbers(&mut self, show: bool)
Sets whether line numbers are shown.
§Example
use envision::component::TextAreaState;
let mut state = TextAreaState::new();
state.set_show_line_numbers(true);
assert!(state.show_line_numbers());Sourcepub fn update(&mut self, msg: TextAreaMessage) -> Option<TextAreaOutput>
pub fn update(&mut self, msg: TextAreaMessage) -> Option<TextAreaOutput>
Updates the textarea state with a message, returning any output.
§Example
use envision::component::{TextAreaMessage, TextAreaOutput, TextAreaState};
let mut state = TextAreaState::new();
state.update(TextAreaMessage::Insert('a'));
state.update(TextAreaMessage::Insert('b'));
assert_eq!(state.value(), "ab");Trait Implementations§
Source§impl Clone for TextAreaState
impl Clone for TextAreaState
Source§fn clone(&self) -> TextAreaState
fn clone(&self) -> TextAreaState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TextAreaState
impl Debug for TextAreaState
Source§impl Default for TextAreaState
impl Default for TextAreaState
Source§impl<'de> Deserialize<'de> for TextAreaState
impl<'de> Deserialize<'de> for TextAreaState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for TextAreaState
impl PartialEq for TextAreaState
Source§impl Serialize for TextAreaState
impl Serialize for TextAreaState
impl StructuralPartialEq for TextAreaState
Auto Trait Implementations§
impl Freeze for TextAreaState
impl RefUnwindSafe for TextAreaState
impl Send for TextAreaState
impl Sync for TextAreaState
impl Unpin for TextAreaState
impl UnsafeUnpin for TextAreaState
impl UnwindSafe for TextAreaState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more