From ab1bfac9a938053663bc6e0412545a19bd786878 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 23 Jun 2022 14:37:13 -0400 Subject: [PATCH] First size fix (#67) * A first attempt at improving the adjust size approach This is a first shot at making adjust size / set size work. It naively just applies aspect ratio to with which makes corner and bottom drags work but not side drags. An algorithm to work for all drags without knowing the direction of the user gesture which is stable continues to elude me, but I wanted at least something more reasonable in teh codebase before I go on vacation. Addresses #56 * Fix for pipelin * There is no std clamp in c++14 * Respond to review comments --- clap-libs/clap-helpers | 2 +- src/wrapper/clap-juce-wrapper.cpp | 61 ++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/clap-libs/clap-helpers b/clap-libs/clap-helpers index 2bb43c1..b101259 160000 --- a/clap-libs/clap-helpers +++ b/clap-libs/clap-helpers @@ -1 +1 @@ -Subproject commit 2bb43c18788c689708ead6f127a2d75e772ab389 +Subproject commit b101259ae06964c76c6806f02ec07847acb7b6dc diff --git a/src/wrapper/clap-juce-wrapper.cpp b/src/wrapper/clap-juce-wrapper.cpp index 87a4fd1..10b8423 100644 --- a/src/wrapper/clap-juce-wrapper.cpp +++ b/src/wrapper/clap-juce-wrapper.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1 #include @@ -108,6 +109,10 @@ JUCE_BEGIN_IGNORE_WARNINGS_MSVC(4996) // allow strncpy #define CLAP_MISBEHAVIOUR_HANDLER_LEVEL "Ignore" #endif +// This is useful for debugging overrides +// #undef CLAP_MISBEHAVIOUR_HANDLER_LEVEL +// #define CLAP_MISBEHAVIOUR_HANDLER_LEVEL Terminate + /* * A little class that sets an atomic bool to a value across its lifetime and * restores it on exit. @@ -992,6 +997,12 @@ class ClapJuceWrapper : public clap::helpers::Plugin< return editor->isResizable(); return true; } + + /* + * guiAdjustSize is called before guiSetSize and given the option to + * reset the size the host hands to the subsequent setSize. This is a + * relatively naive and unsatisfactory initial implementation. + */ bool guiAdjustSize(uint32_t *w, uint32_t *h) noexcept override { if (!editor) @@ -1000,7 +1011,55 @@ class ClapJuceWrapper : public clap::helpers::Plugin< if (!editor->isResizable()) return false; - editor->setSize(static_cast(*w), static_cast(*h)); + auto cst = editor->getConstrainer(); + + if (!cst) + return true; // we have no constraints. Whaever is fine! + + auto minW = (uint32_t)cst->getMinimumWidth(); + auto maxW = (uint32_t)cst->getMaximumWidth(); + auto minH = (uint32_t)cst->getMinimumHeight(); + auto maxH = (uint32_t)cst->getMaximumHeight(); + + // There is no std::clamp in c++14 + auto width = juce::jlimit(minW, maxW, *w); + auto height = juce::jlimit(minH, maxH, *h); + + auto aspectRatio = (float)cst->getFixedAspectRatio(); + + if (aspectRatio != 0.0) + { + /* + * This is obviously an unsatisfactory algorithm, but we wanted to have + * something at least workable here. + * + * The problem with other algorithms I tried is that this function gets + * called by BWS for sub-single pixel motions on macOS, so it is hard to make + * a version which is *stable* (namely adjust(w,h); cw=w;ch=h; adjust(cw,ch); + * cw == w; ch == h) that deals with directions. I tried all sorts of stuff + * and then ran into vacation. + * + * So for now here's this approach. See the discussion in CJE PR #67 + * and interop-tracker issue #30. + */ + width = std::round(aspectRatio * height); + } + + *w = width; + *h = height; + + return true; + } + + bool guiSetSize(uint32_t width, uint32_t height) noexcept override + { + if (!editor) + return false; + + if (!editor->isResizable()) + return false; + + editor->setSize(static_cast(width), static_cast(height)); return true; }