menu.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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(OnMouseDownOut, 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(m, 0, 0)
  139. m.styles = &StyleDefault().Menu
  140. m.items = make([]*MenuItem, 0)
  141. m.Panel.Subscribe(OnKeyDown, m.onKey)
  142. m.Panel.Subscribe(OnResize, m.onResize)
  143. m.update()
  144. return m
  145. }
  146. // AddOption creates and adds a new menu item to this menu with the
  147. // specified text and returns the pointer to the created menu item.
  148. func (m *Menu) AddOption(text string) *MenuItem {
  149. mi := newMenuItem(text, m.styles.Item)
  150. m.Panel.Add(mi)
  151. m.items = append(m.items, mi)
  152. mi.menu = m
  153. m.recalc()
  154. return mi
  155. }
  156. // AddSeparator creates and adds a new separator to the menu
  157. func (m *Menu) AddSeparator() *MenuItem {
  158. mi := newMenuItem("", m.styles.Item)
  159. m.Panel.Add(mi)
  160. m.items = append(m.items, mi)
  161. mi.menu = m
  162. m.recalc()
  163. return mi
  164. }
  165. // AddMenu creates and adds a new menu item to this menu with the
  166. // specified text and sub menu.
  167. // Returns the pointer to the created menu item.
  168. func (m *Menu) AddMenu(text string, subm *Menu) *MenuItem {
  169. mi := newMenuItem(text, m.styles.Item)
  170. mi.zLayerDelta = 1
  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. // onKey process subscribed key events
  192. func (m *Menu) onKey(evname string, ev interface{}) {
  193. sel := m.selectedPos()
  194. kev := ev.(*window.KeyEvent)
  195. switch kev.Key {
  196. // Select next enabled menu item
  197. case window.KeyDown:
  198. if sel < 0 {
  199. return
  200. }
  201. mi := m.items[sel]
  202. // Select next enabled menu item
  203. if m.bar {
  204. // If selected item is not a sub menu, ignore
  205. if mi.submenu == nil {
  206. return
  207. }
  208. // Sets autoOpen and selects sub menu
  209. m.autoOpen = true
  210. mi.update()
  211. Manager().SetKeyFocus(mi.submenu)
  212. mi.submenu.setSelectedPos(0)
  213. return
  214. }
  215. // Select next enabled menu item for vertical menu
  216. next := m.nextItem(sel)
  217. m.setSelectedPos(next)
  218. // Up -> Previous item for vertical menus
  219. case window.KeyUp:
  220. if sel < 0 {
  221. return
  222. }
  223. if m.bar {
  224. return
  225. }
  226. prev := m.prevItem(sel)
  227. m.setSelectedPos(prev)
  228. // Left -> Previous menu item for menu bar
  229. case window.KeyLeft:
  230. if sel < 0 {
  231. return
  232. }
  233. // For menu bar, select previous menu item
  234. if m.bar {
  235. prev := m.prevItem(sel)
  236. m.setSelectedPos(prev)
  237. return
  238. }
  239. // If menu has parent menu item
  240. if m.mitem != nil {
  241. if m.mitem.menu.bar {
  242. sel := m.mitem.menu.selectedPos()
  243. prev := m.mitem.menu.prevItem(sel)
  244. m.mitem.menu.setSelectedPos(prev)
  245. } else {
  246. m.mitem.menu.setSelectedItem(m.mitem)
  247. }
  248. Manager().SetKeyFocus(m.mitem.menu)
  249. return
  250. }
  251. // Right -> Next menu bar item || Next sub menu
  252. case window.KeyRight:
  253. if sel < 0 {
  254. return
  255. }
  256. mi := m.items[sel]
  257. // For menu bar, select next menu item
  258. if m.bar {
  259. next := m.nextItem(sel)
  260. m.setSelectedPos(next)
  261. return
  262. }
  263. // Enter into sub menu
  264. if mi.submenu != nil {
  265. Manager().SetKeyFocus(mi.submenu)
  266. mi.submenu.setSelectedPos(0)
  267. return
  268. }
  269. // If parent menu of this menu item is bar menu
  270. if m.mitem != nil && m.mitem.menu.bar {
  271. sel := m.mitem.menu.selectedPos()
  272. next := m.mitem.menu.nextItem(sel)
  273. m.mitem.menu.setSelectedPos(next)
  274. Manager().SetKeyFocus(m.mitem.menu)
  275. }
  276. // Enter -> Select menu option
  277. case window.KeyEnter:
  278. if sel < 0 {
  279. return
  280. }
  281. mi := m.items[sel]
  282. mi.activate()
  283. // Check for menu items shortcuts
  284. default:
  285. var root *Menu
  286. if sel < 0 {
  287. root = m
  288. } else {
  289. mi := m.items[sel]
  290. root = mi.rootMenu()
  291. }
  292. found := root.checkKey(kev)
  293. if found == nil {
  294. return
  295. }
  296. if found.submenu == nil {
  297. found.activate()
  298. return
  299. }
  300. if found.menu.bar {
  301. found.menu.autoOpen = true
  302. }
  303. found.menu.setSelectedItem(found)
  304. }
  305. }
  306. // onMouse process subscribed mouse events for the menu
  307. func (m *Menu) onMouse(evname string, ev interface{}) {
  308. // Clear menu bar after some time, to give time for menu items
  309. // to receive onMouse events.
  310. Manager().SetTimeout(1*time.Millisecond, nil, func(arg interface{}) {
  311. m.autoOpen = false
  312. m.setSelectedPos(-1)
  313. })
  314. }
  315. // onResize process menu onResize events
  316. func (m *Menu) onResize(evname string, ev interface{}) {
  317. if m.bar {
  318. m.recalcBar(false)
  319. }
  320. }
  321. // checkKey checks if this menu and any of its children contains
  322. // a menu item with the specified key shortcut
  323. func (m *Menu) checkKey(kev *window.KeyEvent) *MenuItem {
  324. for i := 0; i < len(m.items); i++ {
  325. mi := m.items[i]
  326. if mi.keyCode == kev.Key && mi.keyMods == kev.Mods {
  327. return mi
  328. }
  329. if mi.submenu != nil {
  330. found := mi.submenu.checkKey(kev)
  331. if found != nil {
  332. return found
  333. }
  334. }
  335. }
  336. return nil
  337. }
  338. // setSelectedPos sets the menu item at the specified position as selected
  339. // and all others as not selected.
  340. func (m *Menu) setSelectedPos(pos int) {
  341. for i := 0; i < len(m.items); i++ {
  342. mi := m.items[i]
  343. if i == pos {
  344. mi.selected = true
  345. } else {
  346. mi.selected = false
  347. }
  348. // If menu item has a sub menu, unselects the sub menu options recursively
  349. if mi.submenu != nil {
  350. mi.submenu.setSelectedPos(-1)
  351. }
  352. mi.update()
  353. }
  354. }
  355. // setSelectedItem sets the specified menu item as selected
  356. // and all others as not selected
  357. func (m *Menu) setSelectedItem(mitem *MenuItem) {
  358. for i := 0; i < len(m.items); i++ {
  359. mi := m.items[i]
  360. if mi == mitem {
  361. mi.selected = true
  362. } else {
  363. mi.selected = false
  364. }
  365. // If menu item has a sub menu, unselects the sub menu options recursively
  366. if mi.submenu != nil {
  367. mi.submenu.setSelectedItem(nil)
  368. }
  369. mi.update()
  370. }
  371. }
  372. // selectedPos returns the position of the current selected menu item
  373. // Returns -1 if no item selected
  374. func (m *Menu) selectedPos() int {
  375. for i := 0; i < len(m.items); i++ {
  376. mi := m.items[i]
  377. if mi.selected {
  378. return i
  379. }
  380. }
  381. return -1
  382. }
  383. // nextItem returns the position of the next enabled option from the
  384. // specified position
  385. func (m *Menu) nextItem(pos int) int {
  386. res := 0
  387. for i := pos + 1; i < len(m.items); i++ {
  388. mi := m.items[i]
  389. if mi.disabled || mi.label == nil {
  390. continue
  391. }
  392. res = i
  393. break
  394. }
  395. return res
  396. }
  397. // prevItem returns the position of previous enabled menu item from
  398. // the specified position
  399. func (m *Menu) prevItem(pos int) int {
  400. res := len(m.items) - 1
  401. for i := pos - 1; i >= 0 && i < len(m.items); i-- {
  402. mi := m.items[i]
  403. if mi.disabled || mi.label == nil {
  404. continue
  405. }
  406. res = i
  407. break
  408. }
  409. return res
  410. }
  411. // update updates the menu visual state
  412. func (m *Menu) update() {
  413. m.applyStyle(&m.styles.Body.Normal)
  414. }
  415. // applyStyle applies the specified menu body style
  416. func (m *Menu) applyStyle(mbs *MenuBodyStyle) {
  417. m.Panel.ApplyStyle(&mbs.PanelStyle)
  418. }
  419. // recalc recalculates the positions of this menu internal items
  420. // and the content width and height of the menu
  421. func (m *Menu) recalc() {
  422. if m.bar {
  423. m.recalcBar(true)
  424. return
  425. }
  426. // Find the maximum icon and label widths
  427. minWidth := float32(0)
  428. iconWidth := float32(0)
  429. labelWidth := float32(0)
  430. shortcutWidth := float32(0)
  431. riconWidth := float32(0)
  432. for i := 0; i < len(m.items); i++ {
  433. mi := m.items[i]
  434. minWidth = mi.MinWidth()
  435. // Separator
  436. if mi.label == nil {
  437. continue
  438. }
  439. // Left icon width
  440. if mi.licon != nil && mi.licon.width > iconWidth {
  441. iconWidth = mi.licon.width
  442. }
  443. // Option label width
  444. if mi.label.width > labelWidth {
  445. labelWidth = mi.label.width
  446. }
  447. // Shortcut label width
  448. if mi.shortcut != nil && mi.shortcut.width > shortcutWidth {
  449. shortcutWidth = mi.shortcut.width
  450. }
  451. // Right icon (submenu indicator) width
  452. if mi.ricon != nil && mi.ricon.width > riconWidth {
  453. riconWidth = mi.ricon.width
  454. }
  455. }
  456. width := minWidth + iconWidth + labelWidth + shortcutWidth + riconWidth
  457. // Sets the position and width of the menu items
  458. // The height is defined by the menu item itself
  459. px := float32(0)
  460. py := float32(0)
  461. for i := 0; i < len(m.items); i++ {
  462. mi := m.items[i]
  463. mi.SetPosition(px, py)
  464. mh := mi.minHeight()
  465. py += mh
  466. mi.SetSize(width, mh)
  467. mi.recalc(iconWidth, labelWidth, shortcutWidth)
  468. }
  469. m.SetContentSize(width, py)
  470. }
  471. // recalcBar recalculates the positions of this MenuBar internal items
  472. // If setSize is true it also sets the size of the menu bar
  473. func (m *Menu) recalcBar(setSize bool) {
  474. // Calculate the maximum item height
  475. height := float32(0)
  476. for i := 0; i < len(m.items); i++ {
  477. mi := m.items[i]
  478. if mi.minHeight() > height {
  479. height = mi.minHeight()
  480. }
  481. }
  482. // Calculates the y position of the items to center inside the menu panel
  483. py := (m.ContentHeight() - height) / 2
  484. if py < 0 {
  485. py = 0
  486. }
  487. // Sets the position of each item
  488. px := float32(0)
  489. for i := 0; i < len(m.items); i++ {
  490. mi := m.items[i]
  491. mi.SetPosition(px, py)
  492. width := float32(0)
  493. width = mi.minWidth()
  494. mi.SetSize(width, height)
  495. px += mi.Width()
  496. }
  497. // Sets the size of this menu if requested
  498. if setSize {
  499. m.SetContentSize(px, height)
  500. }
  501. }
  502. // newMenuItem creates and returns a pointer to a new menu item
  503. // with the specified text.
  504. func newMenuItem(text string, styles *MenuItemStyles) *MenuItem {
  505. mi := new(MenuItem)
  506. mi.Panel.Initialize(mi, 0, 0)
  507. mi.styles = styles
  508. if text != "" {
  509. mi.label = NewLabel(text)
  510. mi.Panel.Add(mi.label)
  511. mi.Panel.Subscribe(OnCursorEnter, mi.onCursor)
  512. mi.Panel.Subscribe(OnCursor, mi.onCursor)
  513. mi.Panel.Subscribe(OnMouseDown, mi.onMouse)
  514. }
  515. mi.update()
  516. return mi
  517. }
  518. // SetIcon sets the left icon of this menu item
  519. // If an image was previously set it is replaced by this icon
  520. func (mi *MenuItem) SetIcon(icon string) *MenuItem {
  521. // Remove and dispose previous icon
  522. if mi.licon != nil {
  523. mi.Panel.Remove(mi.licon)
  524. mi.Dispose()
  525. mi.licon = nil
  526. }
  527. // Sets the new icon
  528. mi.licon = NewIcon(icon)
  529. mi.Panel.Add(mi.licon)
  530. mi.update()
  531. return mi
  532. }
  533. // SetImage sets the left image of this menu item
  534. // If an icon was previously set it is replaced by this image
  535. func (mi *MenuItem) SetImage(img *Image) {
  536. }
  537. // SetText sets the text of this menu item
  538. func (mi *MenuItem) SetText(text string) *MenuItem {
  539. if mi.label == nil {
  540. return mi
  541. }
  542. mi.label.SetText(text)
  543. mi.update()
  544. mi.menu.recalc()
  545. return mi
  546. }
  547. // SetShortcut sets the keyboard shortcut of this menu item
  548. func (mi *MenuItem) SetShortcut(mods window.ModifierKey, key window.Key) *MenuItem {
  549. if mapKeyText[key] == "" {
  550. panic("Invalid menu shortcut key")
  551. }
  552. mi.keyMods = mods
  553. mi.keyCode = key
  554. // If parent menu is a menu bar, nothing more to do
  555. if mi.menu.bar {
  556. return mi
  557. }
  558. // Builds shortcut text
  559. text := ""
  560. if mi.keyMods&window.ModShift != 0 {
  561. text = mapKeyModifier[window.ModShift]
  562. }
  563. if mi.keyMods&window.ModControl != 0 {
  564. if text != "" {
  565. text += "+"
  566. }
  567. text += mapKeyModifier[window.ModControl]
  568. }
  569. if mi.keyMods&window.ModAlt != 0 {
  570. if text != "" {
  571. text += "+"
  572. }
  573. text += mapKeyModifier[window.ModAlt]
  574. }
  575. if text != "" {
  576. text += "+"
  577. }
  578. text += mapKeyText[key]
  579. // Creates and adds shortcut label
  580. mi.shortcut = NewLabel(text)
  581. mi.Panel.Add(mi.shortcut)
  582. mi.update()
  583. mi.menu.recalc()
  584. return mi
  585. }
  586. // SetSubmenu sets an associated sub menu item for this menu item
  587. func (mi *MenuItem) SetSubmenu(smi *MenuItem) *MenuItem {
  588. return mi
  589. }
  590. // SetEnabled sets the enabled state of this menu item
  591. func (mi *MenuItem) SetEnabled(enabled bool) {
  592. mi.disabled = !enabled
  593. mi.update()
  594. }
  595. // SetId sets this menu item string id which can be used to identify
  596. // the selected menu option.
  597. func (mi *MenuItem) SetId(id string) *MenuItem {
  598. mi.id = id
  599. return mi
  600. }
  601. // Id returns this menu item current id
  602. func (mi *MenuItem) Id() string {
  603. return mi.id
  604. }
  605. // IdPath returns a slice with the path of menu items ids to this menu item
  606. func (mi *MenuItem) IdPath() []string {
  607. // Builds lists of menu items ids
  608. path := []string{mi.id}
  609. menu := mi.menu
  610. for menu.mitem != nil {
  611. path = append(path, menu.mitem.id)
  612. menu = menu.mitem.menu
  613. }
  614. // Reverse and returns id list
  615. res := make([]string, 0, len(path))
  616. for i := len(path) - 1; i >= 0; i-- {
  617. res = append(res, path[i])
  618. }
  619. return res
  620. }
  621. // onCursor processes subscribed cursor events over the menu item
  622. func (mi *MenuItem) onCursor(evname string, ev interface{}) {
  623. switch evname {
  624. case OnCursorEnter:
  625. mi.menu.setSelectedItem(mi)
  626. }
  627. }
  628. // onMouse processes subscribed mouse events over the menu item
  629. func (mi *MenuItem) onMouse(evname string, ev interface{}) {
  630. switch evname {
  631. case OnMouseDown:
  632. // MenuBar option
  633. if mi.menu.bar {
  634. mi.menu.autoOpen = !mi.menu.autoOpen
  635. if mi.submenu != nil && mi.submenu.Visible() {
  636. mi.submenu.SetVisible(false)
  637. Manager().SetKeyFocus(mi.menu)
  638. } else {
  639. mi.update()
  640. }
  641. }
  642. if mi.submenu != nil {
  643. return
  644. }
  645. mi.activate()
  646. }
  647. }
  648. // activate activates this menu item dispatching OnClick events
  649. func (mi *MenuItem) activate() {
  650. rm := mi.rootMenu()
  651. if rm.bar {
  652. rm.autoOpen = false
  653. }
  654. rm.setSelectedPos(-1)
  655. Manager().SetKeyFocus(rm)
  656. mi.dispatchAll(OnClick, mi)
  657. }
  658. // rootMenu returns the root menu for this menu item
  659. func (mi *MenuItem) rootMenu() *Menu {
  660. root := mi.menu
  661. for root.mitem != nil {
  662. root = root.mitem.menu
  663. }
  664. return root
  665. }
  666. // dispatchAll dispatch the specified event for this menu item
  667. // and all its parents
  668. func (mi *MenuItem) dispatchAll(evname string, ev interface{}) {
  669. mi.Dispatch(evname, ev)
  670. pmenu := mi.menu
  671. for {
  672. pmenu.Dispatch(evname, ev)
  673. if pmenu.mitem == nil {
  674. break
  675. }
  676. pmenu = pmenu.mitem.menu
  677. }
  678. }
  679. // update updates the menu item visual state
  680. func (mi *MenuItem) update() {
  681. // Separator
  682. if mi.label == nil {
  683. mi.applyStyle(&mi.styles.Separator)
  684. return
  685. }
  686. // Disabled item
  687. if mi.disabled {
  688. mi.applyStyle(&mi.styles.Disabled)
  689. return
  690. }
  691. // Selected item
  692. if mi.selected {
  693. mi.applyStyle(&mi.styles.Over)
  694. if mi.submenu != nil && mi.menu.autoOpen {
  695. mi.menu.SetTopChild(mi)
  696. mi.submenu.SetVisible(true)
  697. if mi.menu != nil && mi.menu.bar {
  698. mi.submenu.SetPosition(0, mi.Height()-2)
  699. } else {
  700. mi.submenu.SetPosition(mi.Width()-2, 0)
  701. }
  702. }
  703. return
  704. }
  705. // If this menu item has a sub menu and the sub menu is not active,
  706. // hides the sub menu
  707. if mi.submenu != nil {
  708. mi.submenu.SetVisible(false)
  709. }
  710. mi.applyStyle(&mi.styles.Normal)
  711. }
  712. // applyStyle applies the specified menu item style
  713. func (mi *MenuItem) applyStyle(mis *MenuItemStyle) {
  714. mi.Panel.ApplyStyle(&mis.PanelStyle)
  715. if mi.licon != nil {
  716. mi.licon.SetPaddingsFrom(&mis.IconPaddings)
  717. }
  718. if mi.label != nil {
  719. mi.label.SetColor4(&mis.FgColor)
  720. }
  721. if mi.shortcut != nil {
  722. mi.shortcut.SetPaddingsFrom(&mis.ShortcutPaddings)
  723. }
  724. if mi.ricon != nil {
  725. mi.ricon.SetPaddingsFrom(&mis.RiconPaddings)
  726. }
  727. }
  728. // recalc recalculates the positions of this menu item internal panels
  729. func (mi *MenuItem) recalc(iconWidth, labelWidth, shortcutWidth float32) {
  730. // Separator
  731. if mi.label == nil {
  732. return
  733. }
  734. if mi.licon != nil {
  735. py := (mi.label.height - mi.licon.height) / 2
  736. mi.licon.SetPosition(0, py)
  737. }
  738. mi.label.SetPosition(iconWidth, 0)
  739. if mi.shortcut != nil {
  740. mi.shortcut.SetPosition(iconWidth+labelWidth, 0)
  741. }
  742. if mi.ricon != nil {
  743. mi.ricon.SetPosition(iconWidth+labelWidth+shortcutWidth, 0)
  744. }
  745. }
  746. // minHeight returns the minimum height of this menu item
  747. func (mi *MenuItem) minHeight() float32 {
  748. mh := mi.MinHeight()
  749. if mi.label == nil {
  750. return mh + 1
  751. }
  752. mh += mi.label.height
  753. return mh
  754. }
  755. // minWidth returns the minimum width of this menu item
  756. func (mi *MenuItem) minWidth() float32 {
  757. mw := mi.MinWidth()
  758. if mi.label == nil {
  759. return mw + 1
  760. }
  761. mw += mi.label.width
  762. return mw
  763. }