ditty/gui_mouse.go

114 lines
2.6 KiB
Go
Raw Normal View History

2020-01-08 23:38:41 +00:00
package main
import (
"path"
"strings"
"time"
"github.com/faiface/beep/speaker"
"github.com/gdamore/tcell"
2020-01-22 23:37:05 +00:00
"gitlab.com/tslocum/cview"
2020-01-08 23:38:41 +00:00
)
func handleMouse(event *cview.EventMouse) *cview.EventMouse {
if event.Action()&cview.MouseDown != 0 && event.Buttons()&tcell.Button1 != 0 {
mouseX, mouseY := event.Position()
2020-01-28 22:53:38 +00:00
if mouseY >= 0 && mouseY < mainBufHeight {
if queueFocused {
queueFocused = false
go app.QueueUpdateDraw(updateLists)
}
2020-01-08 23:38:41 +00:00
// TODO Delay playing while cursor is moved
2020-01-28 22:53:38 +00:00
if mouseY > 0 && mouseY < mainBufHeight-1 && mouseY-1 < len(mainBufferFiles)+1 {
2020-01-09 01:50:52 +00:00
mainBufferCursor = mainBufferOrigin + (mouseY - 1)
2020-01-28 22:53:38 +00:00
go app.QueueUpdateDraw(updateLists)
2020-01-09 23:51:37 +00:00
go listSelect()
2020-01-08 23:38:41 +00:00
}
return nil
2020-01-28 22:53:38 +00:00
} else if mouseY >= mainBufHeight && mouseY < screenHeight-2 {
if !queueFocused {
queueFocused = true
go app.QueueUpdateDraw(updateLists)
}
if mouseY > mainBufHeight && mouseY < screenHeight-3 {
mouseHit := (mouseY - mainBufHeight) - 1
if mouseHit < len(queueFiles) {
queueCursor = queueOrigin + mouseHit
go app.QueueUpdateDraw(updateLists)
go queueSelect()
}
}
return nil
2020-01-08 23:38:41 +00:00
} else if mouseY == screenHeight-1 {
if mouseX >= seekStart && mouseX <= seekEnd {
if strings.ToLower(path.Ext(playingFileName)) == ".flac" {
statusText = "Seeking FLAC files is unsupported"
go func() {
time.Sleep(5 * time.Second)
statusText = ""
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateMain)
2020-01-08 23:38:41 +00:00
}()
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateMain)
2020-01-08 23:38:41 +00:00
return nil
}
2020-01-09 23:51:37 +00:00
audioLock.Lock()
2020-01-08 23:38:41 +00:00
speaker.Lock()
2020-01-22 22:54:55 +00:00
if playingStreamer == nil {
speaker.Unlock()
audioLock.Unlock()
return nil
}
pos := float64(mouseX-seekStart) / float64(seekEnd-seekStart)
if pos > 1 {
pos = 1
}
seekTo := int(float64(playingStreamer.Len()-1) * pos)
2020-01-08 23:38:41 +00:00
_ = playingStreamer.Seek(seekTo) // Ignore seek errors
speaker.Unlock()
2020-01-09 23:51:37 +00:00
audioLock.Unlock()
2020-01-09 17:48:00 +00:00
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateStatus)
2020-01-08 23:38:41 +00:00
return nil
} else if mouseX >= volumeStart && mouseX <= volumeEnd+1 {
if mouseX > volumeEnd {
mouseX = volumeEnd
}
if mouseX-volumeStart <= 3 {
2020-01-09 23:51:37 +00:00
audioLock.Lock()
2020-01-08 23:38:41 +00:00
speaker.Lock()
2020-01-22 22:54:55 +00:00
if volume == nil {
speaker.Unlock()
audioLock.Unlock()
return nil
}
2020-01-08 23:38:41 +00:00
volume.Silent = !volume.Silent
speaker.Unlock()
2020-01-09 23:51:37 +00:00
audioLock.Unlock()
2020-01-09 17:48:00 +00:00
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateStatus)
2020-01-08 23:38:41 +00:00
} else {
2020-01-22 01:10:20 +00:00
vol := -7.5 + float64(7.5)*(float64(mouseX-volumeStart-3)/float64(volumeEnd-volumeStart-3))
if vol < -7.0 {
vol = -7.0
2020-01-08 23:38:41 +00:00
}
2020-01-22 01:10:20 +00:00
setVolume(roundUnit(vol, 0.5))
2020-01-09 17:48:00 +00:00
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateStatus)
2020-01-08 23:38:41 +00:00
}
return nil
}
}
}
return event
}