sampler-fork/event/handler.go

124 lines
2.8 KiB
Go
Raw Normal View History

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"
"github.com/sqshq/sampler/config"
2019-02-11 02:51:55 +00:00
"github.com/sqshq/sampler/console"
2019-06-07 05:39:11 +00:00
"github.com/sqshq/sampler/data"
"time"
)
const (
2019-02-27 04:23:56 +00:00
refreshRateToRenderRateRatio = 0.5
)
type Handler struct {
2019-06-07 05:39:11 +00:00
samplers []*data.Sampler
options config.Options
2019-03-08 04:04:46 +00:00
layout *layout.Layout
renderTicker *time.Ticker
consoleEvents <-chan ui.Event
renderRate time.Duration
}
2019-06-07 05:39:11 +00:00
func NewHandler(samplers []*data.Sampler, options config.Options, layout *layout.Layout) *Handler {
renderRate := calcMinRenderRate(layout)
return &Handler{
2019-06-07 05:39:11 +00:00
samplers: samplers,
options: options,
layout: layout,
consoleEvents: ui.PollEvents(),
renderTicker: time.NewTicker(renderRate),
renderRate: renderRate,
}
}
func (h *Handler) HandleEvents() {
// initial render
ui.Render(h.layout)
for {
select {
case mode := <-h.layout.ChangeModeEvents:
h.handleModeChange(mode)
case <-h.renderTicker.C:
ui.Render(h.layout)
case e := <-h.consoleEvents:
switch e.ID {
2019-04-14 00:24:24 +00:00
case console.SignalClick:
payload := e.Payload.(ui.Mouse)
h.layout.HandleMouseClick(payload.X, payload.Y)
case console.KeyQuit1, console.KeyQuit2, console.KeyQuit3:
if h.layout.WerePositionsChanged() {
h.updateConfigFile()
}
return
case console.SignalResize:
payload := e.Payload.(ui.Resize)
h.layout.ChangeDimensions(payload.Width, payload.Height)
2019-02-11 02:51:55 +00:00
default:
2019-04-14 00:24:24 +00:00
h.layout.HandleKeyboardEvent(e.ID)
}
}
}
}
2019-03-08 04:04:46 +00:00
func (h *Handler) handleModeChange(m layout.Mode) {
2019-03-03 01:04:02 +00:00
// render the change before switching the tickers
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:
h.renderTicker = time.NewTicker(h.renderRate)
2019-06-07 05:39:11 +00:00
h.pause(false)
2019-03-08 04:04:46 +00:00
case layout.ModePause:
2019-06-07 05:39:11 +00:00
h.pause(true)
2019-03-03 01:04:02 +00:00
// proceed with stopped timer
default:
h.renderTicker = time.NewTicker(console.MinRenderInterval)
2019-06-07 05:39:11 +00:00
h.pause(false)
}
}
func (h *Handler) pause(pause bool) {
for _, s := range h.samplers {
s.Pause(pause)
}
}
func (h *Handler) updateConfigFile() {
var settings []config.ComponentSettings
for _, c := range h.layout.Components {
settings = append(settings,
config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Location: c.Location})
}
2019-03-20 02:12:45 +00:00
config.Update(settings, h.options)
}
2019-03-08 04:04:46 +00:00
func calcMinRenderRate(layout *layout.Layout) time.Duration {
2019-04-07 15:17:28 +00:00
minRateMs := layout.Components[0].RateMs
for _, c := range layout.Components {
2019-04-07 15:17:28 +00:00
if c.RateMs < minRateMs {
minRateMs = c.RateMs
}
}
renderRate := time.Duration(
2019-04-07 15:17:28 +00:00
int(float64(minRateMs)*refreshRateToRenderRateRatio)) * time.Millisecond
if renderRate < console.MinRenderInterval {
return console.MinRenderInterval
}
if renderRate > console.MaxRenderInterval {
return console.MaxRenderInterval
}
return renderRate
}