2019-02-02 04:39:34 +00:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
2019-02-17 01:54:48 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
2019-02-11 02:51:55 +00:00
|
|
|
"github.com/sqshq/sampler/console"
|
2019-02-02 14:45:53 +00:00
|
|
|
"github.com/sqshq/sampler/widgets"
|
2019-02-02 04:39:34 +00:00
|
|
|
ui "github.com/sqshq/termui"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
Layout *widgets.Layout
|
|
|
|
RenderEvents <-chan time.Time
|
|
|
|
ConsoleEvents <-chan ui.Event
|
|
|
|
}
|
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
func (h *Handler) HandleEvents() {
|
2019-02-02 04:39:34 +00:00
|
|
|
|
|
|
|
pause := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2019-02-17 01:54:48 +00:00
|
|
|
case <-h.RenderEvents:
|
2019-02-02 04:39:34 +00:00
|
|
|
if !pause {
|
2019-02-17 01:54:48 +00:00
|
|
|
ui.Render(h.Layout)
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
2019-02-17 01:54:48 +00:00
|
|
|
case e := <-h.ConsoleEvents:
|
2019-02-02 04:39:34 +00:00
|
|
|
switch e.ID {
|
2019-02-11 02:51:55 +00:00
|
|
|
case console.KeyQuit, console.KeyExit:
|
2019-02-17 01:54:48 +00:00
|
|
|
h.handleExit()
|
2019-02-02 04:39:34 +00:00
|
|
|
return
|
2019-02-11 02:51:55 +00:00
|
|
|
case console.KeyPause:
|
2019-02-02 04:39:34 +00:00
|
|
|
pause = !pause
|
2019-02-11 03:26:51 +00:00
|
|
|
case console.SignalResize:
|
2019-02-02 04:39:34 +00:00
|
|
|
payload := e.Payload.(ui.Resize)
|
2019-02-17 01:54:48 +00:00
|
|
|
h.Layout.ChangeDimensions(payload.Width, payload.Height)
|
2019-02-11 02:51:55 +00:00
|
|
|
default:
|
2019-02-17 01:54:48 +00:00
|
|
|
h.Layout.HandleConsoleEvent(e.ID)
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 01:54:48 +00:00
|
|
|
|
|
|
|
func (h *Handler) handleExit() {
|
|
|
|
var settings []config.ComponentSettings
|
|
|
|
for _, c := range h.Layout.Components {
|
|
|
|
settings = append(settings,
|
|
|
|
config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Position: c.Position})
|
|
|
|
}
|
|
|
|
config.Update(settings)
|
|
|
|
}
|