Skip to main content

TextAreaState

Struct TextAreaState 

Source
pub struct TextAreaState { /* private fields */ }
Expand description

State for a TextArea component.

Implementations§

Source§

impl TextAreaState

Source

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

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

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

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

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

Source

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

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

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

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

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Source§

fn clone(&self) -> TextAreaState

Returns a duplicate 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 TextAreaState

Source§

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

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

impl Default for TextAreaState

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for TextAreaState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for TextAreaState

Source§

fn eq(&self, other: &TextAreaState) -> 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 Serialize for TextAreaState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for TextAreaState

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,