added auto arrangement, so user can add new components without specified position

This commit is contained in:
sqshq 2019-04-26 01:11:40 -04:00
parent 7a1197d446
commit 7334efa9ff
5 changed files with 129 additions and 11 deletions

View File

@ -34,8 +34,6 @@ const (
) )
const ( const (
columnsCount = 80
rowsCount = 40
minDimension = 3 minDimension = 3
statusbarHeight = 1 statusbarHeight = 1
) )
@ -132,6 +130,11 @@ func (l *Layout) HandleKeyboardEvent(e string) {
case ModeMenuOptionSelect: case ModeMenuOptionSelect:
l.menu.Idle() l.menu.Idle()
l.changeMode(ModeDefault) l.changeMode(ModeDefault)
case ModeComponentMove:
fallthrough
case ModeComponentResize:
l.menu.Idle()
l.changeMode(ModeDefault)
} }
case console.KeyLeft: case console.KeyLeft:
switch l.mode { switch l.mode {
@ -273,8 +276,8 @@ func (l *Layout) moveSelection(direction string) {
func (l *Layout) Draw(buffer *ui.Buffer) { func (l *Layout) Draw(buffer *ui.Buffer) {
columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount) columnWidth := float64(l.GetRect().Dx()) / float64(console.ColumnsCount)
rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount) rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(console.RowsCount)
for _, c := range l.Components { for _, c := range l.Components {
rectangle := calculateComponentCoordinates(c, columnWidth, rowHeight) rectangle := calculateComponentCoordinates(c, columnWidth, rowHeight)
@ -292,8 +295,8 @@ func (l *Layout) Draw(buffer *ui.Buffer) {
func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component, int) { func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component, int) {
columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount) columnWidth := float64(l.GetRect().Dx()) / float64(console.ColumnsCount)
rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount) rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(console.RowsCount)
for i, c := range l.Components { for i, c := range l.Components {

108
config/arrangement.go Normal file
View File

@ -0,0 +1,108 @@
package config
import (
"github.com/sqshq/sampler/console"
)
func (c *Config) setDefaultArrangement() {
components := getComponents(c)
if allHaveNoPosition(components) {
setSingleComponentPosition(components[0])
}
for _, component := range components {
if component.Position == nil {
lc := getLargestComponent(components)
lr := lc.GetRectangle()
var w, h, ws, hs int
if lr.Dx()/2 > lr.Dy() {
w = lr.Dx() / 2
h = lr.Dy()
ws = w
hs = 0
} else {
w = lr.Dx()
h = lr.Dy() / 2
ws = 0
hs = h
}
component.Position = [][]int{
{lr.Min.X + ws, lr.Min.Y + hs},
{w, h},
}
lc.Position = [][]int{
{lr.Min.X, lr.Min.Y},
{w, h},
}
}
}
}
func allHaveNoPosition(components []*ComponentConfig) bool {
for _, component := range components {
if component.Position != nil {
return false
}
}
return true
}
func getLargestComponent(components []*ComponentConfig) *ComponentConfig {
largestComponent := components[0]
for _, component := range components {
if getSquare(component) > getSquare(largestComponent) {
largestComponent = component
}
}
return largestComponent
}
func getSquare(c *ComponentConfig) int {
r := c.GetRectangle()
return r.Dx() * r.Dy()
}
func getComponents(c *Config) []*ComponentConfig {
var components []*ComponentConfig
for i := range c.RunCharts {
components = append(components, &c.RunCharts[i].ComponentConfig)
}
for i := range c.BarCharts {
components = append(components, &c.BarCharts[i].ComponentConfig)
}
for i := range c.Gauges {
components = append(components, &c.Gauges[i].ComponentConfig)
}
for i := range c.SparkLines {
components = append(components, &c.SparkLines[i].ComponentConfig)
}
for i := range c.AsciiBoxes {
components = append(components, &c.AsciiBoxes[i].ComponentConfig)
}
for i := range c.TextBoxes {
components = append(components, &c.TextBoxes[i].ComponentConfig)
}
return components
}
func setSingleComponentPosition(c *ComponentConfig) {
w := int(console.ColumnsCount * 0.8)
h := int(console.RowsCount * 0.7)
c.Position = [][]int{
{(console.ColumnsCount - w) / 2, (console.RowsCount - h) / 2},
{w, h},
}
}

View File

@ -3,6 +3,7 @@ package config
import ( import (
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/console" "github.com/sqshq/sampler/console"
"image"
) )
type ComponentType rune type ComponentType rune
@ -32,6 +33,14 @@ func (c *ComponentConfig) GetSize() Size {
return Size{X: c.Position[1][0], Y: c.Position[1][1]} return Size{X: c.Position[1][0], Y: c.Position[1][1]}
} }
func (c *ComponentConfig) GetRectangle() image.Rectangle {
if c.Position == nil {
return image.ZR
} else {
return image.Rect(c.Position[0][0], c.Position[0][1], c.Position[0][0]+c.Position[1][0], c.Position[0][1]+c.Position[1][1])
}
}
type TriggerConfig struct { type TriggerConfig struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Condition string `yaml:"condition"` Condition string `yaml:"condition"`

View File

@ -13,7 +13,7 @@ const (
func (c *Config) setDefaults() { func (c *Config) setDefaults() {
c.setDefaultValues() c.setDefaultValues()
c.setDefaultColors() c.setDefaultColors()
c.setDefaultLayout() c.setDefaultArrangement()
} }
func (c *Config) setDefaultValues() { func (c *Config) setDefaultValues() {
@ -173,10 +173,6 @@ func setDefaultTriggersValues(triggers []TriggerConfig) {
} }
} }
func (c *Config) setDefaultLayout() {
// TODO auto-arrange components
}
func (c *Config) setDefaultColors() { func (c *Config) setDefaultColors() {
palette := console.GetPalette(*c.Theme) palette := console.GetPalette(*c.Theme)

View File

@ -10,6 +10,8 @@ import (
const ( const (
MaxRenderInterval = 1000 * time.Millisecond MaxRenderInterval = 1000 * time.Millisecond
MinRenderInterval = 100 * time.Millisecond MinRenderInterval = 100 * time.Millisecond
ColumnsCount = 80
RowsCount = 40
AppTitle = "sampler" AppTitle = "sampler"
AppVersion = "0.9.0" AppVersion = "0.9.0"
) )