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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wunguarded-availability", "-Wdeprecated-declarations")
  16. extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId,
  17. int topLevelIndex, bool addDelegate);
  18. //==============================================================================
  19. struct StatusItemContainer : public Timer
  20. {
  21. //==============================================================================
  22. StatusItemContainer (SystemTrayIconComponent& iconComp, const Image& im)
  23. : owner (iconComp), statusIcon (imageToNSImage (im))
  24. {
  25. }
  26. virtual void configureIcon() = 0;
  27. virtual void setHighlighted (bool shouldHighlight) = 0;
  28. //==============================================================================
  29. void setIconSize()
  30. {
  31. [statusIcon.get() setSize: NSMakeSize (20.0f, 20.0f)];
  32. }
  33. void updateIcon (const Image& newImage)
  34. {
  35. statusIcon.reset (imageToNSImage (newImage));
  36. setIconSize();
  37. configureIcon();
  38. }
  39. void showMenu (const PopupMenu& menu)
  40. {
  41. if (NSMenu* m = createNSMenu (menu, "MenuBarItem", -2, -3, true))
  42. {
  43. setHighlighted (true);
  44. stopTimer();
  45. // There's currently no good alternative to this.
  46. [statusItem.get() popUpStatusItemMenu: m];
  47. startTimer (1);
  48. }
  49. }
  50. //==============================================================================
  51. void timerCallback() override
  52. {
  53. stopTimer();
  54. setHighlighted (false);
  55. }
  56. //==============================================================================
  57. SystemTrayIconComponent& owner;
  58. std::unique_ptr<NSStatusItem, NSObjectDeleter> statusItem;
  59. std::unique_ptr<NSImage, NSObjectDeleter> statusIcon;
  60. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StatusItemContainer)
  61. };
  62. //==============================================================================
  63. struct ButtonBasedStatusItem : public StatusItemContainer
  64. {
  65. //==============================================================================
  66. ButtonBasedStatusItem (SystemTrayIconComponent& iconComp, const Image& im)
  67. : StatusItemContainer (iconComp, im)
  68. {
  69. static ButtonEventForwarderClass cls;
  70. eventForwarder.reset ([cls.createInstance() init]);
  71. ButtonEventForwarderClass::setOwner (eventForwarder.get(), this);
  72. setIconSize();
  73. configureIcon();
  74. statusItem.reset ([[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain]);
  75. auto button = [statusItem.get() button];
  76. button.image = statusIcon.get();
  77. button.target = eventForwarder.get();
  78. button.action = @selector (handleEvent:);
  79. #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
  80. [button sendActionOn: NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskScrollWheel];
  81. #else
  82. [button sendActionOn: NSLeftMouseDownMask | NSRightMouseDownMask | NSScrollWheelMask];
  83. #endif
  84. }
  85. void configureIcon() override
  86. {
  87. [statusIcon.get() setTemplate: true];
  88. [statusItem.get() button].image = statusIcon.get();
  89. }
  90. void setHighlighted (bool shouldHighlight) override
  91. {
  92. [[statusItem.get() button] setHighlighted: shouldHighlight];
  93. }
  94. //==============================================================================
  95. void handleEvent()
  96. {
  97. auto e = [NSApp currentEvent];
  98. NSEventType type = [e type];
  99. const bool isLeft = (type == NSEventTypeLeftMouseDown);
  100. const bool isRight = (type == NSEventTypeRightMouseDown);
  101. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  102. {
  103. if (isLeft || isRight)
  104. if (auto* current = Component::getCurrentlyModalComponent())
  105. current->inputAttemptWhenModal();
  106. }
  107. else
  108. {
  109. auto eventMods = ComponentPeer::getCurrentModifiersRealtime();
  110. if (([e modifierFlags] & NSEventModifierFlagCommand) != 0)
  111. eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
  112. auto now = Time::getCurrentTime();
  113. auto mouseSource = Desktop::getInstance().getMainMouseSource();
  114. auto pressure = (float) e.pressure;
  115. if (isLeft || isRight)
  116. {
  117. owner.mouseDown ({ mouseSource, {},
  118. eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
  119. : ModifierKeys::rightButtonModifier),
  120. pressure,
  121. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  122. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  123. &owner, &owner, now, {}, now, 1, false });
  124. owner.mouseUp ({ mouseSource, {},
  125. eventMods.withoutMouseButtons(),
  126. pressure,
  127. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  128. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  129. &owner, &owner, now, {}, now, 1, false });
  130. }
  131. else if (type == NSEventTypeMouseMoved)
  132. {
  133. owner.mouseMove (MouseEvent (mouseSource, {}, eventMods, pressure,
  134. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  135. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  136. &owner, &owner, now, {}, now, 1, false));
  137. }
  138. }
  139. }
  140. //==============================================================================
  141. class ButtonEventForwarderClass : public ObjCClass<NSObject>
  142. {
  143. public:
  144. ButtonEventForwarderClass() : ObjCClass<NSObject> ("JUCEButtonEventForwarderClass_")
  145. {
  146. addIvar<ButtonBasedStatusItem*> ("owner");
  147. addMethod (@selector (handleEvent:), handleEvent, "v@:@");
  148. registerClass();
  149. }
  150. static ButtonBasedStatusItem* getOwner (id self) { return getIvar<ButtonBasedStatusItem*> (self, "owner"); }
  151. static void setOwner (id self, ButtonBasedStatusItem* owner) { object_setInstanceVariable (self, "owner", owner); }
  152. private:
  153. static void handleEvent (id self, SEL, id)
  154. {
  155. if (auto* owner = getOwner (self))
  156. owner->handleEvent();
  157. }
  158. };
  159. //==============================================================================
  160. std::unique_ptr<NSObject, NSObjectDeleter> eventForwarder;
  161. };
  162. //==============================================================================
  163. struct ViewBasedStatusItem : public StatusItemContainer
  164. {
  165. //==============================================================================
  166. ViewBasedStatusItem (SystemTrayIconComponent& iconComp, const Image& im)
  167. : StatusItemContainer (iconComp, im)
  168. {
  169. static SystemTrayViewClass cls;
  170. view.reset ([cls.createInstance() init]);
  171. SystemTrayViewClass::setOwner (view.get(), this);
  172. SystemTrayViewClass::setImage (view.get(), statusIcon.get());
  173. setIconSize();
  174. statusItem.reset ([[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain]);
  175. [statusItem.get() setView: view.get()];
  176. SystemTrayViewClass::frameChanged (view.get(), SEL(), nullptr);
  177. [[NSNotificationCenter defaultCenter] addObserver: view.get()
  178. selector: @selector (frameChanged:)
  179. name: NSWindowDidMoveNotification
  180. object: nil];
  181. }
  182. ~ViewBasedStatusItem() override
  183. {
  184. [[NSNotificationCenter defaultCenter] removeObserver: view.get()];
  185. [[NSStatusBar systemStatusBar] removeStatusItem: statusItem.get()];
  186. SystemTrayViewClass::setOwner (view.get(), nullptr);
  187. SystemTrayViewClass::setImage (view.get(), nil);
  188. }
  189. void configureIcon() override
  190. {
  191. SystemTrayViewClass::setImage (view.get(), statusIcon.get());
  192. [statusItem.get() setView: view.get()];
  193. }
  194. void setHighlighted (bool shouldHighlight) override
  195. {
  196. isHighlighted = shouldHighlight;
  197. [view.get() setNeedsDisplay: true];
  198. }
  199. //==============================================================================
  200. void handleStatusItemAction (NSEvent* e)
  201. {
  202. NSEventType type = [e type];
  203. const bool isLeft = (type == NSEventTypeLeftMouseDown || type == NSEventTypeLeftMouseUp);
  204. const bool isRight = (type == NSEventTypeRightMouseDown || type == NSEventTypeRightMouseUp);
  205. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  206. {
  207. if (isLeft || isRight)
  208. if (auto* current = Component::getCurrentlyModalComponent())
  209. current->inputAttemptWhenModal();
  210. }
  211. else
  212. {
  213. auto eventMods = ComponentPeer::getCurrentModifiersRealtime();
  214. if (([e modifierFlags] & NSEventModifierFlagCommand) != 0)
  215. eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
  216. auto now = Time::getCurrentTime();
  217. auto mouseSource = Desktop::getInstance().getMainMouseSource();
  218. auto pressure = (float) e.pressure;
  219. if (isLeft || isRight) // Only mouse up is sent by the OS, so simulate a down/up
  220. {
  221. setHighlighted (true);
  222. startTimer (150);
  223. owner.mouseDown (MouseEvent (mouseSource, {},
  224. eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
  225. : ModifierKeys::rightButtonModifier),
  226. pressure, MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  227. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  228. &owner, &owner, now, {}, now, 1, false));
  229. owner.mouseUp (MouseEvent (mouseSource, {}, eventMods.withoutMouseButtons(), pressure,
  230. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  231. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  232. &owner, &owner, now, {}, now, 1, false));
  233. }
  234. else if (type == NSEventTypeMouseMoved)
  235. {
  236. owner.mouseMove (MouseEvent (mouseSource, {}, eventMods, pressure,
  237. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  238. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  239. &owner, &owner, now, {}, now, 1, false));
  240. }
  241. }
  242. }
  243. //==============================================================================
  244. struct SystemTrayViewClass : public ObjCClass<NSControl>
  245. {
  246. SystemTrayViewClass() : ObjCClass<NSControl> ("JUCESystemTrayView_")
  247. {
  248. addIvar<ViewBasedStatusItem*> ("owner");
  249. addIvar<NSImage*> ("image");
  250. addMethod (@selector (mouseDown:), handleEventDown, "v@:@");
  251. addMethod (@selector (rightMouseDown:), handleEventDown, "v@:@");
  252. addMethod (@selector (drawRect:), drawRect, "v@:@");
  253. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  254. registerClass();
  255. }
  256. static ViewBasedStatusItem* getOwner (id self) { return getIvar<ViewBasedStatusItem*> (self, "owner"); }
  257. static NSImage* getImage (id self) { return getIvar<NSImage*> (self, "image"); }
  258. static void setOwner (id self, ViewBasedStatusItem* owner) { object_setInstanceVariable (self, "owner", owner); }
  259. static void setImage (id self, NSImage* image) { object_setInstanceVariable (self, "image", image); }
  260. static void frameChanged (id self, SEL, NSNotification*)
  261. {
  262. if (auto* owner = getOwner (self))
  263. {
  264. NSRect r = [[[owner->statusItem.get() view] window] frame];
  265. NSRect sr = [[[NSScreen screens] objectAtIndex: 0] frame];
  266. r.origin.y = sr.size.height - r.origin.y - r.size.height;
  267. owner->owner.setBounds (convertToRectInt (r));
  268. }
  269. }
  270. private:
  271. static void handleEventDown (id self, SEL, NSEvent* e)
  272. {
  273. if (auto* owner = getOwner (self))
  274. owner->handleStatusItemAction (e);
  275. }
  276. static void drawRect (id self, SEL, NSRect)
  277. {
  278. NSRect bounds = [self bounds];
  279. if (auto* owner = getOwner (self))
  280. [owner->statusItem.get() drawStatusBarBackgroundInRect: bounds
  281. withHighlight: owner->isHighlighted];
  282. if (NSImage* const im = getImage (self))
  283. {
  284. NSSize imageSize = [im size];
  285. [im drawInRect: NSMakeRect (bounds.origin.x + ((bounds.size.width - imageSize.width) / 2.0f),
  286. bounds.origin.y + ((bounds.size.height - imageSize.height) / 2.0f),
  287. imageSize.width, imageSize.height)
  288. fromRect: NSZeroRect
  289. operation: NSCompositingOperationSourceOver
  290. fraction: 1.0f];
  291. }
  292. }
  293. };
  294. //==============================================================================
  295. std::unique_ptr<NSControl, NSObjectDeleter> view;
  296. bool isHighlighted = false;
  297. };
  298. //==============================================================================
  299. class SystemTrayIconComponent::Pimpl
  300. {
  301. public:
  302. //==============================================================================
  303. Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
  304. {
  305. if (std::floor (NSFoundationVersionNumber) > NSFoundationVersionNumber10_10)
  306. statusItemHolder = std::make_unique<ButtonBasedStatusItem> (iconComp, im);
  307. else
  308. statusItemHolder = std::make_unique<ViewBasedStatusItem> (iconComp, im);
  309. }
  310. //==============================================================================
  311. std::unique_ptr<StatusItemContainer> statusItemHolder;
  312. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  313. };
  314. //==============================================================================
  315. void SystemTrayIconComponent::setIconImage (const Image&, const Image& templateImage)
  316. {
  317. if (templateImage.isValid())
  318. {
  319. if (pimpl == nullptr)
  320. pimpl.reset (new Pimpl (*this, templateImage));
  321. else
  322. pimpl->statusItemHolder->updateIcon (templateImage);
  323. }
  324. else
  325. {
  326. pimpl.reset();
  327. }
  328. }
  329. void SystemTrayIconComponent::setIconTooltip (const String&)
  330. {
  331. // xxx not yet implemented!
  332. }
  333. void SystemTrayIconComponent::setHighlighted (bool shouldHighlight)
  334. {
  335. if (pimpl != nullptr)
  336. pimpl->statusItemHolder->setHighlighted (shouldHighlight);
  337. }
  338. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  339. {
  340. // xxx Not implemented!
  341. }
  342. void SystemTrayIconComponent::hideInfoBubble()
  343. {
  344. // xxx Not implemented!
  345. }
  346. void* SystemTrayIconComponent::getNativeHandle() const
  347. {
  348. return pimpl != nullptr ? pimpl->statusItemHolder->statusItem.get() : nullptr;
  349. }
  350. void SystemTrayIconComponent::showDropdownMenu (const PopupMenu& menu)
  351. {
  352. if (pimpl != nullptr)
  353. pimpl->statusItemHolder->showMenu (menu);
  354. }
  355. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  356. } // namespace juce