diff options
author | Jeffas <dev@jeffas.io> | 2019-09-05 23:32:36 +0100 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-09-11 11:41:34 -0400 |
commit | f6216bb6213a15cdcdf50089f3c4a8e9a30d9337 (patch) | |
tree | 3075b68379ae7c13cb9eedc53b7cf033672478a9 /lib/ui/tab.go | |
parent | a441e3b3a55d11e7cc9444298051ff339d1b7519 (diff) | |
download | aerc-f6216bb6213a15cdcdf50089f3c4a8e9a30d9337.tar.gz |
Add Mouseable
This adds the Mouseable interface. When this is implemented for a component that item can accept and process mouseevents. At the top level when a mouse event is received it is passed to the grid's handler and then it trickles down until it reaches a component that can actually handle it, such as the tablist, dirlist or msglist. A mouse event is passed so that components can handle other things such as scrolling with the mousewheel. The components themselves then perform the necessary actions. Clicking emails in the messagelist opens them in a new tab. Textinputs can be clicked to position the cursor inside them. Mouseevents are not forwarded to the terminal at the moment. Elements which do not handle mouse events are not required to implement the Mouseable interface.
Diffstat (limited to 'lib/ui/tab.go')
-rw-r--r-- | lib/ui/tab.go | 102 |
1 files changed, 81 insertions, 21 deletions
diff --git a/lib/ui/tab.go b/lib/ui/tab.go index 90c7ce9..1fd2b80 100644 --- a/lib/ui/tab.go +++ b/lib/ui/tab.go @@ -14,6 +14,9 @@ type Tabs struct { onInvalidateStrip func(d Drawable) onInvalidateContent func(d Drawable) + + parent *Tabs + CloseTab func(index int) } type Tab struct { @@ -28,7 +31,9 @@ type TabContent Tabs func NewTabs() *Tabs { tabs := &Tabs{} tabs.TabStrip = (*TabStrip)(tabs) + tabs.TabStrip.parent = tabs tabs.TabContent = (*TabContent)(tabs) + tabs.TabContent.parent = tabs tabs.history = []int{} return tabs } @@ -114,6 +119,22 @@ func (tabs *Tabs) SelectPrevious() bool { return true } +func (tabs *Tabs) NextTab() { + next := tabs.Selected + 1 + if next >= len(tabs.Tabs) { + next = 0 + } + tabs.Select(next) +} + +func (tabs *Tabs) PrevTab() { + next := tabs.Selected - 1 + if next < 0 { + next = len(tabs.Tabs) - 1 + } + tabs.Select(next) +} + func (tabs *Tabs) pushHistory(index int) { tabs.history = append(tabs.history, index) } @@ -146,19 +167,6 @@ 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 @@ -187,21 +195,65 @@ func (strip *TabStrip) Invalidate() { } } +func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) { + changeFocus := func(focus bool) { + interactive, ok := strip.parent.Tabs[strip.parent.Selected].Content.(Interactive) + if ok { + interactive.Focus(focus) + } + } + unfocus := func() { changeFocus(false) } + refocus := func() { changeFocus(true) } + switch event := event.(type) { + case *tcell.EventMouse: + switch event.Buttons() { + case tcell.Button1: + selectedTab, ok := strip.Clicked(localX, localY) + if !ok || selectedTab == strip.parent.Selected { + return + } + unfocus() + strip.parent.Select(selectedTab) + refocus() + case tcell.WheelDown: + unfocus() + strip.parent.NextTab() + refocus() + case tcell.WheelUp: + unfocus() + strip.parent.PrevTab() + refocus() + case tcell.Button3: + selectedTab, ok := strip.Clicked(localX, localY) + if !ok { + return + } + unfocus() + if selectedTab == strip.parent.Selected { + strip.parent.CloseTab(selectedTab) + } else { + current := strip.parent.Selected + strip.parent.CloseTab(selectedTab) + strip.parent.Select(current) + } + refocus() + } + } +} + 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 + 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 } @@ -225,6 +277,14 @@ func (content *TabContent) Draw(ctx *Context) { tab.Content.Draw(ctx) } +func (content *TabContent) MouseEvent(localX int, localY int, event tcell.Event) { + tab := content.Tabs[content.Selected] + switch tabContent := tab.Content.(type) { + case Mouseable: + tabContent.MouseEvent(localX, localY, event) + } +} + func (content *TabContent) Invalidate() { if content.onInvalidateContent != nil { content.onInvalidateContent(content) |