2017-12-16 06:03:01 +08:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
2020-10-18 20:15:57 +08:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-16 06:03:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Button is labeled box that triggers an action when selected.
|
2018-01-07 23:39:06 +08:00
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/Button for an example.
|
2017-12-16 06:03:01 +08:00
|
|
|
type Button struct {
|
2017-12-17 05:48:26 +08:00
|
|
|
*Box
|
2017-12-16 06:03:01 +08:00
|
|
|
|
|
|
|
// The text to be displayed before the input area.
|
|
|
|
label string
|
|
|
|
|
|
|
|
// The label color.
|
|
|
|
labelColor tcell.Color
|
|
|
|
|
|
|
|
// The label color when the button is in focus.
|
|
|
|
labelColorActivated tcell.Color
|
|
|
|
|
|
|
|
// The background color when the button is in focus.
|
|
|
|
backgroundColorActivated tcell.Color
|
|
|
|
|
|
|
|
// An optional function which is called when the button was selected.
|
|
|
|
selected func()
|
|
|
|
|
|
|
|
// An optional function which is called when the user leaves the button. A
|
|
|
|
// key is provided indicating which key was pressed to leave (tab or backtab).
|
|
|
|
blur func(tcell.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewButton returns a new input field.
|
|
|
|
func NewButton(label string) *Button {
|
2018-01-10 16:43:32 +08:00
|
|
|
box := NewBox().SetBackgroundColor(Styles.ContrastBackgroundColor)
|
2019-03-19 19:12:40 +08:00
|
|
|
box.SetRect(0, 0, TaggedStringWidth(label)+4, 1)
|
2017-12-16 06:03:01 +08:00
|
|
|
return &Button{
|
2017-12-17 05:48:26 +08:00
|
|
|
Box: box,
|
2017-12-16 06:03:01 +08:00
|
|
|
label: label,
|
2018-01-10 16:43:32 +08:00
|
|
|
labelColor: Styles.PrimaryTextColor,
|
|
|
|
labelColorActivated: Styles.InverseTextColor,
|
|
|
|
backgroundColorActivated: Styles.PrimaryTextColor,
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabel sets the button text.
|
|
|
|
func (b *Button) SetLabel(label string) *Button {
|
|
|
|
b.label = label
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabel returns the button text.
|
|
|
|
func (b *Button) GetLabel() string {
|
|
|
|
return b.label
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabelColor sets the color of the button text.
|
|
|
|
func (b *Button) SetLabelColor(color tcell.Color) *Button {
|
|
|
|
b.labelColor = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabelColorActivated sets the color of the button text when the button is
|
|
|
|
// in focus.
|
|
|
|
func (b *Button) SetLabelColorActivated(color tcell.Color) *Button {
|
|
|
|
b.labelColorActivated = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBackgroundColorActivated sets the background color of the button text when
|
|
|
|
// the button is in focus.
|
|
|
|
func (b *Button) SetBackgroundColorActivated(color tcell.Color) *Button {
|
|
|
|
b.backgroundColorActivated = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSelectedFunc sets a handler which is called when the button was selected.
|
|
|
|
func (b *Button) SetSelectedFunc(handler func()) *Button {
|
|
|
|
b.selected = handler
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBlurFunc sets a handler which is called when the user leaves the button.
|
|
|
|
// The callback function is provided with the key that was pressed, which is one
|
|
|
|
// of the following:
|
|
|
|
//
|
|
|
|
// - KeyEscape: Leaving the button with no specific direction.
|
|
|
|
// - KeyTab: Move to the next field.
|
|
|
|
// - KeyBacktab: Move to the previous field.
|
|
|
|
func (b *Button) SetBlurFunc(handler func(key tcell.Key)) *Button {
|
|
|
|
b.blur = handler
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (b *Button) Draw(screen tcell.Screen) {
|
|
|
|
// Draw the box.
|
2020-10-18 20:15:57 +08:00
|
|
|
borderColor := b.GetBorderColor()
|
|
|
|
backgroundColor := b.GetBackgroundColor()
|
2017-12-17 05:48:26 +08:00
|
|
|
if b.focus.HasFocus() {
|
2020-10-18 20:15:57 +08:00
|
|
|
b.SetBackgroundColor(b.backgroundColorActivated)
|
|
|
|
b.SetBorderColor(b.labelColorActivated)
|
2017-12-27 23:04:21 +08:00
|
|
|
defer func() {
|
2020-10-18 20:15:57 +08:00
|
|
|
b.SetBorderColor(borderColor)
|
2017-12-27 23:04:21 +08:00
|
|
|
}()
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
b.Box.Draw(screen)
|
|
|
|
b.backgroundColor = backgroundColor
|
|
|
|
|
|
|
|
// Draw label.
|
2017-12-22 01:08:53 +08:00
|
|
|
x, y, width, height := b.GetInnerRect()
|
2017-12-27 23:04:21 +08:00
|
|
|
if width > 0 && height > 0 {
|
|
|
|
y = y + height/2
|
|
|
|
labelColor := b.labelColor
|
|
|
|
if b.focus.HasFocus() {
|
|
|
|
labelColor = b.labelColorActivated
|
|
|
|
}
|
|
|
|
Print(screen, b.label, x, y, width, AlignCenter, labelColor)
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-03-20 04:25:30 +08:00
|
|
|
return b.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2017-12-16 06:03:01 +08:00
|
|
|
// Process key event.
|
|
|
|
switch key := event.Key(); key {
|
|
|
|
case tcell.KeyEnter: // Selected.
|
|
|
|
if b.selected != nil {
|
|
|
|
b.selected()
|
|
|
|
}
|
|
|
|
case tcell.KeyBacktab, tcell.KeyTab, tcell.KeyEscape: // Leave. No action.
|
|
|
|
if b.blur != nil {
|
|
|
|
b.blur(key)
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 04:29:34 +08:00
|
|
|
})
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
2019-11-04 13:34:46 +08:00
|
|
|
|
2019-11-04 14:30:25 +08:00
|
|
|
// MouseHandler returns the mouse handler for this primitive.
|
2020-02-14 10:09:09 +08:00
|
|
|
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) {
|
2020-01-25 04:40:34 +08:00
|
|
|
if !b.InRect(event.Position()) {
|
2020-02-14 10:09:09 +08:00
|
|
|
return false, nil
|
2020-01-25 04:40:34 +08:00
|
|
|
}
|
2020-03-28 01:41:44 +08:00
|
|
|
|
2019-11-04 13:34:46 +08:00
|
|
|
// Process mouse event.
|
2020-02-28 09:19:36 +08:00
|
|
|
if action == MouseLeftClick {
|
2020-03-28 01:41:44 +08:00
|
|
|
setFocus(b)
|
2019-11-04 13:34:46 +08:00
|
|
|
if b.selected != nil {
|
|
|
|
b.selected()
|
|
|
|
}
|
2020-03-28 01:41:44 +08:00
|
|
|
consumed = true
|
2019-11-04 13:34:46 +08:00
|
|
|
}
|
2020-03-28 01:41:44 +08:00
|
|
|
|
|
|
|
return
|
2019-11-04 13:34:46 +08:00
|
|
|
})
|
|
|
|
}
|