From c29eed3a94d7b38a2c9ff9d66db1cb1a1f32929d Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 19 Oct 2022 11:08:15 -0600 Subject: [PATCH] Implement Home/End --- examples/editor-libcosmic/src/text_box.rs | 8 ++++++++ examples/editor-orbclient/src/main.rs | 2 ++ src/buffer.rs | 17 +++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index 49fc090..9655bbd 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -239,6 +239,14 @@ where buffer.action(TextAction::Delete); return Status::Captured; }, + KeyCode::Home => { + buffer.action(TextAction::Home); + return Status::Captured; + }, + KeyCode::End => { + buffer.action(TextAction::End); + return Status::Captured; + }, KeyCode::PageUp => { buffer.action(TextAction::PageUp); return Status::Captured; diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index bdf58a5..3413f33 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -166,6 +166,8 @@ fn main() { orbclient::K_DOWN if event.pressed => buffer.action(TextAction::Down), orbclient::K_BKSP if event.pressed => buffer.action(TextAction::Backspace), orbclient::K_DEL if event.pressed => buffer.action(TextAction::Delete), + orbclient::K_HOME if event.pressed => buffer.action(TextAction::Home), + orbclient::K_END if event.pressed => buffer.action(TextAction::End), orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp), orbclient::K_PGDN if event.pressed => buffer.action(TextAction::PageDown), orbclient::K_0 if event.pressed && ctrl_pressed => { diff --git a/src/buffer.rs b/src/buffer.rs index cc37d7c..57f94c7 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -21,6 +21,10 @@ pub enum TextAction { Backspace, /// Delete text in front of cursor Delete, + /// Move cursor to start of line + Home, + /// Move cursor to end of line + End, /// Scroll up one page PageUp, /// Scroll down one page @@ -382,6 +386,19 @@ impl<'a> TextBuffer<'a> { self.reshape_line(line.line_i); } }, + TextAction::Home => { + if self.cursor.glyph > 0 { + self.cursor.glyph = 0; + self.redraw = true; + } + }, + TextAction::End => { + let line = &self.layout_lines[self.cursor.line]; + if self.cursor.glyph < line.glyphs.len() { + self.cursor.glyph = line.glyphs.len(); + self.redraw = true; + } + } TextAction::PageUp => { self.scroll -= self.lines(); self.redraw = true;