2019-02-02 04:39:34 +00:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
2019-03-14 03:01:44 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2019-03-08 04:04:46 +00:00
|
|
|
"github.com/sqshq/sampler/component/layout"
|
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 04:39:34 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-02-24 04:42:52 +00:00
|
|
|
const (
|
2019-02-27 04:23:56 +00:00
|
|
|
refreshRateToRenderRateRatio = 0.5
|
2019-02-24 04:42:52 +00:00
|
|
|
)
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
type Handler struct {
|
2019-03-08 04:04:46 +00:00
|
|
|
layout *layout.Layout
|
2019-02-24 04:42:52 +00:00
|
|
|
renderTicker *time.Ticker
|
|
|
|
consoleEvents <-chan ui.Event
|
|
|
|
renderRate time.Duration
|
2019-03-20 02:12:45 +00:00
|
|
|
options config.Options
|
2019-02-24 04:42:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 02:12:45 +00:00
|
|
|
func NewHandler(layout *layout.Layout, options config.Options) *Handler {
|
2019-02-24 04:42:52 +00:00
|
|
|
renderRate := calcMinRenderRate(layout)
|
2019-03-16 04:35:00 +00:00
|
|
|
return &Handler{
|
2019-02-24 04:42:52 +00:00
|
|
|
layout: layout,
|
|
|
|
consoleEvents: ui.PollEvents(),
|
|
|
|
renderTicker: time.NewTicker(renderRate),
|
|
|
|
renderRate: renderRate,
|
2019-03-20 02:12:45 +00:00
|
|
|
options: options,
|
2019-02-24 04:42:52 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
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 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-03-08 04:04:46 +00:00
|
|
|
func (h *Handler) handleModeChange(m layout.Mode) {
|
2019-02-24 04:42:52 +00:00
|
|
|
|
2019-03-03 01:04:02 +00:00
|
|
|
// render the change before switching the tickers
|
2019-02-24 04:42:52 +00:00
|
|
|
ui.Render(h.layout)
|
|
|
|
h.renderTicker.Stop()
|
|
|
|
|
2019-03-03 01:04:02 +00:00
|
|
|
switch m {
|
2019-03-08 04:04:46 +00:00
|
|
|
case layout.ModeDefault:
|
2019-02-24 04:42:52 +00:00
|
|
|
h.renderTicker = time.NewTicker(h.renderRate)
|
2019-03-08 04:04:46 +00:00
|
|
|
case layout.ModePause:
|
2019-03-03 01:04:02 +00:00
|
|
|
// proceed with stopped timer
|
|
|
|
default:
|
2019-02-24 04:42:52 +00:00
|
|
|
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,
|
2019-03-18 02:41:23 +00:00
|
|
|
config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Location: c.Location})
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
2019-03-20 02:12:45 +00:00
|
|
|
config.Update(settings, h.options)
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
2019-02-24 04:42:52 +00:00
|
|
|
|
2019-03-08 04:04:46 +00:00
|
|
|
func calcMinRenderRate(layout *layout.Layout) time.Duration {
|
2019-02-24 04:42:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|