From 35e5bf47e74b2019db3c10d9408a8fd80fa680fc Mon Sep 17 00:00:00 2001 From: Graham Clark Date: Sat, 9 Jul 2022 12:33:01 -0400 Subject: [PATCH] Tweak the way the terminal widget ends scrolling Thanks to @Peter2121 for this implementation! Prior to this change, if the user hit the terminal hot-key and scrolled back up then back down to the prompt again, a further keypress - q or Q - would still be required to end scroll-mode. This brings the terminal widget more in-line with other terminal implementations in that scroll-mode is now ended automatically if the user scrolls back to the prompt; no extra keypress needed by default. I have added a new option to the terminal Options struct to preserve the legacy behavior, if you need it - KeyPressToEndScrollMode (default false). --- widgets/terminal/terminal.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/widgets/terminal/terminal.go b/widgets/terminal/terminal.go index 3378540..bb970ae 100644 --- a/widgets/terminal/terminal.go +++ b/widgets/terminal/terminal.go @@ -129,14 +129,16 @@ type title struct{} type hotkey struct{} type Options struct { - Command []string - Env []string - HotKey IHotKeyProvider - HotKeyPersistence IHotKeyPersistence // the period of time a hotKey sticks after the first post-hotKey keypress - Scrollback int - Scrollbar bool // disabled regardless of setting if there is no scrollback - HotKeyFns []HotKeyInputFn // allow custom behavior after pressing the hotkey - EnableBracketedPaste bool + Command []string + Env []string + HotKey IHotKeyProvider + HotKeyPersistence IHotKeyPersistence // the period of time a hotKey sticks after the first post-hotKey keypress + Scrollback int + Scrollbar bool // disabled regardless of setting if there is no scrollback + HotKeyFns []HotKeyInputFn // allow custom behavior after pressing the hotkey + EnableBracketedPaste bool + KeyPressToEndScrollMode bool // set to true to enable legacy behavior - when the user has scrolled + // back to the prompt, still require a keypress (q or Q) to end scroll-mode. } // Widget is a widget that hosts a terminal-based application. The user provides the @@ -379,9 +381,18 @@ func (w *Widget) Scroll(dir ScrollDir, page bool, lines int) { } else { lines = w.canvas.ScrollBuffer(dir, false, gwutil.SomeInt(lines)) } - // Scrolling is now true if it (a) was previously, or (b) wasn't, but we - // scrolled more than one line - w.isScrolling = w.isScrolling || lines != 0 + + wasScrolling := w.isScrolling + if lines != 0 { + w.isScrolling = true + } else if !w.params.KeyPressToEndScrollMode && dir == ScrollDown { + // Disable scroll if we are at the bottom and we tried to scroll down + // Thanks @Peter2121 ! + w.isScrolling = false + } + if wasScrolling && !w.isScrolling { + w.ResetScroll() + } } func (w *Widget) ResetScroll() {