mirror of https://github.com/rivo/tview
Added Checkbox, Modal, and Pages.
parent
2bd80aa513
commit
dad7891c89
@ -0,0 +1,175 @@
|
||||
package tview
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// Checkbox is a one-line box (three lines if there is a title) where the
|
||||
// user can enter text.
|
||||
type Checkbox struct {
|
||||
*Box
|
||||
|
||||
// Whether or not this box is checked.
|
||||
checked bool
|
||||
|
||||
// The text to be displayed before the input area.
|
||||
label string
|
||||
|
||||
// The label color.
|
||||
labelColor tcell.Color
|
||||
|
||||
// The background color of the input area.
|
||||
fieldBackgroundColor tcell.Color
|
||||
|
||||
// The text color of the input area.
|
||||
fieldTextColor tcell.Color
|
||||
|
||||
// An optional function which is called when the user changes the checked
|
||||
// state of this checkbox.
|
||||
changed func(checked bool)
|
||||
|
||||
// An optional function which is called when the user indicated that they
|
||||
// are done entering text. The key which was pressed is provided (tab,
|
||||
// shift-tab, or escape).
|
||||
done func(tcell.Key)
|
||||
}
|
||||
|
||||
// NewCheckbox returns a new input field.
|
||||
func NewCheckbox() *Checkbox {
|
||||
return &Checkbox{
|
||||
Box: NewBox(),
|
||||
labelColor: tcell.ColorYellow,
|
||||
fieldBackgroundColor: tcell.ColorBlue,
|
||||
fieldTextColor: tcell.ColorWhite,
|
||||
}
|
||||
}
|
||||
|
||||
// SetChecked sets the state of the checkbox.
|
||||
func (c *Checkbox) SetChecked(checked bool) *Checkbox {
|
||||
c.checked = checked
|
||||
return c
|
||||
}
|
||||
|
||||
// SetLabel sets the text to be displayed before the input area.
|
||||
func (c *Checkbox) SetLabel(label string) *Checkbox {
|
||||
c.label = label
|
||||
return c
|
||||
}
|
||||
|
||||
// GetLabel returns the text to be displayed before the input area.
|
||||
func (c *Checkbox) GetLabel() string {
|
||||
return c.label
|
||||
}
|
||||
|
||||
// SetLabelColor sets the color of the label.
|
||||
func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
|
||||
c.labelColor = color
|
||||
return c
|
||||
}
|
||||
|
||||
// SetFieldBackgroundColor sets the background color of the input area.
|
||||
func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox {
|
||||
c.fieldBackgroundColor = color
|
||||
return c
|
||||
}
|
||||
|
||||
// SetFieldTextColor sets the text color of the input area.
|
||||
func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
|
||||
c.fieldTextColor = color
|
||||
return c
|
||||
}
|
||||
|
||||
// SetFormAttributes sets attributes shared by all form items.
|
||||
func (c *Checkbox) SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
||||
c.label = label
|
||||
c.labelColor = labelColor
|
||||
c.backgroundColor = bgColor
|
||||
c.fieldTextColor = fieldTextColor
|
||||
c.fieldBackgroundColor = fieldBgColor
|
||||
return c
|
||||
}
|
||||
|
||||
// SetChangedFunc sets a handler which is called when the checked state of this
|
||||
// checkbox was changed by the user. The handler function receives the new
|
||||
// state.
|
||||
func (c *Checkbox) SetChangedFunc(handler func(checked bool)) *Checkbox {
|
||||
c.changed = handler
|
||||
return c
|
||||
}
|
||||
|
||||
// SetDoneFunc sets a handler which is called when the user is done entering
|
||||
// text. The callback function is provided with the key that was pressed, which
|
||||
// is one of the following:
|
||||
//
|
||||
// - KeyEscape: Abort text input.
|
||||
// - KeyTab: Move to the next field.
|
||||
// - KeyBacktab: Move to the previous field.
|
||||
func (c *Checkbox) SetDoneFunc(handler func(key tcell.Key)) *Checkbox {
|
||||
c.done = handler
|
||||
return c
|
||||
}
|
||||
|
||||
// SetFinishedFunc calls SetDoneFunc().
|
||||
func (c *Checkbox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
||||
return c.SetDoneFunc(handler)
|
||||
}
|
||||
|
||||
// Draw draws this primitive onto the screen.
|
||||
func (c *Checkbox) Draw(screen tcell.Screen) {
|
||||
c.Box.Draw(screen)
|
||||
|
||||
// Prepare
|
||||
x := c.x
|
||||
y := c.y
|
||||
rightLimit := x + c.width
|
||||
height := c.height
|
||||
if c.border {
|
||||
x++
|
||||
y++
|
||||
rightLimit -= 2
|
||||
height -= 2
|
||||
}
|
||||
if height < 1 || rightLimit <= x {
|
||||
return
|
||||
}
|
||||
|
||||
// Draw label.
|
||||
x += Print(screen, c.label, x, y, rightLimit-x, AlignLeft, c.labelColor)
|
||||
|
||||
// Draw checkbox.
|
||||
fieldStyle := tcell.StyleDefault.Background(c.fieldBackgroundColor).Foreground(c.fieldTextColor)
|
||||
if c.focus.HasFocus() {
|
||||
fieldStyle = fieldStyle.Background(c.fieldTextColor).Foreground(c.fieldBackgroundColor)
|
||||
}
|
||||
checkedRune := 'X'
|
||||
if !c.checked {
|
||||
checkedRune = ' '
|
||||
}
|
||||
screen.SetContent(x, y, checkedRune, nil, fieldStyle)
|
||||
|
||||
// Hide cursor.
|
||||
if c.focus.HasFocus() {
|
||||
screen.HideCursor()
|
||||
}
|
||||
}
|
||||
|
||||
// InputHandler returns the handler for this primitive.
|
||||
func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
return func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
// Process key event.
|
||||
switch key := event.Key(); key {
|
||||
case tcell.KeyRune, tcell.KeyEnter: // Check.
|
||||
if key == tcell.KeyRune && event.Rune() != ' ' {
|
||||
break
|
||||
}
|
||||
c.checked = !c.checked
|
||||
if c.changed != nil {
|
||||
c.changed(c.checked)
|
||||
}
|
||||
case tcell.KeyTab, tcell.KeyBacktab, tcell.KeyEscape: // We're done.
|
||||
if c.done != nil {
|
||||
c.done(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
/*
|
||||
Package tview implements primitives for terminal based applications. It uses
|
||||
github.com/gdamore/tcell.
|
||||
|
||||
No mouse input (yet).
|
||||
*/
|
||||
package tview
|
||||
|
@ -0,0 +1,121 @@
|
||||
package tview
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// Modal is a centered message window used to inform the user or prompt them
|
||||
// for an immediate decision. It needs to have at least one button (added via
|
||||
// AddButtons()) or it will never disappear.
|
||||
type Modal struct {
|
||||
*Frame
|
||||
|
||||
// The form embedded in the modal's frame.
|
||||
form *Form
|
||||
|
||||
// The message text (original, not word-wrapped).
|
||||
text string
|
||||
|
||||
// The text color.
|
||||
textColor tcell.Color
|
||||
|
||||
// The optional callback for when the user clicked one of the buttons. It
|
||||
// receives the index of the clicked button and the button's label.
|
||||
done func(buttonIndex int, buttonLabel string)
|
||||
}
|
||||
|
||||
// NewModal returns a new modal message window.
|
||||
func NewModal() *Modal {
|
||||
m := &Modal{
|
||||
textColor: tcell.ColorWhite,
|
||||
}
|
||||
m.form = NewForm().
|
||||
SetPadding(0, 0, 0, 0).
|
||||
SetButtonsAlign(AlignCenter).
|
||||
SetButtonBackgroundColor(tcell.ColorBlack).
|
||||
SetButtonTextColor(tcell.ColorWhite)
|
||||
m.form.SetBackgroundColor(tcell.ColorBlue)
|
||||
m.Frame = NewFrame(m.form)
|
||||
m.Box.SetBorder(true).SetBackgroundColor(tcell.ColorBlue)
|
||||
return m
|
||||
}
|
||||
|
||||
// SetTextColor sets the color of the message text.
|
||||
func (m *Modal) SetTextColor(color tcell.Color) *Modal {
|
||||
m.textColor = color
|
||||
return m
|
||||
}
|
||||
|
||||
// SetDoneFunc sets a handler which is called when one of the buttons was
|
||||
// pressed. It receives the index of the button as well as its label text. The
|
||||
// handler is also called when the user presses the Escape key. The index will
|
||||
// then be negative and the label text an emptry string.
|
||||
func (m *Modal) SetDoneFunc(handler func(buttonIndex int, buttonLabel string)) *Modal {
|
||||
m.done = handler
|
||||
return m
|
||||
}
|
||||
|
||||
// SetText sets the message text of the window. The text may contain line
|
||||
// breaks. Note that words are wrapped, too, based on the final size of the
|
||||
// window.
|
||||
func (m *Modal) SetText(text string) *Modal {
|
||||
m.text = text
|
||||
return m
|
||||
}
|
||||
|
||||
// AddButtons adds buttons to the window. There must be at least one button and
|
||||
// a "done" handler so the window can be closed again.
|
||||
func (m *Modal) AddButtons(labels []string) *Modal {
|
||||
for index, label := range labels {
|
||||
func(i int, l string) {
|
||||
m.form.AddButton(label, func() {
|
||||
if m.done != nil {
|
||||
m.done(i, l)
|
||||
}
|
||||
})
|
||||
}(index, label)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Focus is called when this primitive receives focus.
|
||||
func (m *Modal) Focus(delegate func(p Primitive)) {
|
||||
delegate(m.form)
|
||||
}
|
||||
|
||||
// HasFocus returns whether or not this primitive has focus.
|
||||
func (m *Modal) HasFocus() bool {
|
||||
return m.form.HasFocus()
|
||||
}
|
||||
|
||||
// Draw draws this primitive onto the screen.
|
||||
func (m *Modal) Draw(screen tcell.Screen) {
|
||||
// Calculate the width of this modal.
|
||||
buttonsWidth := 0
|
||||
for _, button := range m.form.buttons {
|
||||
buttonsWidth += len([]rune(button.label)) + 4 + 2
|
||||
}
|
||||
buttonsWidth -= 2
|
||||
screenWidth, screenHeight := screen.Size()
|
||||
width := screenWidth / 3
|
||||
if width < buttonsWidth {
|
||||
width = buttonsWidth
|
||||
}
|
||||
// width is now without the box border.
|
||||
|
||||
// Reset the text and find out how wide it is.
|
||||
m.Frame.ClearText()
|
||||
lines := WordWrap(m.text, width)
|
||||
for _, line := range lines {
|
||||
m.Frame.AddText(line, true, AlignCenter, m.textColor)
|
||||
}
|
||||
|
||||
// Set the modal's position and size.
|
||||
height := len(lines) + 6
|
||||
x := (screenWidth - width) / 2
|
||||
y := (screenHeight - height) / 2
|
||||
m.SetRect(x, y, width, height)
|
||||
|
||||
// Draw the frame.
|
||||
m.Frame.Draw(screen)
|
||||
}
|
@ -0,0 +1,169 @@
|
||||
package tview
|
||||
|
||||
import "github.com/gdamore/tcell"
|
||||
|
||||
// page represents one page of a Pages object.
|
||||
type page struct {
|
||||
Name string // The page's name.
|
||||
Item Primitive // The page's primitive.
|
||||
Visible bool // Whether or not this page is visible.
|
||||
}
|
||||
|
||||
// Pages is a container for other primitives often used as the application's
|
||||
// root primitive. It allows to easily switch the visibility of the contained
|
||||
// primitives.
|
||||
type Pages struct {
|
||||
*Box
|
||||
|
||||
// The contained pages.
|
||||
pages []*page
|
||||
|
||||
// An optional handler which is called whenever the visibility or the order of
|
||||
// pages changes.
|
||||
changed func()
|
||||
}
|
||||
|
||||
// NewPages returns a new Pages object.
|
||||
func NewPages() *Pages {
|
||||
p := &Pages{
|
||||
Box: NewBox(),
|
||||
}
|
||||
p.focus = p
|
||||
return p
|
||||
}
|
||||
|
||||
// SetChangedFunc sets a handler which is called whenever the visibility or the
|
||||
// order of any visible pages changes. This can be used to redraw the pages.
|
||||
func (p *Pages) SetChangedFunc(handler func()) *Pages {
|
||||
p.changed = handler
|
||||
return p
|
||||
}
|
||||
|
||||
// AddPage adds a new page with the given name and primitive. Leaving the name
|
||||
// empty or using the same name for multiple items may cause conflicts in other
|
||||
// functions.
|
||||
//
|
||||
// Visible pages will be drawn in the order they were added (unless that order
|
||||
// was changed in one of the other functions).
|
||||
func (p *Pages) AddPage(name string, item Primitive, visible bool) *Pages {
|
||||
p.pages = append(p.pages, &page{Item: item, Name: name, Visible: true})
|
||||
if p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// RemovePage removes the page with the given name.
|
||||
func (p *Pages) RemovePage(name string) *Pages {
|
||||
for index, page := range p.pages {
|
||||
if page.Name == name {
|
||||
p.pages = append(p.pages[:index], p.pages[index+1:]...)
|
||||
if page.Visible && p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// ShowPage sets a page's visibility to "true" (in addition to any other pages
|
||||
// which are already visible).
|
||||
func (p *Pages) ShowPage(name string) *Pages {
|
||||
for _, page := range p.pages {
|
||||
if page.Name == name {
|
||||
page.Visible = true
|
||||
if p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// HidePage sets a page's visibility to "false".
|
||||
func (p *Pages) HidePage(name string) *Pages {
|
||||
for _, page := range p.pages {
|
||||
if page.Name == name {
|
||||
page.Visible = false
|
||||
if p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// SwitchToPage sets a page's visibility to "true" and all other pages'
|
||||
// visibility to "false".
|
||||
func (p *Pages) SwitchToPage(name string) *Pages {
|
||||
for _, page := range p.pages {
|
||||
if page.Name == name {
|
||||
page.Visible = true
|
||||
} else {
|
||||
page.Visible = false
|
||||
}
|
||||
}
|
||||
if p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// SendToFront changes the order of the pages such that the page with the given
|
||||
// name comes last, causing it to be drawn last with the next update (if
|
||||
// visible).
|
||||
func (p *Pages) SendToFront(name string) *Pages {
|
||||
for index, page := range p.pages {
|
||||
if page.Name == name {
|
||||
if index < len(p.pages)-1 {
|
||||
p.pages = append(append(p.pages[:index], p.pages[index+1:]...), page)
|
||||
}
|
||||
if page.Visible && p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// SendToBack changes the order of the pages such that the page with the given
|
||||
// name comes first, causing it to be drawn first with the next update (if
|
||||
// visible).
|
||||
func (p *Pages) SendToBack(name string) *Pages {
|
||||
for index, pg := range p.pages {
|
||||
if pg.Name == name {
|
||||
if index > 0 {
|
||||
p.pages = append(append([]*page{pg}, p.pages[:index]...), p.pages[index+1:]...)
|
||||
}
|
||||
if pg.Visible && p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// HasFocus returns whether or not this primitive has focus.
|
||||
func (p *Pages) HasFocus() bool {
|
||||
for _, page := range p.pages {
|
||||
if page.Item.GetFocusable().HasFocus() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Draw draws this primitive onto the screen.
|
||||
func (p *Pages) Draw(screen tcell.Screen) {
|
||||
for _, page := range p.pages {
|
||||
if !page.Visible {
|
||||
continue
|
||||
}
|
||||
page.Item.Draw(screen)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue