menu.go 20 KB

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