updated structure
This commit is contained in:
+29
-64
@@ -10,80 +10,55 @@ import (
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type ButtonParams struct {
|
||||
Style ButtonStyle
|
||||
Toggle bool
|
||||
OnClick func(*ButtonComponent) bool
|
||||
Width int
|
||||
}
|
||||
|
||||
type ButtonStyle struct {
|
||||
Background string `default:"" type:"bg"`
|
||||
Foreground string `default:"" type:"fg"`
|
||||
Outline string `default:"" type:"fg"`
|
||||
|
||||
ActiveBackground string `default:"" type:"bg"`
|
||||
ActiveForeground string `default:"\x1b[34m" type:"fg"`
|
||||
ActiveOutline string `default:"" type:"fg"`
|
||||
|
||||
ClickedBackground string `default:"" type:"bg"`
|
||||
ClickedForeground string `default:"\x1b[32m" type:"fg"`
|
||||
ClickedOutline string `default:"" type:"fg"`
|
||||
}
|
||||
|
||||
type ButtonComponent struct {
|
||||
Data osui.ComponentData
|
||||
Style *ButtonStyle
|
||||
Text string
|
||||
Toggle bool
|
||||
Clicked bool
|
||||
on_click func(*ButtonComponent) bool
|
||||
Data osui.ComponentData
|
||||
Text string
|
||||
clicked bool
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Render() string {
|
||||
osui.UseStyle(b.Style)
|
||||
b.Data.Style.UseStyle()
|
||||
|
||||
if b.Clicked {
|
||||
if b.clicked {
|
||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||
colors.Reset+b.Style.ClickedOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Style.ClickedOutline,
|
||||
colors.Reset+colors.Combine(b.Style.ClickedBackground, b.Style.ClickedForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Style.ClickedOutline,
|
||||
colors.Reset+b.Data.Style.ClickedOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.Style.ClickedOutline,
|
||||
colors.Reset+colors.Combine(b.Data.Style.ClickedBackground, b.Data.Style.ClickedForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Data.Style.ClickedOutline,
|
||||
colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Style.ClickedOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.Style.ClickedOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
if b.Data.IsActive {
|
||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||
colors.Reset+b.Style.ActiveOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Style.ActiveOutline,
|
||||
colors.Reset+colors.Combine(b.Style.ActiveBackground, b.Style.ActiveForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Style.ActiveOutline,
|
||||
colors.Reset+b.Data.Style.ActiveOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.Style.ActiveOutline,
|
||||
colors.Reset+colors.Combine(b.Data.Style.ActiveBackground, b.Data.Style.ActiveForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Data.Style.ActiveOutline,
|
||||
colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Style.ActiveOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.Style.ActiveOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||
colors.Reset+b.Style.Outline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Style.Outline,
|
||||
colors.Reset+colors.Combine(b.Style.Background, b.Style.Foreground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Style.Outline,
|
||||
colors.Reset+b.Data.Style.Outline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.Style.Outline,
|
||||
colors.Reset+colors.Combine(b.Data.Style.Background, b.Data.Style.Foreground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Data.Style.Outline,
|
||||
colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Style.Outline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.Style.Outline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Update(key string) bool {
|
||||
if isKey.Enter(key) {
|
||||
if !b.on_click(b) {
|
||||
if b.Toggle {
|
||||
b.Clicked = !b.Clicked
|
||||
return false
|
||||
}
|
||||
b.Clicked = true
|
||||
b.Data.Screen.Render()
|
||||
time.Sleep(time.Millisecond * 120)
|
||||
b.Clicked = false
|
||||
b.Data.OnClick()
|
||||
if b.Data.Toggle {
|
||||
b.clicked = !b.clicked
|
||||
return false
|
||||
}
|
||||
b.clicked = true
|
||||
b.Data.Screen.Render()
|
||||
time.Sleep(time.Millisecond * 120)
|
||||
b.clicked = false
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -92,22 +67,12 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
|
||||
return &b.Data
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Params(param ButtonParams) *ButtonComponent {
|
||||
b.Style = osui.SetDefaults(¶m.Style).(*ButtonStyle)
|
||||
if param.OnClick != nil {
|
||||
b.on_click = param.OnClick
|
||||
}
|
||||
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
|
||||
return b
|
||||
}
|
||||
|
||||
func Button(text string) *ButtonComponent {
|
||||
return &ButtonComponent{Text: text,
|
||||
Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle),
|
||||
func Button(param osui.Param, text string) *ButtonComponent {
|
||||
return param.UseParam(&ButtonComponent{Text: text,
|
||||
Data: osui.ComponentData{
|
||||
Width: 20,
|
||||
Height: 1,
|
||||
},
|
||||
on_click: func(bc *ButtonComponent) bool { return false },
|
||||
}
|
||||
clicked: false,
|
||||
}).(*ButtonComponent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user