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"
|
|
|
|
)
|
|
|
|
|
2019-02-24 04:42:52 +00:00
|
|
|
const (
|
|
|
|
refreshRateToRenderRateRatio = 0.8
|
|
|
|
)
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
type Handler struct {
|
2019-02-24 04:42:52 +00:00
|
|
|
layout *widgets.Layout
|
|
|
|
renderTicker *time.Ticker
|
|
|
|
consoleEvents <-chan ui.Event
|
|
|
|
renderRate time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(layout *widgets.Layout) Handler {
|
|
|
|
renderRate := calcMinRenderRate(layout)
|
|
|
|
return Handler{
|
|
|
|
layout: layout,
|
|
|
|
consoleEvents: ui.PollEvents(),
|
|
|
|
renderTicker: time.NewTicker(renderRate),
|
|
|
|
renderRate: renderRate,
|
|
|
|
}
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
func (h *Handler) HandleEvents() {
|
2019-02-02 04:39:34 +00:00
|
|
|
|
2019-02-24 04:42:52 +00:00
|
|
|
// initial render
|
|
|
|
ui.Render(h.layout)
|
2019-02-02 04:39:34 +00:00
|
|
|
pause := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2019-02-24 04:42:52 +00:00
|
|
|
case mode := <-h.layout.ChangeModeEvents:
|
|
|
|
h.handleModeChange(mode)
|
|
|
|
case <-h.renderTicker.C:
|
|
|
|
ui.Render(h.layout)
|
|
|
|
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-24 04:42:52 +00:00
|
|
|
pause = !pause // TODO move to layout, since it will show PAUSE sign in status line, pause/unpause can be sent via channel
|
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-24 04:42:52 +00:00
|
|
|
h.layout.ChangeDimensions(payload.Width, payload.Height)
|
2019-02-11 02:51:55 +00:00
|
|
|
default:
|
2019-02-24 04:42:52 +00:00
|
|
|
h.layout.HandleConsoleEvent(e.ID)
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 01:54:48 +00:00
|
|
|
|
2019-02-24 04:42:52 +00:00
|
|
|
func (h *Handler) handleModeChange(m widgets.Mode) {
|
|
|
|
|
|
|
|
// render mode change before switching the tickers
|
|
|
|
ui.Render(h.layout)
|
|
|
|
h.renderTicker.Stop()
|
|
|
|
|
|
|
|
if m == widgets.ModeDefault {
|
|
|
|
h.renderTicker = time.NewTicker(h.renderRate)
|
|
|
|
} else {
|
|
|
|
h.renderTicker = time.NewTicker(console.MinRenderInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
func (h *Handler) handleExit() {
|
|
|
|
var settings []config.ComponentSettings
|
2019-02-24 04:42:52 +00:00
|
|
|
for _, c := range h.layout.Components {
|
2019-02-17 01:54:48 +00:00
|
|
|
settings = append(settings,
|
|
|
|
config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Position: c.Position})
|
|
|
|
}
|
|
|
|
config.Update(settings)
|
|
|
|
}
|
2019-02-24 04:42:52 +00:00
|
|
|
|
|
|
|
func calcMinRenderRate(layout *widgets.Layout) time.Duration {
|
|
|
|
|
|
|
|
minRefreshRateMs := layout.Components[0].RefreshRateMs
|
|
|
|
for _, c := range layout.Components {
|
|
|
|
if c.RefreshRateMs < minRefreshRateMs {
|
|
|
|
minRefreshRateMs = c.RefreshRateMs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRate := time.Duration(
|
|
|
|
int(float64(minRefreshRateMs)*refreshRateToRenderRateRatio)) * time.Millisecond
|
|
|
|
|
|
|
|
if renderRate < console.MinRenderInterval {
|
|
|
|
return console.MinRenderInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
if renderRate > console.MaxRenderInterval {
|
|
|
|
return console.MaxRenderInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderRate
|
|
|
|
}
|