tui/layouts/utils.go

74 lines
1.4 KiB
Go
Raw Normal View History

2023-04-24 11:41:38 +02:00
package layouts
import (
"math"
2023-04-24 11:55:04 +02:00
"git.milar.in/milarin/gmath"
"git.milar.in/milarin/tui"
2023-04-24 11:41:38 +02:00
)
func iff[T any](condition bool, trueValue, falseValue T) T {
if condition {
return trueValue
}
return falseValue
}
type LayoutResult struct {
Sizes map[tui.View]tui.Size
Sum tui.Size
Min tui.Size
Max tui.Size
Pref tui.Size
Count int
VerticalNegativeCount int
HorizontalNegativeCount int
}
func CalculateLayoutResult(views []tui.View) *LayoutResult {
result := &LayoutResult{
Sizes: map[tui.View]tui.Size{},
Sum: tui.Size{Width: 0, Height: 0},
Min: tui.Size{Width: math.MaxInt, Height: math.MaxInt},
Max: tui.Size{Width: -1, Height: -1},
Count: 0,
VerticalNegativeCount: 0,
HorizontalNegativeCount: 0,
}
for _, view := range views {
if view == nil {
continue
}
result.Count++
width, height := view.Layout()
result.Sizes[view] = tui.Size{Width: width, Height: height}
if width > 0 {
result.Sum.Width += width
2023-04-24 11:55:04 +02:00
result.Min.Width = gmath.Min(result.Min.Width, width)
result.Max.Width = gmath.Max(result.Max.Width, width)
2023-04-24 11:41:38 +02:00
} else if width < 0 {
result.HorizontalNegativeCount++
}
if height > 0 {
result.Sum.Height += height
2023-04-24 11:55:04 +02:00
result.Min.Height = gmath.Min(result.Min.Height, height)
result.Max.Height = gmath.Max(result.Max.Height, height)
2023-04-24 11:41:38 +02:00
} else if height < 0 {
result.VerticalNegativeCount++
}
}
return result
}