Tabs
Tab containers show one of several pages, with a bar of tab buttons to
switch between them. Each page is a
container.TabItem:
a title, an optional icon, and the content to show.
| Container | Use it for |
|---|---|
container.AppTabs | The main areas of your app, such as Home, Documents and Settings. A fixed set. |
container.DocTabs | Open documents. Each tab has a close button, and the bar can have a button to add a tab. |
AppTabs
tabs := container.NewAppTabs(
container.NewTabItemWithIcon("Home", theme.HomeIcon(), widget.NewLabel("Welcome")),
container.NewTabItem("Documents", docs),
container.NewTabItemWithIcon("Settings", theme.SettingsIcon(), widget.NewLabel("Settings")),
)
tabs.SetTabLocation(container.TabLocationLeading)
tabs.OnSelected = func(item *container.TabItem) {
w.SetTitle("Tabs - " + item.Text)
}container.NewTabItem(text, content)andcontainer.NewTabItemWithIcon(text, icon, content)create the pages.SetTabLocationmoves the tab bar:container.TabLocationTop(the default),TabLocationBottom, or one of the sides withTabLocationLeadingandTabLocationTrailing.OnSelectedandOnUnselectedare called when the current tab changes.
To change tabs from code, use Select(item) or SelectIndex(i);
Selected() and SelectedIndex() tell you which one is showing. Append,
Remove, RemoveIndex and SetItems change the list of tabs. Removing the
selected tab selects the tab after it, or the one before when it was the
last. OnUnselected then receives the removed tab and OnSelected the new
one, as when the user picks a tab.
DisableIndex(i) and DisableItem(item) grey out a tab so it cannot be
chosen, and EnableIndex and EnableItem undo that.
Hiding the bar, dragging, disabling
// Let the user drag along the titles to move the selection.
tabs.DragToSelect = true
hide := widget.NewCheck("Hide tab bar", func(on bool) {
tabs.HideTabBar = on
tabs.Refresh()
})
lock := widget.NewCheck("Lock", func(on bool) {
if on {
tabs.Disable() // also disables the widgets inside every tab
} else {
tabs.Enable()
}
})DragToSelectlets the user drag along the tab titles. The selection indicator follows the pointer, and letting go selects the nearest enabled tab. Tapping a title still works. It is off by default.HideTabBarhides the tab bar, its divider and the selection indicator, so the selected tab’s content fills the whole container. The tabs and the selection stay as they are. CallRefreshafter changing it.Disable()disables the whole container. It keeps its place, but the user cannot switch tabs or open the overflow menu. Every widget inside the tabs that can be disabled is disabled too, in every tab, including content you add while it is disabled.Enable()turns back on only the widgets thatDisableturned off, so a widget you disabled yourself stays disabled.Selectand the other calls from code still work.
TabLocationLeading becomes TabLocationTop and
TabLocationTrailing becomes TabLocationBottom. In landscape, top becomes
leading and bottom becomes trailing.DocTabs
docs := container.NewDocTabs(
container.NewTabItem("untitled-1", widget.NewMultiLineEntry()),
)
n := 1
// Setting CreateTab adds a "+" button to the tab bar.
docs.CreateTab = func() *container.TabItem {
n++
return container.NewTabItem(fmt.Sprintf("untitled-%d", n), widget.NewMultiLineEntry())
}
docs.OnClosed = func(item *container.TabItem) {
fmt.Println("closed", item.Text)
}DocTabs has the same methods as AppTabs, plus:
CreateTab: when set, a + button appears in the tab bar. Your function returns the new item; the container appends it and selects it. Returnnilto add nothing.OnClosed: called after the user closes a tab and it has been removed.CloseIntercept: when set, closing a tab calls this function instead. The tab stays open until you callRemove(item)yourself, andOnClosedis not called. Use it to ask about unsaved changes before closing.
Closing the selected tab reports the new selection through OnUnselected
and OnSelected, as Remove does. A disabled DocTabs works like a disabled
AppTabs: the user cannot select, close or create tabs, and the widgets inside
are disabled with it.
The whole program
The example puts a DocTabs inside one page of an AppTabs.
// App tabs for top-level navigation, with document tabs the user can open
// and close inside one of them.
package main
import (
"fmt"
"github.com/belluikit/bellui"
"github.com/belluikit/bellui/app"
"github.com/belluikit/bellui/container"
"github.com/belluikit/bellui/geo"
"github.com/belluikit/bellui/theme"
"github.com/belluikit/bellui/widget"
)
func main() {
a := app.NewWithID("com.example.tabs")
w := a.NewWindow("Tabs")
docs := container.NewDocTabs(
container.NewTabItem("untitled-1", widget.NewMultiLineEntry()),
)
n := 1
// Setting CreateTab adds a "+" button to the tab bar.
docs.CreateTab = func() *container.TabItem {
n++
return container.NewTabItem(fmt.Sprintf("untitled-%d", n), widget.NewMultiLineEntry())
}
docs.OnClosed = func(item *container.TabItem) {
fmt.Println("closed", item.Text)
}
tabs := container.NewAppTabs(
container.NewTabItemWithIcon("Home", theme.HomeIcon(), widget.NewLabel("Welcome")),
container.NewTabItem("Documents", docs),
container.NewTabItemWithIcon("Settings", theme.SettingsIcon(), widget.NewLabel("Settings")),
)
tabs.SetTabLocation(container.TabLocationLeading)
tabs.OnSelected = func(item *container.TabItem) {
w.SetTitle("Tabs - " + item.Text)
}
// Let the user drag along the titles to move the selection.
tabs.DragToSelect = true
hide := widget.NewCheck("Hide tab bar", func(on bool) {
tabs.HideTabBar = on
tabs.Refresh()
})
lock := widget.NewCheck("Lock", func(on bool) {
if on {
tabs.Disable() // also disables the widgets inside every tab
} else {
tabs.Enable()
}
})
w.SetContent(container.NewBorder(container.NewHBox(hide, lock), nil, nil, nil, tabs))
w.Resize(geo.NewSize(640, 420))
bellui.ShowAndRun(w)
}