First version
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
type DivStyle struct {
|
||||
Background string `default:"" type:"bg"`
|
||||
Foreground string `default:"" type:"fg"`
|
||||
Outline string `default:"" type:"fg"`
|
||||
}
|
||||
|
||||
type DivComponent struct {
|
||||
@@ -18,7 +19,6 @@ type DivComponent struct {
|
||||
Style *DivStyle
|
||||
Components []osui.Component
|
||||
ActiveComponent int
|
||||
onKey func(*DivComponent, string) string
|
||||
}
|
||||
|
||||
func (d *DivComponent) GetComponentData() *osui.ComponentData {
|
||||
@@ -31,7 +31,7 @@ func (d *DivComponent) Render() string {
|
||||
for i, c := range d.Components {
|
||||
data := c.GetComponentData()
|
||||
if i == d.ActiveComponent {
|
||||
data.IsActive = true
|
||||
data.IsActive = d.Data.IsActive
|
||||
} else {
|
||||
data.IsActive = false
|
||||
}
|
||||
@@ -45,41 +45,28 @@ func (d *DivComponent) Render() string {
|
||||
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
|
||||
if d.Style.Outline == "" {
|
||||
for i, f := range frame {
|
||||
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset
|
||||
}
|
||||
return strings.Join(frame, "\n")
|
||||
} else {
|
||||
for i, f := range frame {
|
||||
frame[i] = d.Style.Outline + "│" + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Style.Outline + "│" + colors.Reset
|
||||
}
|
||||
}
|
||||
return strings.Join(frame, "\n")
|
||||
return " " + d.Style.Outline + strings.Repeat("_", d.Data.Width) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Style.Outline + strings.Repeat("‾", d.Data.Width) + colors.Reset
|
||||
}
|
||||
|
||||
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)
|
||||
if isKey.CtrlW(key) {
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up"))
|
||||
} else if isKey.CtrlS(key) {
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
|
||||
} else if isKey.CtrlA(key) {
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left"))
|
||||
} else if isKey.CtrlD(key) {
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right"))
|
||||
} else {
|
||||
if len(d.Components) > 0 {
|
||||
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
|
||||
@@ -105,11 +92,6 @@ func (d *DivComponent) updateActive(newIndex int) {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
+3
-3
@@ -52,13 +52,13 @@ func (m *MenuComponent) Render() string {
|
||||
}
|
||||
|
||||
func (m *MenuComponent) Update(key string) bool {
|
||||
if isKey.Down(key) {
|
||||
if isKey.Char(key, "s") {
|
||||
if m.SelectedItem+1 < len(m.Items) {
|
||||
m.SelectedItem++
|
||||
} else {
|
||||
m.SelectedItem = 0
|
||||
}
|
||||
} else if isKey.Up(key) {
|
||||
} else if isKey.Char(key, "w") {
|
||||
if m.SelectedItem > 0 {
|
||||
m.SelectedItem--
|
||||
} else {
|
||||
@@ -67,7 +67,7 @@ func (m *MenuComponent) Update(key string) bool {
|
||||
} else if isKey.Enter(key) {
|
||||
m.onSelected(m, true)
|
||||
return true
|
||||
} else if isKey.Escape(key) {
|
||||
} else if isKey.Char(key, "q") {
|
||||
m.onSelected(m, false)
|
||||
return true
|
||||
}
|
||||
|
||||
+50
@@ -2,6 +2,7 @@ package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
@@ -44,3 +45,52 @@ func WithSize(width, height int, c osui.Component) osui.Component {
|
||||
data.Height = height
|
||||
return c
|
||||
}
|
||||
|
||||
func findClosestComponent(a []osui.Component, i int, d string) int {
|
||||
if len(a) == 0 || i < 0 || i >= len(a) {
|
||||
return -1 // Invalid input
|
||||
}
|
||||
|
||||
current := a[i].GetComponentData()
|
||||
closestIndex := -1
|
||||
minDistance := math.MaxFloat64
|
||||
|
||||
for j, c := range a {
|
||||
component := c.GetComponentData()
|
||||
if i == j {
|
||||
continue // Skip the current component
|
||||
}
|
||||
|
||||
// Check if the component is in the correct direction
|
||||
isValidDirection := false
|
||||
switch d {
|
||||
case "up":
|
||||
if component.Y < current.Y {
|
||||
isValidDirection = true
|
||||
}
|
||||
case "down":
|
||||
if component.Y > current.Y {
|
||||
isValidDirection = true
|
||||
}
|
||||
case "left":
|
||||
if component.X < current.X {
|
||||
isValidDirection = true
|
||||
}
|
||||
case "right":
|
||||
if component.X > current.X {
|
||||
isValidDirection = true
|
||||
}
|
||||
}
|
||||
|
||||
// If the direction is valid, calculate distance
|
||||
if isValidDirection {
|
||||
distance := math.Hypot(float64(component.X-current.X), float64(component.Y-current.Y))
|
||||
if distance < minDistance {
|
||||
minDistance = distance
|
||||
closestIndex = j
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return closestIndex
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user