diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index 33e16648a4..a02f6a56e1 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1838,6 +1838,11 @@ void Component::setRepaintsOnMouseActivity (const bool shouldRepaint) noexcept } //============================================================================== +float Component::getAlpha() const noexcept +{ + return (255 - componentTransparency) / 255.0f; +} + void Component::setAlpha (const float newAlpha) { const uint8 newIntAlpha = (uint8) (255 - jlimit (0, 255, roundToInt (newAlpha * 255.0))); @@ -1845,22 +1850,21 @@ void Component::setAlpha (const float newAlpha) if (componentTransparency != newIntAlpha) { componentTransparency = newIntAlpha; - - if (flags.hasHeavyweightPeerFlag) - { - if (ComponentPeer* const peer = getPeer()) - peer->setAlpha (newAlpha); - } - else - { - repaint(); - } + alphaChanged(); } } -float Component::getAlpha() const +void Component::alphaChanged() { - return (255 - componentTransparency) / 255.0f; + if (flags.hasHeavyweightPeerFlag) + { + if (ComponentPeer* const peer = getPeer()) + peer->setAlpha (getAlpha()); + } + else + { + repaint(); + } } //============================================================================== diff --git a/modules/juce_gui_basics/components/juce_Component.h b/modules/juce_gui_basics/components/juce_Component.h index 56f1cd06d3..9b93c4ef08 100644 --- a/modules/juce_gui_basics/components/juce_Component.h +++ b/modules/juce_gui_basics/components/juce_Component.h @@ -1353,19 +1353,26 @@ public: */ virtual void enablementChanged(); + //============================================================================== + /** Returns the component's current transparancy level. + See setAlpha() for more details. + */ + float getAlpha() const noexcept; + /** Changes the transparency of this component. When painted, the entire component and all its children will be rendered with this as the overall opacity level, where 0 is completely invisible, and 1.0 is fully opaque (i.e. normal). - @see getAlpha + @see getAlpha, alphaChanged */ void setAlpha (float newAlpha); - /** Returns the component's current transparancy level. - See setAlpha() for more details. + /** Called when setAlpha() is used to change the alpha value of this component. + If you override this, you should also invoke the base class's implementation + during your overridden function, as it performs some repainting behaviour. */ - float getAlpha() const; + virtual void alphaChanged(); //============================================================================== /** Changes the mouse cursor shape to use when the mouse is over this component. diff --git a/modules/juce_gui_extra/embedding/juce_NSViewComponent.h b/modules/juce_gui_extra/embedding/juce_NSViewComponent.h index aa71b53355..6903f2cbd8 100644 --- a/modules/juce_gui_extra/embedding/juce_NSViewComponent.h +++ b/modules/juce_gui_extra/embedding/juce_NSViewComponent.h @@ -73,6 +73,8 @@ public: /** @internal */ void paint (Graphics&) override; /** @internal */ + void alphaChanged() override; + /** @internal */ static ReferenceCountedObject* attachViewToComponent (Component&, void*); private: diff --git a/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm b/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm index 27ce47c491..01ab9d31ec 100644 --- a/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm +++ b/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm @@ -100,6 +100,7 @@ public: { [view retain]; [view setPostsFrameChangedNotifications: YES]; + updateAlpha(); if (owner.isShowing()) componentPeerChanged(); @@ -169,6 +170,11 @@ public: owner.childBoundsChanged (nullptr); } + void updateAlpha() + { + [view setAlphaValue: (CGFloat) owner.getAlpha()]; + } + NSView* const view; private: @@ -217,6 +223,12 @@ void NSViewComponent::resizeToFitView() void NSViewComponent::paint (Graphics&) {} +void NSViewComponent::alphaChanged() +{ + if (attachment != nullptr) + (static_cast (attachment.get()))->updateAlpha(); +} + ReferenceCountedObject* NSViewComponent::attachViewToComponent (Component& comp, void* const view) { return new NSViewAttachment ((NSView*) view, comp);