menu.go 20 KB

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