ditty/gui_key.go

129 lines
2.2 KiB
Go

package main
import (
"github.com/faiface/beep/speaker"
"github.com/gdamore/tcell"
)
func handleKeyPress(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case '-':
audioLock.Lock()
defer audioLock.Unlock()
speaker.Lock()
volume.Volume -= 0.5
if volume.Volume <= -7.5 {
volume.Volume = -7.5
volume.Silent = true
}
speaker.Unlock()
updateStatus()
return nil
case '+':
audioLock.Lock()
defer audioLock.Unlock()
speaker.Lock()
volume.Volume += 0.5
if volume.Volume > 0 {
volume.Volume = 0
}
volume.Silent = false
speaker.Unlock()
updateStatus()
return nil
case ' ':
audioLock.Lock()
defer audioLock.Unlock()
speaker.Lock()
ctrl.Paused = !ctrl.Paused
speaker.Unlock()
updateStatus()
return nil
case 'j':
listNext()
return nil
case 'k':
listPrevious()
return nil
case 'p':
if mainBufferCursor > 1 {
if offsetEntry(-1).File.IsDir() {
return nil
}
listPrevious()
go selectTrack()
}
return nil
case 'n':
if mainBufferCursor < len(mainBufferFiles) {
if offsetEntry(1).File.IsDir() {
return nil
}
listNext()
go selectTrack()
}
return nil
case 'q':
// Queue non-recursively
return nil
case 'Q':
// Queue recursively
return nil
}
switch event.Key() {
case tcell.KeyEscape:
done <- true
return nil
case tcell.KeyEnter:
go selectTrack()
return nil
case tcell.KeyDown:
listNext()
return nil
case tcell.KeyUp:
listPrevious()
return nil
case tcell.KeyPgDn:
numEntries := len(mainBufferFiles)
if mainBufferOrigin >= numEntries-(mainBufHeight-1) {
mainBufferCursor = numEntries
updateMain()
return nil
}
mainBufferOrigin += (mainBufHeight - 1) - 2
if mainBufferOrigin > (numEntries-(mainBufHeight-1))+2 {
mainBufferOrigin = (numEntries - (mainBufHeight - 1)) + 2
}
mainBufferCursor = mainBufferOrigin
updateMain()
return nil
case tcell.KeyPgUp:
if mainBufferOrigin == 0 {
mainBufferCursor = 0
updateMain()
return nil
}
mainBufferOrigin -= (mainBufHeight - 1) - 2
if mainBufferOrigin < 0 {
mainBufferOrigin = 0
}
mainBufferCursor = mainBufferOrigin
updateMain()
return nil
}
return event
}