From f6216bb6213a15cdcdf50089f3c4a8e9a30d9337 Mon Sep 17 00:00:00 2001 From: Jeffas Date: Thu, 5 Sep 2019 23:32:36 +0100 Subject: 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. --- widgets/msgviewer.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'widgets/msgviewer.go') diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go index e210616..c179070 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) } -- cgit v1.2.3