aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/msgviewer.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 /widgets/msgviewer.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 'widgets/msgviewer.go')
-rw-r--r--widgets/msgviewer.go72
1 files changed, 71 insertions, 1 deletions
diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go
index e210616a..c1790707 100644
--- a/widgets/msgviewer.go
+++ b/widgets/msgviewer.go
@@ -42,6 +42,9 @@ type PartSwitcher struct {
selected int
showHeaders bool
alwaysShowMime bool
+
+ height int
+ mv *MessageViewer
}
func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
@@ -77,7 +80,7 @@ func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
grid.AddChild(header).At(0, 0)
grid.AddChild(switcher).At(1, 0)
- return &MessageViewer{
+ mv := &MessageViewer{
acct: acct,
conf: conf,
grid: grid,
@@ -85,6 +88,9 @@ func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
store: store,
switcher: switcher,
}
+ switcher.mv = mv
+
+ return mv
}
func fmtHeader(msg *models.MessageInfo, header string) string {
@@ -194,6 +200,13 @@ func (mv *MessageViewer) Draw(ctx *ui.Context) {
mv.grid.Draw(ctx)
}
+func (mv *MessageViewer) MouseEvent(localX int, localY int, event tcell.Event) {
+ if mv.err != nil {
+ return
+ }
+ mv.grid.MouseEvent(localX, localY, event)
+}
+
func (mv *MessageViewer) Invalidate() {
mv.grid.Invalidate()
}
@@ -295,6 +308,7 @@ func (ps *PartSwitcher) Draw(ctx *ui.Context) {
return
}
// TODO: cap height and add scrolling for messages with many parts
+ ps.height = ctx.Height()
y := ctx.Height() - height
for i, part := range ps.parts {
style := tcell.StyleDefault.Reverse(ps.selected == i)
@@ -311,6 +325,62 @@ func (ps *PartSwitcher) Draw(ctx *ui.Context) {
0, 0, ctx.Width(), ctx.Height()-height))
}
+func (ps *PartSwitcher) MouseEvent(localX int, localY int, event tcell.Event) {
+ switch event := event.(type) {
+ case *tcell.EventMouse:
+ switch event.Buttons() {
+ case tcell.Button1:
+ height := len(ps.parts)
+ y := ps.height - height
+ if localY < y {
+ ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
+ }
+ for i, _ := range ps.parts {
+ if localY != y+i {
+ continue
+ }
+ if ps.parts[i].part.MIMEType == "multipart" {
+ continue
+ }
+ if ps.parts[ps.selected].term != nil {
+ ps.parts[ps.selected].term.Focus(false)
+ }
+ ps.selected = i
+ ps.Invalidate()
+ if ps.parts[ps.selected].term != nil {
+ ps.parts[ps.selected].term.Focus(true)
+ }
+ }
+ case tcell.WheelDown:
+ height := len(ps.parts)
+ y := ps.height - height
+ if localY < y {
+ ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
+ }
+ if ps.parts[ps.selected].term != nil {
+ ps.parts[ps.selected].term.Focus(false)
+ }
+ ps.mv.NextPart()
+ if ps.parts[ps.selected].term != nil {
+ ps.parts[ps.selected].term.Focus(true)
+ }
+ case tcell.WheelUp:
+ height := len(ps.parts)
+ y := ps.height - height
+ if localY < y {
+ ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
+ }
+ if ps.parts[ps.selected].term != nil {
+ ps.parts[ps.selected].term.Focus(false)
+ }
+ ps.mv.PreviousPart()
+ if ps.parts[ps.selected].term != nil {
+ ps.parts[ps.selected].term.Focus(true)
+ }
+ }
+ }
+}
+
func (mv *MessageViewer) Event(event tcell.Event) bool {
return mv.switcher.Event(event)
}