From a4c51a4812ab532051b4b9d8aafb51afe939497a Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 14 Oct 2022 21:25:44 +0900 Subject: [PATCH] implement scrolling down/up by half/full page --- src/textarea.rs | 25 ++++++++++++++++++++++++- src/widget.rs | 10 +++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/textarea.rs b/src/textarea.rs index be26def..a13aec0 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -45,13 +45,36 @@ pub enum Scrolling { /// textarea.scroll((1, 0)); /// assert_eq!(textarea.cursor(), (3, 0)); /// ``` - Delta { rows: i16, cols: i16 }, + Delta { + rows: i16, + cols: i16, + }, + PageDown, + PageUp, + HalfPageDown, + HalfPageUp, } impl Scrolling { fn scroll(self, viewport: &mut Viewport) { let (rows, cols) = match self { Self::Delta { rows, cols } => (rows, cols), + Self::PageDown => { + let (_, _, _, height) = viewport.rect(); + (height as i16, 0) + } + Self::PageUp => { + let (_, _, _, height) = viewport.rect(); + (-(height as i16), 0) + } + Self::HalfPageDown => { + let (_, _, _, height) = viewport.rect(); + ((height as i16) / 2, 0) + } + Self::HalfPageUp => { + let (_, _, _, height) = viewport.rect(); + (-(height as i16) / 2, 0) + } }; viewport.scroll(rows, cols); } diff --git a/src/widget.rs b/src/widget.rs index a26a219..c71d71a 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -31,13 +31,17 @@ impl Viewport { ((u >> 16) as u16, u as u16) } - pub fn position(&self) -> (u16, u16, u16, u16) { + pub fn rect(&self) -> (u16, u16, u16, u16) { let u = self.0.load(Ordering::Relaxed); let width = (u >> 48) as u16; let height = (u >> 32) as u16; - let row_top = (u >> 16) as u16; - let col_top = u as u16; + let row = (u >> 16) as u16; + let col = u as u16; + (row, col, width, height) + } + pub fn position(&self) -> (u16, u16, u16, u16) { + let (row_top, col_top, width, height) = self.rect(); let row_bottom = row_top.saturating_add(height).saturating_sub(1); let col_bottom = col_top.saturating_add(width).saturating_sub(1);