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.

248 lines
7.3KB

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