util.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package ui
  2. import (
  3. "github.com/ayn2op/discordo/internal/config"
  4. "github.com/ayn2op/tview"
  5. )
  6. // ConfigureBox configures the provided box according to the provided theme.
  7. func ConfigureBox(box *tview.Box, cfg *config.Theme) *tview.Box {
  8. border := cfg.Border
  9. title := cfg.Title
  10. normalBorderStyle, activeBorderStyle := border.NormalStyle.Style, border.ActiveStyle.Style
  11. normalTitleStyle, activeTitleStyle := title.NormalStyle.Style, title.ActiveStyle.Style
  12. p := border.Padding
  13. box.
  14. SetBorderStyle(normalBorderStyle).
  15. SetBorderSet(border.Set.BorderSet).
  16. SetBorderPadding(p[0], p[1], p[2], p[3]).
  17. SetTitleStyle(normalTitleStyle).
  18. SetTitleAlignment(title.Alignment.Alignment).
  19. SetFocusFunc(func() {
  20. box.SetBorderStyle(activeBorderStyle)
  21. box.SetTitleStyle(activeTitleStyle)
  22. }).
  23. SetBlurFunc(func() {
  24. box.SetBorderStyle(normalBorderStyle)
  25. box.SetTitleStyle(normalTitleStyle)
  26. })
  27. if border.Enabled {
  28. box.SetBorders(tview.BordersAll)
  29. }
  30. return box
  31. }
  32. // Centered creates a new grid with provided primitive aligned in the center.
  33. func Centered(p tview.Primitive, width, height int) tview.Primitive {
  34. return tview.NewGrid().
  35. SetColumns(0, width, 0).
  36. SetRows(0, height, 0).
  37. AddItem(p, 1, 1, 1, 1, 0, 0, true)
  38. }