Files
osui-go/ui/div.go
T
2024-09-28 19:29:01 +02:00

124 lines
2.9 KiB
Go

package ui
import (
"strings"
"github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey"
)
type DivStyle struct {
Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"`
}
type DivComponent struct {
Data osui.ComponentData
Style *DivStyle
Components []osui.Component
ActiveComponent int
onKey func(*DivComponent, string) string
}
func (d *DivComponent) GetComponentData() *osui.ComponentData {
return &d.Data
}
func (d *DivComponent) Render() string {
osui.UseStyle(d.Style)
frame := osui.NewFrame(d.Data.Width, d.Data.Height)
for i, c := range d.Components {
data := c.GetComponentData()
if i == d.ActiveComponent {
data.IsActive = true
} else {
data.IsActive = false
}
if data.Width == 0 {
data.Width = d.Data.Width
}
if data.Height == 0 {
data.Height = d.Data.Height
}
data.DefaultColor = colors.Combine(d.Style.Background, d.Style.Foreground)
data.Screen = d.Data.Screen
osui.RenderOnFrame(c, &frame)
}
for i, f := range frame {
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset
}
return strings.Join(frame, "\n")
}
func (d *DivComponent) Update(key string) bool {
if d.onKey != nil {
o := d.onKey(d, key)
if o == "up" {
d.updateActive(d.ActiveComponent - 1)
} else if o == "down" {
d.updateActive(d.ActiveComponent + 1)
} else if o == "true" {
return true
} else if o == "false" {
return false
} else {
if len(d.Components) > 0 {
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if d.Components[d.ActiveComponent].Update(key) {
if d.ActiveComponent < len(d.Components)-1 {
d.updateActive(d.ActiveComponent + 1)
} else {
return true
}
}
}
}
return false
}
if isKey.Up(key) {
d.updateActive(d.ActiveComponent - 1)
} else if isKey.Down(key) {
d.updateActive(d.ActiveComponent + 1)
} else {
if len(d.Components) > 0 {
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if d.Components[d.ActiveComponent].Update(key) {
if d.ActiveComponent < len(d.Components)-1 {
d.updateActive(d.ActiveComponent + 1)
} else {
return true
}
}
}
}
return false
}
func (d *DivComponent) SetStyle(c interface{}) {
d.Style = osui.SetDefaults(c.(*DivStyle)).(*DivStyle)
}
func (d *DivComponent) updateActive(newIndex int) {
if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
d.ActiveComponent = newIndex
}
}
func (d *DivComponent) OnKey(f func(*DivComponent, string) string) *DivComponent {
d.onKey = f
return d
}
func (d *DivComponent) AddTo(c *DivComponent) *DivComponent {
*c = *d
return d
}
func Div(components ...osui.Component) *DivComponent {
return &DivComponent{
Components: components,
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
}
}