summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Gläßer <tobimensch@users.noreply.github.com>2018-10-29 09:20:52 -0400
committerThomas Buckley-Houston <tom@tombh.co.uk>2019-06-24 09:09:58 +0300
commit7b7e6bc308321220d52e5c7d39890321755bade8 (patch)
tree7431d0cac0cfe5a9a80341bebfefa74aeb80a9a2
parent86acac617be451fa693f0cdfe97dbd9d015e67f2 (diff)
Refactored code using switchToTab and added new features.
Added moveTabLeft and moveTabRight functions, which take a tab ID and try to move the tab as far right or left in the tabOrder as possible. Added previouslyVisitedTab function that switches to the previously selected tab.
-rw-r--r--interfacer/src/browsh/tab.go75
1 files changed, 71 insertions, 4 deletions
diff --git a/interfacer/src/browsh/tab.go b/interfacer/src/browsh/tab.go
index 19461a5..900b111 100644
--- a/interfacer/src/browsh/tab.go
+++ b/interfacer/src/browsh/tab.go
@@ -18,6 +18,9 @@ var tabsOrder []int
// the tab being deleted, so we need to keep track of all deleted IDs
var tabsDeleted []int
+// ID of the tab that was active before the current one
+var previouslyVisitedTabID int
+
// A single tab synced from the browser
type tab struct {
ID int `json:"id"`
@@ -61,6 +64,10 @@ func newTab(id int) {
}
}
+func restoreTab() {
+ sendMessageToWebExtension("/restore_tab")
+}
+
func removeTab(id int) {
if len(Tabs) == 1 {
quitBrowsh()
@@ -84,6 +91,40 @@ func removeTabIDfromTabsOrder(id int) {
}
}
+func moveTabLeft(id int) {
+ // If the tab ID is already completely to the left in the tab order
+ // there's nothing to do
+ if tabsOrder[0] == id {
+ return
+ }
+
+ for i, tabId := range tabsOrder {
+ if tabId == id {
+ tabsOrder[i-1], tabsOrder[i] = tabsOrder[i], tabsOrder[i-1]
+ break
+ }
+ }
+}
+
+func moveTabRight(id int) {
+ // If the tab ID is already completely to the right in the tab order
+ // there's nothing to do
+ if tabsOrder[len(tabsOrder)-1] == id {
+ return
+ }
+
+ for i, tabId := range tabsOrder {
+ if tabId == id {
+ tabsOrder[i+1], tabsOrder[i] = tabsOrder[i], tabsOrder[i+1]
+ break
+ }
+ }
+}
+
+func duplicateTab(id int) {
+ sendMessageToWebExtension(fmt.Sprintf("/duplicate_tab,%d", id))
+}
+
// Creating a new tab in the browser without a URI means it won't register with the
// web extension, which means that, come the moment when we actually have a URI for the new
// tab then we can't talk to it to tell it navigate. So we need to only create a real new
@@ -116,15 +157,41 @@ func nextTab() {
} else {
i++
}
- sendMessageToWebExtension(fmt.Sprintf("/switch_to_tab,%d", tabsOrder[i]))
- CurrentTab = Tabs[tabsOrder[i]]
- renderUI()
- renderCurrentTabWindow()
+ switchToTab(tabsOrder[i])
break
}
}
}
+func prevTab() {
+ for i := 0; i < len(tabsOrder); i++ {
+ if tabsOrder[i] == CurrentTab.ID {
+ if i-1 < 0 {
+ i = len(tabsOrder) - 1
+ } else {
+ i--
+ }
+ switchToTab(tabsOrder[i])
+ break
+ }
+ }
+}
+
+func previouslyVisitedTab() {
+ if previouslyVisitedTabID == 0 {
+ return
+ }
+ switchToTab(previouslyVisitedTabID)
+}
+
+func switchToTab(num int) {
+ sendMessageToWebExtension(fmt.Sprintf("/switch_to_tab,%d", num))
+ previouslyVisitedTabID = CurrentTab.ID
+ CurrentTab = Tabs[num]
+ renderUI()
+ renderCurrentTabWindow()
+}
+
func isTabPreviouslyDeleted(id int) bool {
for i := 0; i < len(tabsDeleted); i++ {
if tabsDeleted[i] == id {