dropdown.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/window"
  8. )
  9. // DropDown represents a dropdown GUI element
  10. type DropDown struct {
  11. Panel // Embedded panel
  12. icon *Label // internal label with icon
  13. list *List // internal list
  14. styles *DropDownStyles // pointer to dropdown styles
  15. litem *ImageLabel // Item shown in drop box (copy of selected)
  16. selItem *ImageLabel // selected item from list
  17. overDropdown bool
  18. overList bool
  19. focus bool
  20. clickOut bool
  21. }
  22. // DropDownStyle
  23. type DropDownStyle BasicStyle
  24. // DropDownStyles
  25. type DropDownStyles struct {
  26. Normal DropDownStyle
  27. Over DropDownStyle
  28. Focus DropDownStyle
  29. Disabled DropDownStyle
  30. }
  31. // NewDropDown creates and returns a pointer to a new drop down widget with the specified width.
  32. func NewDropDown(width float32, item *ImageLabel) *DropDown {
  33. dd := new(DropDown)
  34. dd.styles = &StyleDefault().DropDown
  35. dd.litem = item
  36. dd.Panel.Initialize(width, 0)
  37. dd.Panel.Subscribe(OnKeyDown, dd.onKeyEvent)
  38. dd.Panel.Subscribe(OnMouseDown, dd.onMouse)
  39. dd.Panel.Subscribe(OnCursorEnter, dd.onCursor)
  40. dd.Panel.Subscribe(OnCursorLeave, dd.onCursor)
  41. dd.Panel.Subscribe(OnResize, func(name string, ev interface{}) { dd.recalc() })
  42. // ListItem
  43. dd.Panel.Add(dd.litem)
  44. // Create icon
  45. dd.icon = NewLabel(" ", true)
  46. dd.icon.SetFontSize(StyleDefault().Font.Size() * 1.3)
  47. dd.icon.SetText(string(icon.ArrowDropDown))
  48. dd.Panel.Add(dd.icon)
  49. /// Create list
  50. dd.list = NewVList(0, 0)
  51. dd.list.bounded = false
  52. dd.list.dropdown = true
  53. dd.list.SetVisible(false)
  54. dd.list.Subscribe(OnMouseDown, dd.onListMouse)
  55. dd.list.Subscribe(OnMouseOut, dd.onListMouse)
  56. dd.list.Subscribe(OnChange, dd.onListChangeEvent)
  57. dd.list.Subscribe(OnCursor, func(evname string, ev interface{}) { dd.root.StopPropagation(StopAll) })
  58. dd.Panel.Add(dd.list)
  59. dd.update()
  60. // This will trigger recalc()
  61. dd.Panel.SetContentHeight(item.Height())
  62. return dd
  63. }
  64. // Add adds a list item at the end of the list
  65. func (dd *DropDown) Add(item *ImageLabel) {
  66. dd.list.Add(item)
  67. }
  68. // InsertAt inserts a list item at the specified position
  69. // Returs true if the item was successfully inserted
  70. func (dd *DropDown) InsertAt(pos int, item *ImageLabel) {
  71. dd.list.InsertAt(pos, item)
  72. }
  73. // RemoveAt removes the list item from the specified position
  74. // Returs true if the item was successfully removed
  75. func (dd *DropDown) RemoveAt(pos int) {
  76. dd.list.RemoveAt(pos)
  77. }
  78. // ItemAt returns the list item at the specified position
  79. func (dd *DropDown) ItemAt(pos int) *ImageLabel {
  80. return dd.list.ItemAt(pos).(*ImageLabel)
  81. }
  82. // Len returns the number of items in the dropdown's list.
  83. func (dd *DropDown) Len() int {
  84. return dd.list.Len()
  85. }
  86. // Selected returns the currently selected item or nil if no item was selected
  87. func (dd *DropDown) Selected() *ImageLabel {
  88. return dd.selItem
  89. }
  90. // SetSelected sets the selected item
  91. func (dd *DropDown) SetSelected(item *ImageLabel) {
  92. dd.list.SetSelected(item, true)
  93. }
  94. // SelectPos selects the item at the specified position
  95. func (dd *DropDown) SelectPos(pos int) {
  96. dd.list.SelectPos(pos, true)
  97. }
  98. // onKeyEvent is called when key event is received when this dropdown has the key focus.
  99. func (dd *DropDown) onKeyEvent(evname string, ev interface{}) {
  100. kev := ev.(*window.KeyEvent)
  101. switch kev.Keycode {
  102. case window.KeyF1:
  103. if dd.list.Visible() {
  104. dd.list.SetVisible(false)
  105. }
  106. default:
  107. return
  108. }
  109. }
  110. // onMouse receives subscribed mouse events over the dropdown
  111. func (dd *DropDown) onMouse(evname string, ev interface{}) {
  112. if evname == OnMouseDown {
  113. // If clickOut list already closed
  114. if dd.clickOut {
  115. dd.clickOut = false
  116. return
  117. }
  118. dd.list.SetVisible(true)
  119. dd.root.SetKeyFocus(dd.list)
  120. return
  121. }
  122. }
  123. // onCursor receives subscribed cursor events over the dropdown
  124. func (dd *DropDown) onCursor(evname string, ev interface{}) {
  125. if evname == OnCursorEnter {
  126. dd.overDropdown = true
  127. }
  128. if evname == OnCursorLeave {
  129. dd.overDropdown = false
  130. }
  131. dd.update()
  132. dd.root.StopPropagation(StopAll)
  133. }
  134. // onListMouseEvent receives mouse events over the list
  135. func (dd *DropDown) onListMouse(evname string, ev interface{}) {
  136. mev := ev.(*window.MouseEvent)
  137. // List was clicked
  138. if evname == OnMouseDown {
  139. // If click occurred inside the list scrollbar ignore it
  140. if dd.list.vscroll != nil {
  141. if dd.list.vscroll.InsideBorders(mev.Xpos, mev.Ypos) {
  142. return
  143. }
  144. }
  145. // Otherwise, closes the list
  146. dd.list.SetVisible(false)
  147. //dd.copySelected()
  148. dd.overList = false
  149. dd.update()
  150. return
  151. }
  152. // Hide list when clicked out
  153. if evname == OnMouseOut {
  154. if dd.list.Visible() {
  155. dd.list.SetVisible(false)
  156. }
  157. // If list clickout occurred inside the dropdown, set 'clickOut' to
  158. // indicate that the list was already closed
  159. if dd.Panel.InsideBorders(mev.Xpos, mev.Ypos) {
  160. dd.clickOut = true
  161. }
  162. }
  163. }
  164. // onListCursor receives subscribed events over the list
  165. //func (dd *DropDown) onListCursor(evname string, ev interface{}) {
  166. //
  167. // if evname == OnCursorEnter {
  168. // dd.overList = true
  169. // dd.update()
  170. // return
  171. // }
  172. // if evname == OnCursorLeave {
  173. // dd.overList = false
  174. // dd.update()
  175. // return
  176. // }
  177. // dd.root.StopPropagation(StopAll)
  178. //}
  179. // copySelected copy to the dropdown panel the selected item
  180. // from the list.
  181. func (dd *DropDown) copySelected() {
  182. dd.selItem = dd.list.Selected()[0].(*ImageLabel)
  183. dd.litem.CopyFields(dd.selItem)
  184. dd.litem.SetWidth(dd.selItem.Width())
  185. dd.recalc()
  186. dd.Dispatch(OnChange, nil)
  187. }
  188. // onListChangeEvent is called when an item in the list is selected
  189. func (dd *DropDown) onListChangeEvent(evname string, ev interface{}) {
  190. dd.copySelected()
  191. }
  192. // recalc recalculates the dimensions and positions of the dropdown
  193. // panel, children and list
  194. func (dd *DropDown) recalc() {
  195. // Dropdown icon position
  196. posx := dd.Panel.ContentWidth() - dd.icon.Width()
  197. dd.icon.SetPosition(posx, 0)
  198. // List item position and width
  199. ipan := dd.litem.GetPanel()
  200. ipan.SetPosition(0, 0)
  201. height := ipan.Height()
  202. // List position
  203. dd.list.SetWidth(dd.Panel.Width())
  204. dd.list.SetHeight(6*height + 1)
  205. dd.list.SetPositionX(0)
  206. dd.list.SetPositionY(dd.Panel.Height())
  207. }
  208. // update updates the visual state
  209. func (dd *DropDown) update() {
  210. if dd.overDropdown || dd.overList {
  211. dd.applyStyle(&dd.styles.Over)
  212. dd.list.ApplyStyle(StyleOver)
  213. return
  214. }
  215. if dd.focus {
  216. dd.applyStyle(&dd.styles.Focus)
  217. dd.list.ApplyStyle(StyleFocus)
  218. return
  219. }
  220. dd.applyStyle(&dd.styles.Normal)
  221. dd.list.ApplyStyle(StyleNormal)
  222. }
  223. // applyStyle applies the specified style
  224. func (dd *DropDown) applyStyle(s *DropDownStyle) {
  225. dd.Panel.ApplyStyle(&s.PanelStyle)
  226. }