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.

105 lines
3.1KB

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