package cview
import (
"sync"
"github.com/gdamore/tcell/v2"
)
type Modal struct {
*Box
frame *Frame
form *Form
text string
textColor tcell .Color
textAlign int
done func (buttonIndex int , buttonLabel string )
sync .RWMutex
}
func NewModal () *Modal {
m := &Modal {
Box : NewBox (),
textColor : Styles .PrimaryTextColor ,
textAlign : AlignCenter ,
}
m .form = NewForm ()
m .form .SetButtonsAlign (AlignCenter )
m .form .SetPadding (0 , 0 , 0 , 0 )
m .form .SetCancelFunc (func () {
if m .done != nil {
m .done (-1 , "" )
}
})
m .frame = NewFrame (m .form )
m .frame .SetBorder (true )
m .frame .SetBorders (0 , 0 , 1 , 0 , 0 , 0 )
m .frame .SetPadding (1 , 1 , 1 , 1 )
m .focus = m
return m
}
func (m *Modal ) SetBackgroundColor (color tcell .Color ) {
m .Lock ()
defer m .Unlock ()
m .form .SetBackgroundColor (color )
m .frame .SetBackgroundColor (color )
}
func (m *Modal ) SetTextColor (color tcell .Color ) {
m .Lock ()
defer m .Unlock ()
m .textColor = color
}
func (m *Modal ) SetButtonBackgroundColor (color tcell .Color ) {
m .Lock ()
defer m .Unlock ()
m .form .SetButtonBackgroundColor (color )
}
func (m *Modal ) SetButtonTextColor (color tcell .Color ) {
m .Lock ()
defer m .Unlock ()
m .form .SetButtonTextColor (color )
}
func (m *Modal ) SetButtonsAlign (align int ) {
m .Lock ()
defer m .Unlock ()
m .form .SetButtonsAlign (align )
}
func (m *Modal ) SetDoneFunc (handler func (buttonIndex int , buttonLabel string )) {
m .Lock ()
defer m .Unlock ()
m .done = handler
}
func (m *Modal ) SetText (text string ) {
m .Lock ()
defer m .Unlock ()
m .text = text
}
func (m *Modal ) SetTextAlign (align int ) {
m .Lock ()
defer m .Unlock ()
m .textAlign = align
}
func (m *Modal ) GetForm () *Form {
m .RLock ()
defer m .RUnlock ()
return m .form
}
func (m *Modal ) GetFrame () *Frame {
m .RLock ()
defer m .RUnlock ()
return m .frame
}
func (m *Modal ) AddButtons (labels []string ) {
m .Lock ()
defer m .Unlock ()
for index , label := range labels {
func (i int , l string ) {
m .form .AddButton (label , func () {
if m .done != nil {
m .done (i , l )
}
})
button := m .form .GetButton (m .form .GetButtonCount () - 1 )
button .SetInputCapture (func (event *tcell .EventKey ) *tcell .EventKey {
switch event .Key () {
case tcell .KeyDown , tcell .KeyRight :
return tcell .NewEventKey (tcell .KeyTab , 0 , tcell .ModNone )
case tcell .KeyUp , tcell .KeyLeft :
return tcell .NewEventKey (tcell .KeyBacktab , 0 , tcell .ModNone )
}
return event
})
}(index , label )
}
}
func (m *Modal ) ClearButtons () {
m .Lock ()
defer m .Unlock ()
m .form .ClearButtons ()
}
func (m *Modal ) SetFocus (index int ) {
m .Lock ()
defer m .Unlock ()
m .form .SetFocus (index )
}
func (m *Modal ) Focus (delegate func (p Primitive )) {
delegate (m .form )
}
func (m *Modal ) HasFocus () bool {
return m .GetForm ().HasFocus ()
}
func (m *Modal ) Draw (screen tcell .Screen ) {
if !m .GetVisible () {
return
}
formItemCount := m .form .GetFormItemCount ()
m .Lock ()
defer m .Unlock ()
buttonsWidth := 0
for _ , button := range m .form .buttons {
buttonsWidth += TaggedTextWidth (button .label ) + 4 + 2
}
buttonsWidth -= 2
screenWidth , screenHeight := screen .Size ()
width := screenWidth / 3
if width < buttonsWidth {
width = buttonsWidth
}
m .frame .Clear ()
lines := WordWrap (m .text , width )
for _ , line := range lines {
m .frame .AddText (line , true , m .textAlign , m .textColor )
}
height := len (lines ) + (formItemCount * 2 ) + 6
width += 4
x := (screenWidth - width ) / 2
y := (screenHeight - height ) / 2
m .SetRect (x , y , width , height )
m .frame .SetRect (x , y , width , height )
m .frame .Draw (screen )
}
func (m *Modal ) MouseHandler () func (action MouseAction , event *tcell .EventMouse , setFocus func (p Primitive )) (consumed bool , capture Primitive ) {
return m .WrapMouseHandler (func (action MouseAction , event *tcell .EventMouse , setFocus func (p Primitive )) (consumed bool , capture Primitive ) {
consumed , capture = m .form .MouseHandler ()(action , event , setFocus )
if !consumed && action == MouseLeftClick && m .InRect (event .Position ()) {
setFocus (m )
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 .