From 969f1a25fca2ad03e76b47bcb9fb582903effb07 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 28 Jul 2015 15:44:38 +0100 Subject: [PATCH] Added an OSX-only method SystemTrayIconComponent::showDropdownMenu() and tweaked the highlighting of OSX tray icons. --- .../misc/juce_SystemTrayIconComponent.h | 5 +++ .../native/juce_mac_SystemTrayIcon.cpp | 36 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/modules/juce_gui_extra/misc/juce_SystemTrayIconComponent.h b/modules/juce_gui_extra/misc/juce_SystemTrayIconComponent.h index 4b759bfdc3..573ea3c8b8 100644 --- a/modules/juce_gui_extra/misc/juce_SystemTrayIconComponent.h +++ b/modules/juce_gui_extra/misc/juce_SystemTrayIconComponent.h @@ -84,6 +84,11 @@ public: void paint (Graphics&) override; #endif + #if JUCE_MAC + /** Shows a menu attached to the OSX menu bar icon. */ + void showDropdownMenu (const PopupMenu& menu); + #endif + private: //============================================================================== JUCE_PUBLIC_IN_DLL_BUILD (class Pimpl) diff --git a/modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp b/modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp index 12f89cab77..c1d66ff247 100644 --- a/modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp +++ b/modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp @@ -27,13 +27,16 @@ namespace MouseCursorHelpers extern NSImage* createNSImage (const Image&); } -class SystemTrayIconComponent::Pimpl +extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId, + int topLevelIndex, bool addDelegate); + +class SystemTrayIconComponent::Pimpl : private Timer { public: Pimpl (SystemTrayIconComponent& iconComp, const Image& im) : owner (iconComp), statusItem (nil), statusIcon (MouseCursorHelpers::createNSImage (im)), - isHighlighted (false) + view (nil), isHighlighted (false) { static SystemTrayViewClass cls; view = [cls.createInstance() init]; @@ -104,6 +107,9 @@ public: if (isLeft || isRight) // Only mouse up is sent by the OS, so simulate a down/up { + setHighlighted (true); + startTimer (150); + owner.mouseDown (MouseEvent (mouseSource, Point(), eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier : ModifierKeys::rightButtonModifier), @@ -123,6 +129,17 @@ public: } } + void showMenu (const PopupMenu& menu) + { + if (NSMenu* m = createNSMenu (menu, "MenuBarItem", -2, -3, true)) + { + setHighlighted (true); + stopTimer(); + [statusItem popUpStatusItemMenu: m]; + startTimer (1); + } + } + SystemTrayIconComponent& owner; NSStatusItem* statusItem; @@ -136,6 +153,12 @@ private: [statusIcon setSize: NSMakeSize (20.0f, 20.0f)]; } + void timerCallback() override + { + stopTimer(); + setHighlighted (false); + } + struct SystemTrayViewClass : public ObjCClass { SystemTrayViewClass() : ObjCClass ("JUCESystemTrayView_") @@ -171,10 +194,7 @@ private: static void handleEventDown (id self, SEL, NSEvent* e) { if (Pimpl* const owner = getOwner (self)) - { - owner->setHighlighted (! owner->isHighlighted); owner->handleStatusItemAction (e); - } } static void drawRect (id self, SEL, NSRect) @@ -244,3 +264,9 @@ void* SystemTrayIconComponent::getNativeHandle() const { return pimpl != nullptr ? pimpl->statusItem : nullptr; } + +void SystemTrayIconComponent::showDropdownMenu (const PopupMenu& menu) +{ + if (pimpl != nullptr) + pimpl->showMenu (menu); +}