diff options
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/lib.rs | 2 | ||||
-rw-r--r-- | native/src/widget/text_input/cursor.rs | 108 |
2 files changed, 65 insertions, 45 deletions
diff --git a/native/src/lib.rs b/native/src/lib.rs index 5c6ab3ee..d17dd918 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -34,7 +34,7 @@ //! [`window::Renderer`]: window/trait.Renderer.html //! [`UserInterface`]: struct.UserInterface.html //! [renderer]: renderer/index.html -//#![deny(missing_docs)] +#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![forbid(unsafe_code)] diff --git a/native/src/widget/text_input/cursor.rs b/native/src/widget/text_input/cursor.rs index d8938777..16e7a01b 100644 --- a/native/src/widget/text_input/cursor.rs +++ b/native/src/widget/text_input/cursor.rs @@ -1,15 +1,27 @@ //! Track the cursor of a text input. use crate::widget::text_input::Value; +/// The cursor of a text input. #[derive(Debug, Copy, Clone)] -pub enum State { - Index(usize), - Selection { start: usize, end: usize }, +pub struct Cursor { + state: State, } +/// The state of a [`Cursor`]. +/// +/// [`Cursor`]: struct.Cursor.html #[derive(Debug, Copy, Clone)] -pub struct Cursor { - state: State, +pub enum State { + /// Cursor without a selection + Index(usize), + + /// Cursor selecting a range of text + Selection { + /// The start of the selection + start: usize, + /// The end of the selection + end: usize, + }, } impl Default for Cursor { @@ -21,19 +33,43 @@ impl Default for Cursor { } impl Cursor { - pub fn move_to(&mut self, position: usize) { + /// Returns the [`State`] of the [`Cursor`]. + /// + /// [`State`]: struct.State.html + /// [`Cursor`]: struct.Cursor.html + pub fn state(&self, value: &Value) -> State { + match self.state { + State::Index(index) => State::Index(index.min(value.len())), + State::Selection { start, end } => { + let start = start.min(value.len()); + let end = end.min(value.len()); + + if start == end { + State::Index(start) + } else { + State::Selection { start, end } + } + } + } + } + + pub(crate) fn move_to(&mut self, position: usize) { self.state = State::Index(position); } - pub fn move_right(&mut self, value: &Value) { + pub(crate) fn move_right(&mut self, value: &Value) { self.move_right_by_amount(value, 1) } - pub fn move_right_by_words(&mut self, value: &Value) { + pub(crate) fn move_right_by_words(&mut self, value: &Value) { self.move_to(value.next_end_of_word(self.right(value))) } - pub fn move_right_by_amount(&mut self, value: &Value, amount: usize) { + pub(crate) fn move_right_by_amount( + &mut self, + value: &Value, + amount: usize, + ) { match self.state(value) { State::Index(index) => { self.move_to(index.saturating_add(amount).min(value.len())) @@ -42,7 +78,7 @@ impl Cursor { } } - pub fn move_left(&mut self, value: &Value) { + pub(crate) fn move_left(&mut self, value: &Value) { match self.state(value) { State::Index(index) if index > 0 => self.move_to(index - 1), State::Selection { start, end } => self.move_to(start.min(end)), @@ -50,11 +86,11 @@ impl Cursor { } } - pub fn move_left_by_words(&mut self, value: &Value) { + pub(crate) fn move_left_by_words(&mut self, value: &Value) { self.move_to(value.previous_start_of_word(self.left(value))); } - pub fn select_range(&mut self, start: usize, end: usize) { + pub(crate) fn select_range(&mut self, start: usize, end: usize) { if start == end { self.state = State::Index(start); } else { @@ -62,7 +98,7 @@ impl Cursor { } } - pub fn select_left(&mut self, value: &Value) { + pub(crate) fn select_left(&mut self, value: &Value) { match self.state(value) { State::Index(index) if index > 0 => { self.select_range(index, index - 1) @@ -74,7 +110,7 @@ impl Cursor { } } - pub fn select_right(&mut self, value: &Value) { + pub(crate) fn select_right(&mut self, value: &Value) { match self.state(value) { State::Index(index) if index < value.len() => { self.select_range(index, index + 1) @@ -86,7 +122,7 @@ impl Cursor { } } - pub fn select_left_by_words(&mut self, value: &Value) { + pub(crate) fn select_left_by_words(&mut self, value: &Value) { match self.state(value) { State::Index(index) => { self.select_range(index, value.previous_start_of_word(index)) @@ -97,7 +133,7 @@ impl Cursor { } } - pub fn select_right_by_words(&mut self, value: &Value) { + pub(crate) fn select_right_by_words(&mut self, value: &Value) { match self.state(value) { State::Index(index) => { self.select_range(index, value.next_end_of_word(index)) @@ -108,27 +144,11 @@ impl Cursor { } } - pub fn select_all(&mut self, value: &Value) { + pub(crate) fn select_all(&mut self, value: &Value) { self.select_range(0, value.len()); } - pub fn state(&self, value: &Value) -> State { - match self.state { - State::Index(index) => State::Index(index.min(value.len())), - State::Selection { start, end } => { - let start = start.min(value.len()); - let end = end.min(value.len()); - - if start == end { - State::Index(start) - } else { - State::Selection { start, end } - } - } - } - } - - pub fn start(&self, value: &Value) -> usize { + pub(crate) fn start(&self, value: &Value) -> usize { let start = match self.state { State::Index(index) => index, State::Selection { start, .. } => start, @@ -137,7 +157,7 @@ impl Cursor { start.min(value.len()) } - pub fn end(&self, value: &Value) -> usize { + pub(crate) fn end(&self, value: &Value) -> usize { let end = match self.state { State::Index(index) => index, State::Selection { end, .. } => end, @@ -146,6 +166,15 @@ impl Cursor { end.min(value.len()) } + pub(crate) fn selection(&self) -> Option<(usize, usize)> { + match self.state { + State::Selection { start, end } => { + Some((start.min(end), start.max(end))) + } + _ => None, + } + } + fn left(&self, value: &Value) -> usize { match self.state(value) { State::Index(index) => index, @@ -159,13 +188,4 @@ impl Cursor { State::Selection { start, end } => start.max(end), } } - - pub fn selection(&self) -> Option<(usize, usize)> { - match self.state { - State::Selection { start, end } => { - Some((start.min(end), start.max(end))) - } - _ => None, - } - } } |