ditty/gui_key.go

151 lines
2.6 KiB
Go
Raw Normal View History

2020-01-08 23:38:41 +00:00
package main
import (
"path"
2020-01-08 23:38:41 +00:00
"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()
2020-01-09 17:48:00 +00:00
if volume == nil {
return nil
}
2020-01-08 23:38:41 +00:00
speaker.Lock()
volume.Volume -= 0.5
if volume.Volume <= -7.5 {
volume.Volume = -7.5
volume.Silent = true
}
speaker.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
case '+':
audioLock.Lock()
defer audioLock.Unlock()
2020-01-09 17:48:00 +00:00
if ctrl == nil {
return nil
}
2020-01-08 23:38:41 +00:00
speaker.Lock()
volume.Volume += 0.5
if volume.Volume > 0 {
volume.Volume = 0
}
volume.Silent = false
speaker.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
case ' ':
audioLock.Lock()
defer audioLock.Unlock()
2020-01-09 17:48:00 +00:00
if ctrl == nil {
return nil
}
2020-01-08 23:38:41 +00:00
speaker.Lock()
ctrl.Paused = !ctrl.Paused
speaker.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
case 'j':
2020-01-09 01:50:52 +00:00
listNext()
2020-01-08 23:38:41 +00:00
return nil
case 'k':
2020-01-09 01:50:52 +00:00
listPrevious()
2020-01-08 23:38:41 +00:00
return nil
case 'p':
if mainBufferCursor > 1 {
2020-01-09 01:50:52 +00:00
if offsetEntry(-1).File.IsDir() {
2020-01-08 23:38:41 +00:00
return nil
}
2020-01-09 01:50:52 +00:00
listPrevious()
2020-01-09 23:51:37 +00:00
go listSelect()
2020-01-08 23:38:41 +00:00
}
return nil
case 'n':
if mainBufferCursor < len(mainBufferFiles) {
2020-01-09 01:50:52 +00:00
if offsetEntry(1).File.IsDir() {
2020-01-08 23:38:41 +00:00
return nil
}
2020-01-09 01:50:52 +00:00
listNext()
2020-01-09 23:51:37 +00:00
go listSelect()
2020-01-08 23:38:41 +00:00
}
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.KeyBackspace, tcell.KeyBackspace2:
mainBufferAutoFocus = mainBufferDirectory
go browseFolder(path.Join(mainBufferDirectory, ".."))
return nil
2020-01-08 23:38:41 +00:00
case tcell.KeyEnter:
2020-01-09 23:51:37 +00:00
go listSelect()
2020-01-08 23:38:41 +00:00
return nil
2020-01-09 01:50:52 +00:00
case tcell.KeyDown:
listNext()
return nil
2020-01-08 23:38:41 +00:00
case tcell.KeyUp:
2020-01-09 01:50:52 +00:00
listPrevious()
return nil
case tcell.KeyPgDn:
numEntries := len(mainBufferFiles)
if mainBufferOrigin >= numEntries-(mainBufHeight-1) {
mainBufferCursor = numEntries
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateMain)
2020-01-09 01:50:52 +00:00
return nil
2020-01-08 23:38:41 +00:00
}
2020-01-09 01:50:52 +00:00
mainBufferOrigin += (mainBufHeight - 1) - 2
if mainBufferOrigin > (numEntries-(mainBufHeight-1))+2 {
mainBufferOrigin = (numEntries - (mainBufHeight - 1)) + 2
}
mainBufferCursor = mainBufferOrigin
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 01:50:52 +00:00
case tcell.KeyPgUp:
if mainBufferOrigin == 0 {
mainBufferCursor = 0
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateMain)
2020-01-09 01:50:52 +00:00
return nil
}
mainBufferOrigin -= (mainBufHeight - 1) - 2
if mainBufferOrigin < 0 {
mainBufferOrigin = 0
2020-01-08 23:38:41 +00:00
}
2020-01-09 01:50:52 +00:00
mainBufferCursor = mainBufferOrigin
2020-01-09 23:51:37 +00:00
go app.QueueUpdateDraw(updateMain)
2020-01-08 23:38:41 +00:00
return nil
}
return event
}