|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- /*
- ==============================================================================
-
- This file is part of the JUCE 6 technical preview.
- Copyright (c) 2020 - Raw Material Software Limited
-
- You may use this code under the terms of the GPL v3
- (see www.gnu.org/licenses).
-
- For this technical preview, this file is not subject to commercial licensing.
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wunguarded-availability", "-Wdeprecated-declarations")
-
- extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId,
- int topLevelIndex, bool addDelegate);
-
- //==============================================================================
- struct StatusItemContainer : public Timer
- {
- //==============================================================================
- StatusItemContainer (SystemTrayIconComponent& iconComp, const Image& im)
- : owner (iconComp), statusIcon (imageToNSImage (im))
- {
- }
-
- virtual void configureIcon() = 0;
- virtual void setHighlighted (bool shouldHighlight) = 0;
-
- //==============================================================================
- void setIconSize()
- {
- [statusIcon.get() setSize: NSMakeSize (20.0f, 20.0f)];
- }
-
- void updateIcon (const Image& newImage)
- {
- statusIcon.reset (imageToNSImage (newImage));
- setIconSize();
- configureIcon();
- }
-
- void showMenu (const PopupMenu& menu)
- {
- if (NSMenu* m = createNSMenu (menu, "MenuBarItem", -2, -3, true))
- {
- setHighlighted (true);
- stopTimer();
-
- // There's currently no good alternative to this.
- [statusItem.get() popUpStatusItemMenu: m];
-
- startTimer (1);
- }
- }
-
- //==============================================================================
- void timerCallback() override
- {
- stopTimer();
- setHighlighted (false);
- }
-
- //==============================================================================
- SystemTrayIconComponent& owner;
-
- std::unique_ptr<NSStatusItem, NSObjectDeleter> statusItem;
- std::unique_ptr<NSImage, NSObjectDeleter> statusIcon;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StatusItemContainer)
- };
-
- //==============================================================================
- struct ButtonBasedStatusItem : public StatusItemContainer
- {
- //==============================================================================
- ButtonBasedStatusItem (SystemTrayIconComponent& iconComp, const Image& im)
- : StatusItemContainer (iconComp, im)
- {
- static ButtonEventForwarderClass cls;
- eventForwarder.reset ([cls.createInstance() init]);
- ButtonEventForwarderClass::setOwner (eventForwarder.get(), this);
-
- setIconSize();
- configureIcon();
-
- statusItem.reset ([[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain]);
- auto button = [statusItem.get() button];
- button.image = statusIcon.get();
- button.target = eventForwarder.get();
- button.action = @selector (handleEvent:);
- #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
- [button sendActionOn: NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskScrollWheel];
- #else
- [button sendActionOn: NSLeftMouseDownMask | NSRightMouseDownMask | NSScrollWheelMask];
- #endif
- }
-
- void configureIcon() override
- {
- [statusIcon.get() setTemplate: true];
- [statusItem.get() button].image = statusIcon.get();
- }
-
- void setHighlighted (bool shouldHighlight) override
- {
- [[statusItem.get() button] setHighlighted: shouldHighlight];
- }
-
- //==============================================================================
- void handleEvent()
- {
- auto e = [NSApp currentEvent];
- NSEventType type = [e type];
-
- const bool isLeft = (type == NSEventTypeLeftMouseDown);
- const bool isRight = (type == NSEventTypeRightMouseDown);
-
- if (owner.isCurrentlyBlockedByAnotherModalComponent())
- {
- if (isLeft || isRight)
- if (auto* current = Component::getCurrentlyModalComponent())
- current->inputAttemptWhenModal();
- }
- else
- {
- auto eventMods = ComponentPeer::getCurrentModifiersRealtime();
-
- if (([e modifierFlags] & NSEventModifierFlagCommand) != 0)
- eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
-
- auto now = Time::getCurrentTime();
- auto mouseSource = Desktop::getInstance().getMainMouseSource();
- auto pressure = (float) e.pressure;
-
- if (isLeft || isRight)
- {
- owner.mouseDown ({ mouseSource, {},
- eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
- : ModifierKeys::rightButtonModifier),
- pressure,
- MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
- MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
- &owner, &owner, now, {}, now, 1, false });
-
- owner.mouseUp ({ mouseSource, {},
- eventMods.withoutMouseButtons(),
- pressure,
- MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
- MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
- &owner, &owner, now, {}, now, 1, false });
- }
- else if (type == NSEventTypeMouseMoved)
- {
- owner.mouseMove (MouseEvent (mouseSource, {}, eventMods, pressure,
- MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
- MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
- &owner, &owner, now, {}, now, 1, false));
- }
- }
- }
-
- //==============================================================================
- class ButtonEventForwarderClass : public ObjCClass<NSObject>
- {
- public:
- ButtonEventForwarderClass() : ObjCClass<NSObject> ("JUCEButtonEventForwarderClass_")
- {
- addIvar<ButtonBasedStatusItem*> ("owner");
-
- addMethod (@selector (handleEvent:), handleEvent, "v@:@");
-
- registerClass();
- }
-
- static ButtonBasedStatusItem* getOwner (id self) { return getIvar<ButtonBasedStatusItem*> (self, "owner"); }
- static void setOwner (id self, ButtonBasedStatusItem* owner) { object_setInstanceVariable (self, "owner", owner); }
-
- private:
- static void handleEvent (id self, SEL, id)
- {
- if (auto* owner = getOwner (self))
- owner->handleEvent();
- }
- };
-
- //==============================================================================
- std::unique_ptr<NSObject, NSObjectDeleter> eventForwarder;
- };
-
- //==============================================================================
- struct ViewBasedStatusItem : public StatusItemContainer
- {
- //==============================================================================
- ViewBasedStatusItem (SystemTrayIconComponent& iconComp, const Image& im)
- : StatusItemContainer (iconComp, im)
- {
- static SystemTrayViewClass cls;
- view.reset ([cls.createInstance() init]);
- SystemTrayViewClass::setOwner (view.get(), this);
- SystemTrayViewClass::setImage (view.get(), statusIcon.get());
-
- setIconSize();
-
- statusItem.reset ([[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain]);
- [statusItem.get() setView: view.get()];
-
- SystemTrayViewClass::frameChanged (view.get(), SEL(), nullptr);
-
- [[NSNotificationCenter defaultCenter] addObserver: view.get()
- selector: @selector (frameChanged:)
- name: NSWindowDidMoveNotification
- object: nil];
- }
-
- ~ViewBasedStatusItem() override
- {
- [[NSNotificationCenter defaultCenter] removeObserver: view.get()];
- [[NSStatusBar systemStatusBar] removeStatusItem: statusItem.get()];
- SystemTrayViewClass::setOwner (view.get(), nullptr);
- SystemTrayViewClass::setImage (view.get(), nil);
- }
-
- void configureIcon() override
- {
- SystemTrayViewClass::setImage (view.get(), statusIcon.get());
- [statusItem.get() setView: view.get()];
- }
-
- void setHighlighted (bool shouldHighlight) override
- {
- isHighlighted = shouldHighlight;
- [view.get() setNeedsDisplay: true];
- }
-
- //==============================================================================
- void handleStatusItemAction (NSEvent* e)
- {
- NSEventType type = [e type];
-
- const bool isLeft = (type == NSEventTypeLeftMouseDown || type == NSEventTypeLeftMouseUp);
- const bool isRight = (type == NSEventTypeRightMouseDown || type == NSEventTypeRightMouseUp);
-
- if (owner.isCurrentlyBlockedByAnotherModalComponent())
- {
- if (isLeft || isRight)
- if (auto* current = Component::getCurrentlyModalComponent())
- current->inputAttemptWhenModal();
- }
- else
- {
- auto eventMods = ComponentPeer::getCurrentModifiersRealtime();
-
- if (([e modifierFlags] & NSEventModifierFlagCommand) != 0)
- eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
-
- auto now = Time::getCurrentTime();
- auto mouseSource = Desktop::getInstance().getMainMouseSource();
- auto pressure = (float) e.pressure;
-
- if (isLeft || isRight) // Only mouse up is sent by the OS, so simulate a down/up
- {
- setHighlighted (true);
- startTimer (150);
-
- owner.mouseDown (MouseEvent (mouseSource, {},
- eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
- : ModifierKeys::rightButtonModifier),
- pressure, MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
- MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
- &owner, &owner, now, {}, now, 1, false));
-
- owner.mouseUp (MouseEvent (mouseSource, {}, eventMods.withoutMouseButtons(), pressure,
- MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
- MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
- &owner, &owner, now, {}, now, 1, false));
- }
- else if (type == NSEventTypeMouseMoved)
- {
- owner.mouseMove (MouseEvent (mouseSource, {}, eventMods, pressure,
- MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
- MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
- &owner, &owner, now, {}, now, 1, false));
- }
- }
- }
-
- //==============================================================================
- struct SystemTrayViewClass : public ObjCClass<NSControl>
- {
- SystemTrayViewClass() : ObjCClass<NSControl> ("JUCESystemTrayView_")
- {
- addIvar<ViewBasedStatusItem*> ("owner");
- addIvar<NSImage*> ("image");
-
- addMethod (@selector (mouseDown:), handleEventDown, "v@:@");
- addMethod (@selector (rightMouseDown:), handleEventDown, "v@:@");
- addMethod (@selector (drawRect:), drawRect, "v@:@");
- addMethod (@selector (frameChanged:), frameChanged, "v@:@");
-
- registerClass();
- }
-
- static ViewBasedStatusItem* getOwner (id self) { return getIvar<ViewBasedStatusItem*> (self, "owner"); }
- static NSImage* getImage (id self) { return getIvar<NSImage*> (self, "image"); }
- static void setOwner (id self, ViewBasedStatusItem* owner) { object_setInstanceVariable (self, "owner", owner); }
- static void setImage (id self, NSImage* image) { object_setInstanceVariable (self, "image", image); }
-
- static void frameChanged (id self, SEL, NSNotification*)
- {
- if (auto* owner = getOwner (self))
- {
- NSRect r = [[[owner->statusItem.get() view] window] frame];
- NSRect sr = [[[NSScreen screens] objectAtIndex: 0] frame];
- r.origin.y = sr.size.height - r.origin.y - r.size.height;
- owner->owner.setBounds (convertToRectInt (r));
- }
- }
-
- private:
- static void handleEventDown (id self, SEL, NSEvent* e)
- {
- if (auto* owner = getOwner (self))
- owner->handleStatusItemAction (e);
- }
-
- static void drawRect (id self, SEL, NSRect)
- {
- NSRect bounds = [self bounds];
-
- if (auto* owner = getOwner (self))
- [owner->statusItem.get() drawStatusBarBackgroundInRect: bounds
- withHighlight: owner->isHighlighted];
-
- if (NSImage* const im = getImage (self))
- {
- NSSize imageSize = [im size];
-
- [im drawInRect: NSMakeRect (bounds.origin.x + ((bounds.size.width - imageSize.width) / 2.0f),
- bounds.origin.y + ((bounds.size.height - imageSize.height) / 2.0f),
- imageSize.width, imageSize.height)
- fromRect: NSZeroRect
- operation: NSCompositingOperationSourceOver
- fraction: 1.0f];
- }
- }
- };
-
- //==============================================================================
- std::unique_ptr<NSControl, NSObjectDeleter> view;
- bool isHighlighted = false;
- };
-
- //==============================================================================
- class SystemTrayIconComponent::Pimpl
- {
- public:
- //==============================================================================
- Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
- {
- if (std::floor (NSFoundationVersionNumber) > NSFoundationVersionNumber10_10)
- statusItemHolder = std::make_unique<ButtonBasedStatusItem> (iconComp, im);
- else
- statusItemHolder = std::make_unique<ViewBasedStatusItem> (iconComp, im);
- }
-
- //==============================================================================
- std::unique_ptr<StatusItemContainer> statusItemHolder;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
- };
-
- //==============================================================================
- void SystemTrayIconComponent::setIconImage (const Image&, const Image& templateImage)
- {
- if (templateImage.isValid())
- {
- if (pimpl == nullptr)
- pimpl.reset (new Pimpl (*this, templateImage));
- else
- pimpl->statusItemHolder->updateIcon (templateImage);
- }
- else
- {
- pimpl.reset();
- }
- }
-
- void SystemTrayIconComponent::setIconTooltip (const String&)
- {
- // xxx not yet implemented!
- }
-
- void SystemTrayIconComponent::setHighlighted (bool shouldHighlight)
- {
- if (pimpl != nullptr)
- pimpl->statusItemHolder->setHighlighted (shouldHighlight);
- }
-
- void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
- {
- // xxx Not implemented!
- }
-
- void SystemTrayIconComponent::hideInfoBubble()
- {
- // xxx Not implemented!
- }
-
- void* SystemTrayIconComponent::getNativeHandle() const
- {
- return pimpl != nullptr ? pimpl->statusItemHolder->statusItem.get() : nullptr;
- }
-
- void SystemTrayIconComponent::showDropdownMenu (const PopupMenu& menu)
- {
- if (pimpl != nullptr)
- pimpl->statusItemHolder->showMenu (menu);
- }
-
- JUCE_END_IGNORE_WARNINGS_GCC_LIKE
-
- } // namespace juce
|