Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_mac_NSViewComponent.mm 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. struct NSViewResizeWatcher
  16. {
  17. NSViewResizeWatcher() : callback (nil) {}
  18. virtual ~NSViewResizeWatcher()
  19. {
  20. // must call detachViewWatcher() first
  21. jassert (callback == nil);
  22. }
  23. void attachViewWatcher (NSView* view)
  24. {
  25. static ViewFrameChangeCallbackClass cls;
  26. callback = [cls.createInstance() init];
  27. ViewFrameChangeCallbackClass::setTarget (callback, this);
  28. [[NSNotificationCenter defaultCenter] addObserver: callback
  29. selector: @selector (frameChanged:)
  30. name: NSViewFrameDidChangeNotification
  31. object: view];
  32. }
  33. void detachViewWatcher()
  34. {
  35. if (callback != nil)
  36. {
  37. [[NSNotificationCenter defaultCenter] removeObserver: callback];
  38. [callback release];
  39. callback = nil;
  40. }
  41. }
  42. virtual void viewResized() = 0;
  43. private:
  44. id callback;
  45. //==============================================================================
  46. struct ViewFrameChangeCallbackClass : public ObjCClass<NSObject>
  47. {
  48. ViewFrameChangeCallbackClass() : ObjCClass<NSObject> ("JUCE_NSViewCallback_")
  49. {
  50. addIvar<NSViewResizeWatcher*> ("target");
  51. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  52. registerClass();
  53. }
  54. static void setTarget (id self, NSViewResizeWatcher* c)
  55. {
  56. object_setInstanceVariable (self, "target", c);
  57. }
  58. private:
  59. static void frameChanged (id self, SEL, NSNotification*)
  60. {
  61. if (auto* target = getIvar<NSViewResizeWatcher*> (self, "target"))
  62. target->viewResized();
  63. }
  64. JUCE_DECLARE_NON_COPYABLE (ViewFrameChangeCallbackClass)
  65. };
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NSViewResizeWatcher)
  67. };
  68. //==============================================================================
  69. class NSViewAttachment : public ReferenceCountedObject,
  70. public ComponentMovementWatcher,
  71. private NSViewResizeWatcher
  72. {
  73. public:
  74. NSViewAttachment (NSView* v, Component& comp)
  75. : ComponentMovementWatcher (&comp),
  76. view (v), owner (comp),
  77. currentPeer (nullptr)
  78. {
  79. [view retain];
  80. [view setPostsFrameChangedNotifications: YES];
  81. updateAlpha();
  82. if (owner.isShowing())
  83. componentPeerChanged();
  84. attachViewWatcher (view);
  85. }
  86. ~NSViewAttachment() override
  87. {
  88. detachViewWatcher();
  89. removeFromParent();
  90. [view release];
  91. }
  92. void componentMovedOrResized (Component& comp, bool wasMoved, bool wasResized) override
  93. {
  94. ComponentMovementWatcher::componentMovedOrResized (comp, wasMoved, wasResized);
  95. // The ComponentMovementWatcher version of this method avoids calling
  96. // us when the top-level comp is resized, but for an NSView we need to know this
  97. // because with inverted coordinates, we need to update the position even if the
  98. // top-left pos hasn't changed
  99. if (comp.isOnDesktop() && wasResized)
  100. componentMovedOrResized (wasMoved, wasResized);
  101. }
  102. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  103. {
  104. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  105. {
  106. auto r = makeNSRect (peer->getAreaCoveredBy (owner));
  107. r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
  108. [view setFrame: r];
  109. }
  110. }
  111. void componentPeerChanged() override
  112. {
  113. auto* peer = owner.getPeer();
  114. if (currentPeer != peer)
  115. {
  116. currentPeer = peer;
  117. if (peer != nullptr)
  118. {
  119. auto peerView = (NSView*) peer->getNativeHandle();
  120. [peerView addSubview: view];
  121. componentMovedOrResized (false, false);
  122. }
  123. else
  124. {
  125. removeFromParent();
  126. }
  127. }
  128. [view setHidden: ! owner.isShowing()];
  129. }
  130. void componentVisibilityChanged() override
  131. {
  132. componentPeerChanged();
  133. }
  134. void viewResized() override
  135. {
  136. owner.childBoundsChanged (nullptr);
  137. }
  138. void updateAlpha()
  139. {
  140. [view setAlphaValue: (CGFloat) owner.getAlpha()];
  141. }
  142. NSView* const view;
  143. using Ptr = ReferenceCountedObjectPtr<NSViewAttachment>;
  144. private:
  145. Component& owner;
  146. ComponentPeer* currentPeer;
  147. void removeFromParent()
  148. {
  149. if ([view superview] != nil)
  150. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  151. // override the call and use it as a sign that they're being deleted, which breaks everything..
  152. }
  153. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NSViewAttachment)
  154. };
  155. //==============================================================================
  156. NSViewComponent::NSViewComponent() {}
  157. NSViewComponent::~NSViewComponent() {}
  158. void NSViewComponent::setView (void* view)
  159. {
  160. if (view != getView())
  161. {
  162. auto old = attachment;
  163. attachment = nullptr;
  164. if (view != nullptr)
  165. attachment = attachViewToComponent (*this, view);
  166. old = nullptr;
  167. }
  168. }
  169. void* NSViewComponent::getView() const
  170. {
  171. return attachment != nullptr ? static_cast<NSViewAttachment*> (attachment.get())->view
  172. : nullptr;
  173. }
  174. void NSViewComponent::resizeToFitView()
  175. {
  176. if (attachment != nullptr)
  177. {
  178. auto r = [static_cast<NSViewAttachment*> (attachment.get())->view frame];
  179. setBounds (Rectangle<int> ((int) r.size.width, (int) r.size.height));
  180. }
  181. }
  182. void NSViewComponent::paint (Graphics&) {}
  183. void NSViewComponent::alphaChanged()
  184. {
  185. if (attachment != nullptr)
  186. (static_cast<NSViewAttachment*> (attachment.get()))->updateAlpha();
  187. }
  188. ReferenceCountedObject* NSViewComponent::attachViewToComponent (Component& comp, void* view)
  189. {
  190. return new NSViewAttachment ((NSView*) view, comp);
  191. }
  192. } // namespace juce