User interface toolkit for Go with support for SDL2 and HTML Canvas render targets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
1.6 KiB

  1. package ui
  2. import (
  3. "fmt"
  4. "git.kirsle.net/go/render"
  5. "git.kirsle.net/go/ui/theme"
  6. )
  7. // MenuFont is the default font settings for MenuBar buttons.
  8. var MenuFont = render.Text{
  9. Size: 12,
  10. Color: render.Black,
  11. PadX: 4,
  12. PadY: 2,
  13. }
  14. // MenuBar is a frame that holds several MenuButtons, such as for the main
  15. // menu at the top of a window.
  16. type MenuBar struct {
  17. Frame
  18. name string
  19. supervisor *Supervisor
  20. buttons []*MenuButton
  21. }
  22. // NewMenuBar creates a new menu bar frame.
  23. func NewMenuBar(name string) *MenuBar {
  24. w := &MenuBar{
  25. name: name,
  26. buttons: []*MenuButton{},
  27. }
  28. w.SetBackground(theme.ButtonBackgroundColor)
  29. w.Frame.Setup()
  30. w.IDFunc(func() string {
  31. return fmt.Sprintf("MenuBar<%s>", w.name)
  32. })
  33. return w
  34. }
  35. // Supervise the menu bar, making its child menu buttons work correctly.
  36. func (w *MenuBar) Supervise(s *Supervisor) {
  37. w.supervisor = s
  38. // Supervise the existing buttons.
  39. for _, btn := range w.buttons {
  40. s.Add(btn)
  41. btn.Supervise(s)
  42. }
  43. }
  44. // AddMenu adds a new menu button to the bar. Returns the MenuButton
  45. // object so that you can add items to it.
  46. func (w *MenuBar) AddMenu(label string) *MenuButton {
  47. btn := NewMenuButton(label, NewLabel(Label{
  48. Text: label,
  49. Font: MenuFont,
  50. }))
  51. w.buttons = append(w.buttons, btn)
  52. // Pack and supervise it.
  53. w.Pack(btn, Pack{
  54. Side: W,
  55. })
  56. if w.supervisor != nil {
  57. w.supervisor.Add(btn)
  58. btn.Supervise(w.supervisor)
  59. }
  60. return btn
  61. }
  62. // PackTop returns the default Frame Pack settings to place the menu
  63. // at the top of the parent widget.
  64. func (w *MenuBar) PackTop() Pack {
  65. return Pack{
  66. Side: N,
  67. FillX: true,
  68. }
  69. }