menu.go 20 KB

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