objects/tabbedcontainer.go

163 lines
4.3 KiB
Go

package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(TabbedContainer)
// TabbedContainer holds multiple objects, each in their own tab. The user can
// click the tab bar at the top to choose which one is activated.
//
// Sub-components:
// - TabRow sits at the top of the container and contains a row of tabs.
// - TabSpacer sits at either end of the tab row, bookending the list of tabs.
// - Tab appears in the tab row for each tab in the container. The user can
// click on the tab to switch to it.
//
// TabSpacer tags:
// - [left] The spacer is on the left.
// - [right] The spacer is on the right.
//
// Tab tags:
// - [active] The tab is currently active and its contents are visible.
type TabbedContainer struct {
box tomo.ContainerBox
leftSpacer tomo.Box
rightSpacer tomo.Box
tabsRow tomo.ContainerBox
active string
tabs []*tab
}
// NewTabbedContainer creates a new tabbed container.
func NewTabbedContainer () *TabbedContainer {
tabbedContainer := &TabbedContainer {
box: tomo.NewContainerBox(),
}
tabbedContainer.box.SetRole(tomo.R("objects", "TabbedContainer"))
tabbedContainer.box.SetAttr(tomo.ALayout(layouts.Column { false, true }))
tabbedContainer.tabsRow = tomo.NewContainerBox()
tabbedContainer.tabsRow.SetRole(tomo.R("objects", "TabRow"))
tabbedContainer.box.Add(tabbedContainer.tabsRow)
tabbedContainer.leftSpacer = tomo.NewBox()
tabbedContainer.leftSpacer.SetRole(tomo.R("objects", "TabSpacer"))
tabbedContainer.leftSpacer.SetTag("left", true)
tabbedContainer.rightSpacer = tomo.NewBox()
tabbedContainer.rightSpacer.SetRole(tomo.R("objects", "TabSpacer"))
tabbedContainer.rightSpacer.SetTag("right", true)
tabbedContainer.ClearTabs()
tabbedContainer.setTabRowLayout()
return tabbedContainer
}
// GetBox returns the underlying box.
func (this *TabbedContainer) GetBox () tomo.Box {
return this.box
}
// Activate switches to a named tab.
func (this *TabbedContainer) Activate (name string) {
if _, tab := this.findTab(this.active); tab != nil {
tab.setActive(false)
this.box.Remove(tab.root)
}
if _, tab := this.findTab(name); tab != nil {
tab.setActive(true)
this.box.Add(tab.root)
} else {
name = ""
}
this.active = name
}
// AddTab adds an object as a tab with the specified name.
func (this *TabbedContainer) AddTab (name string, root tomo.Object) {
tab := &tab {
TextBox: tomo.NewTextBox(),
name: name,
root: root,
}
tab.SetRole(tomo.R("objects", "Tab"))
tab.SetText(name)
tab.OnButtonDown(func (button input.Button) bool {
if button != input.ButtonLeft { return false }
this.Activate(name)
return true
})
tab.OnButtonUp(func (button input.Button) bool {
if button != input.ButtonLeft { return false }
return true
})
this.tabs = append(this.tabs, tab)
this.tabsRow.Insert(tab, this.rightSpacer)
this.setTabRowLayout()
// if the row was empty before, activate this tab
if len(this.tabs) == 1 {
this.Activate(name)
}
}
// RemoveTab removes the named tab.
func (this *TabbedContainer) RemoveTab (name string) {
index, tab := this.findTab(name)
if index < 0 { return }
nextIndex := index - 1
this.tabsRow.Remove(tab)
this.tabs = append(this.tabs[:index], this.tabs[index - 1:]...)
this.setTabRowLayout()
if nextIndex < 0 { nextIndex = 0 }
if nextIndex >= len(this.tabs) { nextIndex = len(this.tabs) - 1 }
if nextIndex < 0 {
this.Activate("")
} else {
this.Activate(this.tabs[nextIndex].name)
}
}
// ClearTabs removes all tabs.
func (this *TabbedContainer) ClearTabs () {
this.tabs = nil
this.tabsRow.Clear()
this.tabsRow.Add(this.leftSpacer)
this.tabsRow.Add(this.rightSpacer)
}
func (this *TabbedContainer) setTabRowLayout () {
row := make(layouts.Row, 1 + len(this.tabs) + 1)
row[len(row) - 1] = true
this.tabsRow.SetAttr(tomo.ALayout(row))
}
func (this *TabbedContainer) findTab (name string) (int, *tab) {
for index, tab := range this.tabs {
if tab.name == name { return index, tab }
}
return -1, nil
}
type tab struct {
tomo.TextBox
name string
root tomo.Object
}
func (this *tab) setActive (active bool) {
if active {
this.SetRole(tomo.R("objects", "Tab"))
this.SetTag("active", true)
} else {
this.SetRole(tomo.R("objects", "Tab"))
this.SetTag("active", false)
}
}