Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
app.go 1.10 KiB
package app

import "github.com/rivo/tview"

type view interface {
	GetContent() tview.Primitive
}

//App is a GUI-Application bases on tview
type App struct {
	app   *tview.Application
	pages *tview.Pages
}

//NewApp creates a new GUI-Application
func NewApp() *App {
	a := &App{
		app: tview.NewApplication(),
	}
	return a
}

//SetRoot sets the root of the GUI-Application
func (a *App) SetRoot(v view) {
	a.pages = v.GetContent().(*tview.Pages)
	a.app.SetRoot(a.pages, true)
}

//SwitchPage switches to the given (as string) tview page
func (a *App) SwitchPage(s string) {
	if a.pages.HasPage(s) {
		a.pages.SwitchToPage(s)
	}
}

//AddPage adds a new view as page that can be switched to
func (a *App) AddPage(name string, p view) {
	a.pages.AddPage(name, p.GetContent(), true, false)
}

//Run starts the GUI-Application
func (a *App) Run() error {
	return a.app.Run()
}

//Stop stops the GUI-Application
func (a *App) Stop() {
	a.app.Stop()
}

//Draw calls tview.Draw()
func (a *App) Draw() {
	a.app.Draw()
}

//SetFocus sets the focus on new tview.Primitive
func (a *App) SetFocus(v tview.Primitive) {
	a.app.SetFocus(v)
}