termui/widgets/tabs.go

72 lines
1.6 KiB
Go
Raw Normal View History

2019-01-23 21:12:10 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2022-08-28 10:46:41 -06:00
// Use of this source code is governed by a GPLv3 license that can
2019-01-23 21:12:10 -07:00
// be found in the LICENSE file.
package widgets
import (
"image"
. "git.tebitea.media/sashakoshka/termui/v3"
2019-01-23 21:12:10 -07:00
)
// TabPane is a renderable widget which can be used to conditionally render certain tabs/views.
// TabPane shows a list of Tab names.
// The currently selected tab can be found through the `ActiveTabIndex` field.
type TabPane struct {
Block
TabNames []string
2019-02-23 18:15:42 -07:00
ActiveTabIndex int
2019-01-23 21:12:10 -07:00
ActiveTabStyle Style
InactiveTabStyle Style
}
func NewTabPane(names ...string) *TabPane {
return &TabPane{
Block: *NewBlock(),
TabNames: names,
ActiveTabStyle: Theme.Tab.Active,
InactiveTabStyle: Theme.Tab.Inactive,
}
}
func (self *TabPane) FocusLeft() {
if self.ActiveTabIndex > 0 {
self.ActiveTabIndex--
}
}
func (self *TabPane) FocusRight() {
2019-02-23 18:15:42 -07:00
if self.ActiveTabIndex < len(self.TabNames)-1 {
2019-01-23 21:12:10 -07:00
self.ActiveTabIndex++
}
}
func (self *TabPane) Draw(buf *Buffer) {
self.Block.Draw(buf)
xCoordinate := self.Inner.Min.X
for i, name := range self.TabNames {
ColorPair := self.InactiveTabStyle
2019-02-23 18:15:42 -07:00
if i == self.ActiveTabIndex {
2019-01-23 21:12:10 -07:00
ColorPair = self.ActiveTabStyle
}
buf.SetString(
TrimString(name, self.Inner.Max.X-xCoordinate),
ColorPair,
image.Pt(xCoordinate, self.Inner.Min.Y),
)
xCoordinate += 1 + len(name)
if i < len(self.TabNames)-1 && xCoordinate < self.Inner.Max.X {
buf.SetCell(
NewCell(VERTICAL_LINE, NewStyle(ColorWhite)),
image.Pt(xCoordinate, self.Inner.Min.Y),
)
}
xCoordinate += 2
}
}