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.

112 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. #if JUCE_MAC
  19. namespace juce
  20. {
  21. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  22. const auto nsViewFrameChangedSelector = @selector (frameChanged:);
  23. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  24. struct NSViewCallbackInterface
  25. {
  26. virtual ~NSViewCallbackInterface() = default;
  27. virtual void frameChanged() = 0;
  28. };
  29. //==============================================================================
  30. struct NSViewFrameChangeCallbackClass : public ObjCClass<NSObject>
  31. {
  32. NSViewFrameChangeCallbackClass()
  33. : ObjCClass ("JUCE_NSViewCallback_")
  34. {
  35. addIvar<NSViewCallbackInterface*> ("target");
  36. addMethod (nsViewFrameChangedSelector, frameChanged);
  37. registerClass();
  38. }
  39. static void setTarget (id self, NSViewCallbackInterface* c)
  40. {
  41. object_setInstanceVariable (self, "target", c);
  42. }
  43. private:
  44. static void frameChanged (id self, SEL, NSNotification*)
  45. {
  46. if (auto* target = getIvar<NSViewCallbackInterface*> (self, "target"))
  47. target->frameChanged();
  48. }
  49. JUCE_DECLARE_NON_COPYABLE (NSViewFrameChangeCallbackClass)
  50. };
  51. //==============================================================================
  52. class NSViewFrameWatcher : private NSViewCallbackInterface
  53. {
  54. public:
  55. NSViewFrameWatcher (NSView* viewToWatch, std::function<void()> viewResizedIn)
  56. : viewResized (std::move (viewResizedIn)), callback (makeCallbackForView (viewToWatch))
  57. {
  58. }
  59. ~NSViewFrameWatcher() override
  60. {
  61. [[NSNotificationCenter defaultCenter] removeObserver: callback];
  62. [callback release];
  63. callback = nil;
  64. }
  65. JUCE_DECLARE_NON_COPYABLE (NSViewFrameWatcher)
  66. JUCE_DECLARE_NON_MOVEABLE (NSViewFrameWatcher)
  67. private:
  68. id makeCallbackForView (NSView* view)
  69. {
  70. static NSViewFrameChangeCallbackClass cls;
  71. auto* result = [cls.createInstance() init];
  72. NSViewFrameChangeCallbackClass::setTarget (result, this);
  73. [[NSNotificationCenter defaultCenter] addObserver: result
  74. selector: nsViewFrameChangedSelector
  75. name: NSViewFrameDidChangeNotification
  76. object: view];
  77. return result;
  78. }
  79. void frameChanged() override { viewResized(); }
  80. std::function<void()> viewResized;
  81. id callback;
  82. };
  83. } // namespace juce
  84. #endif