From a79e964cb6fc35b95a41b8069b71a2b7c2424406 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 8 Jun 2016 17:47:48 -0400 Subject: [PATCH] Make tabs respond to mouse events --- cmd/micro/bindings.go | 15 ++++++++--- cmd/micro/micro.go | 3 +++ cmd/micro/tab.go | 60 ++++++++++++++++++++++++++++++++++++++++--- runtime/help/help.md | 1 + 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index 14de9ac5..ff888261 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -1097,8 +1097,8 @@ func (v *View) Quit() bool { curTab-- } if curTab == 0 { - tab := tabs[curTab] - tab.views[tab.curView].Resize(screen.Size()) + CurView().Resize(screen.Size()) + CurView().matches = Match(CurView()) } } } else { @@ -1114,6 +1114,13 @@ func (v *View) AddTab() bool { tab.SetNum(len(tabs)) tabs = append(tabs, tab) curTab++ + if len(tabs) == 2 { + for _, t := range tabs { + for _, v := range t.views { + v.Resize(screen.Size()) + } + } + } return true } @@ -1121,14 +1128,14 @@ func (v *View) LastTab() bool { if curTab > 0 { curTab-- } - return true + return false } func (v *View) NextTab() bool { if curTab < len(tabs)-1 { curTab++ } - return true + return false } // None is no action diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 4c9ea68f..3ff79e6d 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -257,6 +257,9 @@ func main() { // Wait for the user's action event := screen.PollEvent() + if TabbarHandleMouseEvent(event) { + continue + } if searching { // Since searching is done in real time, we need to redraw every time diff --git a/cmd/micro/tab.go b/cmd/micro/tab.go index d4d4d498..cada1b72 100644 --- a/cmd/micro/tab.go +++ b/cmd/micro/tab.go @@ -1,5 +1,11 @@ package main +import ( + "sort" + + "github.com/zyedidia/tcell" +) + type Tab struct { // This contains all the views in this tab // There is generally only one view per tab, but you can have @@ -30,11 +36,10 @@ func CurView() *View { return curTab.views[curTab.curView] } -func DisplayTabs() { - if len(tabs) <= 1 { - return - } +func TabbarString() (string, map[int]int) { str := "" + indicies := make(map[int]int) + indicies[0] = 0 for i, t := range tabs { if i == curTab { str += "[" @@ -47,8 +52,55 @@ func DisplayTabs() { } else { str += " " } + indicies[len(str)-1] = i + 1 str += " " } + return str, indicies +} + +func TabbarHandleMouseEvent(event tcell.Event) bool { + if len(tabs) <= 1 { + return false + } + + switch e := event.(type) { + case *tcell.EventMouse: + button := e.Buttons() + if button == tcell.Button1 { + x, y := e.Position() + if y != 0 { + return false + } + str, indicies := TabbarString() + if x >= len(str) { + return false + } + var tabnum int + var keys []int + for k := range indicies { + keys = append(keys, k) + } + sort.Ints(keys) + for _, k := range keys { + if x <= k { + tabnum = indicies[k] - 1 + break + } + } + curTab = tabnum + return true + } + } + + return false +} + +func DisplayTabs() { + if len(tabs) <= 1 { + return + } + + str, _ := TabbarString() tabBarStyle := defStyle.Reverse(true) if style, ok := colorscheme["tabbar"]; ok { diff --git a/runtime/help/help.md b/runtime/help/help.md index 79f017c2..02ecffca 100644 --- a/runtime/help/help.md +++ b/runtime/help/help.md @@ -63,6 +63,7 @@ you can rebind them to your liking. "CtrlD": "DuplicateLine", "CtrlV": "Paste", "CtrlA": "SelectAll", + "CtrlT": "AddTab" "Home": "Start", "End": "End", "PageUp": "CursorPageUp",