Browse Source

Added an OSX-only method SystemTrayIconComponent::showDropdownMenu() and tweaked the highlighting of OSX tray icons.

tags/2021-05-28
jules 10 years ago
parent
commit
969f1a25fc
2 changed files with 36 additions and 5 deletions
  1. +5
    -0
      modules/juce_gui_extra/misc/juce_SystemTrayIconComponent.h
  2. +31
    -5
      modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp

+ 5
- 0
modules/juce_gui_extra/misc/juce_SystemTrayIconComponent.h View File

@@ -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)


+ 31
- 5
modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp View File

@@ -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<float>(),
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<NSControl>
{
SystemTrayViewClass() : ObjCClass<NSControl> ("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);
}

Loading…
Cancel
Save