From 405c77bb47209764d3831198057fd9975a1a791f Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 19 Oct 2022 08:34:34 -0600 Subject: [PATCH] More documentation --- examples/editor-libcosmic/src/text_box.rs | 4 +++- examples/editor-orbclient/src/main.rs | 4 +++- src/buffer.rs | 22 +++++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index 28afa35..ca17aa9 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -249,7 +249,9 @@ where }, MouseEvent::WheelScrolled { delta } => match delta { ScrollDelta::Lines { x, y } => { - buffer.action(TextAction::Scroll((-y * 6.0) as i32)); + buffer.action(TextAction::Scroll { + lines: (-y * 6.0) as i32, + }); Status::Captured }, _ => Status::Ignored, diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index 8292a9e..6b3bf96 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -227,7 +227,9 @@ fn main() { ); } EventOption::Scroll(event) => { - buffer.action(TextAction::Scroll(-event.y * 3)); + buffer.action(TextAction::Scroll { + lines: -event.y * 3, + }); } EventOption::Quit(_) => return, _ => (), diff --git a/src/buffer.rs b/src/buffer.rs index 6acfd2d..ce90e52 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -9,17 +9,28 @@ use crate::{FontLayoutLine, FontMatches, FontShapeLine}; /// An action to perform on a [TextBuffer] #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum TextAction { + /// Move cursor left Left, + /// Move cursor right Right, + /// Move cursor up Up, + /// Move cursor down Down, + /// Delete text behind cursor Backspace, + /// Delete text in front of cursor Delete, + /// Scroll up one page PageUp, + /// Scroll down one page PageDown, + /// Insert character at cursor Insert(char), + /// Mouse click at specified position Click { x: i32, y: i32 }, - Scroll(i32), + /// Scroll specified number of lines + Scroll { lines: i32 }, } /// Current cursor location @@ -87,7 +98,7 @@ pub struct TextBuffer<'a> { width: i32, height: i32, scroll: i32, - pub cursor: TextCursor, + cursor: TextCursor, pub redraw: bool, } @@ -220,6 +231,11 @@ impl<'a> TextBuffer<'a> { log::debug!("relayout line {}: {:?}", line_i.get(), duration); } + /// Get the current cursor position + pub fn cursor(&self) -> TextCursor { + self.cursor + } + /// Get the current [TextMetrics] pub fn metrics(&self) -> TextMetrics { self.metrics @@ -387,7 +403,7 @@ impl<'a> TextBuffer<'a> { TextAction::Click { x, y } => { self.click(x, y); }, - TextAction::Scroll(lines) => { + TextAction::Scroll { lines } => { self.scroll += lines; self.redraw = true; self.shape_until_scroll();