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.
 
 
 
 
 

84 lines
2.3 KiB

  1. package ui_test
  2. import (
  3. "git.kirsle.net/go/ui"
  4. )
  5. // Example of using the menu widgets.
  6. func ExampleMenu() {
  7. mw, err := ui.NewMainWindow("Menu Bar Example", 800, 600)
  8. if err != nil {
  9. panic(err)
  10. }
  11. // Create a main menu for your window.
  12. menu := ui.NewMenuBar("Main Menu")
  13. // File menu. Some items with accelerators, some without.
  14. // NOTE: key bindings are up to you, the accelerators are
  15. // purely decorative.
  16. file := menu.AddMenu("File")
  17. file.AddItemAccel("New", "Ctrl-N", func() {})
  18. file.AddItemAccel("Open", "Ctrl-O", func() {})
  19. file.AddItemAccel("Save", "Ctrl-S", func() {})
  20. file.AddItem("Save as...", func() {})
  21. file.AddSeparator()
  22. file.AddItem("Close window", func() {})
  23. file.AddItemAccel("Exit", "Alt-F4", func() {})
  24. // Help menu.
  25. help := menu.AddMenu("Help")
  26. help.AddItemAccel("Contents", "F1", func() {})
  27. help.AddItem("About", func() {})
  28. // Give the menu bar your Supervisor so it can wire all
  29. // events up and make the menus work.
  30. menu.Supervise(mw.Supervisor())
  31. // Compute and pack the menu bar against the top of
  32. // the main window (or other parent container)
  33. menu.Compute(mw.Engine)
  34. mw.Pack(menu, menu.PackTop()) // Side: N, FillX: true
  35. // Each loop you must then:
  36. // - Call Supervisor.Loop() as normal to handle events.
  37. // - Call Supervisor.Present() to draw the modal popup menus.
  38. // MainLoop() of the MainWindow does this for you.
  39. mw.MainLoop()
  40. }
  41. // Example of using the MenuButton.
  42. func ExampleMenuButton() {
  43. mw, err := ui.NewMainWindow("Menu Button", 800, 600)
  44. if err != nil {
  45. panic(err)
  46. }
  47. // Create a MenuButton much as you would a normal Button.
  48. btn := ui.NewMenuButton("Button1", ui.NewLabel(ui.Label{
  49. Text: "File",
  50. }))
  51. mw.Place(btn, ui.Place{ // place it in the center
  52. Center: true,
  53. Middle: true,
  54. })
  55. // Add menu items to it.
  56. btn.AddItemAccel("New", "Ctrl-N", func() {})
  57. btn.AddItemAccel("Open", "Ctrl-O", func() {})
  58. btn.AddItemAccel("Save", "Ctrl-S", func() {})
  59. btn.AddItem("Save as...", func() {})
  60. btn.AddSeparator()
  61. btn.AddItem("Close window", func() {})
  62. btn.AddItemAccel("Exit", "Alt-F4", func() {})
  63. // Add the button to Supervisor for events to work.
  64. btn.Supervise(mw.Supervisor())
  65. // Each loop you must then:
  66. // - Call Supervisor.Loop() as normal to handle events.
  67. // - Call Supervisor.Present() to draw the modal popup menus.
  68. // MainLoop() of the MainWindow does this for you.
  69. mw.MainLoop()
  70. }