package cview
import (
"sync"
"github.com/gdamore/tcell/v2"
)
type Button struct {
*Box
label []byte
labelColor tcell .Color
labelColorFocused tcell .Color
backgroundColorFocused tcell .Color
selected func ()
blur func (tcell .Key )
cursorRune rune
sync .RWMutex
}
func NewButton (label string ) *Button {
box := NewBox ()
box .SetRect (0 , 0 , TaggedStringWidth (label )+4 , 1 )
box .SetBackgroundColor (Styles .MoreContrastBackgroundColor )
return &Button {
Box : box ,
label : []byte (label ),
labelColor : Styles .PrimaryTextColor ,
labelColorFocused : Styles .PrimaryTextColor ,
cursorRune : Styles .ButtonCursorRune ,
backgroundColorFocused : Styles .ContrastBackgroundColor ,
}
}
func (b *Button ) SetLabel (label string ) {
b .Lock ()
defer b .Unlock ()
b .label = []byte (label )
}
func (b *Button ) GetLabel () string {
b .RLock ()
defer b .RUnlock ()
return string (b .label )
}
func (b *Button ) SetLabelColor (color tcell .Color ) {
b .Lock ()
defer b .Unlock ()
b .labelColor = color
}
func (b *Button ) SetLabelColorFocused (color tcell .Color ) {
b .Lock ()
defer b .Unlock ()
b .labelColorFocused = color
}
func (b *Button ) SetCursorRune (rune rune ) {
b .Lock ()
defer b .Unlock ()
b .cursorRune = rune
}
func (b *Button ) SetBackgroundColorFocused (color tcell .Color ) {
b .Lock ()
defer b .Unlock ()
b .backgroundColorFocused = color
}
func (b *Button ) SetSelectedFunc (handler func ()) {
b .Lock ()
defer b .Unlock ()
b .selected = handler
}
func (b *Button ) SetBlurFunc (handler func (key tcell .Key )) {
b .Lock ()
defer b .Unlock ()
b .blur = handler
}
func (b *Button ) Draw (screen tcell .Screen ) {
if !b .GetVisible () {
return
}
b .Lock ()
defer b .Unlock ()
borderColor := b .borderColor
backgroundColor := b .backgroundColor
if b .focus .HasFocus () {
b .backgroundColor = b .backgroundColorFocused
b .borderColor = b .labelColorFocused
defer func () {
b .borderColor = borderColor
}()
}
b .Unlock ()
b .Box .Draw (screen )
b .Lock ()
b .backgroundColor = backgroundColor
x , y , width , height := b .GetInnerRect ()
if width > 0 && height > 0 {
y = y + height /2
labelColor := b .labelColor
if b .focus .HasFocus () {
labelColor = b .labelColorFocused
}
_ , pw := Print (screen , b .label , x , y , width , AlignCenter , labelColor )
if b .focus .HasFocus () && b .cursorRune != 0 {
cursorX := x + int (float64 (width )/2 +float64 (pw )/2 )
if cursorX > x +width -1 {
cursorX = x + width - 1
} else if cursorX < x +width {
cursorX ++
}
Print (screen , []byte (string (b .cursorRune )), cursorX , y , width , AlignLeft , labelColor )
}
}
}
func (b *Button ) InputHandler () func (event *tcell .EventKey , setFocus func (p Primitive )) {
return b .WrapInputHandler (func (event *tcell .EventKey , setFocus func (p Primitive )) {
if HitShortcut (event , Keys .Select , Keys .Select2 ) {
if b .selected != nil {
b .selected ()
}
} else if HitShortcut (event , Keys .Cancel , Keys .MovePreviousField , Keys .MoveNextField ) {
if b .blur != nil {
b .blur (event .Key ())
}
}
})
}
func (b *Button ) MouseHandler () func (action MouseAction , event *tcell .EventMouse , setFocus func (p Primitive )) (consumed bool , capture Primitive ) {
return b .WrapMouseHandler (func (action MouseAction , event *tcell .EventMouse , setFocus func (p Primitive )) (consumed bool , capture Primitive ) {
if !b .InRect (event .Position ()) {
return false , nil
}
if action == MouseLeftClick {
setFocus (b )
if b .selected != nil {
b .selected ()
}
consumed = true
}
return
})
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .