systray_darwin.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package systray
  2. /*
  3. #cgo darwin CFLAGS: -DDARWIN -x objective-c -fobjc-arc
  4. #cgo darwin LDFLAGS: -framework Cocoa
  5. #include <stdbool.h>
  6. #include "systray.h"
  7. void setInternalLoop(bool);
  8. */
  9. import "C"
  10. import (
  11. "unsafe"
  12. )
  13. // SetTemplateIcon sets the systray icon as a template icon (on Mac), falling back
  14. // to a regular icon on other platforms.
  15. // templateIconBytes and regularIconBytes should be the content of .ico for windows and
  16. // .ico/.jpg/.png for other platforms.
  17. func SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte) {
  18. cstr := (*C.char)(unsafe.Pointer(&templateIconBytes[0]))
  19. C.setIcon(cstr, (C.int)(len(templateIconBytes)), true)
  20. }
  21. // SetIcon sets the icon of a menu item. Only works on macOS and Windows.
  22. // iconBytes should be the content of .ico/.jpg/.png
  23. func (item *MenuItem) SetIcon(iconBytes []byte) {
  24. cstr := (*C.char)(unsafe.Pointer(&iconBytes[0]))
  25. C.setMenuItemIcon(cstr, (C.int)(len(iconBytes)), C.int(item.id), false)
  26. }
  27. // SetTemplateIcon sets the icon of a menu item as a template icon (on macOS). On Windows, it
  28. // falls back to the regular icon bytes and on Linux it does nothing.
  29. // templateIconBytes and regularIconBytes should be the content of .ico for windows and
  30. // .ico/.jpg/.png for other platforms.
  31. func (item *MenuItem) SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte) {
  32. cstr := (*C.char)(unsafe.Pointer(&templateIconBytes[0]))
  33. C.setMenuItemIcon(cstr, (C.int)(len(templateIconBytes)), C.int(item.id), true)
  34. }
  35. func registerSystray() {
  36. C.registerSystray()
  37. }
  38. func nativeLoop() {
  39. C.nativeLoop()
  40. }
  41. func nativeEnd() {
  42. C.nativeEnd()
  43. }
  44. func nativeStart() {
  45. C.nativeStart()
  46. }
  47. func quit() {
  48. C.quit()
  49. }
  50. func setInternalLoop(internal bool) {
  51. C.setInternalLoop(C.bool(internal))
  52. }
  53. // SetIcon sets the systray icon.
  54. // iconBytes should be the content of .ico for windows and .ico/.jpg/.png
  55. // for other platforms.
  56. func SetIcon(iconBytes []byte) {
  57. cstr := (*C.char)(unsafe.Pointer(&iconBytes[0]))
  58. C.setIcon(cstr, (C.int)(len(iconBytes)), false)
  59. }
  60. // SetTitle sets the systray title, only available on Mac and Linux.
  61. func SetTitle(title string) {
  62. C.setTitle(C.CString(title))
  63. }
  64. // SetTooltip sets the systray tooltip to display on mouse hover of the tray icon,
  65. // only available on Mac and Windows.
  66. func SetTooltip(tooltip string) {
  67. C.setTooltip(C.CString(tooltip))
  68. }
  69. func addOrUpdateMenuItem(item *MenuItem) {
  70. var disabled C.short
  71. if item.disabled {
  72. disabled = 1
  73. }
  74. var checked C.short
  75. if item.checked {
  76. checked = 1
  77. }
  78. var isCheckable C.short
  79. if item.isCheckable {
  80. isCheckable = 1
  81. }
  82. var parentID uint32 = 0
  83. if item.parent != nil {
  84. parentID = item.parent.id
  85. }
  86. C.add_or_update_menu_item(
  87. C.int(item.id),
  88. C.int(parentID),
  89. C.CString(item.title),
  90. C.CString(item.tooltip),
  91. disabled,
  92. checked,
  93. isCheckable,
  94. )
  95. }
  96. func addSeparator(id uint32, parent uint32) {
  97. C.add_separator(C.int(id), C.int(parent))
  98. }
  99. func hideMenuItem(item *MenuItem) {
  100. C.hide_menu_item(
  101. C.int(item.id),
  102. )
  103. }
  104. func showMenuItem(item *MenuItem) {
  105. C.show_menu_item(
  106. C.int(item.id),
  107. )
  108. }
  109. func removeMenuItem(item *MenuItem) {
  110. C.remove_menu_item(
  111. C.int(item.id),
  112. )
  113. }
  114. func resetMenu() {
  115. C.reset_menu()
  116. }
  117. //export systray_ready
  118. func systray_ready() {
  119. systrayReady()
  120. }
  121. //export systray_on_exit
  122. func systray_on_exit() {
  123. runSystrayExit()
  124. }
  125. //export systray_menu_item_selected
  126. func systray_menu_item_selected(cID C.int) {
  127. systrayMenuItemSelected(uint32(cID))
  128. }