2
0
mirror of https://github.com/rivo/tview.git synced 2024-11-15 06:12:46 +00:00

Add Modal.GetForm and Modal.GetFrame

This commit is contained in:
Trevor Slocum 2020-06-09 15:51:28 -07:00
parent fe95322038
commit d2d0a5ca2c

View File

@ -6,7 +6,8 @@ import (
// 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.
// AddButtons()) or it will never disappear. You may embed primitives within a
// Modal by adding them to the Form returned by GetForm.
//
// See https://github.com/rivo/tview/wiki/Modal for an example.
type Modal struct {
@ -95,6 +96,16 @@ func (m *Modal) SetText(text string) *Modal {
return m
}
// GetForm returns the form embedded in the window.
func (m *Modal) GetForm() *Form {
return m.form
}
// GetFrame returns the frame embedded in the window.
func (m *Modal) GetFrame() *Frame {
return m.frame
}
// 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 {
@ -144,6 +155,8 @@ func (m *Modal) HasFocus() bool {
// Draw draws this primitive onto the screen.
func (m *Modal) Draw(screen tcell.Screen) {
formItemCount := m.form.GetFormItemCount()
// Calculate the width of this modal.
buttonsWidth := 0
for _, button := range m.form.buttons {
@ -165,7 +178,7 @@ func (m *Modal) Draw(screen tcell.Screen) {
}
// Set the modal's position and size.
height := len(lines) + 6
height := len(lines) + (formItemCount * 2) + 6
width += 4
x := (screenWidth - width) / 2
y := (screenHeight - height) / 2