menu.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/gui/assets/icon"
  7. "github.com/g3n/engine/math32"
  8. "github.com/g3n/engine/window"
  9. "time"
  10. )
  11. // Menu is the menu GUI element
  12. type Menu struct {
  13. Panel // embedded panel
  14. styles *MenuStyles // pointer to current styles
  15. bar bool // true for menu bar
  16. items []*MenuItem // menu items
  17. autoOpen bool // open sub menus when mouse over if true
  18. mitem *MenuItem // parent menu item for sub menu
  19. }
  20. // MenuBodyStyle describes the style of the menu body
  21. type MenuBodyStyle BasicStyle
  22. // MenuBodyStyles describes all styles of the menu body
  23. type MenuBodyStyles struct {
  24. Normal MenuBodyStyle
  25. Over MenuBodyStyle
  26. Focus MenuBodyStyle
  27. Disabled MenuBodyStyle
  28. }
  29. // MenuStyles describes all styles of the menu body and menu item
  30. type MenuStyles struct {
  31. Body *MenuBodyStyles // Menu body styles
  32. Item *MenuItemStyles // Menu item styles
  33. }
  34. // MenuItem is an option of a Menu
  35. type MenuItem struct {
  36. Panel // embedded panel
  37. styles *MenuItemStyles // pointer to current styles
  38. menu *Menu // pointer to parent menu
  39. licon *Label // optional left icon label
  40. label *Label // optional text label (nil for separators)
  41. shortcut *Label // optional shorcut text label
  42. ricon *Label // optional right internal icon label for submenu
  43. id string // optional text id
  44. icode int // icon code (if icon is set)
  45. submenu *Menu // pointer to optional associated sub menu
  46. keyMods window.ModifierKey // shortcut key modifier
  47. keyCode window.Key // shortcut key code
  48. disabled bool // item disabled state
  49. selected bool // selection state
  50. }
  51. // MenuItemStyle describes the style of a menu item
  52. type MenuItemStyle struct {
  53. PanelStyle
  54. FgColor math32.Color4
  55. IconPaddings RectBounds
  56. ShortcutPaddings RectBounds
  57. RiconPaddings RectBounds
  58. }
  59. // MenuItemStyles describes all the menu item styles
  60. type MenuItemStyles struct {
  61. Normal MenuItemStyle
  62. Over MenuItemStyle
  63. Disabled MenuItemStyle
  64. Separator MenuItemStyle
  65. }
  66. var mapKeyModifier = map[window.ModifierKey]string{
  67. window.ModShift: "Shift",
  68. window.ModControl: "Ctrl",
  69. window.ModAlt: "Alt",
  70. }
  71. var mapKeyText = map[window.Key]string{
  72. window.KeyApostrophe: "'",
  73. window.KeyComma: ",",
  74. window.KeyMinus: "-",
  75. window.KeyPeriod: ".",
  76. window.KeySlash: "/",
  77. window.Key0: "0",
  78. window.Key1: "1",
  79. window.Key2: "2",
  80. window.Key3: "3",
  81. window.Key4: "4",
  82. window.Key5: "5",
  83. window.Key6: "6",
  84. window.Key7: "7",
  85. window.Key8: "8",
  86. window.Key9: "9",
  87. window.KeySemicolon: ";",
  88. window.KeyEqual: "=",
  89. window.KeyA: "A",
  90. window.KeyB: "B",
  91. window.KeyC: "C",
  92. window.KeyD: "D",
  93. window.KeyE: "E",
  94. window.KeyF: "F",
  95. window.KeyG: "G",
  96. window.KeyH: "H",
  97. window.KeyI: "I",
  98. window.KeyJ: "J",
  99. window.KeyK: "K",
  100. window.KeyL: "L",
  101. window.KeyM: "M",
  102. window.KeyN: "N",
  103. window.KeyO: "O",
  104. window.KeyP: "P",
  105. window.KeyQ: "Q",
  106. window.KeyR: "R",
  107. window.KeyS: "S",
  108. window.KeyT: "T",
  109. window.KeyU: "U",
  110. window.KeyV: "V",
  111. window.KeyW: "W",
  112. window.KeyX: "X",
  113. window.KeyY: "Y",
  114. window.KeyZ: "Z",
  115. window.KeyF1: "F1",
  116. window.KeyF2: "F2",
  117. window.KeyF3: "F3",
  118. window.KeyF4: "F4",
  119. window.KeyF5: "F5",
  120. window.KeyF6: "F6",
  121. window.KeyF7: "F7",
  122. window.KeyF8: "F8",
  123. window.KeyF9: "F9",
  124. window.KeyF10: "F10",
  125. window.KeyF11: "F11",
  126. window.KeyF12: "F12",
  127. }
  128. // NewMenuBar creates and returns a pointer to a new empty menu bar
  129. func NewMenuBar() *Menu {
  130. m := NewMenu()
  131. m.bar = true
  132. m.Panel.Subscribe(OnMouseOut, m.onMouse)
  133. return m
  134. }
  135. // NewMenu creates and returns a pointer to a new empty vertical menu
  136. func NewMenu() *Menu {
  137. m := new(Menu)
  138. m.Panel.Initialize(0, 0)
  139. m.styles = &StyleDefault().Menu
  140. m.items = make([]*MenuItem, 0)
  141. m.Panel.Subscribe(OnCursorEnter, m.onCursor)
  142. m.Panel.Subscribe(OnCursor, m.onCursor)
  143. m.Panel.Subscribe(OnKeyDown, m.onKey)
  144. m.Panel.Subscribe(OnResize, m.onResize)
  145. m.update()
  146. return m
  147. }
  148. // AddOption creates and adds a new menu item to this menu with the
  149. // specified text and returns the pointer to the created menu item.
  150. func (m *Menu) AddOption(text string) *MenuItem {
  151. mi := newMenuItem(text, m.styles.Item)
  152. m.Panel.Add(mi)
  153. m.items = append(m.items, mi)
  154. mi.menu = m
  155. m.recalc()
  156. return mi
  157. }
  158. // AddSeparator creates and adds a new separator to the menu
  159. func (m *Menu) AddSeparator() *MenuItem {
  160. mi := newMenuItem("", m.styles.Item)
  161. m.Panel.Add(mi)
  162. m.items = append(m.items, mi)
  163. mi.menu = m
  164. m.recalc()
  165. return mi
  166. }
  167. // AddMenu creates and adds a new menu item to this menu with the
  168. // specified text and sub menu.
  169. // Returns the pointer to the created menu item.
  170. func (m *Menu) AddMenu(text string, subm *Menu) *MenuItem {
  171. mi := newMenuItem(text, m.styles.Item)
  172. m.Panel.Add(mi)
  173. m.items = append(m.items, mi)
  174. mi.submenu = subm
  175. mi.submenu.SetVisible(false)
  176. mi.submenu.SetBounded(false)
  177. mi.submenu.mitem = mi
  178. mi.submenu.autoOpen = true
  179. mi.menu = m
  180. if !m.bar {
  181. mi.ricon = NewIcon(string(icon.PlayArrow))
  182. mi.Panel.Add(mi.ricon)
  183. }
  184. mi.Panel.Add(mi.submenu)
  185. mi.update()
  186. m.recalc()
  187. return mi
  188. }
  189. // RemoveItem removes the specified menu item from this menu
  190. func (m *Menu) RemoveItem(mi *MenuItem) {
  191. }
  192. // onCursor process subscribed cursor events
  193. func (m *Menu) onCursor(evname string, ev interface{}) {
  194. switch evname {
  195. case OnCursorEnter:
  196. m.root.SetKeyFocus(m)
  197. }
  198. m.root.StopPropagation(StopAll)
  199. }
  200. // onKey process subscribed key events
  201. func (m *Menu) onKey(evname string, ev interface{}) {
  202. sel := m.selectedPos()
  203. kev := ev.(*window.KeyEvent)
  204. switch kev.Keycode {
  205. // Select next enabled menu item
  206. case window.KeyDown:
  207. if sel < 0 {
  208. return
  209. }
  210. mi := m.items[sel]
  211. // Select next enabled menu item
  212. if m.bar {
  213. // If selected item is not a sub menu, ignore
  214. if mi.submenu == nil {
  215. return
  216. }
  217. // Sets autoOpen and selects sub menu
  218. m.autoOpen = true
  219. mi.update()
  220. m.root.SetKeyFocus(mi.submenu)
  221. mi.submenu.setSelectedPos(0)
  222. return
  223. }
  224. // Select next enabled menu item for vertical menu
  225. next := m.nextItem(sel)
  226. m.setSelectedPos(next)
  227. // Up -> Previous item for vertical menus
  228. case window.KeyUp:
  229. if sel < 0 {
  230. return
  231. }
  232. if m.bar {
  233. return
  234. }
  235. prev := m.prevItem(sel)
  236. m.setSelectedPos(prev)
  237. // Left -> Previous menu item for menu bar
  238. case window.KeyLeft:
  239. if sel < 0 {
  240. return
  241. }
  242. // For menu bar, select previous menu item
  243. if m.bar {
  244. prev := m.prevItem(sel)
  245. m.setSelectedPos(prev)
  246. return
  247. }
  248. // If menu has parent menu item
  249. if m.mitem != nil {
  250. if m.mitem.menu.bar {
  251. sel := m.mitem.menu.selectedPos()
  252. prev := m.mitem.menu.prevItem(sel)
  253. m.mitem.menu.setSelectedPos(prev)
  254. } else {
  255. m.mitem.menu.setSelectedItem(m.mitem)
  256. }
  257. m.root.SetKeyFocus(m.mitem.menu)
  258. return
  259. }
  260. // Right -> Next menu bar item || Next sub menu
  261. case window.KeyRight:
  262. if sel < 0 {
  263. return
  264. }
  265. mi := m.items[sel]
  266. // For menu bar, select next menu item
  267. if m.bar {
  268. next := m.nextItem(sel)
  269. m.setSelectedPos(next)
  270. return
  271. }
  272. // Enter into sub menu
  273. if mi.submenu != nil {
  274. m.root.SetKeyFocus(mi.submenu)
  275. mi.submenu.setSelectedPos(0)
  276. return
  277. }
  278. // If parent menu of this menu item is bar menu
  279. if m.mitem != nil && m.mitem.menu.bar {
  280. sel := m.mitem.menu.selectedPos()
  281. next := m.mitem.menu.nextItem(sel)
  282. m.mitem.menu.setSelectedPos(next)
  283. m.root.SetKeyFocus(m.mitem.menu)
  284. }
  285. // Enter -> Select menu option
  286. case window.KeyEnter:
  287. if sel < 0 {
  288. return
  289. }
  290. mi := m.items[sel]
  291. mi.activate()
  292. // Check for menu items shortcuts
  293. default:
  294. var root *Menu
  295. if sel < 0 {
  296. root = m
  297. } else {
  298. mi := m.items[sel]
  299. root = mi.rootMenu()
  300. }
  301. found := root.checkKey(kev)
  302. if found == nil {
  303. return
  304. }
  305. if found.submenu == nil {
  306. found.activate()
  307. return
  308. }
  309. if found.menu.bar {
  310. found.menu.autoOpen = true
  311. }
  312. found.menu.setSelectedItem(found)
  313. }
  314. }
  315. // onMouse process subscribed mouse events for the menu
  316. func (m *Menu) onMouse(evname string, ev interface{}) {
  317. // Clear menu bar after some time, to give time for menu items
  318. // to receive onMouse events.
  319. m.Root().SetTimeout(1*time.Millisecond, nil, func(arg interface{}) {
  320. m.autoOpen = false
  321. m.setSelectedPos(-1)
  322. })
  323. }
  324. // onResize process menu onResize events
  325. func (m *Menu) onResize(evname string, ev interface{}) {
  326. if m.bar {
  327. m.recalcBar(false)
  328. }
  329. }
  330. // checkKey checks if this menu and any of its children contains
  331. // a menu item with the specified key shortcut
  332. func (m *Menu) checkKey(kev *window.KeyEvent) *MenuItem {
  333. for i := 0; i < len(m.items); i++ {
  334. mi := m.items[i]
  335. if mi.keyCode == kev.Keycode && mi.keyMods == kev.Mods {
  336. return mi
  337. }
  338. if mi.submenu != nil {
  339. found := mi.submenu.checkKey(kev)
  340. if found != nil {
  341. return found
  342. }
  343. }
  344. }
  345. return nil
  346. }
  347. // setSelectedPos sets the menu item at the specified position as selected
  348. // and all others as not selected.
  349. func (m *Menu) setSelectedPos(pos int) {
  350. for i := 0; i < len(m.items); i++ {
  351. mi := m.items[i]
  352. if i == pos {
  353. mi.selected = true
  354. } else {
  355. mi.selected = false
  356. }
  357. // If menu item has a sub menu, unselects the sub menu options recursively
  358. if mi.submenu != nil {
  359. mi.submenu.setSelectedPos(-1)
  360. }
  361. mi.update()
  362. }
  363. }
  364. // setSelectedItem sets the specified menu item as selected
  365. // and all others as not selected
  366. func (m *Menu) setSelectedItem(mitem *MenuItem) {
  367. for i := 0; i < len(m.items); i++ {
  368. mi := m.items[i]
  369. if mi == mitem {
  370. mi.selected = true
  371. } else {
  372. mi.selected = false
  373. }
  374. // If menu item has a sub menu, unselects the sub menu options recursively
  375. if mi.submenu != nil {
  376. mi.submenu.setSelectedItem(nil)
  377. }
  378. mi.update()
  379. }
  380. }
  381. // selectedPos returns the position of the current selected menu item
  382. // Returns -1 if no item selected
  383. func (m *Menu) selectedPos() int {
  384. for i := 0; i < len(m.items); i++ {
  385. mi := m.items[i]
  386. if mi.selected {
  387. return i
  388. }
  389. }
  390. return -1
  391. }
  392. // nextItem returns the position of the next enabled option from the
  393. // specified position
  394. func (m *Menu) nextItem(pos int) int {
  395. res := 0
  396. for i := pos + 1; i < len(m.items); i++ {
  397. mi := m.items[i]
  398. if mi.disabled || mi.label == nil {
  399. continue
  400. }
  401. res = i
  402. break
  403. }
  404. return res
  405. }
  406. // prevItem returns the position of previous enabled menu item from
  407. // the specified position
  408. func (m *Menu) prevItem(pos int) int {
  409. res := len(m.items) - 1
  410. for i := pos - 1; i >= 0 && i < len(m.items); i-- {
  411. mi := m.items[i]
  412. if mi.disabled || mi.label == nil {
  413. continue
  414. }
  415. res = i
  416. break
  417. }
  418. return res
  419. }
  420. // update updates the menu visual state
  421. func (m *Menu) update() {
  422. m.applyStyle(&m.styles.Body.Normal)
  423. }
  424. // applyStyle applies the specified menu body style
  425. func (m *Menu) applyStyle(mbs *MenuBodyStyle) {
  426. m.Panel.ApplyStyle(&mbs.PanelStyle)
  427. }
  428. // recalc recalculates the positions of this menu internal items
  429. // and the content width and height of the menu
  430. func (m *Menu) recalc() {
  431. if m.bar {
  432. m.recalcBar(true)
  433. return
  434. }
  435. // Find the maximum icon and label widths
  436. minWidth := float32(0)
  437. iconWidth := float32(0)
  438. labelWidth := float32(0)
  439. shortcutWidth := float32(0)
  440. riconWidth := float32(0)
  441. for i := 0; i < len(m.items); i++ {
  442. mi := m.items[i]
  443. minWidth = mi.MinWidth()
  444. // Separator
  445. if mi.label == nil {
  446. continue
  447. }
  448. // Left icon width
  449. if mi.licon != nil && mi.licon.width > iconWidth {
  450. iconWidth = mi.licon.width
  451. }
  452. // Option label width
  453. if mi.label.width > labelWidth {
  454. labelWidth = mi.label.width
  455. }
  456. // Shortcut label width
  457. if mi.shortcut != nil && mi.shortcut.width > shortcutWidth {
  458. shortcutWidth = mi.shortcut.width
  459. }
  460. // Right icon (submenu indicator) width
  461. if mi.ricon != nil && mi.ricon.width > riconWidth {
  462. riconWidth = mi.ricon.width
  463. }
  464. }
  465. width := minWidth + iconWidth + labelWidth + shortcutWidth + riconWidth
  466. // Sets the position and width of the menu items
  467. // The height is defined by the menu item itself
  468. px := float32(0)
  469. py := float32(0)
  470. for i := 0; i < len(m.items); i++ {
  471. mi := m.items[i]
  472. mi.SetPosition(px, py)
  473. mh := mi.minHeight()
  474. py += mh
  475. mi.SetSize(width, mh)
  476. mi.recalc(iconWidth, labelWidth, shortcutWidth)
  477. }
  478. m.SetContentSize(width, py)
  479. }
  480. // recalcBar recalculates the positions of this MenuBar internal items
  481. // If setSize is true it also sets the size of the menu bar
  482. func (m *Menu) recalcBar(setSize bool) {
  483. // Calculate the maximum item height
  484. height := float32(0)
  485. for i := 0; i < len(m.items); i++ {
  486. mi := m.items[i]
  487. if mi.minHeight() > height {
  488. height = mi.minHeight()
  489. }
  490. }
  491. // Calculates the y position of the items to center inside the menu panel
  492. py := (m.ContentHeight() - height) / 2
  493. if py < 0 {
  494. py = 0
  495. }
  496. // Sets the position of each item
  497. px := float32(0)
  498. for i := 0; i < len(m.items); i++ {
  499. mi := m.items[i]
  500. mi.SetPosition(px, py)
  501. width := float32(0)
  502. width = mi.minWidth()
  503. mi.SetSize(width, height)
  504. px += mi.Width()
  505. }
  506. // Sets the size of this menu if requested
  507. if setSize {
  508. m.SetContentSize(px, height)
  509. }
  510. }
  511. // newMenuItem creates and returns a pointer to a new menu item
  512. // with the specified text.
  513. func newMenuItem(text string, styles *MenuItemStyles) *MenuItem {
  514. mi := new(MenuItem)
  515. mi.Panel.Initialize(0, 0)
  516. mi.styles = styles
  517. if text != "" {
  518. mi.label = NewLabel(text)
  519. mi.Panel.Add(mi.label)
  520. mi.Panel.Subscribe(OnCursorEnter, mi.onCursor)
  521. mi.Panel.Subscribe(OnCursor, mi.onCursor)
  522. mi.Panel.Subscribe(OnMouseDown, mi.onMouse)
  523. }
  524. mi.update()
  525. return mi
  526. }
  527. // SetIcon sets the left icon of this menu item
  528. // If an image was previously set it is replaced by this icon
  529. func (mi *MenuItem) SetIcon(icon string) *MenuItem {
  530. // Remove and dispose previous icon
  531. if mi.licon != nil {
  532. mi.Panel.Remove(mi.licon)
  533. mi.Dispose()
  534. mi.licon = nil
  535. }
  536. // Sets the new icon
  537. mi.licon = NewIcon(icon)
  538. mi.Panel.Add(mi.licon)
  539. mi.update()
  540. return mi
  541. }
  542. // SetImage sets the left image of this menu item
  543. // If an icon was previously set it is replaced by this image
  544. func (mi *MenuItem) SetImage(img *Image) {
  545. }
  546. // SetText sets the text of this menu item
  547. func (mi *MenuItem) SetText(text string) *MenuItem {
  548. if mi.label == nil {
  549. return mi
  550. }
  551. mi.label.SetText(text)
  552. mi.update()
  553. mi.menu.recalc()
  554. return mi
  555. }
  556. // SetShortcut sets the keyboard shortcut of this menu item
  557. func (mi *MenuItem) SetShortcut(mods window.ModifierKey, key window.Key) *MenuItem {
  558. if mapKeyText[key] == "" {
  559. panic("Invalid menu shortcut key")
  560. }
  561. mi.keyMods = mods
  562. mi.keyCode = key
  563. // If parent menu is a menu bar, nothing more to do
  564. if mi.menu.bar {
  565. return mi
  566. }
  567. // Builds shortcut text
  568. text := ""
  569. if mi.keyMods&window.ModShift != 0 {
  570. text = mapKeyModifier[window.ModShift]
  571. }
  572. if mi.keyMods&window.ModControl != 0 {
  573. if text != "" {
  574. text += "+"
  575. }
  576. text += mapKeyModifier[window.ModControl]
  577. }
  578. if mi.keyMods&window.ModAlt != 0 {
  579. if text != "" {
  580. text += "+"
  581. }
  582. text += mapKeyModifier[window.ModAlt]
  583. }
  584. if text != "" {
  585. text += "+"
  586. }
  587. text += mapKeyText[key]
  588. // Creates and adds shortcut label
  589. mi.shortcut = NewLabel(text)
  590. mi.Panel.Add(mi.shortcut)
  591. mi.update()
  592. mi.menu.recalc()
  593. return mi
  594. }
  595. // SetSubmenu sets an associated sub menu item for this menu item
  596. func (mi *MenuItem) SetSubmenu(smi *MenuItem) *MenuItem {
  597. return mi
  598. }
  599. // SetEnabled sets the enabled state of this menu item
  600. func (mi *MenuItem) SetEnabled(enabled bool) *MenuItem {
  601. mi.disabled = !enabled
  602. mi.update()
  603. return mi
  604. }
  605. // SetId sets this menu item string id which can be used to identify
  606. // the selected menu option.
  607. func (mi *MenuItem) SetId(id string) *MenuItem {
  608. mi.id = id
  609. return mi
  610. }
  611. // Id returns this menu item current id
  612. func (mi *MenuItem) Id() string {
  613. return mi.id
  614. }
  615. // IdPath returns a slice with the path of menu items ids to this menu item
  616. func (mi *MenuItem) IdPath() []string {
  617. // Builds lists of menu items ids
  618. path := []string{mi.id}
  619. menu := mi.menu
  620. for menu.mitem != nil {
  621. path = append(path, menu.mitem.id)
  622. menu = menu.mitem.menu
  623. }
  624. // Reverse and returns id list
  625. res := make([]string, 0, len(path))
  626. for i := len(path) - 1; i >= 0; i-- {
  627. res = append(res, path[i])
  628. }
  629. return res
  630. }
  631. // onCursor processes subscribed cursor events over the menu item
  632. func (mi *MenuItem) onCursor(evname string, ev interface{}) {
  633. switch evname {
  634. case OnCursorEnter:
  635. mi.menu.setSelectedItem(mi)
  636. }
  637. mi.root.StopPropagation(StopAll)
  638. }
  639. // onMouse processes subscribed mouse events over the menu item
  640. func (mi *MenuItem) onMouse(evname string, ev interface{}) {
  641. switch evname {
  642. case OnMouseDown:
  643. // MenuBar option
  644. if mi.menu.bar {
  645. mi.menu.autoOpen = !mi.menu.autoOpen
  646. if mi.submenu != nil && mi.submenu.Visible() {
  647. mi.submenu.SetVisible(false)
  648. mi.root.SetKeyFocus(mi.menu)
  649. } else {
  650. mi.update()
  651. }
  652. }
  653. if mi.submenu != nil {
  654. return
  655. }
  656. mi.activate()
  657. }
  658. mi.root.StopPropagation(StopAll)
  659. }
  660. // activate activates this menu item dispatching OnClick events
  661. func (mi *MenuItem) activate() {
  662. rm := mi.rootMenu()
  663. if rm.bar {
  664. rm.autoOpen = false
  665. }
  666. rm.setSelectedPos(-1)
  667. mi.root.SetKeyFocus(rm)
  668. mi.dispatchAll(OnClick, mi)
  669. }
  670. // rootMenu returns the root menu for this menu item
  671. func (mi *MenuItem) rootMenu() *Menu {
  672. root := mi.menu
  673. for root.mitem != nil {
  674. root = root.mitem.menu
  675. }
  676. return root
  677. }
  678. // dispatchAll dispatch the specified event for this menu item
  679. // and all its parents
  680. func (mi *MenuItem) dispatchAll(evname string, ev interface{}) {
  681. mi.Dispatch(evname, ev)
  682. pmenu := mi.menu
  683. for {
  684. pmenu.Dispatch(evname, ev)
  685. if pmenu.mitem == nil {
  686. break
  687. }
  688. pmenu = pmenu.mitem.menu
  689. }
  690. }
  691. // update updates the menu item visual state
  692. func (mi *MenuItem) update() {
  693. // Separator
  694. if mi.label == nil {
  695. mi.applyStyle(&mi.styles.Separator)
  696. return
  697. }
  698. // Disabled item
  699. if mi.disabled {
  700. mi.applyStyle(&mi.styles.Disabled)
  701. return
  702. }
  703. // Selected item
  704. if mi.selected {
  705. mi.applyStyle(&mi.styles.Over)
  706. if mi.submenu != nil && mi.menu.autoOpen {
  707. mi.menu.SetTopChild(mi)
  708. mi.submenu.SetVisible(true)
  709. if mi.menu != nil && mi.menu.bar {
  710. mi.submenu.SetPosition(0, mi.Height()-2)
  711. } else {
  712. mi.submenu.SetPosition(mi.Width()-2, 0)
  713. }
  714. }
  715. return
  716. }
  717. // If this menu item has a sub menu and the sub menu is not active,
  718. // hides the sub menu
  719. if mi.submenu != nil {
  720. mi.submenu.SetVisible(false)
  721. }
  722. mi.applyStyle(&mi.styles.Normal)
  723. }
  724. // applyStyle applies the specified menu item style
  725. func (mi *MenuItem) applyStyle(mis *MenuItemStyle) {
  726. mi.Panel.ApplyStyle(&mis.PanelStyle)
  727. if mi.licon != nil {
  728. mi.licon.SetPaddingsFrom(&mis.IconPaddings)
  729. }
  730. if mi.label != nil {
  731. mi.label.SetColor4(&mis.FgColor)
  732. }
  733. if mi.shortcut != nil {
  734. mi.shortcut.SetPaddingsFrom(&mis.ShortcutPaddings)
  735. }
  736. if mi.ricon != nil {
  737. mi.ricon.SetPaddingsFrom(&mis.RiconPaddings)
  738. }
  739. }
  740. // recalc recalculates the positions of this menu item internal panels
  741. func (mi *MenuItem) recalc(iconWidth, labelWidth, shortcutWidth float32) {
  742. // Separator
  743. if mi.label == nil {
  744. return
  745. }
  746. if mi.licon != nil {
  747. py := (mi.label.height - mi.licon.height) / 2
  748. mi.licon.SetPosition(0, py)
  749. }
  750. mi.label.SetPosition(iconWidth, 0)
  751. if mi.shortcut != nil {
  752. mi.shortcut.SetPosition(iconWidth+labelWidth, 0)
  753. }
  754. if mi.ricon != nil {
  755. mi.ricon.SetPosition(iconWidth+labelWidth+shortcutWidth, 0)
  756. }
  757. }
  758. // minHeight returns the minimum height of this menu item
  759. func (mi *MenuItem) minHeight() float32 {
  760. mh := mi.MinHeight()
  761. if mi.label == nil {
  762. return mh + 1
  763. }
  764. mh += mi.label.height
  765. return mh
  766. }
  767. // minWidth returns the minimum width of this menu item
  768. func (mi *MenuItem) minWidth() float32 {
  769. mw := mi.MinWidth()
  770. if mi.label == nil {
  771. return mw + 1
  772. }
  773. mw += mi.label.width
  774. return mw
  775. }