Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_mac_SystemTrayIcon.cpp 9.5KB

9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace MouseCursorHelpers
  18. {
  19. extern NSImage* createNSImage (const Image&);
  20. }
  21. extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId,
  22. int topLevelIndex, bool addDelegate);
  23. class SystemTrayIconComponent::Pimpl : private Timer
  24. {
  25. public:
  26. Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
  27. : owner (iconComp), statusItem (nil),
  28. statusIcon (MouseCursorHelpers::createNSImage (im)),
  29. view (nil), isHighlighted (false)
  30. {
  31. static SystemTrayViewClass cls;
  32. view = [cls.createInstance() init];
  33. SystemTrayViewClass::setOwner (view, this);
  34. SystemTrayViewClass::setImage (view, statusIcon);
  35. setIconSize();
  36. statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain];
  37. [statusItem setView: view];
  38. SystemTrayViewClass::frameChanged (view, SEL(), nullptr);
  39. [[NSNotificationCenter defaultCenter] addObserver: view
  40. selector: @selector (frameChanged:)
  41. name: NSWindowDidMoveNotification
  42. object: nil];
  43. }
  44. ~Pimpl()
  45. {
  46. [[NSNotificationCenter defaultCenter] removeObserver: view];
  47. [[NSStatusBar systemStatusBar] removeStatusItem: statusItem];
  48. SystemTrayViewClass::setOwner (view, nullptr);
  49. SystemTrayViewClass::setImage (view, nil);
  50. [statusItem release];
  51. [view release];
  52. [statusIcon release];
  53. }
  54. void updateIcon (const Image& newImage)
  55. {
  56. [statusIcon release];
  57. statusIcon = MouseCursorHelpers::createNSImage (newImage);
  58. setIconSize();
  59. SystemTrayViewClass::setImage (view, statusIcon);
  60. }
  61. void setHighlighted (bool shouldHighlight)
  62. {
  63. isHighlighted = shouldHighlight;
  64. [view setNeedsDisplay: true];
  65. }
  66. void handleStatusItemAction (NSEvent* e)
  67. {
  68. NSEventType type = [e type];
  69. const bool isLeft = (type == NSLeftMouseDown || type == NSLeftMouseUp);
  70. const bool isRight = (type == NSRightMouseDown || type == NSRightMouseUp);
  71. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  72. {
  73. if (isLeft || isRight)
  74. if (Component* const current = Component::getCurrentlyModalComponent())
  75. current->inputAttemptWhenModal();
  76. }
  77. else
  78. {
  79. ModifierKeys eventMods (ModifierKeys::getCurrentModifiersRealtime());
  80. if (([e modifierFlags] & NSCommandKeyMask) != 0)
  81. eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
  82. const Time now (Time::getCurrentTime());
  83. MouseInputSource mouseSource = Desktop::getInstance().getMainMouseSource();
  84. const float pressure = (float) e.pressure;
  85. if (isLeft || isRight) // Only mouse up is sent by the OS, so simulate a down/up
  86. {
  87. setHighlighted (true);
  88. startTimer (150);
  89. owner.mouseDown (MouseEvent (mouseSource, Point<float>(),
  90. eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
  91. : ModifierKeys::rightButtonModifier),
  92. pressure, &owner, &owner, now,
  93. Point<float>(), now, 1, false));
  94. owner.mouseUp (MouseEvent (mouseSource, Point<float>(), eventMods.withoutMouseButtons(),
  95. pressure, &owner, &owner, now,
  96. Point<float>(), now, 1, false));
  97. }
  98. else if (type == NSMouseMoved)
  99. {
  100. owner.mouseMove (MouseEvent (mouseSource, Point<float>(), eventMods,
  101. pressure, &owner, &owner, now,
  102. Point<float>(), now, 1, false));
  103. }
  104. }
  105. }
  106. void showMenu (const PopupMenu& menu)
  107. {
  108. if (NSMenu* m = createNSMenu (menu, "MenuBarItem", -2, -3, true))
  109. {
  110. setHighlighted (true);
  111. stopTimer();
  112. [statusItem popUpStatusItemMenu: m];
  113. startTimer (1);
  114. }
  115. }
  116. SystemTrayIconComponent& owner;
  117. NSStatusItem* statusItem;
  118. private:
  119. NSImage* statusIcon;
  120. NSControl* view;
  121. bool isHighlighted;
  122. void setIconSize()
  123. {
  124. [statusIcon setSize: NSMakeSize (20.0f, 20.0f)];
  125. }
  126. void timerCallback() override
  127. {
  128. stopTimer();
  129. setHighlighted (false);
  130. }
  131. struct SystemTrayViewClass : public ObjCClass<NSControl>
  132. {
  133. SystemTrayViewClass() : ObjCClass<NSControl> ("JUCESystemTrayView_")
  134. {
  135. addIvar<Pimpl*> ("owner");
  136. addIvar<NSImage*> ("image");
  137. addMethod (@selector (mouseDown:), handleEventDown, "v@:@");
  138. addMethod (@selector (rightMouseDown:), handleEventDown, "v@:@");
  139. addMethod (@selector (drawRect:), drawRect, "v@:@");
  140. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  141. registerClass();
  142. }
  143. static Pimpl* getOwner (id self) { return getIvar<Pimpl*> (self, "owner"); }
  144. static NSImage* getImage (id self) { return getIvar<NSImage*> (self, "image"); }
  145. static void setOwner (id self, Pimpl* owner) { object_setInstanceVariable (self, "owner", owner); }
  146. static void setImage (id self, NSImage* image) { object_setInstanceVariable (self, "image", image); }
  147. static void frameChanged (id self, SEL, NSNotification*)
  148. {
  149. if (Pimpl* const owner = getOwner (self))
  150. {
  151. NSRect r = [[[owner->statusItem view] window] frame];
  152. NSRect sr = [[[NSScreen screens] objectAtIndex: 0] frame];
  153. r.origin.y = sr.size.height - r.origin.y - r.size.height;
  154. owner->owner.setBounds (convertToRectInt (r));
  155. }
  156. }
  157. private:
  158. static void handleEventDown (id self, SEL, NSEvent* e)
  159. {
  160. if (Pimpl* const owner = getOwner (self))
  161. owner->handleStatusItemAction (e);
  162. }
  163. static void drawRect (id self, SEL, NSRect)
  164. {
  165. NSRect bounds = [self bounds];
  166. if (Pimpl* const owner = getOwner (self))
  167. [owner->statusItem drawStatusBarBackgroundInRect: bounds
  168. withHighlight: owner->isHighlighted];
  169. if (NSImage* const im = getImage (self))
  170. {
  171. NSSize imageSize = [im size];
  172. [im drawInRect: NSMakeRect (bounds.origin.x + ((bounds.size.width - imageSize.width) / 2.0f),
  173. bounds.origin.y + ((bounds.size.height - imageSize.height) / 2.0f),
  174. imageSize.width, imageSize.height)
  175. fromRect: NSZeroRect
  176. operation: NSCompositeSourceOver
  177. fraction: 1.0f];
  178. }
  179. }
  180. };
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  182. };
  183. //==============================================================================
  184. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  185. {
  186. if (newImage.isValid())
  187. {
  188. if (pimpl == nullptr)
  189. pimpl = new Pimpl (*this, newImage);
  190. else
  191. pimpl->updateIcon (newImage);
  192. }
  193. else
  194. {
  195. pimpl = nullptr;
  196. }
  197. }
  198. void SystemTrayIconComponent::setIconTooltip (const String&)
  199. {
  200. // xxx not yet implemented!
  201. }
  202. void SystemTrayIconComponent::setHighlighted (bool highlight)
  203. {
  204. if (pimpl != nullptr)
  205. pimpl->setHighlighted (highlight);
  206. }
  207. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  208. {
  209. // xxx Not implemented!
  210. }
  211. void SystemTrayIconComponent::hideInfoBubble()
  212. {
  213. // xxx Not implemented!
  214. }
  215. void* SystemTrayIconComponent::getNativeHandle() const
  216. {
  217. return pimpl != nullptr ? pimpl->statusItem : nullptr;
  218. }
  219. void SystemTrayIconComponent::showDropdownMenu (const PopupMenu& menu)
  220. {
  221. if (pimpl != nullptr)
  222. pimpl->showMenu (menu);
  223. }