menu.go 20 KB

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