2020-07-08 04:13:45 +03:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
2021-05-15 01:09:04 +03:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2021-12-29 00:39:42 +03:00
|
|
|
"path"
|
2020-07-08 04:13:45 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-07 19:56:32 +03:00
|
|
|
"code.rocketnine.space/tslocum/cview"
|
2021-05-15 01:09:04 +03:00
|
|
|
"github.com/atotto/clipboard"
|
2021-02-17 22:17:13 +03:00
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-07-08 04:13:45 +03:00
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tabMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
tabModeDone tabMode = iota
|
|
|
|
tabModeLoading
|
|
|
|
)
|
|
|
|
|
2021-12-27 00:22:29 +03:00
|
|
|
// tabHistoryPageCache is fields from the Page struct, cached here to solve #122
|
|
|
|
// See structs/structs.go for an explanation of the fields.
|
|
|
|
type tabHistoryPageCache struct {
|
|
|
|
row int
|
|
|
|
column int
|
|
|
|
selected string
|
|
|
|
selectedID string
|
|
|
|
mode structs.PageMode
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
type tabHistory struct {
|
2021-12-27 00:22:29 +03:00
|
|
|
urls []string
|
|
|
|
pos int // Position: where in the list of URLs we are
|
|
|
|
pageCache []*tabHistoryPageCache
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// tab hold the information needed for each browser tab.
|
|
|
|
type tab struct {
|
2021-12-29 01:35:08 +03:00
|
|
|
page *structs.Page
|
|
|
|
view *cview.TextView
|
|
|
|
history *tabHistory
|
|
|
|
mode tabMode
|
|
|
|
barLabel string // The bottomBar label for the tab
|
|
|
|
barText string // The bottomBar text for the tab
|
|
|
|
preferURLHandler bool // For #143, use URL handler over proxy
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeNewTab initializes an tab struct with no content.
|
|
|
|
func makeNewTab() *tab {
|
|
|
|
t := tab{
|
2021-02-17 22:17:13 +03:00
|
|
|
page: &structs.Page{Mode: structs.ModeOff},
|
|
|
|
view: cview.NewTextView(),
|
|
|
|
history: &tabHistory{},
|
|
|
|
mode: tabModeDone,
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
2021-02-17 22:17:13 +03:00
|
|
|
t.view.SetDynamicColors(true)
|
|
|
|
t.view.SetRegions(true)
|
|
|
|
t.view.SetScrollable(true)
|
|
|
|
t.view.SetWrap(false)
|
|
|
|
t.view.SetScrollBarVisibility(config.ScrollBar)
|
|
|
|
t.view.SetScrollBarColor(config.GetColor("scrollbar"))
|
|
|
|
t.view.SetChangedFunc(func() {
|
|
|
|
App.Draw()
|
|
|
|
})
|
2020-07-08 04:13:45 +03:00
|
|
|
t.view.SetDoneFunc(func(key tcell.Key) {
|
2021-02-17 22:17:13 +03:00
|
|
|
// Altered from:
|
|
|
|
// https://gitlab.com/tslocum/cview/-/blob/1f765c8695c3f4b35dae57f469d3aee0b1adbde7/demos/textview/main.go
|
2020-07-08 04:13:45 +03:00
|
|
|
// Handles being able to select and "click" links with the enter and tab keys
|
|
|
|
|
|
|
|
tab := curTab // Don't let it change in the middle of the code
|
|
|
|
|
2020-07-11 02:49:17 +03:00
|
|
|
if tabs[tab].mode != tabModeDone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == tcell.KeyEsc {
|
2020-07-08 04:13:45 +03:00
|
|
|
// Stop highlighting
|
|
|
|
bottomBar.SetLabel("")
|
2020-08-26 04:03:21 +03:00
|
|
|
bottomBar.SetText(tabs[tab].page.URL)
|
2020-07-08 04:13:45 +03:00
|
|
|
tabs[tab].clearSelected()
|
|
|
|
tabs[tab].saveBottomBar()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:17:06 +03:00
|
|
|
if len(tabs[tab].page.Links) == 0 {
|
2020-07-08 04:13:45 +03:00
|
|
|
// No links on page
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSelection := tabs[tab].view.GetHighlights()
|
|
|
|
numSelections := len(tabs[tab].page.Links)
|
|
|
|
|
2020-07-19 18:09:33 +03:00
|
|
|
if key == tcell.KeyEnter && len(currentSelection) > 0 {
|
|
|
|
// A link is selected and enter was pressed: "click" it and load the page it's for
|
|
|
|
bottomBar.SetLabel("")
|
|
|
|
linkN, _ := strconv.Atoi(currentSelection[0])
|
|
|
|
tabs[tab].page.Selected = tabs[tab].page.Links[linkN]
|
|
|
|
tabs[tab].page.SelectedID = currentSelection[0]
|
2021-12-29 01:35:08 +03:00
|
|
|
tabs[tab].preferURLHandler = false // Reset in case
|
2021-12-23 01:09:47 +03:00
|
|
|
go followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[linkN])
|
2020-07-19 18:09:33 +03:00
|
|
|
return
|
|
|
|
}
|
2020-08-26 02:17:06 +03:00
|
|
|
if len(currentSelection) == 0 && (key == tcell.KeyEnter || key == tcell.KeyTab) {
|
2020-07-19 18:09:33 +03:00
|
|
|
// They've started link highlighting
|
|
|
|
tabs[tab].page.Mode = structs.ModeLinkSelect
|
|
|
|
|
2021-02-17 22:17:13 +03:00
|
|
|
tabs[tab].view.Highlight("0")
|
2021-06-27 21:21:40 +03:00
|
|
|
tabs[tab].scrollToHighlight()
|
2020-07-19 18:09:33 +03:00
|
|
|
// Display link URL in bottomBar
|
|
|
|
bottomBar.SetLabel("[::b]Link: [::-]")
|
|
|
|
bottomBar.SetText(tabs[tab].page.Links[0])
|
|
|
|
tabs[tab].saveBottomBar()
|
|
|
|
tabs[tab].page.Selected = tabs[tab].page.Links[0]
|
|
|
|
tabs[tab].page.SelectedID = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(currentSelection) > 0 {
|
2020-07-08 04:13:45 +03:00
|
|
|
// There's still a selection, but a different key was pressed, not Enter
|
|
|
|
|
|
|
|
index, _ := strconv.Atoi(currentSelection[0])
|
2020-08-28 19:18:30 +03:00
|
|
|
if key == tcell.KeyTab {
|
2020-07-08 04:13:45 +03:00
|
|
|
index = (index + 1) % numSelections
|
|
|
|
} else if key == tcell.KeyBacktab {
|
|
|
|
index = (index - 1 + numSelections) % numSelections
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2021-02-17 22:17:13 +03:00
|
|
|
tabs[tab].view.Highlight(strconv.Itoa(index))
|
2021-06-27 21:21:40 +03:00
|
|
|
tabs[tab].scrollToHighlight()
|
2020-07-08 04:13:45 +03:00
|
|
|
// Display link URL in bottomBar
|
|
|
|
bottomBar.SetLabel("[::b]Link: [::-]")
|
|
|
|
bottomBar.SetText(tabs[tab].page.Links[index])
|
|
|
|
tabs[tab].saveBottomBar()
|
|
|
|
tabs[tab].page.Selected = tabs[tab].page.Links[index]
|
|
|
|
tabs[tab].page.SelectedID = strconv.Itoa(index)
|
|
|
|
}
|
|
|
|
})
|
2021-02-28 02:17:49 +03:00
|
|
|
t.view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
2021-04-22 04:59:05 +03:00
|
|
|
// Capture scrolling and change the left margin size accordingly, see #197
|
|
|
|
// This was also touched by #222
|
2021-05-15 01:09:04 +03:00
|
|
|
// This also captures any tab-specific events now
|
|
|
|
|
|
|
|
if t.mode != tabModeDone {
|
|
|
|
// Any events that should be caught when the tab is loading is handled in display.go
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-28 02:17:49 +03:00
|
|
|
|
2021-04-22 04:59:05 +03:00
|
|
|
cmd := config.TranslateKeyEvent(event)
|
2021-02-28 02:17:49 +03:00
|
|
|
|
2021-05-15 01:09:04 +03:00
|
|
|
// Cmds that aren't single row/column scrolling
|
2021-05-15 01:18:03 +03:00
|
|
|
//nolint:exhaustive
|
2021-05-15 01:09:04 +03:00
|
|
|
switch cmd {
|
|
|
|
case config.CmdBookmarks:
|
|
|
|
Bookmarks(&t)
|
|
|
|
t.addToHistory("about:bookmarks")
|
|
|
|
return nil
|
|
|
|
case config.CmdAddBookmark:
|
|
|
|
go addBookmark()
|
|
|
|
return nil
|
|
|
|
case config.CmdPgup:
|
|
|
|
t.pageUp()
|
|
|
|
return nil
|
|
|
|
case config.CmdPgdn:
|
|
|
|
t.pageDown()
|
|
|
|
return nil
|
|
|
|
case config.CmdSave:
|
|
|
|
if t.hasContent() {
|
|
|
|
savePath, err := downloadPage(t.page)
|
|
|
|
if err != nil {
|
2021-12-23 01:09:47 +03:00
|
|
|
go Error("Download Error", fmt.Sprintf("Error saving page content: %v", err))
|
2021-05-15 01:09:04 +03:00
|
|
|
} else {
|
2021-12-23 01:09:47 +03:00
|
|
|
go Info(fmt.Sprintf("Page content saved to %s. ", savePath))
|
2021-05-15 01:09:04 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-23 01:09:47 +03:00
|
|
|
go Info("The current page has no content, so it couldn't be downloaded.")
|
2021-05-15 01:09:04 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case config.CmdBack:
|
|
|
|
histBack(&t)
|
|
|
|
return nil
|
|
|
|
case config.CmdForward:
|
|
|
|
histForward(&t)
|
|
|
|
return nil
|
|
|
|
case config.CmdSub:
|
|
|
|
Subscriptions(&t, "about:subscriptions")
|
|
|
|
tabs[curTab].addToHistory("about:subscriptions")
|
|
|
|
return nil
|
|
|
|
case config.CmdCopyPageURL:
|
|
|
|
currentURL := tabs[curTab].page.URL
|
|
|
|
err := clipboard.WriteAll(currentURL)
|
|
|
|
if err != nil {
|
2021-12-23 01:09:47 +03:00
|
|
|
go Error("Copy Error", err.Error())
|
2021-05-15 01:09:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case config.CmdCopyTargetURL:
|
|
|
|
currentURL := t.page.URL
|
2021-06-27 21:21:40 +03:00
|
|
|
selectedURL := t.highlightedURL()
|
2021-05-15 01:09:04 +03:00
|
|
|
if selectedURL == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
u, _ := url.Parse(currentURL)
|
|
|
|
copiedURL, err := u.Parse(selectedURL)
|
|
|
|
if err != nil {
|
|
|
|
err := clipboard.WriteAll(selectedURL)
|
|
|
|
if err != nil {
|
2021-12-23 01:09:47 +03:00
|
|
|
go Error("Copy Error", err.Error())
|
2021-05-15 01:09:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = clipboard.WriteAll(copiedURL.String())
|
|
|
|
if err != nil {
|
2021-12-23 01:09:47 +03:00
|
|
|
go Error("Copy Error", err.Error())
|
2021-05-15 01:09:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
2021-12-29 01:35:08 +03:00
|
|
|
case config.CmdURLHandlerOpen:
|
|
|
|
currentSelection := t.view.GetHighlights()
|
|
|
|
t.preferURLHandler = true
|
|
|
|
// Copied code from when enter key is pressed
|
|
|
|
if len(currentSelection) > 0 {
|
|
|
|
bottomBar.SetLabel("")
|
|
|
|
linkN, _ := strconv.Atoi(currentSelection[0])
|
|
|
|
t.page.Selected = t.page.Links[linkN]
|
|
|
|
t.page.SelectedID = currentSelection[0]
|
|
|
|
go followLink(&t, t.page.URL, t.page.Links[linkN])
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-15 01:09:04 +03:00
|
|
|
}
|
|
|
|
// Number key: 1-9, 0, LINK1-LINK10
|
|
|
|
if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 {
|
|
|
|
if int(cmd) <= len(t.page.Links) {
|
|
|
|
// It's a valid link number
|
2021-12-29 01:35:08 +03:00
|
|
|
t.preferURLHandler = false // Reset in case
|
2021-12-23 01:09:47 +03:00
|
|
|
go followLink(&t, t.page.URL, t.page.Links[cmd-1])
|
2021-05-15 01:09:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scrolling stuff
|
2021-06-27 21:21:40 +03:00
|
|
|
// Copied in scrollTo
|
2021-05-15 01:09:04 +03:00
|
|
|
|
|
|
|
key := event.Key()
|
|
|
|
mod := event.Modifiers()
|
2021-04-02 18:36:33 +03:00
|
|
|
height, width := t.view.GetBufferSize()
|
2021-02-28 05:18:03 +03:00
|
|
|
_, _, 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--
|
|
|
|
}
|
2021-02-28 02:17:49 +03:00
|
|
|
|
2021-04-22 04:59:05 +03:00
|
|
|
if cmd == config.CmdMoveRight || (key == tcell.KeyRight && mod == tcell.ModNone) {
|
2021-02-28 02:17:49 +03:00
|
|
|
// Scrolling to the right
|
2021-02-28 05:18:03 +03:00
|
|
|
|
|
|
|
if t.page.Column >= leftMargin() {
|
|
|
|
// Scrolled right far enought that no left margin is needed
|
|
|
|
if (t.page.Column-leftMargin())+boxW >= width {
|
|
|
|
// And scrolled as far as possible to the right
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Left margin still exists
|
|
|
|
if boxW-(leftMargin()-t.page.Column) >= width {
|
|
|
|
// But still scrolled as far as possible
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 02:17:49 +03:00
|
|
|
t.page.Column++
|
2021-04-22 04:59:05 +03:00
|
|
|
} else if cmd == config.CmdMoveLeft || (key == tcell.KeyLeft && mod == tcell.ModNone) {
|
2021-02-28 02:17:49 +03:00
|
|
|
// Scrolling to the left
|
|
|
|
if t.page.Column == 0 {
|
|
|
|
// Can't scroll to the left anymore
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
t.page.Column--
|
2021-04-22 04:59:05 +03:00
|
|
|
} else if cmd == config.CmdMoveUp || (key == tcell.KeyUp && mod == tcell.ModNone) {
|
2021-02-28 02:17:49 +03:00
|
|
|
// Scrolling up
|
|
|
|
if t.page.Row > 0 {
|
|
|
|
t.page.Row--
|
|
|
|
}
|
|
|
|
return event
|
2021-04-22 04:59:05 +03:00
|
|
|
} else if cmd == config.CmdMoveDown || (key == tcell.KeyDown && mod == tcell.ModNone) {
|
2021-02-28 02:17:49 +03:00
|
|
|
// Scrolling down
|
2021-02-28 05:18:03 +03:00
|
|
|
if t.page.Row < height {
|
|
|
|
t.page.Row++
|
|
|
|
}
|
2021-02-28 02:17:49 +03:00
|
|
|
return event
|
2021-05-15 01:09:04 +03:00
|
|
|
} else if cmd == config.CmdBeginning {
|
|
|
|
t.page.Row = 0
|
|
|
|
// This is required because cview will also set the column (incorrectly)
|
|
|
|
// if it handles this event itself
|
|
|
|
t.applyScroll()
|
|
|
|
App.Draw()
|
|
|
|
return nil
|
|
|
|
} else if cmd == config.CmdEnd {
|
|
|
|
t.page.Row = height
|
|
|
|
t.applyScroll()
|
|
|
|
App.Draw()
|
|
|
|
return nil
|
2021-02-28 02:17:49 +03:00
|
|
|
} else {
|
|
|
|
// Some other key, stop processing it
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
t.applyHorizontalScroll()
|
|
|
|
App.Draw()
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-08 04:13:45 +03:00
|
|
|
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
2021-12-27 00:22:29 +03:00
|
|
|
// historyCachePage caches certain info about the current page in the tab's history,
|
|
|
|
// see #122 for details.
|
|
|
|
func (t *tab) historyCachePage() {
|
|
|
|
if t.page == nil || t.page.URL == "" || t.history.pageCache == nil || len(t.history.pageCache) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.history.pageCache[t.history.pos] = &tabHistoryPageCache{
|
|
|
|
row: t.page.Row,
|
|
|
|
column: t.page.Column,
|
|
|
|
selected: t.page.Selected,
|
|
|
|
selectedID: t.page.SelectedID,
|
|
|
|
mode: t.page.Mode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
// addToHistory adds the given URL to history.
|
|
|
|
// It assumes the URL is currently being loaded and displayed on the page.
|
|
|
|
func (t *tab) addToHistory(u string) {
|
|
|
|
if t.history.pos < len(t.history.urls)-1 {
|
|
|
|
// We're somewhere in the middle of the history instead, with URLs ahead and behind.
|
|
|
|
// The URLs ahead need to be removed so this new URL is the most recent item in the history
|
|
|
|
t.history.urls = t.history.urls[:t.history.pos+1]
|
2021-12-27 00:22:29 +03:00
|
|
|
// Same for page cache
|
|
|
|
t.history.pageCache = t.history.pageCache[:t.history.pos+1]
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
t.history.urls = append(t.history.urls, u)
|
|
|
|
t.history.pos++
|
2021-12-27 00:22:29 +03:00
|
|
|
|
|
|
|
// Cache page info for #122
|
|
|
|
t.history.pageCache = append(t.history.pageCache, &tabHistoryPageCache{}) // Add new spot
|
|
|
|
t.historyCachePage() // Fill it with data
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pageUp scrolls up 75% of the height of the terminal, like Bombadillo.
|
|
|
|
func (t *tab) pageUp() {
|
2021-05-15 01:09:04 +03:00
|
|
|
t.page.Row -= (termH / 4) * 3
|
|
|
|
if t.page.Row < 0 {
|
|
|
|
t.page.Row = 0
|
|
|
|
}
|
|
|
|
t.applyScroll()
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pageDown scrolls down 75% of the height of the terminal, like Bombadillo.
|
|
|
|
func (t *tab) pageDown() {
|
2021-05-15 01:09:04 +03:00
|
|
|
height, _ := t.view.GetBufferSize()
|
|
|
|
|
|
|
|
t.page.Row += (termH / 4) * 3
|
|
|
|
if t.page.Row > height {
|
|
|
|
t.page.Row = height
|
|
|
|
}
|
|
|
|
|
|
|
|
t.applyScroll()
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
2020-12-06 04:35:15 +03:00
|
|
|
// hasContent returns false when the tab's page is malformed,
|
2021-05-13 23:38:53 +03:00
|
|
|
// has no content or URL.
|
2020-07-08 04:13:45 +03:00
|
|
|
func (t *tab) hasContent() bool {
|
|
|
|
if t.page == nil || t.view == nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-26 04:03:21 +03:00
|
|
|
if t.page.URL == "" {
|
2020-07-08 04:13:45 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.page.Content == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-13 23:38:53 +03:00
|
|
|
// isAnAboutPage returns true when the tab's page is an about page
|
|
|
|
func (t *tab) isAnAboutPage() bool {
|
|
|
|
return strings.HasPrefix(t.page.URL, "about:")
|
|
|
|
}
|
|
|
|
|
2021-02-28 02:17:49 +03:00
|
|
|
// applyHorizontalScroll handles horizontal scroll logic including left margin resizing,
|
|
|
|
// see #197 for details. Use applyScroll instead.
|
|
|
|
//
|
|
|
|
// In certain cases it will still use and apply the saved Row.
|
|
|
|
func (t *tab) applyHorizontalScroll() {
|
|
|
|
i := tabNumber(t)
|
|
|
|
if i == -1 {
|
|
|
|
// Tab is not actually being used and should not be (re)added to the browser
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.page.Column >= leftMargin() {
|
|
|
|
// Scrolled to the right far enough that no left margin is needed
|
|
|
|
browser.AddTab(
|
|
|
|
strconv.Itoa(i),
|
2021-12-29 00:39:42 +03:00
|
|
|
t.label(),
|
2021-02-28 02:17:49 +03:00
|
|
|
makeContentLayout(t.view, 0),
|
|
|
|
)
|
|
|
|
t.view.ScrollTo(t.page.Row, t.page.Column-leftMargin())
|
|
|
|
} else {
|
|
|
|
// Left margin is still needed, but is not necessarily at the right size by default
|
|
|
|
browser.AddTab(
|
|
|
|
strconv.Itoa(i),
|
2021-12-29 00:39:42 +03:00
|
|
|
t.label(),
|
2021-02-28 02:17:49 +03:00
|
|
|
makeContentLayout(t.view, leftMargin()-t.page.Column),
|
|
|
|
)
|
|
|
|
}
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// applyScroll applies the saved scroll values to the page and tab.
|
|
|
|
// It should only be used when going backward and forward.
|
|
|
|
func (t *tab) applyScroll() {
|
2021-02-28 02:17:49 +03:00
|
|
|
t.view.ScrollTo(t.page.Row, 0)
|
|
|
|
t.applyHorizontalScroll()
|
2020-07-08 04:13:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:21:40 +03:00
|
|
|
// 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) {
|
|
|
|
height, width := t.view.GetBufferSize()
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
// saveBottomBar saves the current bottomBar values in the tab.
|
|
|
|
func (t *tab) saveBottomBar() {
|
|
|
|
t.barLabel = bottomBar.GetLabel()
|
|
|
|
t.barText = bottomBar.GetText()
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyBottomBar sets the bottomBar using the stored tab values
|
|
|
|
func (t *tab) applyBottomBar() {
|
|
|
|
bottomBar.SetLabel(t.barLabel)
|
|
|
|
bottomBar.SetText(t.barText)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearSelected turns off any selection that was going on.
|
|
|
|
// It does not affect the bottomBar.
|
|
|
|
func (t *tab) clearSelected() {
|
|
|
|
t.page.Mode = structs.ModeOff
|
|
|
|
t.page.Selected = ""
|
|
|
|
t.page.SelectedID = ""
|
|
|
|
t.view.Highlight("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// applySelected selects whatever is stored as the selected element in the struct,
|
|
|
|
// and sets the mode accordingly.
|
|
|
|
// It is safe to call if nothing was selected previously.
|
|
|
|
//
|
|
|
|
// applyBottomBar should be called after, as this func might set some bottomBar values.
|
|
|
|
func (t *tab) applySelected() {
|
|
|
|
if t.page.Mode == structs.ModeOff {
|
|
|
|
// Just in case
|
|
|
|
t.page.Selected = ""
|
|
|
|
t.page.SelectedID = ""
|
|
|
|
t.view.Highlight("")
|
|
|
|
return
|
|
|
|
} else if t.page.Mode == structs.ModeLinkSelect {
|
|
|
|
t.view.Highlight(t.page.SelectedID)
|
|
|
|
|
|
|
|
if t.mode == tabModeDone {
|
|
|
|
// Page is not loading so bottomBar can change
|
|
|
|
t.barLabel = "[::b]Link: [::-]"
|
|
|
|
t.barText = t.page.Selected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyAll uses applyScroll and applySelected to put a tab's TextView back the way it was.
|
|
|
|
// It also uses applyBottomBar if this is the current tab.
|
|
|
|
func (t *tab) applyAll() {
|
|
|
|
t.applySelected()
|
|
|
|
t.applyScroll()
|
|
|
|
if t == tabs[curTab] {
|
|
|
|
t.applyBottomBar()
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 00:00:34 +03:00
|
|
|
|
2021-06-27 21:21:40 +03:00
|
|
|
// highlightedURL returns the currently selected URL
|
|
|
|
func (t *tab) highlightedURL() string {
|
2021-04-21 00:00:34 +03:00
|
|
|
currentSelection := tabs[curTab].view.GetHighlights()
|
|
|
|
|
|
|
|
if len(currentSelection) > 0 {
|
|
|
|
linkN, _ := strconv.Atoi(currentSelection[0])
|
|
|
|
selectedURL := tabs[curTab].page.Links[linkN]
|
|
|
|
return selectedURL
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-12-29 00:39:42 +03:00
|
|
|
|
|
|
|
// label returns the label to use for the tab name
|
|
|
|
func (t *tab) label() string {
|
|
|
|
tn := tabNumber(t)
|
|
|
|
if tn < 0 {
|
|
|
|
// Invalid tab, shouldn't happen
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment so there's no tab 0 in the label
|
|
|
|
tn++
|
|
|
|
|
|
|
|
if t.page.URL == "" || t.page.URL == "about:newtab" {
|
|
|
|
// Just use tab number
|
|
|
|
// Spaces around to keep original Amfora look
|
|
|
|
return fmt.Sprintf(" %d ", tn)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(t.page.URL, "about:") {
|
|
|
|
// Don't look for domain, put the whole URL except query strings
|
|
|
|
return strings.SplitN(t.page.URL, "?", 2)[0]
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(t.page.URL, "file://") {
|
|
|
|
// File URL, use file or folder as tab name
|
|
|
|
return path.Base(t.page.URL[7:])
|
|
|
|
}
|
|
|
|
// Otherwise, it's a Gemini URL
|
|
|
|
pu, err := url.Parse(t.page.URL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Sprintf(" %d ", tn)
|
|
|
|
}
|
|
|
|
return pu.Host
|
|
|
|
}
|