github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/dos/ui/button.go (about)

     1  package ui
     2  
     3  import termbox "github.com/nsf/termbox-go"
     4  
     5  type Button struct {
     6  	Caption string
     7  	Click   func(form *Form)
     8  }
     9  
    10  func (button *Button) Focusable() bool { return true }
    11  
    12  func (button *Button) Handle(form *Form, action Action) {
    13  	switch action := action.(type) {
    14  	case KeyPress:
    15  		switch action.Key {
    16  		case termbox.KeyEnter:
    17  			button.Click(form)
    18  		}
    19  	}
    20  }
    21  
    22  func (button *Button) Unserialize(data []byte) error {
    23  	button.Caption = string(data)
    24  	return nil
    25  }
    26  func (button *Button) Serialize() ([]byte, error) {
    27  	return []byte(button.Caption), nil
    28  }
    29  
    30  func (button *Button) Render(form *Form, component *Component) {
    31  	form.DrawText(component.Rect, button.Caption, component.Focused)
    32  }