...

Source file src/gitlab.com/tslocum/cview/modal.go

Documentation: gitlab.com/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/gdamore/tcell/v2"
     7  )
     8  
     9  // Modal is a centered message window used to inform the user or prompt them
    10  // for an immediate decision. It needs to have at least one button (added via
    11  // AddButtons) or it will never disappear. You may change the title and
    12  // appearance of the window by modifying the Frame returned by GetFrame. You
    13  // may include additional elements within the window by modifying the Form
    14  // returned by GetForm.
    15  type Modal struct {
    16  	*Box
    17  
    18  	// The Frame embedded in the Modal.
    19  	frame *Frame
    20  
    21  	// The Form embedded in the Modal's Frame.
    22  	form *Form
    23  
    24  	// The message text (original, not word-wrapped).
    25  	text string
    26  
    27  	// The text color.
    28  	textColor tcell.Color
    29  
    30  	// The optional callback for when the user clicked one of the buttons. It
    31  	// receives the index of the clicked button and the button's label.
    32  	done func(buttonIndex int, buttonLabel string)
    33  
    34  	sync.RWMutex
    35  }
    36  
    37  // NewModal returns a new centered message window.
    38  func NewModal() *Modal {
    39  	m := &Modal{
    40  		Box:       NewBox(),
    41  		textColor: Styles.PrimaryTextColor,
    42  	}
    43  
    44  	m.form = NewForm()
    45  	m.form.SetButtonsAlign(AlignCenter)
    46  	m.form.SetButtonBackgroundColor(Styles.PrimitiveBackgroundColor)
    47  	m.form.SetButtonTextColor(Styles.PrimaryTextColor)
    48  	m.form.SetBackgroundColor(Styles.ContrastBackgroundColor)
    49  	m.form.SetPadding(0, 0, 0, 0)
    50  	m.form.SetCancelFunc(func() {
    51  		if m.done != nil {
    52  			m.done(-1, "")
    53  		}
    54  	})
    55  
    56  	m.frame = NewFrame(m.form)
    57  	m.frame.SetBorder(true)
    58  	m.frame.SetBorders(0, 0, 1, 0, 0, 0)
    59  	m.frame.SetBackgroundColor(Styles.ContrastBackgroundColor)
    60  	m.frame.SetPadding(1, 1, 1, 1)
    61  
    62  	m.focus = m
    63  	return m
    64  }
    65  
    66  // SetBackgroundColor sets the color of the Modal Frame background.
    67  func (m *Modal) SetBackgroundColor(color tcell.Color) {
    68  	m.Lock()
    69  	defer m.Unlock()
    70  
    71  	m.form.SetBackgroundColor(color)
    72  	m.frame.SetBackgroundColor(color)
    73  }
    74  
    75  // SetTextColor sets the color of the message text.
    76  func (m *Modal) SetTextColor(color tcell.Color) {
    77  	m.Lock()
    78  	defer m.Unlock()
    79  
    80  	m.textColor = color
    81  }
    82  
    83  // SetButtonBackgroundColor sets the background color of the buttons.
    84  func (m *Modal) SetButtonBackgroundColor(color tcell.Color) {
    85  	m.Lock()
    86  	defer m.Unlock()
    87  
    88  	m.form.SetButtonBackgroundColor(color)
    89  }
    90  
    91  // SetButtonTextColor sets the color of the button texts.
    92  func (m *Modal) SetButtonTextColor(color tcell.Color) {
    93  	m.Lock()
    94  	defer m.Unlock()
    95  
    96  	m.form.SetButtonTextColor(color)
    97  }
    98  
    99  // SetDoneFunc sets a handler which is called when one of the buttons was
   100  // pressed. It receives the index of the button as well as its label text. The
   101  // handler is also called when the user presses the Escape key. The index will
   102  // then be negative and the label text an empty string.
   103  func (m *Modal) SetDoneFunc(handler func(buttonIndex int, buttonLabel string)) {
   104  	m.Lock()
   105  	defer m.Unlock()
   106  
   107  	m.done = handler
   108  }
   109  
   110  // SetText sets the message text of the window. The text may contain line
   111  // breaks. Note that words are wrapped, too, based on the final size of the
   112  // window.
   113  func (m *Modal) SetText(text string) {
   114  	m.Lock()
   115  	defer m.Unlock()
   116  
   117  	m.text = text
   118  }
   119  
   120  // GetForm returns the Form embedded in the window. The returned Form may be
   121  // modified to include additional elements (e.g. AddInputField, AddFormItem).
   122  func (m *Modal) GetForm() *Form {
   123  	m.RLock()
   124  	defer m.RUnlock()
   125  
   126  	return m.form
   127  }
   128  
   129  // GetFrame returns the Frame embedded in the window.
   130  func (m *Modal) GetFrame() *Frame {
   131  	m.RLock()
   132  	defer m.RUnlock()
   133  
   134  	return m.frame
   135  }
   136  
   137  // AddButtons adds buttons to the window. There must be at least one button and
   138  // a "done" handler so the window can be closed again.
   139  func (m *Modal) AddButtons(labels []string) {
   140  	m.Lock()
   141  	defer m.Unlock()
   142  
   143  	for index, label := range labels {
   144  		func(i int, l string) {
   145  			m.form.AddButton(label, func() {
   146  				if m.done != nil {
   147  					m.done(i, l)
   148  				}
   149  			})
   150  			button := m.form.GetButton(m.form.GetButtonCount() - 1)
   151  			button.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
   152  				switch event.Key() {
   153  				case tcell.KeyDown, tcell.KeyRight:
   154  					return tcell.NewEventKey(tcell.KeyTab, 0, tcell.ModNone)
   155  				case tcell.KeyUp, tcell.KeyLeft:
   156  					return tcell.NewEventKey(tcell.KeyBacktab, 0, tcell.ModNone)
   157  				}
   158  				return event
   159  			})
   160  		}(index, label)
   161  	}
   162  }
   163  
   164  // ClearButtons removes all buttons from the window.
   165  func (m *Modal) ClearButtons() {
   166  	m.Lock()
   167  	defer m.Unlock()
   168  
   169  	m.form.ClearButtons()
   170  }
   171  
   172  // SetFocus shifts the focus to the button with the given index.
   173  func (m *Modal) SetFocus(index int) {
   174  	m.Lock()
   175  	defer m.Unlock()
   176  
   177  	m.form.SetFocus(index)
   178  }
   179  
   180  // Focus is called when this primitive receives focus.
   181  func (m *Modal) Focus(delegate func(p Primitive)) {
   182  	delegate(m.form)
   183  }
   184  
   185  // HasFocus returns whether or not this primitive has focus.
   186  func (m *Modal) HasFocus() bool {
   187  	return m.GetForm().HasFocus()
   188  }
   189  
   190  // Draw draws this primitive onto the screen.
   191  func (m *Modal) Draw(screen tcell.Screen) {
   192  	if !m.GetVisible() {
   193  		return
   194  	}
   195  
   196  	formItemCount := m.form.GetFormItemCount()
   197  
   198  	m.Lock()
   199  	defer m.Unlock()
   200  
   201  	// Calculate the width of this Modal.
   202  	buttonsWidth := 0
   203  	for _, button := range m.form.buttons {
   204  		buttonsWidth += TaggedTextWidth(button.label) + 4 + 2
   205  	}
   206  	buttonsWidth -= 2
   207  	screenWidth, screenHeight := screen.Size()
   208  	width := screenWidth / 3
   209  	if width < buttonsWidth {
   210  		width = buttonsWidth
   211  	}
   212  	// width is now without the box border.
   213  
   214  	// Reset the text and find out how wide it is.
   215  	m.frame.Clear()
   216  	lines := WordWrap(m.text, width)
   217  	for _, line := range lines {
   218  		m.frame.AddText(line, true, AlignCenter, m.textColor)
   219  	}
   220  
   221  	// Set the Modal's position and size.
   222  	height := len(lines) + (formItemCount * 2) + 6
   223  	width += 4
   224  	x := (screenWidth - width) / 2
   225  	y := (screenHeight - height) / 2
   226  	m.SetRect(x, y, width, height)
   227  
   228  	// Draw the frame.
   229  	m.frame.SetRect(x, y, width, height)
   230  	m.frame.Draw(screen)
   231  }
   232  
   233  // MouseHandler returns the mouse handler for this primitive.
   234  func (m *Modal) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
   235  	return m.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
   236  		// Pass mouse events on to the form.
   237  		consumed, capture = m.form.MouseHandler()(action, event, setFocus)
   238  		if !consumed && action == MouseLeftClick && m.InRect(event.Position()) {
   239  			setFocus(m)
   240  			consumed = true
   241  		}
   242  		return
   243  	})
   244  }
   245  

View as plain text