menu.go 20 KB

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