From 6ec7ed16686ab188a8831bbba1c118adff4489ea Mon Sep 17 00:00:00 2001 From: makeworld Date: Sun, 27 Jun 2021 14:21:40 -0400 Subject: [PATCH] Custom impl. of scrollTo & scrollToHighlight for #196 https://github.com/makeworld-the-better-one/amfora/issues/197#issuecomment-869200677 --- display/tab.go | 54 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/display/tab.go b/display/tab.go index 13bc11d..bdc4334 100644 --- a/display/tab.go +++ b/display/tab.go @@ -94,7 +94,7 @@ func makeNewTab() *tab { tabs[tab].page.Mode = structs.ModeLinkSelect tabs[tab].view.Highlight("0") - tabs[tab].view.ScrollToHighlight() + tabs[tab].scrollToHighlight() // Display link URL in bottomBar bottomBar.SetLabel("[::b]Link: [::-]") bottomBar.SetText(tabs[tab].page.Links[0]) @@ -115,7 +115,7 @@ func makeNewTab() *tab { return } tabs[tab].view.Highlight(strconv.Itoa(index)) - tabs[tab].view.ScrollToHighlight() + tabs[tab].scrollToHighlight() // Display link URL in bottomBar bottomBar.SetLabel("[::b]Link: [::-]") bottomBar.SetText(tabs[tab].page.Links[index]) @@ -184,7 +184,7 @@ func makeNewTab() *tab { return nil case config.CmdCopyTargetURL: currentURL := t.page.URL - selectedURL := t.HighlightedURL() + selectedURL := t.highlightedURL() if selectedURL == "" { return nil } @@ -215,6 +215,7 @@ func makeNewTab() *tab { } // Scrolling stuff + // Copied in scrollTo key := event.Key() mod := event.Modifiers() @@ -377,6 +378,49 @@ func (t *tab) applyScroll() { t.applyHorizontalScroll() } +// scrollTo scrolls the current tab to specified position. Like +// cview.TextView.ScrollTo but using the custom scrolling logic required by #196. +func (t *tab) scrollTo(row, col int) { + // Logic copied from scrolling stuff around line 217. + + height, width := t.view.GetBufferSize() + _, _, boxW, boxH := t.view.GetInnerRect() + + // Make boxW accurate by subtracting one if a scrollbar is covering the last + // column of text + if config.ScrollBar == cview.ScrollBarAlways || + (config.ScrollBar == cview.ScrollBarAuto && height > boxH) { + boxW-- + } + + // Keep row and col within limits + + if row < 0 { + row = 0 + } else if row > height { + row = height + } + if col < 0 { + col = 0 + } else if col > width { + col = width + } + + t.page.Row = row + t.page.Column = col + t.applyScroll() + App.Draw() +} + +// scrollToHighlight scrolls the current tab to specified position. Like +// cview.TextView.ScrollToHighlight but using the custom scrolling logic +// required by #196. +func (t *tab) scrollToHighlight() { + t.view.ScrollToHighlight() + App.Draw() + t.scrollTo(t.view.GetScrollOffset()) +} + // saveBottomBar saves the current bottomBar values in the tab. func (t *tab) saveBottomBar() { t.barLabel = bottomBar.GetLabel() @@ -431,8 +475,8 @@ func (t *tab) applyAll() { } } -// HighlightedURL returns the currently selected URL -func (t *tab) HighlightedURL() string { +// highlightedURL returns the currently selected URL +func (t *tab) highlightedURL() string { currentSelection := tabs[curTab].view.GetHighlights() if len(currentSelection) > 0 {