about summary refs log tree commit diff stats
path: root/lib/ui/grid.go
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-09-05 23:32:36 +0100
committerDrew DeVault <sir@cmpwn.com>2019-09-11 11:41:34 -0400
commitf6216bb6213a15cdcdf50089f3c4a8e9a30d9337 (patch)
tree3075b68379ae7c13cb9eedc53b7cf033672478a9 /lib/ui/grid.go
parenta441e3b3a55d11e7cc9444298051ff339d1b7519 (diff)
downloadaerc-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/grid.go')
-rw-r--r--lib/ui/grid.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/ui/grid.go b/lib/ui/grid.go
index 7f131bd..b47c6bd 100644
--- a/lib/ui/grid.go
+++ b/lib/ui/grid.go
@@ -5,6 +5,8 @@ import (
 	"math"
 	"sync"
 	"sync/atomic"
+
+	"github.com/gdamore/tcell"
 )
 
 type Grid struct {
@@ -141,6 +143,45 @@ func (grid *Grid) Draw(ctx *Context) {
 	}
 }
 
+func (grid *Grid) MouseEvent(localX int, localY int, event tcell.Event) {
+	switch event := event.(type) {
+	case *tcell.EventMouse:
+		invalid := grid.invalid
+
+		grid.mutex.RLock()
+		defer grid.mutex.RUnlock()
+
+		for _, cell := range grid.cells {
+			cellInvalid := cell.invalid.Load().(bool)
+			if !cellInvalid && !invalid {
+				continue
+			}
+			rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan]
+			cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan]
+			x := cols[0].Offset
+			y := rows[0].Offset
+			width := 0
+			height := 0
+			for _, col := range cols {
+				width += col.Size
+			}
+			for _, row := range rows {
+				height += row.Size
+			}
+			if x <= localX && localX < x+width && y <= localY && localY < y+height {
+				switch content := cell.Content.(type) {
+				case MouseableDrawableInteractive:
+					content.MouseEvent(localX-x, localY-y, event)
+				case Mouseable:
+					content.MouseEvent(localX-x, localY-y, event)
+				case MouseHandler:
+					content.MouseEvent(localX-x, localY-y, event)
+				}
+			}
+		}
+	}
+}
+
 func (grid *Grid) reflow(ctx *Context) {
 	grid.rowLayout = nil
 	grid.columnLayout = nil