github.com/wtfutil/wtf@v0.43.0/view/billboard_modal.go (about)

     1  package view
     2  
     3  import (
     4  	"github.com/gdamore/tcell/v2"
     5  	"github.com/rivo/tview"
     6  )
     7  
     8  const offscreen = -1000
     9  const modalWidth = 80
    10  const modalHeight = 22
    11  
    12  // NewBillboardModal creates and returns a modal dialog suitable for displaying
    13  // a wall of text
    14  // An example of this is the keyboard help modal that shows up for all widgets
    15  // that support keyboard control when '/' is pressed
    16  func NewBillboardModal(text string, closeFunc func()) *tview.Frame {
    17  	keyboardIntercept := func(event *tcell.EventKey) *tcell.EventKey {
    18  		if string(event.Rune()) == "/" {
    19  			closeFunc()
    20  			return nil
    21  		}
    22  
    23  		switch event.Key() {
    24  		case tcell.KeyEsc:
    25  			closeFunc()
    26  			return nil
    27  		case tcell.KeyTab:
    28  			return nil
    29  		default:
    30  			return event
    31  		}
    32  	}
    33  
    34  	textView := tview.NewTextView()
    35  	textView.SetDynamicColors(true)
    36  	textView.SetInputCapture(keyboardIntercept)
    37  	textView.SetText(text)
    38  	textView.SetWrap(true)
    39  
    40  	frame := tview.NewFrame(textView)
    41  	frame.SetRect(offscreen, offscreen, modalWidth, modalHeight)
    42  
    43  	drawFunc := func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
    44  		w, h := screen.Size()
    45  		frame.SetRect((w/2)-(width/2), (h/2)-(height/2), width, height)
    46  		return x, y, width, height
    47  	}
    48  
    49  	frame.SetBorder(true)
    50  	frame.SetBorders(1, 1, 0, 0, 1, 1)
    51  	frame.SetDrawFunc(drawFunc)
    52  
    53  	return frame
    54  }