From bd144c2430499dc834fa1436260b11e6323d77fc Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 25 May 2021 13:09:56 -0700 Subject: [PATCH] Allow specifying TabbedPanels switcher height Resolves #54. --- CHANGELOG | 1 + tabbedpanels.go | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 234f44c..46a83f6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ v1.5.6 (WIP) - Draw additional accents when rendering a list divider - Update List, Table and TreeView to not handle Tab or Backtab +- Allow specifying TabbedPanels switcher height - When resetting color with "-" tag, set background color to primitive background color, rather than the terminal background color diff --git a/tabbedpanels.go b/tabbedpanels.go index c008ab5..e0aa714 100644 --- a/tabbedpanels.go +++ b/tabbedpanels.go @@ -29,6 +29,7 @@ type TabbedPanels struct { switcherVertical bool switcherAfterContent bool + switcherHeight int width, lastWidth int @@ -55,6 +56,7 @@ func NewTabbedPanels() *TabbedPanels { s := t.Switcher s.SetDynamicColors(true) s.SetRegions(true) + s.SetScrollable(true) s.SetWrap(true) s.SetWordWrap(true) s.SetHighlightedFunc(func(added, removed, remaining []string) { @@ -62,11 +64,11 @@ func NewTabbedPanels() *TabbedPanels { return } + s.ScrollToHighlight() t.SetCurrentTab(added[0]) if t.setFocus != nil { t.setFocus(t.panels) } - s.Highlight() }) t.rebuild() @@ -122,6 +124,7 @@ func (t *TabbedPanels) SetCurrentTab(name string) { t.Unlock() t.Switcher.Highlight(t.currentTab) + t.Switcher.ScrollToHighlight() } // GetCurrentTab returns the currently visible tab. @@ -180,6 +183,17 @@ func (t *TabbedPanels) SetTabSwitcherDivider(start, mid, end string) { t.dividerStart, t.dividerMid, t.dividerEnd = start, mid, end } +// SetTabSwitcherHeight sets the tab switcher height. This setting only applies +// when rendering horizontally. A value of 0 (the default) indicates the height +// should automatically adjust to fit all of the tab labels. +func (t *TabbedPanels) SetTabSwitcherHeight(height int) { + t.Lock() + defer t.Unlock() + + t.switcherHeight = height + t.rebuild() +} + // SetTabSwitcherVertical sets the orientation of the tab switcher. func (t *TabbedPanels) SetTabSwitcherVertical(vertical bool) { t.Lock() @@ -224,6 +238,8 @@ func (t *TabbedPanels) rebuild() { } t.updateTabLabels() + + t.Switcher.SetMaxLines(t.switcherHeight) } func (t *TabbedPanels) updateTabLabels() { @@ -287,9 +303,13 @@ func (t *TabbedPanels) updateTabLabels() { if t.switcherVertical { reqLines = maxWidth + 2 } else { - reqLines = len(WordWrap(t.Switcher.GetText(true), t.width)) - if reqLines < 1 { - reqLines = 1 + if t.switcherHeight > 0 { + reqLines = t.switcherHeight + } else { + reqLines = len(WordWrap(t.Switcher.GetText(true), t.width)) + if reqLines < 1 { + reqLines = 1 + } } } t.Flex.ResizeItem(t.Switcher, reqLines, 1)