1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added notifications in NSViewComponent so that its childBoundsChanged method will be called if the embedded NSView changes bounds.

This commit is contained in:
jules 2014-01-24 20:35:16 +00:00
parent 4e8df54b4f
commit e11ca3533e

View file

@ -28,18 +28,30 @@ class NSViewAttachment : public ReferenceCountedObject,
public:
NSViewAttachment (NSView* const v, Component& comp)
: ComponentMovementWatcher (&comp),
view (v),
owner (comp),
currentPeer (nullptr)
view (v), owner (comp),
currentPeer (nullptr), frameChangeCallback (nullptr)
{
[view retain];
[view setPostsFrameChangedNotifications: YES];
if (owner.isShowing())
componentPeerChanged();
static ViewFrameChangeCallbackClass cls;
frameChangeCallback = [cls.createInstance() init];
ViewFrameChangeCallbackClass::setTarget (frameChangeCallback, &owner);
[[NSNotificationCenter defaultCenter] addObserver: frameChangeCallback
selector: @selector (frameChanged:)
name: NSViewFrameDidChangeNotification
object: view];
}
~NSViewAttachment()
{
[[NSNotificationCenter defaultCenter] removeObserver: frameChangeCallback];
[frameChangeCallback release];
removeFromParent();
[view release];
}
@ -96,6 +108,7 @@ public:
private:
Component& owner;
ComponentPeer* currentPeer;
id frameChangeCallback;
void removeFromParent()
{
@ -104,6 +117,31 @@ private:
// override the call and use it as a sign that they're being deleted, which breaks everything..
}
//==============================================================================
struct ViewFrameChangeCallbackClass : public ObjCClass<NSObject>
{
ViewFrameChangeCallbackClass() : ObjCClass<NSObject> ("JUCE_NSViewCallback_")
{
addIvar<Component*> ("target");
addMethod (@selector (frameChanged:), frameChanged, "v@:@");
registerClass();
}
static void setTarget (id self, Component* c)
{
object_setInstanceVariable (self, "target", c);
}
private:
static void frameChanged (id self, SEL, NSNotification*)
{
if (Component* const target = getIvar<Component*> (self, "target"))
target->childBoundsChanged (nullptr);
}
JUCE_DECLARE_NON_COPYABLE (ViewFrameChangeCallbackClass);
};
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NSViewAttachment)
};
@ -124,7 +162,7 @@ void NSViewComponent::setView (void* const view)
void* NSViewComponent::getView() const
{
return attachment != nullptr ? static_cast <NSViewAttachment*> (attachment.get())->view
return attachment != nullptr ? static_cast<NSViewAttachment*> (attachment.get())->view
: nullptr;
}
@ -132,7 +170,7 @@ void NSViewComponent::resizeToFitView()
{
if (attachment != nullptr)
{
NSRect r = [static_cast <NSViewAttachment*> (attachment.get())->view frame];
NSRect r = [static_cast<NSViewAttachment*> (attachment.get())->view frame];
setBounds (Rectangle<int> ((int) r.size.width, (int) r.size.height));
}
}