ditty/gui_mouse.go

62 lines
1.3 KiB
Go
Raw Normal View History

2020-01-08 23:38:41 +00:00
package main
import (
2021-04-09 15:07:07 +00:00
"code.rocketnine.space/tslocum/cview"
2020-01-08 23:38:41 +00:00
"github.com/faiface/beep/speaker"
2020-09-08 22:06:17 +00:00
"github.com/gdamore/tcell/v2"
2020-01-08 23:38:41 +00:00
)
2020-04-24 22:39:02 +00:00
func handleMouse(event *tcell.EventMouse, action cview.MouseAction) (*tcell.EventMouse, cview.MouseAction) {
mouseX, mouseY := event.Position()
if action == cview.MouseLeftClick && mouseY == screenHeight-1 {
if mouseX >= seekStart && mouseX <= seekEnd {
speaker.Lock()
if playingStreamer == nil {
speaker.Unlock()
return nil, 0
2020-01-28 22:53:38 +00:00
}
2020-01-08 23:38:41 +00:00
2020-04-24 22:39:02 +00:00
pos := float64(mouseX-seekStart) / float64(seekEnd-seekStart)
if pos > 1 {
pos = 1
}
seekTo := int(float64(playingStreamer.Len()-1) * pos)
_ = playingStreamer.Seek(seekTo) // Ignore seek errors
speaker.Unlock()
updateStatus()
2020-04-24 22:39:02 +00:00
return nil, 0
} else if mouseX >= volumeStart && mouseX <= volumeEnd+1 {
if mouseX > volumeEnd {
mouseX = volumeEnd
}
2020-01-08 23:38:41 +00:00
2020-04-24 22:39:02 +00:00
if mouseX-volumeStart <= 3 {
2020-01-08 23:38:41 +00:00
speaker.Lock()
2020-01-22 22:54:55 +00:00
2020-04-24 22:39:02 +00:00
if volume == nil {
2020-01-22 22:54:55 +00:00
speaker.Unlock()
2020-04-24 22:39:02 +00:00
return nil, 0
2020-01-22 22:54:55 +00:00
}
2020-04-24 22:39:02 +00:00
volume.Silent = !volume.Silent
2020-01-08 23:38:41 +00:00
speaker.Unlock()
2020-01-09 17:48:00 +00:00
updateStatus()
2020-04-24 22:39:02 +00:00
} else {
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-04-24 22:39:02 +00:00
setVolume(roundUnit(vol, 0.5))
2020-01-08 23:38:41 +00:00
updateStatus()
2020-01-08 23:38:41 +00:00
}
2020-04-24 22:39:02 +00:00
return nil, 0
2020-01-08 23:38:41 +00:00
}
}
2020-04-24 22:39:02 +00:00
return event, action
2020-01-08 23:38:41 +00:00
}