feat: update keyboard handling to use new library

- remove dependency on atomicgo.dev/keyboard
- add dependency on github.com/eiannone/keyboard
- include keyhandling as non-blocking in frondend/main
- refactor keyhandler()

🤖
This commit is contained in:
Sebastian Mark 2024-10-29 20:13:39 +01:00
parent 5f65b9aa56
commit f1071f33c6
4 changed files with 33 additions and 87 deletions

View file

@ -1,8 +1,7 @@
package frontend
import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/eiannone/keyboard"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
@ -11,7 +10,7 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func start_pause_resume(message *GoTomato.ServerMessage) string {
func start_pause_resume(message GoTomato.ServerMessage) string {
if !message.Ongoing {
return "start"
}
@ -22,29 +21,19 @@ func start_pause_resume(message *GoTomato.ServerMessage) string {
}
}
func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.ServerMessage, quit chan bool) {
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
select {
case <-websocket.Done:
quit <- true
return true, nil
default:
switch key.String() {
case "space":
cmd := start_pause_resume(message)
websocket.SendCmd(conn, config.Password, cmd)
case "s":
websocket.SendCmd(conn, config.Password, "stop")
case "r":
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
}
case "q":
quit <- true
return true, nil
}
func keyhandler(key keyboard.KeyEvent, conn *ws.Conn, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
switch key.Rune {
case 0: // space
cmd := start_pause_resume(message)
websocket.SendCmd(conn, config.Password, cmd)
case 115: // s
websocket.SendCmd(conn, config.Password, "stop")
case 114: // r
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
}
return false, nil
})
case 113: // q
return false
}
return true
}

View file

@ -1,24 +1,31 @@
package frontend
import (
"github.com/gorilla/websocket"
"github.com/eiannone/keyboard"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func UpdateLoop(conn *websocket.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
var message GoTomato.ServerMessage
keyhandler_quit := make(chan bool, 1)
go keyhandler(conn, config, &message, keyhandler_quit)
keysEvents, _ := keyboard.GetKeys(1)
defer keyboard.Close()
for {
select {
case message = <-channel:
desktopNotifications(message)
terminalOutput(message)
case <-keyhandler_quit:
case keypress := <-keysEvents:
if !keyhandler(keypress, conn, config, message) {
return
}
case <-websocket.Done:
return
}
}