contextmenu.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package app
  2. // // MenuItemNode is the interface that describes a menu node.
  3. // type MenuItemNode interface {
  4. // UI
  5. // // Disabled specifies whether the menu item is disabled.
  6. // Disabled(bool) MenuItemNode
  7. // // Keys set the menu item keys.
  8. // Keys(string) MenuItemNode
  9. // // Icon set the menu item icon.
  10. // // "cmdorctrl" is replaced by the platform corresponding key.
  11. // Icon(string) MenuItemNode
  12. // // Label set the menu item label.
  13. // Label(string) MenuItemNode
  14. // // OnClick calls the given handler when there is a mouse click on the element.
  15. // OnClick(EventHandler) MenuItemNode
  16. // // Separator specifies that the menu item is a separator.
  17. // Separator() MenuItemNode
  18. // // Title specifies extra information about the item.
  19. // Title(string) MenuItemNode
  20. // }
  21. // // MenuItem returns a menu item.
  22. // func MenuItem() MenuItemNode {
  23. // return &menuItem{}
  24. // }
  25. // type menuItem struct {
  26. // Compo
  27. // Props struct {
  28. // disabled bool
  29. // keys string
  30. // icon string
  31. // label string
  32. // onClick EventHandler
  33. // separator bool
  34. // title string
  35. // }
  36. // }
  37. // func (m *menuItem) Disabled(v bool) MenuItemNode {
  38. // m.Props.disabled = v
  39. // return m
  40. // }
  41. // func (m *menuItem) Keys(k string) MenuItemNode {
  42. // k = strings.ToLower(k)
  43. // switch Window().Get("navigator").Get("platform").String() {
  44. // case "Macintosh", "MacIntel", "MacPPC", "Mac68K":
  45. // k = strings.Replace(k, "cmdorctrl", "⌘", -1)
  46. // k = strings.Replace(k, "cmd", "⌘", -1)
  47. // k = strings.Replace(k, "command", "⌘", -1)
  48. // k = strings.Replace(k, "ctrl", "⌃", -1)
  49. // k = strings.Replace(k, "control", "⌃", -1)
  50. // k = strings.Replace(k, "alt", "⌥", -1)
  51. // k = strings.Replace(k, "option", "⌥", -1)
  52. // k = strings.Replace(k, "shift", "⇧", -1)
  53. // k = strings.Replace(k, "capslock", "⇪", -1)
  54. // k = strings.Replace(k, "del", "⌫", -1)
  55. // k = strings.Replace(k, "delete", "⌫", -1)
  56. // k = strings.Replace(k, "+", "", -1)
  57. // default:
  58. // k = strings.Replace(k, "cmdorctrl", "ctrl", -1)
  59. // k = strings.Replace(k, "cmd", "win", -1)
  60. // k = strings.Replace(k, "command", "win", -1)
  61. // k = strings.Replace(k, "control", "ctrl", -1)
  62. // }
  63. // m.Props.keys = k
  64. // return m
  65. // }
  66. // func (m *menuItem) Icon(src string) MenuItemNode {
  67. // m.Props.icon = src
  68. // return m
  69. // }
  70. // func (m *menuItem) Label(l string) MenuItemNode {
  71. // m.Props.label = l
  72. // return m
  73. // }
  74. // func (m *menuItem) OnClick(h EventHandler) MenuItemNode {
  75. // m.Props.onClick = h
  76. // return m
  77. // }
  78. // func (m *menuItem) Separator() MenuItemNode {
  79. // m.Props.separator = true
  80. // return m
  81. // }
  82. // func (m *menuItem) Title(t string) MenuItemNode {
  83. // m.Props.title = t
  84. // return m
  85. // }
  86. // func (m *menuItem) Render() UI {
  87. // if m.Props.separator {
  88. // return Div().Class("goapp-menuitem-separator")
  89. // }
  90. // item := Button().
  91. // Class("goapp-menuitem").
  92. // Disabled(m.Props.disabled).
  93. // Body(
  94. // If(m.Props.icon != "",
  95. // Img().
  96. // Class("goapp-menuitem-icon").
  97. // Src(m.Props.icon)),
  98. // Div().
  99. // Class("goapp-menuitem-label").
  100. // Body(
  101. // Text(m.Props.label),
  102. // ),
  103. // Div().
  104. // Class("goapp-menuitem-keys").
  105. // Body(
  106. // Text(m.Props.keys),
  107. // ),
  108. // )
  109. // if m.Props.onClick != nil {
  110. // item = item.OnClick(m.Props.onClick)
  111. // } else {
  112. // item = item.Disabled(true)
  113. // }
  114. // if m.Props.title != "" {
  115. // item = item.Title(m.Props.title)
  116. // }
  117. // return item
  118. // }
  119. // type contextMenuLayout struct {
  120. // Compo
  121. // visible bool
  122. // items []MenuItemNode
  123. // }
  124. // func (l *contextMenuLayout) Render() UI {
  125. // class := "goapp-contextmenu-hidden"
  126. // if l.visible {
  127. // class = "goapp-contextmenu-visible"
  128. // }
  129. // return Div().
  130. // ID("app-contextmenu-background").
  131. // Class(class).
  132. // OnClick(l.onHide).
  133. // Body(
  134. // Div().
  135. // ID("app-contextmenu").
  136. // Class("goapp-contextmenu").
  137. // Body(
  138. // Range(l.items).
  139. // Slice(func(i int) UI {
  140. // return l.items[i].(UI)
  141. // }),
  142. // ),
  143. // )
  144. // }
  145. // func (l *contextMenuLayout) hide() {
  146. // l.onHide(makeContext(l, browserPage{}), Event{Value: Null()})
  147. // }
  148. // func (l *contextMenuLayout) onHide(ctx Context, e Event) {
  149. // l.visible = false
  150. // l.items = nil
  151. // l.Update()
  152. // }
  153. // func (l *contextMenuLayout) show(items ...MenuItemNode) {
  154. // l.items = items
  155. // l.visible = true
  156. // l.Update()
  157. // menu := Window().
  158. // Get("document").
  159. // Call("getElementById", "app-contextmenu")
  160. // menuWidth := menu.Get("offsetWidth").Int()
  161. // menuHeight := menu.Get("offsetHeight").Int()
  162. // winWidth, winHeight := Window().Size()
  163. // cursorX, cursorY := Window().CursorPosition()
  164. // x := cursorX
  165. // if x+menuWidth > winWidth {
  166. // x = winWidth - menuWidth
  167. // }
  168. // y := cursorY
  169. // if y+menuHeight > winHeight {
  170. // y = winHeight - menuHeight
  171. // }
  172. // menu.Get("style").Set("left", strconv.Itoa(x)+"px")
  173. // menu.Get("style").Set("top", strconv.Itoa(y)+"px")
  174. // }