From c4be16a0ef8e70163172b4fa1c0a4d031569c08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?k=C3=A9ny=20Henry?= Date: Sun, 25 Aug 2024 23:08:50 -0400 Subject: [PATCH] fix tabs (tabpane) overflow on TabName length > max width change VERTICAL_LINE to VERTICAL_DASH because of unexpected pixel --- _examples/tabs.go | 2 +- widgets/tabs.go | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/_examples/tabs.go b/_examples/tabs.go index f6e0641..8969dcd 100644 --- a/_examples/tabs.go +++ b/_examples/tabs.go @@ -37,7 +37,7 @@ func main() { bc.SetRect(5, 5, 35, 10) bc.Labels = []string{"S0", "S1", "S2", "S3", "S4", "S5"} - tabpane := widgets.NewTabPane("pierwszy", "drugi", "trzeci", "żółw", "four", "five") + tabpane := widgets.NewTabPane("pierwszy", "drugi", "trzeci", "żółw", "four", "five", "glozzom", "equilstirax", "refellibal", "inkinda", "sitiallure", "mattomer") tabpane.SetRect(0, 1, 50, 4) tabpane.Border = true diff --git a/widgets/tabs.go b/widgets/tabs.go index 8cbc92c..0431fc5 100644 --- a/widgets/tabs.go +++ b/widgets/tabs.go @@ -33,12 +33,16 @@ func NewTabPane(names ...string) *TabPane { func (self *TabPane) FocusLeft() { if self.ActiveTabIndex > 0 { self.ActiveTabIndex-- + } else if self.ActiveTabIndex == 0 && len(self.TabNames) > 1 { + self.ActiveTabIndex = 0 } } func (self *TabPane) FocusRight() { if self.ActiveTabIndex < len(self.TabNames)-1 { self.ActiveTabIndex++ + } else if self.ActiveTabIndex >= len(self.TabNames)-1 { + self.ActiveTabIndex = len(self.TabNames) - 1 } } @@ -46,7 +50,20 @@ func (self *TabPane) Draw(buf *Buffer) { self.Block.Draw(buf) xCoordinate := self.Inner.Min.X - for i, name := range self.TabNames { + startIndex := 0 + + totalLength := 0 + for i := self.ActiveTabIndex; i >= 0; i-- { + name := self.TabNames[i] + totalLength += len(name) + 3 + if totalLength > self.Inner.Max.X-self.Inner.Min.X { + startIndex = i + 1 + break + } + } + + for i := startIndex; i < len(self.TabNames); i++ { + name := self.TabNames[i] ColorPair := self.InactiveTabStyle if i == self.ActiveTabIndex { ColorPair = self.ActiveTabStyle @@ -61,11 +78,15 @@ func (self *TabPane) Draw(buf *Buffer) { if i < len(self.TabNames)-1 && xCoordinate < self.Inner.Max.X { buf.SetCell( - NewCell(VERTICAL_LINE, NewStyle(ColorWhite)), + NewCell(VERTICAL_DASH, NewStyle(ColorWhite)), image.Pt(xCoordinate, self.Inner.Min.Y), ) } xCoordinate += 2 + + if xCoordinate > self.Inner.Max.X { + break + } } }