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 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. class SystemTrayIconComponent::Pimpl
  22. {
  23. public:
  24. Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
  25. : owner (iconComp), statusItem (nil),
  26. statusIcon (MouseCursorHelpers::createNSImage (im)),
  27. isHighlighted (false)
  28. {
  29. static SystemTrayViewClass cls;
  30. view = [cls.createInstance() init];
  31. SystemTrayViewClass::setOwner (view, this);
  32. SystemTrayViewClass::setImage (view, statusIcon);
  33. setIconSize();
  34. statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain];
  35. [statusItem setView: view];
  36. [[NSNotificationCenter defaultCenter] addObserver: view
  37. selector: @selector (frameChanged:)
  38. name: NSWindowDidMoveNotification
  39. object: nil];
  40. }
  41. ~Pimpl()
  42. {
  43. [[NSStatusBar systemStatusBar] removeStatusItem: statusItem];
  44. [statusItem release];
  45. [view release];
  46. [statusIcon release];
  47. }
  48. void updateIcon (const Image& newImage)
  49. {
  50. [statusIcon release];
  51. statusIcon = MouseCursorHelpers::createNSImage (newImage);
  52. setIconSize();
  53. SystemTrayViewClass::setImage (view, statusIcon);
  54. }
  55. void setHighlighted (bool shouldHighlight)
  56. {
  57. isHighlighted = shouldHighlight;
  58. [view setNeedsDisplay: true];
  59. }
  60. void handleStatusItemAction (NSEvent* e)
  61. {
  62. NSEventType type = [e type];
  63. const bool isLeft = (type == NSLeftMouseDown || type == NSLeftMouseUp);
  64. const bool isRight = (type == NSRightMouseDown || type == NSRightMouseUp);
  65. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  66. {
  67. if (isLeft || isRight)
  68. if (Component* const current = Component::getCurrentlyModalComponent())
  69. current->inputAttemptWhenModal();
  70. }
  71. else
  72. {
  73. ModifierKeys eventMods (ModifierKeys::getCurrentModifiersRealtime());
  74. if (([e modifierFlags] & NSCommandKeyMask) != 0)
  75. eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
  76. const Time now (Time::getCurrentTime());
  77. MouseInputSource mouseSource = Desktop::getInstance().getMainMouseSource();
  78. if (isLeft || isRight) // Only mouse up is sent by the OS, so simulate a down/up
  79. {
  80. owner.mouseDown (MouseEvent (mouseSource, Point<int>(),
  81. eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
  82. : ModifierKeys::rightButtonModifier),
  83. &owner, &owner, now,
  84. Point<int>(), now, 1, false));
  85. owner.mouseUp (MouseEvent (mouseSource, Point<int>(), eventMods.withoutMouseButtons(),
  86. &owner, &owner, now,
  87. Point<int>(), now, 1, false));
  88. }
  89. else if (type == NSMouseMoved)
  90. {
  91. owner.mouseMove (MouseEvent (mouseSource, Point<int>(), eventMods,
  92. &owner, &owner, now,
  93. Point<int>(), now, 1, false));
  94. }
  95. }
  96. }
  97. SystemTrayIconComponent& owner;
  98. NSStatusItem* statusItem;
  99. private:
  100. NSImage* statusIcon;
  101. NSControl* view;
  102. bool isHighlighted;
  103. void setIconSize()
  104. {
  105. [statusIcon setSize: NSMakeSize (20.0f, 20.0f)];
  106. }
  107. struct SystemTrayViewClass : public ObjCClass <NSControl>
  108. {
  109. SystemTrayViewClass() : ObjCClass <NSControl> ("JUCESystemTrayView_")
  110. {
  111. addIvar<Pimpl*> ("owner");
  112. addIvar<NSImage*> ("image");
  113. addMethod (@selector (mouseDown:), handleEventDown, "v@:@");
  114. addMethod (@selector (rightMouseDown:), handleEventDown, "v@:@");
  115. addMethod (@selector (drawRect:), drawRect, "v@:@");
  116. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  117. registerClass();
  118. }
  119. static Pimpl* getOwner (id self) { return getIvar<Pimpl*> (self, "owner"); }
  120. static NSImage* getImage (id self) { return getIvar<NSImage*> (self, "image"); }
  121. static void setOwner (id self, Pimpl* owner) { object_setInstanceVariable (self, "owner", owner); }
  122. static void setImage (id self, NSImage* image) { object_setInstanceVariable (self, "image", image); }
  123. private:
  124. static void handleEventDown (id self, SEL, NSEvent* e)
  125. {
  126. if (Pimpl* const owner = getOwner (self))
  127. {
  128. owner->setHighlighted (! owner->isHighlighted);
  129. owner->handleStatusItemAction (e);
  130. }
  131. }
  132. static void drawRect (id self, SEL, NSRect)
  133. {
  134. NSRect bounds = [self bounds];
  135. if (Pimpl* const owner = getOwner (self))
  136. [owner->statusItem drawStatusBarBackgroundInRect: bounds
  137. withHighlight: owner->isHighlighted];
  138. if (NSImage* const im = getImage (self))
  139. {
  140. NSSize imageSize = [im size];
  141. [im drawInRect: NSMakeRect (bounds.origin.x + ((bounds.size.width - imageSize.width) / 2.0f),
  142. bounds.origin.y + ((bounds.size.height - imageSize.height) / 2.0f),
  143. imageSize.width, imageSize.height)
  144. fromRect: NSZeroRect
  145. operation: NSCompositeSourceOver
  146. fraction: 1.0f];
  147. }
  148. }
  149. static void frameChanged (id self, SEL, NSNotification*)
  150. {
  151. if (Pimpl* const owner = getOwner (self))
  152. {
  153. NSRect r = [[[owner->statusItem view] window] frame];
  154. NSRect sr = [[[NSScreen screens] objectAtIndex: 0] frame];
  155. r.origin.y = sr.size.height - r.origin.y - r.size.height;
  156. owner->owner.setBounds (convertToRectInt (r));
  157. }
  158. }
  159. };
  160. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  161. };
  162. //==============================================================================
  163. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  164. {
  165. if (newImage.isValid())
  166. {
  167. if (pimpl == nullptr)
  168. pimpl = new Pimpl (*this, newImage);
  169. else
  170. pimpl->updateIcon (newImage);
  171. }
  172. else
  173. {
  174. pimpl = nullptr;
  175. }
  176. }
  177. void SystemTrayIconComponent::setIconTooltip (const String&)
  178. {
  179. // xxx not yet implemented!
  180. }
  181. void SystemTrayIconComponent::setHighlighted (bool highlight)
  182. {
  183. if (pimpl != nullptr)
  184. pimpl->setHighlighted (highlight);
  185. }
  186. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  187. {
  188. // xxx Not implemented!
  189. }
  190. void SystemTrayIconComponent::hideInfoBubble()
  191. {
  192. // xxx Not implemented!
  193. }
  194. void* SystemTrayIconComponent::getNativeHandle() const
  195. {
  196. return pimpl != nullptr ? pimpl->statusItem : nullptr;
  197. }