diff options
author | Jeffas <andrewjeffery97@gmail.com> | 2019-07-11 23:15:15 +0100 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-07-11 19:45:53 -0400 |
commit | 3b09c07e7a75feed8a9086b0a9003c2cf3ffea59 (patch) | |
tree | 9942f0f3f4529c4072946a39b75869021e9e7517 /lib/ui/tab.go | |
parent | 4c7f81d20d1a437b59046846543ccf1a47d3c6a0 (diff) | |
download | aerc-3b09c07e7a75feed8a9086b0a9003c2cf3ffea59.tar.gz |
Add clickable tabs
This introduces a new interface `Clickable`. I'd imagine this would be implemented for most widgets eventually and would allow for programs run in the terminal to also have their mouse events forwarded to them. For the tabs it was relatively simple to check that the position of the click is within the boxes for the tabs. For other components I'd imagine that some state representing their currently drawn bounding box would be useful.
Diffstat (limited to 'lib/ui/tab.go')
-rw-r--r-- | lib/ui/tab.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/ui/tab.go b/lib/ui/tab.go index 61d544a..0061472 100644 --- a/lib/ui/tab.go +++ b/lib/ui/tab.go @@ -126,6 +126,19 @@ func (tabs *Tabs) removeHistory(index int) { tabs.history = newHist } +func (tabs *Tabs) MouseEvent(event tcell.Event) { + switch event := event.(type) { + case *tcell.EventMouse: + if event.Buttons()&tcell.Button1 != 0 { + x, y := event.Position() + selectedTab, ok := tabs.TabStrip.Clicked(x, y) + if ok { + tabs.Select(selectedTab) + } + } + } +} + // TODO: Color repository func (strip *TabStrip) Draw(ctx *Context) { x := 0 @@ -151,6 +164,21 @@ func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) { strip.onInvalidateStrip = onInvalidate } +func (strip *TabStrip) Clicked(mouseX int, mouseY int) (int, bool) { + x := 0 + if mouseY == 0 { + for i, tab := range strip.Tabs { + trunc := runewidth.Truncate(tab.Name, 32, "…") + length := len(trunc) + 2 + if x <= mouseX && mouseX < x+length { + return i, true + } + x += length + } + } + return 0, false +} + func (content *TabContent) Children() []Drawable { children := make([]Drawable, len(content.Tabs)) for i, tab := range content.Tabs { |