| 1 |
package ui |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
|
| 6 |
"github.com/mattn/go-runewidth" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/config" |
| 9 |
"go.rockorager.dev/vaxis" |
| 10 |
) |
| 11 |
|
| 12 |
const tabRuneWidth int = 32 // TODO: make configurable |
| 13 |
|
| 14 |
type Tabs struct { |
| 15 |
tabs []*Tab |
| 16 |
TabStrip *TabStrip |
| 17 |
TabContent *TabContent |
| 18 |
curIndex int |
| 19 |
history []*Tab |
| 20 |
m sync.Mutex |
| 21 |
|
| 22 |
ui func(d Drawable) *config.UIConfig |
| 23 |
|
| 24 |
parent *Tabs //nolint:structcheck // used within this file |
| 25 |
CloseTab func(index int) |
| 26 |
} |
| 27 |
|
| 28 |
type Tab struct { |
| 29 |
Content Drawable |
| 30 |
Name string |
| 31 |
pinned bool |
| 32 |
indexBeforePin int |
| 33 |
title string |
| 34 |
} |
| 35 |
|
| 36 |
func (t *Tab) SetTitle(s string) { |
| 37 |
t.title = s |
| 38 |
} |
| 39 |
|
| 40 |
func (t *Tab) displayName(pinMarker string) string { |
| 41 |
name := t.Name |
| 42 |
if t.title != "" { |
| 43 |
name = t.title |
| 44 |
} |
| 45 |
if t.pinned { |
| 46 |
name = pinMarker + name |
| 47 |
} |
| 48 |
return name |
| 49 |
} |
| 50 |
|
| 51 |
type ( |
| 52 |
TabStrip Tabs |
| 53 |
TabContent Tabs |
| 54 |
) |
| 55 |
|
| 56 |
func NewTabs(ui func(d Drawable) *config.UIConfig) *Tabs { |
| 57 |
tabs := &Tabs{ui: ui} |
| 58 |
tabs.TabStrip = (*TabStrip)(tabs) |
| 59 |
tabs.TabStrip.parent = tabs |
| 60 |
tabs.TabContent = (*TabContent)(tabs) |
| 61 |
tabs.TabContent.parent = tabs |
| 62 |
return tabs |
| 63 |
} |
| 64 |
|
| 65 |
func (tabs *Tabs) Add(content Drawable, name string, background bool) *Tab { |
| 66 |
tab := &Tab{ |
| 67 |
Content: content, |
| 68 |
Name: name, |
| 69 |
} |
| 70 |
tabs.tabs = append(tabs.tabs, tab) |
| 71 |
if !background { |
| 72 |
tabs.selectPriv(len(tabs.tabs)-1, true) |
| 73 |
} |
| 74 |
return tab |
| 75 |
} |
| 76 |
|
| 77 |
func (tabs *Tabs) Names() []string { |
| 78 |
var names []string |
| 79 |
tabs.m.Lock() |
| 80 |
for _, tab := range tabs.tabs { |
| 81 |
names = append(names, tab.Name) |
| 82 |
} |
| 83 |
tabs.m.Unlock() |
| 84 |
return names |
| 85 |
} |
| 86 |
|
| 87 |
func (tabs *Tabs) Remove(content Drawable) { |
| 88 |
tabs.m.Lock() |
| 89 |
defer tabs.m.Unlock() |
| 90 |
index := -1 |
| 91 |
for i, tab := range tabs.tabs { |
| 92 |
if tab.Content == content { |
| 93 |
index = i |
| 94 |
break |
| 95 |
} |
| 96 |
} |
| 97 |
if index == -1 { |
| 98 |
return |
| 99 |
} |
| 100 |
|
| 101 |
tab := tabs.tabs[index] |
| 102 |
if vis, ok := tab.Content.(Visible); ok { |
| 103 |
vis.Show(false) |
| 104 |
} |
| 105 |
if vis, ok := tab.Content.(Focusable); ok { |
| 106 |
vis.Focus(false) |
| 107 |
} |
| 108 |
tabs.tabs = append(tabs.tabs[:index], tabs.tabs[index+1:]...) |
| 109 |
tabs.removeHistory(tab) |
| 110 |
|
| 111 |
if index == tabs.curIndex { |
| 112 |
// only pop the tab history if the closing tab is selected |
| 113 |
prevIndex, ok := tabs.popHistory() |
| 114 |
if !ok { |
| 115 |
if tabs.curIndex < len(tabs.tabs) { |
| 116 |
// history is empty, select tab on the right if possible |
| 117 |
prevIndex = tabs.curIndex |
| 118 |
} else { |
| 119 |
// if removing the last tab, select the now last tab |
| 120 |
prevIndex = len(tabs.tabs) - 1 |
| 121 |
} |
| 122 |
} |
| 123 |
tabs.selectPriv(prevIndex, false) |
| 124 |
} else if index < tabs.curIndex { |
| 125 |
// selected tab is now one to the left of where it was |
| 126 |
tabs.selectPriv(tabs.curIndex-1, false) |
| 127 |
} |
| 128 |
Invalidate() |
| 129 |
} |
| 130 |
|
| 131 |
func (tabs *Tabs) Replace(contentSrc Drawable, contentTarget Drawable, name string) { |
| 132 |
tabs.m.Lock() |
| 133 |
defer tabs.m.Unlock() |
| 134 |
for i, tab := range tabs.tabs { |
| 135 |
if tab.Content == contentSrc { |
| 136 |
if vis, ok := tab.Content.(Visible); ok { |
| 137 |
vis.Show(false) |
| 138 |
} |
| 139 |
if vis, ok := tab.Content.(Focusable); ok { |
| 140 |
vis.Focus(false) |
| 141 |
} |
| 142 |
tab.Content = contentTarget |
| 143 |
tab.title = name |
| 144 |
tabs.selectPriv(i, false) |
| 145 |
Invalidate() |
| 146 |
break |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func (tabs *Tabs) Get(index int) *Tab { |
| 152 |
tabs.m.Lock() |
| 153 |
defer tabs.m.Unlock() |
| 154 |
if index < 0 || index >= len(tabs.tabs) { |
| 155 |
return nil |
| 156 |
} |
| 157 |
return tabs.tabs[index] |
| 158 |
} |
| 159 |
|
| 160 |
func (tabs *Tabs) Selected() *Tab { |
| 161 |
tabs.m.Lock() |
| 162 |
defer tabs.m.Unlock() |
| 163 |
if tabs.curIndex < 0 || tabs.curIndex >= len(tabs.tabs) { |
| 164 |
return nil |
| 165 |
} |
| 166 |
return tabs.tabs[tabs.curIndex] |
| 167 |
} |
| 168 |
|
| 169 |
func (tabs *Tabs) Select(index int) bool { |
| 170 |
tabs.m.Lock() |
| 171 |
defer tabs.m.Unlock() |
| 172 |
return tabs.selectPriv(index, true) |
| 173 |
} |
| 174 |
|
| 175 |
func (tabs *Tabs) selectPriv(index int, unselectPrev bool) bool { |
| 176 |
if index < 0 || index >= len(tabs.tabs) { |
| 177 |
return false |
| 178 |
} |
| 179 |
|
| 180 |
// only push valid tabs onto the history |
| 181 |
if unselectPrev && tabs.curIndex < len(tabs.tabs) { |
| 182 |
prev := tabs.tabs[tabs.curIndex] |
| 183 |
if vis, ok := prev.Content.(Visible); ok { |
| 184 |
vis.Show(false) |
| 185 |
} |
| 186 |
if vis, ok := prev.Content.(Focusable); ok { |
| 187 |
vis.Focus(false) |
| 188 |
} |
| 189 |
tabs.pushHistory(prev) |
| 190 |
} |
| 191 |
|
| 192 |
next := tabs.tabs[index] |
| 193 |
if vis, ok := next.Content.(Visible); ok { |
| 194 |
vis.Show(true) |
| 195 |
} |
| 196 |
if vis, ok := next.Content.(Focusable); ok { |
| 197 |
vis.Focus(true) |
| 198 |
} |
| 199 |
tabs.curIndex = index |
| 200 |
Invalidate() |
| 201 |
|
| 202 |
return true |
| 203 |
} |
| 204 |
|
| 205 |
func (tabs *Tabs) SelectName(name string) bool { |
| 206 |
tabs.m.Lock() |
| 207 |
defer tabs.m.Unlock() |
| 208 |
for i, tab := range tabs.tabs { |
| 209 |
if tab.Name == name { |
| 210 |
return tabs.selectPriv(i, true) |
| 211 |
} |
| 212 |
} |
| 213 |
return false |
| 214 |
} |
| 215 |
|
| 216 |
func (tabs *Tabs) SelectPrevious() bool { |
| 217 |
tabs.m.Lock() |
| 218 |
defer tabs.m.Unlock() |
| 219 |
index, ok := tabs.popHistory() |
| 220 |
if !ok { |
| 221 |
return false |
| 222 |
} |
| 223 |
return tabs.selectPriv(index, true) |
| 224 |
} |
| 225 |
|
| 226 |
func (tabs *Tabs) SelectOffset(offset int) { |
| 227 |
tabs.m.Lock() |
| 228 |
tabCount := len(tabs.tabs) |
| 229 |
newIndex := (tabs.curIndex + offset) % tabCount |
| 230 |
if newIndex < 0 { |
| 231 |
// Handle negative offsets correctly |
| 232 |
newIndex += tabCount |
| 233 |
} |
| 234 |
tabs.selectPriv(newIndex, true) |
| 235 |
tabs.m.Unlock() |
| 236 |
} |
| 237 |
|
| 238 |
func (tabs *Tabs) MoveTab(to int, relative bool) { |
| 239 |
tabs.m.Lock() |
| 240 |
tabs.moveTabPriv(to, relative) |
| 241 |
tabs.m.Unlock() |
| 242 |
} |
| 243 |
|
| 244 |
func (tabs *Tabs) moveTabPriv(to int, relative bool) { |
| 245 |
from := tabs.curIndex |
| 246 |
|
| 247 |
if relative { |
| 248 |
to = from + to |
| 249 |
} |
| 250 |
if to < 0 { |
| 251 |
to = 0 |
| 252 |
} |
| 253 |
if to >= len(tabs.tabs) { |
| 254 |
to = len(tabs.tabs) - 1 |
| 255 |
} |
| 256 |
tabs.tabs[from], tabs.tabs[to] = tabs.tabs[to], tabs.tabs[from] |
| 257 |
tabs.curIndex = to |
| 258 |
Invalidate() |
| 259 |
} |
| 260 |
|
| 261 |
func (tabs *Tabs) PinTab() { |
| 262 |
tabs.m.Lock() |
| 263 |
defer tabs.m.Unlock() |
| 264 |
if tabs.tabs[tabs.curIndex].pinned { |
| 265 |
return |
| 266 |
} |
| 267 |
|
| 268 |
pinEnd := len(tabs.tabs) |
| 269 |
for i, t := range tabs.tabs { |
| 270 |
if !t.pinned { |
| 271 |
pinEnd = i |
| 272 |
break |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
for _, t := range tabs.tabs { |
| 277 |
if t.pinned && t.indexBeforePin > tabs.curIndex-pinEnd { |
| 278 |
t.indexBeforePin -= 1 |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
tabs.tabs[tabs.curIndex].pinned = true |
| 283 |
tabs.tabs[tabs.curIndex].indexBeforePin = tabs.curIndex - pinEnd |
| 284 |
|
| 285 |
tabs.moveTabPriv(pinEnd, false) |
| 286 |
} |
| 287 |
|
| 288 |
func (tabs *Tabs) UnpinTab() { |
| 289 |
tabs.m.Lock() |
| 290 |
defer tabs.m.Unlock() |
| 291 |
if !tabs.tabs[tabs.curIndex].pinned { |
| 292 |
return |
| 293 |
} |
| 294 |
|
| 295 |
pinEnd := len(tabs.tabs) |
| 296 |
for i, t := range tabs.tabs { |
| 297 |
if i != tabs.curIndex && t.pinned && t.indexBeforePin > tabs.tabs[tabs.curIndex].indexBeforePin { |
| 298 |
t.indexBeforePin += 1 |
| 299 |
} |
| 300 |
if !t.pinned { |
| 301 |
pinEnd = i |
| 302 |
break |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
tabs.tabs[tabs.curIndex].pinned = false |
| 307 |
|
| 308 |
tabs.moveTabPriv(tabs.tabs[tabs.curIndex].indexBeforePin+pinEnd-1, false) |
| 309 |
} |
| 310 |
|
| 311 |
func (tabs *Tabs) NextTab() { |
| 312 |
tabs.m.Lock() |
| 313 |
next := tabs.curIndex + 1 |
| 314 |
if next >= len(tabs.tabs) { |
| 315 |
next = 0 |
| 316 |
} |
| 317 |
tabs.selectPriv(next, true) |
| 318 |
tabs.m.Unlock() |
| 319 |
} |
| 320 |
|
| 321 |
func (tabs *Tabs) PrevTab() { |
| 322 |
tabs.m.Lock() |
| 323 |
next := tabs.curIndex - 1 |
| 324 |
if next < 0 { |
| 325 |
next = len(tabs.tabs) - 1 |
| 326 |
} |
| 327 |
tabs.selectPriv(next, true) |
| 328 |
tabs.m.Unlock() |
| 329 |
} |
| 330 |
|
| 331 |
const maxHistory = 256 |
| 332 |
|
| 333 |
func (tabs *Tabs) pushHistory(tab *Tab) { |
| 334 |
tabs.history = append(tabs.history, tab) |
| 335 |
if len(tabs.history) > maxHistory { |
| 336 |
tabs.history = tabs.history[1:] |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
func (tabs *Tabs) popHistory() (int, bool) { |
| 341 |
if len(tabs.history) == 0 { |
| 342 |
return -1, false |
| 343 |
} |
| 344 |
tab := tabs.history[len(tabs.history)-1] |
| 345 |
tabs.history = tabs.history[:len(tabs.history)-1] |
| 346 |
index := -1 |
| 347 |
for i, t := range tabs.tabs { |
| 348 |
if t == tab { |
| 349 |
index = i |
| 350 |
break |
| 351 |
} |
| 352 |
} |
| 353 |
if index == -1 { |
| 354 |
return -1, false |
| 355 |
} |
| 356 |
return index, true |
| 357 |
} |
| 358 |
|
| 359 |
func (tabs *Tabs) removeHistory(tab *Tab) { |
| 360 |
var newHist []*Tab |
| 361 |
for _, item := range tabs.history { |
| 362 |
if item != tab { |
| 363 |
newHist = append(newHist, item) |
| 364 |
} |
| 365 |
} |
| 366 |
tabs.history = newHist |
| 367 |
} |
| 368 |
|
| 369 |
// TODO: Color repository |
| 370 |
func (strip *TabStrip) Draw(ctx *Context) { |
| 371 |
x := 0 |
| 372 |
strip.parent.m.Lock() |
| 373 |
for i, tab := range strip.tabs { |
| 374 |
uiConfig := strip.ui(tab.Content) |
| 375 |
if uiConfig == nil { |
| 376 |
uiConfig = config.Ui() |
| 377 |
} |
| 378 |
style := uiConfig.GetStyle(config.STYLE_TAB) |
| 379 |
if strip.curIndex == i { |
| 380 |
style = uiConfig.GetStyleSelected(config.STYLE_TAB) |
| 381 |
} |
| 382 |
tabWidth := tabRuneWidth |
| 383 |
if ctx.Width()-x < tabWidth { |
| 384 |
tabWidth = ctx.Width() - x - 2 |
| 385 |
} |
| 386 |
name := tab.displayName(uiConfig.PinnedTabMarker) |
| 387 |
trunc := runewidth.Truncate(name, tabWidth, "…") |
| 388 |
x += ctx.Printf(x, 0, style, " %s ", trunc) |
| 389 |
if x >= ctx.Width() { |
| 390 |
break |
| 391 |
} |
| 392 |
} |
| 393 |
strip.parent.m.Unlock() |
| 394 |
ctx.Fill(x, 0, ctx.Width()-x, 1, ' ', |
| 395 |
config.Ui().GetStyle(config.STYLE_TAB)) |
| 396 |
} |
| 397 |
|
| 398 |
func (strip *TabStrip) Invalidate() { |
| 399 |
Invalidate() |
| 400 |
} |
| 401 |
|
| 402 |
func (strip *TabStrip) MouseEvent(localX int, localY int, event vaxis.Event) { |
| 403 |
strip.parent.m.Lock() |
| 404 |
defer strip.parent.m.Unlock() |
| 405 |
changeFocus := func(focus bool) { |
| 406 |
focusable, ok := strip.parent.tabs[strip.parent.curIndex].Content.(Focusable) |
| 407 |
if ok { |
| 408 |
focusable.Focus(focus) |
| 409 |
} |
| 410 |
} |
| 411 |
unfocus := func() { changeFocus(false) } |
| 412 |
refocus := func() { changeFocus(true) } |
| 413 |
if event, ok := event.(vaxis.Mouse); ok { |
| 414 |
switch event.Button { |
| 415 |
case vaxis.MouseLeftButton: |
| 416 |
selectedTab, ok := strip.clicked(localX, localY) |
| 417 |
if !ok || selectedTab == strip.parent.curIndex { |
| 418 |
return |
| 419 |
} |
| 420 |
unfocus() |
| 421 |
strip.parent.selectPriv(selectedTab, true) |
| 422 |
refocus() |
| 423 |
case vaxis.MouseWheelDown: |
| 424 |
unfocus() |
| 425 |
index := strip.parent.curIndex + 1 |
| 426 |
if index >= len(strip.parent.tabs) { |
| 427 |
index = 0 |
| 428 |
} |
| 429 |
strip.parent.selectPriv(index, true) |
| 430 |
refocus() |
| 431 |
case vaxis.MouseWheelUp: |
| 432 |
unfocus() |
| 433 |
index := strip.parent.curIndex - 1 |
| 434 |
if index < 0 { |
| 435 |
index = len(strip.parent.tabs) - 1 |
| 436 |
} |
| 437 |
strip.parent.selectPriv(index, true) |
| 438 |
refocus() |
| 439 |
case vaxis.MouseMiddleButton: |
| 440 |
selectedTab, ok := strip.clicked(localX, localY) |
| 441 |
if !ok { |
| 442 |
return |
| 443 |
} |
| 444 |
unfocus() |
| 445 |
strip.parent.m.Unlock() |
| 446 |
strip.parent.CloseTab(selectedTab) |
| 447 |
strip.parent.m.Lock() |
| 448 |
refocus() |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
func (strip *TabStrip) clicked(mouseX int, mouseY int) (int, bool) { |
| 454 |
x := 0 |
| 455 |
for i, tab := range strip.tabs { |
| 456 |
uiConfig := strip.ui(tab.Content) |
| 457 |
if uiConfig == nil { |
| 458 |
uiConfig = config.Ui() |
| 459 |
} |
| 460 |
name := tab.displayName(uiConfig.PinnedTabMarker) |
| 461 |
trunc := runewidth.Truncate(name, tabRuneWidth, "…") |
| 462 |
length := runewidth.StringWidth(trunc) + 2 |
| 463 |
if x <= mouseX && mouseX < x+length { |
| 464 |
return i, true |
| 465 |
} |
| 466 |
x += length |
| 467 |
} |
| 468 |
return 0, false |
| 469 |
} |
| 470 |
|
| 471 |
func (content *TabContent) Children() []Drawable { |
| 472 |
content.parent.m.Lock() |
| 473 |
children := make([]Drawable, len(content.tabs)) |
| 474 |
for i, tab := range content.tabs { |
| 475 |
children[i] = tab.Content |
| 476 |
} |
| 477 |
content.parent.m.Unlock() |
| 478 |
return children |
| 479 |
} |
| 480 |
|
| 481 |
func (content *TabContent) Draw(ctx *Context) { |
| 482 |
content.parent.m.Lock() |
| 483 |
if content.curIndex >= len(content.tabs) { |
| 484 |
width := ctx.Width() |
| 485 |
height := ctx.Height() |
| 486 |
ctx.Fill(0, 0, width, height, ' ', |
| 487 |
config.Ui().GetStyle(config.STYLE_TAB)) |
| 488 |
} |
| 489 |
tab := content.tabs[content.curIndex] |
| 490 |
content.parent.m.Unlock() |
| 491 |
tab.Content.Draw(ctx) |
| 492 |
} |
| 493 |
|
| 494 |
func (content *TabContent) MouseEvent(localX int, localY int, event vaxis.Event) { |
| 495 |
content.parent.m.Lock() |
| 496 |
tab := content.tabs[content.curIndex] |
| 497 |
content.parent.m.Unlock() |
| 498 |
if tabContent, ok := tab.Content.(Mouseable); ok { |
| 499 |
tabContent.MouseEvent(localX, localY, event) |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
func (content *TabContent) Invalidate() { |
| 504 |
Invalidate() |
| 505 |
} |