Skip to content
Snippets Groups Projects
app.go 1.26 KiB
Newer Older
  • Learn to ignore specific revisions
  • Malte Bauch's avatar
    Malte Bauch committed
    package app
    
    Malte Bauch's avatar
    Malte Bauch committed
    //TODO: App should be a Singleton i guess
    
    import (
    	"github.com/rivo/tview"
    )
    
    
    type view interface {
    	GetContent() tview.Primitive
    }
    
    Malte Bauch's avatar
    Malte Bauch committed
    
    
    //App is a GUI-Application bases on tview
    
    type App struct {
    
    Malte Bauch's avatar
    Malte Bauch committed
    	app   *tview.Application
    	pages *tview.Pages
    
    //NewApp creates a new GUI-Application
    
    func NewApp() *App {
    	a := &App{
    		app: tview.NewApplication(),
    	}
    
    //SetRoot sets the root of the GUI-Application
    
    Malte Bauch's avatar
    Malte Bauch committed
    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
    
    Malte Bauch's avatar
    Malte Bauch committed
    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
    
    Malte Bauch's avatar
    Malte Bauch committed
    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
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (a *App) Stop() {
    	a.app.Stop()
    }
    
    //Draw calls tview.Draw()
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (a *App) Draw() {
    	a.app.Draw()
    }
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    //QueueUpdateDraw calls tview.QueueUpdateDraw()
    func (a *App) QueueUpdateDraw(f func()) {
    	a.app.QueueUpdateDraw(f)
    }
    
    
    //SetFocus sets the focus on new tview.Primitive
    
    func (a *App) SetFocus(v tview.Primitive) {
    	a.app.SetFocus(v)
    }