1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
package cview
import (
"bytes"
"fmt"
"sync"
"github.com/gdamore/tcell/v2"
)
// TabbedPanels is a tabbed container for other primitives. The tab switcher
// may be positioned vertically or horizontally, before or after the content.
type TabbedPanels struct {
*Flex
Switcher *TextView
panels *Panels
tabLabels map[string]string
currentTab string
tabTextColor tcell.Color
tabTextColorFocused tcell.Color
tabBackgroundColor tcell.Color
tabBackgroundColorFocused tcell.Color
dividerStart string
dividerMid string
dividerEnd string
switcherVertical bool
switcherAfterContent bool
width, lastWidth int
setFocus func(Primitive)
sync.RWMutex
}
// NewTabbedPanels returns a new TabbedPanels object.
func NewTabbedPanels() *TabbedPanels {
t := &TabbedPanels{
Flex: NewFlex(),
Switcher: NewTextView(),
panels: NewPanels(),
tabTextColor: Styles.PrimaryTextColor,
tabTextColorFocused: Styles.InverseTextColor,
tabBackgroundColor: ColorUnset,
tabBackgroundColorFocused: Styles.PrimaryTextColor,
dividerMid: string(BoxDrawingsDoubleVertical),
dividerEnd: string(BoxDrawingsLightVertical),
tabLabels: make(map[string]string),
}
s := t.Switcher
s.SetDynamicColors(true)
s.SetRegions(true)
s.SetWrap(true)
s.SetWordWrap(true)
s.SetHighlightedFunc(func(added, removed, remaining []string) {
if len(added) == 0 {
return
}
t.SetCurrentTab(added[0])
if t.setFocus != nil {
t.setFocus(t.panels)
}
s.Highlight()
})
t.rebuild()
return t
}
// AddTab adds a new tab. Tab names should consist only of letters, numbers
// and spaces.
func (t *TabbedPanels) AddTab(name, label string, item Primitive) {
t.Lock()
t.tabLabels[name] = label
t.Unlock()
t.panels.AddPanel(name, item, true, false)
t.updateAll()
}
// RemoveTab removes a tab.
func (t *TabbedPanels) RemoveTab(name string) {
t.panels.RemovePanel(name)
t.updateAll()
}
// HasTab returns true if a tab with the given name exists in this object.
func (t *TabbedPanels) HasTab(name string) bool {
t.RLock()
defer t.RUnlock()
for _, panel := range t.panels.panels {
if panel.Name == name {
return true
}
}
return false
}
// SetCurrentTab sets the currently visible tab.
func (t *TabbedPanels) SetCurrentTab(name string) {
t.Lock()
if t.currentTab == name {
t.Unlock()
return
}
t.currentTab = name
t.updateAll()
t.Unlock()
t.Switcher.Highlight(t.currentTab)
}
// GetCurrentTab returns the currently visible tab.
func (t *TabbedPanels) GetCurrentTab() string {
t.RLock()
defer t.RUnlock()
return t.currentTab
}
// SetTabLabel sets the label of a tab.
func (t *TabbedPanels) SetTabLabel(name, label string) {
t.Lock()
defer t.Unlock()
if t.tabLabels[name] == label {
return
}
t.tabLabels[name] = label
t.updateTabLabels()
}
// SetTabTextColor sets the color of the tab text.
func (t *TabbedPanels) SetTabTextColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabTextColor = color
}
// SetTabTextColorFocused sets the color of the tab text when the tab is in focus.
func (t *TabbedPanels) SetTabTextColorFocused(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabTextColorFocused = color
}
// SetTabBackgroundColor sets the background color of the tab.
func (t *TabbedPanels) SetTabBackgroundColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabBackgroundColor = color
}
// SetTabBackgroundColorFocused sets the background color of the tab when the
// tab is in focus.
func (t *TabbedPanels) SetTabBackgroundColorFocused(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabBackgroundColorFocused = color
}
// SetTabSwitcherDivider sets the tab switcher divider text. Color tags are supported.
func (t *TabbedPanels) SetTabSwitcherDivider(start, mid, end string) {
t.Lock()
defer t.Unlock()
t.dividerStart, t.dividerMid, t.dividerEnd = start, mid, end
}
// SetTabSwitcherVertical sets the orientation of the tab switcher.
func (t *TabbedPanels) SetTabSwitcherVertical(vertical bool) {
t.Lock()
defer t.Unlock()
if t.switcherVertical == vertical {
return
}
t.switcherVertical = vertical
t.rebuild()
}
// SetTabSwitcherAfterContent sets whether the tab switcher is positioned after content.
func (t *TabbedPanels) SetTabSwitcherAfterContent(after bool) {
t.Lock()
defer t.Unlock()
if t.switcherAfterContent == after {
return
}
t.switcherAfterContent = after
t.rebuild()
}
func (t *TabbedPanels) rebuild() {
f := t.Flex
if t.switcherVertical {
f.SetDirection(FlexColumn)
} else {
f.SetDirection(FlexRow)
}
f.RemoveItem(t.panels)
f.RemoveItem(t.Switcher)
if t.switcherAfterContent {
f.AddItem(t.panels, 0, 1, true)
f.AddItem(t.Switcher, 1, 1, false)
} else {
f.AddItem(t.Switcher, 1, 1, false)
f.AddItem(t.panels, 0, 1, true)
}
t.updateTabLabels()
}
func (t *TabbedPanels) updateTabLabels() {
if len(t.panels.panels) == 0 {
t.Switcher.SetText("")
t.Flex.ResizeItem(t.Switcher, 0, 1)
return
}
maxWidth := 0
for _, panel := range t.panels.panels {
label := t.tabLabels[panel.Name]
if len(label) > maxWidth {
maxWidth = len(label)
}
}
var b bytes.Buffer
if !t.switcherVertical {
b.WriteString(t.dividerStart)
}
l := len(t.panels.panels)
spacer := []byte(" ")
for i, panel := range t.panels.panels {
if i > 0 && t.switcherVertical {
b.WriteRune('\n')
}
if t.switcherVertical && t.switcherAfterContent {
b.WriteString(t.dividerMid)
b.WriteRune(' ')
}
textColor := t.tabTextColor
backgroundColor := t.tabBackgroundColor
if panel.Name == t.currentTab {
textColor = t.tabTextColorFocused
backgroundColor = t.tabBackgroundColorFocused
}
label := t.tabLabels[panel.Name]
if !t.switcherVertical {
label = " " + label
}
if t.switcherVertical {
spacer = bytes.Repeat([]byte(" "), maxWidth-len(label)+1)
}
b.WriteString(fmt.Sprintf(`["%s"][%s:%s]%s%s[-:-][""]`, panel.Name, ColorHex(textColor), ColorHex(backgroundColor), label, spacer))
if i == l-1 && !t.switcherVertical {
b.WriteString(t.dividerEnd)
} else if !t.switcherAfterContent {
b.WriteString(t.dividerMid)
}
}
t.Switcher.SetText(b.String())
var reqLines int
if t.switcherVertical {
reqLines = maxWidth + 2
} else {
reqLines = len(WordWrap(t.Switcher.GetText(true), t.width))
if reqLines < 1 {
reqLines = 1
}
}
t.Flex.ResizeItem(t.Switcher, reqLines, 1)
}
func (t *TabbedPanels) updateVisibleTabs() {
allPanels := t.panels.panels
var newTab string
var foundCurrent bool
for _, panel := range allPanels {
if panel.Name == t.currentTab {
newTab = panel.Name
foundCurrent = true
break
}
}
if !foundCurrent {
for _, panel := range allPanels {
if panel.Name != "" {
newTab = panel.Name
break
}
}
}
if t.currentTab != newTab {
t.SetCurrentTab(newTab)
return
}
for _, panel := range allPanels {
if panel.Name == t.currentTab {
t.panels.ShowPanel(panel.Name)
} else {
t.panels.HidePanel(panel.Name)
}
}
}
func (t *TabbedPanels) updateAll() {
t.updateTabLabels()
t.updateVisibleTabs()
}
// Draw draws this primitive onto the screen.
func (t *TabbedPanels) Draw(screen tcell.Screen) {
if !t.GetVisible() {
return
}
t.Box.Draw(screen)
_, _, t.width, _ = t.GetInnerRect()
if t.width != t.lastWidth {
t.updateTabLabels()
}
t.lastWidth = t.width
t.Flex.Draw(screen)
}
// InputHandler returns the handler for this primitive.
func (t *TabbedPanels) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
if t.setFocus == nil {
t.setFocus = setFocus
}
t.Flex.InputHandler()(event, setFocus)
})
}
// MouseHandler returns the mouse handler for this primitive.
func (t *TabbedPanels) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return t.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if t.setFocus == nil {
t.setFocus = setFocus
}
x, y := event.Position()
if !t.InRect(x, y) {
return false, nil
}
if t.Switcher.InRect(x, y) {
if t.setFocus != nil {
defer t.setFocus(t.panels)
}
defer t.Switcher.MouseHandler()(action, event, setFocus)
return true, nil
}
return t.Flex.MouseHandler()(action, event, setFocus)
})
}
|