Some housekeeping.

pull/68/head
Oliver 6 years ago
parent ccd80aa4fc
commit c2f07f9548

@ -24,7 +24,7 @@ type Application struct {
root Primitive root Primitive
// Whether or not the application resizes the root primitive. // Whether or not the application resizes the root primitive.
rootAutoSize bool rootFullscreen bool
// An optional capture function which receives a key event and returns the // An optional capture function which receives a key event and returns the
// event to be forwarded to the default input handler (nil if nothing should // event to be forwarded to the default input handler (nil if nothing should
@ -136,10 +136,6 @@ func (a *Application) Run() error {
case *tcell.EventResize: case *tcell.EventResize:
a.Lock() a.Lock()
screen := a.screen screen := a.screen
if a.rootAutoSize && a.root != nil {
width, height := screen.Size()
a.root.SetRect(0, 0, width, height)
}
a.Unlock() a.Unlock()
screen.Clear() screen.Clear()
a.Draw() a.Draw()
@ -166,7 +162,7 @@ func (a *Application) Draw() *Application {
a.RLock() a.RLock()
screen := a.screen screen := a.screen
root := a.root root := a.root
fullscreen := a.rootAutoSize fullscreen := a.rootFullscreen
before := a.beforeDraw before := a.beforeDraw
after := a.afterDraw after := a.afterDraw
a.RUnlock() a.RUnlock()
@ -227,14 +223,17 @@ func (a *Application) SetAfterDrawFunc(handler func(screen tcell.Screen)) *Appli
return a return a
} }
// SetRoot sets the root primitive for this application. This function must be // SetRoot sets the root primitive for this application. If "fullscreen" is set
// called or nothing will be displayed when the application starts. // to true, the root primitive's position will be changed to fill the screen.
//
// This function must be called at least once or nothing will be displayed when
// the application starts.
// //
// It also calls SetFocus() on the primitive. // It also calls SetFocus() on the primitive.
func (a *Application) SetRoot(root Primitive, autoSize bool) *Application { func (a *Application) SetRoot(root Primitive, fullscreen bool) *Application {
a.Lock() a.Lock()
a.root = root a.root = root
a.rootAutoSize = autoSize a.rootFullscreen = fullscreen
if a.screen != nil { if a.screen != nil {
a.screen.Clear() a.screen.Clear()
} }

@ -9,7 +9,7 @@ func main() {
app.Stop() app.Stop()
}) })
button.SetBorder(true).SetRect(0, 0, 22, 3) button.SetBorder(true).SetRect(0, 0, 22, 3)
if err := app.SetRoot(button, false).SetFocus(button).Run(); err != nil { if err := app.SetRoot(button, false).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -6,7 +6,7 @@ import "github.com/rivo/tview"
func main() { func main() {
app := tview.NewApplication() app := tview.NewApplication()
checkbox := tview.NewCheckbox().SetLabel("Hit Enter to check box: ") checkbox := tview.NewCheckbox().SetLabel("Hit Enter to check box: ")
if err := app.SetRoot(checkbox, true).SetFocus(checkbox).Run(); err != nil { if err := app.SetRoot(checkbox, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -8,7 +8,7 @@ func main() {
dropdown := tview.NewDropDown(). dropdown := tview.NewDropDown().
SetLabel("Select an option (hit Enter): "). SetLabel("Select an option (hit Enter): ").
SetOptions([]string{"First", "Second", "Third", "Fourth", "Fifth"}, nil) SetOptions([]string{"First", "Second", "Third", "Fourth", "Fifth"}, nil)
if err := app.SetRoot(dropdown, true).SetFocus(dropdown).Run(); err != nil { if err := app.SetRoot(dropdown, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -14,7 +14,7 @@ func main() {
AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3, false). AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1, false), 0, 2, false). AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1, false), 0, 2, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1, false) AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1, false)
if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil { if err := app.SetRoot(flex, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -18,7 +18,7 @@ func main() {
app.Stop() app.Stop()
}) })
form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft) form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil { if err := app.SetRoot(form, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -16,7 +16,7 @@ func main() {
AddText("Header second middle", true, tview.AlignCenter, tcell.ColorRed). AddText("Header second middle", true, tview.AlignCenter, tcell.ColorRed).
AddText("Footer middle", false, tview.AlignCenter, tcell.ColorGreen). AddText("Footer middle", false, tview.AlignCenter, tcell.ColorGreen).
AddText("Footer second middle", false, tview.AlignCenter, tcell.ColorGreen) AddText("Footer second middle", false, tview.AlignCenter, tcell.ColorGreen)
if err := app.SetRoot(frame, true).SetFocus(frame).Run(); err != nil { if err := app.SetRoot(frame, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -31,7 +31,7 @@ func main() {
AddItem(main, 1, 1, 1, 1, 0, 100, false). AddItem(main, 1, 1, 1, 1, 0, 100, false).
AddItem(sideBar, 1, 2, 1, 1, 0, 100, false) AddItem(sideBar, 1, 2, 1, 1, 0, 100, false)
if err := tview.NewApplication().SetRoot(grid, true).SetFocus(grid).Run(); err != nil { if err := tview.NewApplication().SetRoot(grid, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -15,7 +15,7 @@ func main() {
SetDoneFunc(func(key tcell.Key) { SetDoneFunc(func(key tcell.Key) {
app.Stop() app.Stop()
}) })
if err := app.SetRoot(inputField, true).SetFocus(inputField).Run(); err != nil { if err := app.SetRoot(inputField, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -15,7 +15,7 @@ func main() {
AddItem("Quit", "Press to exit", 'q', func() { AddItem("Quit", "Press to exit", 'q', func() {
app.Stop() app.Stop()
}) })
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil { if err := app.SetRoot(list, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -15,7 +15,7 @@ func main() {
app.Stop() app.Stop()
} }
}) })
if err := app.SetRoot(modal, false).SetFocus(modal).Run(); err != nil { if err := app.SetRoot(modal, false).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -29,7 +29,7 @@ func main() {
page == 0) page == 0)
}(page) }(page)
} }
if err := app.SetRoot(pages, true).SetFocus(pages).Run(); err != nil { if err := app.SetRoot(pages, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -89,7 +89,7 @@ func main() {
}) })
// Start the application. // Start the application.
if err := app.SetRoot(layout, true).SetFocus(layout).Run(); err != nil { if err := app.SetRoot(layout, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -39,7 +39,7 @@ func main() {
table.GetCell(row, column).SetTextColor(tcell.ColorRed) table.GetCell(row, column).SetTextColor(tcell.ColorRed)
table.SetSelectable(false, false) table.SetSelectable(false, false)
}) })
if err := app.SetRoot(table, true).SetFocus(table).Run(); err != nil { if err := app.SetRoot(table, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -63,7 +63,7 @@ func main() {
} }
}) })
textView.SetBorder(true) textView.SetBorder(true)
if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil { if err := app.SetRoot(textView, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

@ -28,7 +28,7 @@ func main() {
form.SetBorder(true).SetTitle("输入一些内容").SetTitleAlign(tview.AlignLeft) form.SetBorder(true).SetTitle("输入一些内容").SetTitleAlign(tview.AlignLeft)
pages.AddPage("base", form, true, true) pages.AddPage("base", form, true, true)
if err := app.SetRoot(pages, true).SetFocus(pages).Run(); err != nil { if err := app.SetRoot(pages, true).Run(); err != nil {
panic(err) panic(err)
} }
} }

Loading…
Cancel
Save