Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A first attempt at improving the adjust size approach #66

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/wrapper/clap-juce-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>

#define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
#include <juce_audio_processors/juce_audio_processors.h>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -992,6 +997,7 @@ class ClapJuceWrapper : public clap::helpers::Plugin<
return editor->isResizable();
return true;
}

bool guiAdjustSize(uint32_t *w, uint32_t *h) noexcept override
{
if (!editor)
Expand All @@ -1000,7 +1006,45 @@ class ClapJuceWrapper : public clap::helpers::Plugin<
if (!editor->isResizable())
return false;

editor->setSize(static_cast<int>(*w), static_cast<int>(*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();

auto width = std::clamp(*w, minW, maxW);
auto height = std::clamp(*h, minH, maxH);

auto aspectRatio = (float)cst->getFixedAspectRatio();

if (aspectRatio != 0.0)
{
// This is an unsatisfactory but stable algorithm.
// Since we don't know drag direction it is hard to
// find a stable better one, at least it is for me right
// now
width = 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<int>(width), static_cast<int>(height));
return true;
}

Expand Down