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
|
|
|
|
2023-03-20 14:26:01 +08:00
|
|
|
// If set to true, the button cannot be activated.
|
|
|
|
disabled bool
|
|
|
|
|
2022-12-18 02:20:43 +08:00
|
|
|
// The text to be displayed inside the button.
|
|
|
|
text string
|
2017-12-16 06:03:01 +08:00
|
|
|
|
2022-12-18 02:20:43 +08:00
|
|
|
// The button's style (when deactivated).
|
|
|
|
style tcell.Style
|
2017-12-16 06:03:01 +08:00
|
|
|
|
2022-12-18 02:20:43 +08:00
|
|
|
// The button's style (when activated).
|
|
|
|
activatedStyle tcell.Style
|
2017-12-16 06:03:01 +08:00
|
|
|
|
2023-03-20 14:26:01 +08:00
|
|
|
// The button's style (when disabled).
|
|
|
|
disabledStyle tcell.Style
|
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
// 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
|
2021-11-08 23:35:21 +08:00
|
|
|
// key is provided indicating which key was pressed to leave (tab or
|
|
|
|
// backtab).
|
|
|
|
exit func(tcell.Key)
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewButton returns a new input field.
|
|
|
|
func NewButton(label string) *Button {
|
2022-12-18 02:20:43 +08:00
|
|
|
box := NewBox()
|
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{
|
2022-12-18 02:20:43 +08:00
|
|
|
Box: box,
|
|
|
|
text: label,
|
|
|
|
style: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
|
|
|
|
activatedStyle: tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.InverseTextColor),
|
2023-03-20 14:26:01 +08:00
|
|
|
disabledStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.ContrastSecondaryTextColor),
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabel sets the button text.
|
|
|
|
func (b *Button) SetLabel(label string) *Button {
|
2022-12-18 02:20:43 +08:00
|
|
|
b.text = label
|
2017-12-16 06:03:01 +08:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabel returns the button text.
|
|
|
|
func (b *Button) GetLabel() string {
|
2022-12-18 02:20:43 +08:00
|
|
|
return b.text
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabelColor sets the color of the button text.
|
|
|
|
func (b *Button) SetLabelColor(color tcell.Color) *Button {
|
2022-12-18 02:20:43 +08:00
|
|
|
b.style = b.style.Foreground(color)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStyle sets the style of the button used when it is not focused.
|
|
|
|
func (b *Button) SetStyle(style tcell.Style) *Button {
|
|
|
|
b.style = style
|
2017-12-16 06:03:01 +08:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabelColorActivated sets the color of the button text when the button is
|
|
|
|
// in focus.
|
|
|
|
func (b *Button) SetLabelColorActivated(color tcell.Color) *Button {
|
2022-12-18 02:20:43 +08:00
|
|
|
b.activatedStyle = b.activatedStyle.Foreground(color)
|
2017-12-16 06:03:01 +08:00
|
|
|
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 {
|
2022-12-18 02:20:43 +08:00
|
|
|
b.activatedStyle = b.activatedStyle.Background(color)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetActivatedStyle sets the style of the button used when it is focused.
|
|
|
|
func (b *Button) SetActivatedStyle(style tcell.Style) *Button {
|
|
|
|
b.activatedStyle = style
|
2017-12-16 06:03:01 +08:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:26:01 +08:00
|
|
|
// SetDisabledStyle sets the style of the button used when it is disabled.
|
|
|
|
func (b *Button) SetDisabledStyle(style tcell.Style) *Button {
|
|
|
|
b.disabledStyle = style
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDisabled sets whether or not the button is disabled. Disabled buttons
|
|
|
|
// cannot be activated.
|
|
|
|
//
|
|
|
|
// If the button is part of a form, you should set focus to the form itself
|
|
|
|
// after calling this function to set focus to the next non-disabled form item.
|
|
|
|
func (b *Button) SetDisabled(disabled bool) *Button {
|
|
|
|
b.disabled = disabled
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDisabled returns whether or not the button is disabled.
|
|
|
|
func (b *Button) IsDisabled() bool {
|
|
|
|
return b.disabled
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
// SetSelectedFunc sets a handler which is called when the button was selected.
|
|
|
|
func (b *Button) SetSelectedFunc(handler func()) *Button {
|
|
|
|
b.selected = handler
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:35:21 +08:00
|
|
|
// SetExitFunc sets a handler which is called when the user leaves the button.
|
2017-12-16 06:03:01 +08:00
|
|
|
// 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.
|
2021-11-08 23:35:21 +08:00
|
|
|
func (b *Button) SetExitFunc(handler func(key tcell.Key)) *Button {
|
|
|
|
b.exit = handler
|
2017-12-16 06:03:01 +08:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (b *Button) Draw(screen tcell.Screen) {
|
|
|
|
// Draw the box.
|
2022-12-18 02:20:43 +08:00
|
|
|
style := b.style
|
2023-03-20 14:26:01 +08:00
|
|
|
if b.disabled {
|
|
|
|
style = b.disabledStyle
|
|
|
|
}
|
2022-12-18 02:20:43 +08:00
|
|
|
_, backgroundColor, _ := style.Decompose()
|
2023-03-20 14:26:01 +08:00
|
|
|
if b.HasFocus() && !b.disabled {
|
2022-12-18 02:20:43 +08:00
|
|
|
style = b.activatedStyle
|
|
|
|
_, backgroundColor, _ = style.Decompose()
|
|
|
|
|
|
|
|
// Highlight button for one drawing cycle.
|
|
|
|
borderColor := b.GetBorderColor()
|
|
|
|
b.SetBorderColor(backgroundColor)
|
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
|
|
|
}
|
2022-12-18 02:20:43 +08:00
|
|
|
b.SetBackgroundColor(backgroundColor)
|
2020-11-18 02:33:25 +08:00
|
|
|
b.Box.DrawForSubclass(screen, b)
|
2017-12-16 06:03:01 +08:00
|
|
|
|
|
|
|
// 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
|
2022-12-18 02:20:43 +08:00
|
|
|
printWithStyle(screen, b.text, x, y, 0, width, AlignCenter, style, true)
|
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)) {
|
2023-03-20 14:26:01 +08:00
|
|
|
if b.disabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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.
|
2021-11-08 23:35:21 +08:00
|
|
|
if b.exit != nil {
|
|
|
|
b.exit(key)
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2023-03-20 14:26:01 +08:00
|
|
|
if b.disabled {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2022-09-02 23:49:12 +08:00
|
|
|
if action == MouseLeftDown {
|
2020-03-28 01:41:44 +08:00
|
|
|
setFocus(b)
|
2022-09-02 23:49:12 +08:00
|
|
|
consumed = true
|
|
|
|
} else if action == MouseLeftClick {
|
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
|
|
|
})
|
|
|
|
}
|