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.

245 lines
8.6KB

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