mirror of https://github.com/rivo/tview.git
Fixed empty pages when the only visible page was removed. Fixes #250
This commit is contained in:
parent
9d616aee87
commit
90b4da1bd6
22
pages.go
22
pages.go
|
@ -50,13 +50,15 @@ func (p *Pages) SetChangedFunc(handler func()) *Pages {
|
|||
|
||||
// AddPage adds a new page with the given name and primitive. If there was
|
||||
// previously a page with the same name, it is overwritten. Leaving the name
|
||||
// empty may cause conflicts in other functions.
|
||||
// empty may cause conflicts in other functions so always specify a non-empty
|
||||
// name.
|
||||
//
|
||||
// Visible pages will be drawn in the order they were added (unless that order
|
||||
// was changed in one of the other functions). If "resize" is set to true, the
|
||||
// primitive will be set to the size available to the Pages primitive whenever
|
||||
// the pages are drawn.
|
||||
func (p *Pages) AddPage(name string, item Primitive, resize, visible bool) *Pages {
|
||||
hasFocus := p.HasFocus()
|
||||
for index, pg := range p.pages {
|
||||
if pg.Name == name {
|
||||
p.pages = append(p.pages[:index], p.pages[index+1:]...)
|
||||
|
@ -67,7 +69,7 @@ func (p *Pages) AddPage(name string, item Primitive, resize, visible bool) *Page
|
|||
if p.changed != nil {
|
||||
p.changed()
|
||||
}
|
||||
if p.HasFocus() {
|
||||
if hasFocus {
|
||||
p.Focus(p.setFocus)
|
||||
}
|
||||
return p
|
||||
|
@ -81,11 +83,14 @@ func (p *Pages) AddAndSwitchToPage(name string, item Primitive, resize bool) *Pa
|
|||
return p
|
||||
}
|
||||
|
||||
// RemovePage removes the page with the given name.
|
||||
// RemovePage removes the page with the given name. If that page was the only
|
||||
// visible page, visibility is assigned to the last page.
|
||||
func (p *Pages) RemovePage(name string) *Pages {
|
||||
var isVisible bool
|
||||
hasFocus := p.HasFocus()
|
||||
for index, page := range p.pages {
|
||||
if page.Name == name {
|
||||
isVisible = page.Visible
|
||||
p.pages = append(p.pages[:index], p.pages[index+1:]...)
|
||||
if page.Visible && p.changed != nil {
|
||||
p.changed()
|
||||
|
@ -93,6 +98,17 @@ func (p *Pages) RemovePage(name string) *Pages {
|
|||
break
|
||||
}
|
||||
}
|
||||
if isVisible {
|
||||
for index, page := range p.pages {
|
||||
if index < len(p.pages)-1 {
|
||||
if page.Visible {
|
||||
break // There is a remaining visible page.
|
||||
}
|
||||
} else {
|
||||
page.Visible = true // We need at least one visible page.
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasFocus {
|
||||
p.Focus(p.setFocus)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue