util.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package ui
  2. import (
  3. "github.com/ayn2op/discordo/internal/config"
  4. "github.com/ayn2op/tview"
  5. "github.com/gdamore/tcell/v2"
  6. )
  7. func NewConfiguredBox(box *tview.Box, cfg *config.Theme) *tview.Box {
  8. b := cfg.Border
  9. t := cfg.Title
  10. p := b.Padding
  11. box.
  12. SetBorderSet(b.Set.BorderSet).
  13. SetBorderPadding(p[0], p[1], p[2], p[3]).
  14. SetTitleAlignment(t.Alignment.Alignment).
  15. SetFocusFunc(func() {
  16. borderColor := tcell.GetColor(b.ActiveColor)
  17. box.SetBorderStyle(tcell.StyleDefault.Foreground(borderColor))
  18. titleColor := tcell.GetColor(t.ActiveColor)
  19. box.SetTitleStyle(tcell.StyleDefault.Foreground(titleColor))
  20. }).
  21. SetBlurFunc(func() {
  22. borderColor := tcell.GetColor(b.Color)
  23. box.SetBorderStyle(tcell.StyleDefault.Foreground(borderColor))
  24. titleColor := tcell.GetColor(t.Color)
  25. box.SetTitleStyle(tcell.StyleDefault.Foreground(titleColor))
  26. })
  27. if b.Enabled {
  28. box.SetBorders(tview.BordersAll)
  29. }
  30. return box
  31. }
  32. func Centered(p tview.Primitive, width, height int) tview.Primitive {
  33. return tview.NewGrid().
  34. SetColumns(0, width, 0).
  35. SetRows(0, height, 0).
  36. AddItem(p, 1, 1, 1, 1, 0, 0, true)
  37. }