From df972794428e7cf0de21cf9aae1ee3b821f8a411 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 8 Jan 2018 19:02:35 +0100 Subject: [PATCH] TextView: add manual scroll methods --- src/views/text_view.rs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/views/text_view.rs b/src/views/text_view.rs index da2cc65..f301272 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -314,9 +314,29 @@ impl TextView { self.with(|s| s.set_scroll_strategy(strategy)) } - // Apply the scrolling strategy to the current scroll position. - // - // Called when computing rows and when applying a new strategy. + /// Scroll up by `n` lines. + pub fn scroll_up(&mut self, n: usize) { + self.scrollbase.scroll_up(n); + } + + /// Scroll down by `n` lines. + pub fn scroll_down(&mut self, n: usize) { + self.scrollbase.scroll_down(n); + } + + /// Scroll to the bottom of the content. + pub fn scroll_bottom(&mut self) { + self.scrollbase.scroll_bottom(); + } + + /// Scroll to the top of the view. + pub fn scroll_top(&mut self) { + self.scrollbase.scroll_top(); + } + + /// Apply the scrolling strategy to the current scroll position. + /// + /// Called when computing rows and when applying a new strategy. fn adjust_scroll(&mut self) { match self.scroll_strategy { ScrollStrategy::StickToTop => self.scrollbase.scroll_top(), @@ -420,7 +440,7 @@ impl View for TextView { } fn on_event(&mut self, event: Event) -> EventResult { - if !self.scrollbase.scrollable() { + if !self.scrollable || !self.scrollbase.scrollable() { return EventResult::Ignored; } @@ -513,8 +533,14 @@ impl View for TextView { // Compute the text rows. self.last_size = size; self.compute_rows(size); - // Adjust scrolling, in case we're sticking to the bottom for instance. - self.scrollbase.set_heights(size.y, self.rows.len()); + // Adjust scrolling, in case we're sticking to the bottom or something + let available_height = if self.scrollable { + size.y + } else { + self.rows.len() + }; + + self.scrollbase.set_heights(available_height, self.rows.len()); self.adjust_scroll(); } }