From 92435beb6015d359b15478bc18af04670517308c Mon Sep 17 00:00:00 2001 From: Riddhimann Date: Tue, 27 Oct 2026 12:39:14 +0530 Subject: [PATCH] Fix responsive NAMix editor resizing --- src/lv2/namix_lv2_ui.cpp | 15 +++---- src/namgeometry.h | 18 ++++++++ src/namview.cpp | 30 ++++++++++--- src/namview.h | 3 ++ src/platform/x11plugview.cpp | 85 +++++++++++++++++++++++++++++++++++- src/platform/x11plugview.h | 11 +++++ tools/namix_ttlgen.cpp | 10 ++--- tools/panelrender.cpp | 9 ++-- 8 files changed, 157 insertions(+), 24 deletions(-) diff --git a/src/lv2/namix_lv2_ui.cpp b/src/lv2/namix_lv2_ui.cpp index 5af873b..8becb61 100644 --- a/src/lv2/namix_lv2_ui.cpp +++ b/src/lv2/namix_lv2_ui.cpp @@ -26,8 +26,8 @@ // WHAT AN LV2 HOST DOES WITH THE WINDOW, verified against suil rather than assumed: suil's // x11_in_gtk3.c forward_size_request() reads the child's XSizeHints with XGetNormalHints, clamps // its allocation to them, and XResizeWindows the child. So the hints are how a UI states its -// limits. This editor is a fixed size — X11PlugView does not override canResize — so the hints it -// sets are min == max, which is the strongest thing it can say. +// limits. The editor publishes its initial size as a minimum and leaves the maximum open, so a +// host can expand the X11 child and the shared view can resize its drawing surfaces accordingly. #include "lv2message.h" #include "namixlv2.h" @@ -444,9 +444,8 @@ void NamixUi::close() // XSizeHints and clamps its allocation to them before resizing us (src/x11_in_gtk3.c, // forward_size_request). // -// This editor is a fixed size, so min == max == the editor's own rectangle. X11PlugView does not -// override canResize, which means CPluginView::canResize returns kResultFalse and the VST3 build -// tells its host exactly the same thing. +// The editor can grow past its initial size. Keep a 1x canvas as the minimum, but omit a maximum +// so LV2 hosts such as REAPER can allocate the UI to a larger FX window. void NamixUi::applySizeHints() { if (!mHintDisplay || !mView) @@ -460,9 +459,9 @@ void NamixUi::applySizeHints() return; XSizeHints hints = {}; - hints.flags = PMinSize | PMaxSize; - hints.min_width = hints.max_width = size.getWidth(); - hints.min_height = hints.max_height = size.getHeight(); + hints.flags = PMinSize; + hints.min_width = geo::kWinW; + hints.min_height = geo::kWinH; XSetWMNormalHints(mHintDisplay, window, &hints); XFlush(mHintDisplay); } diff --git a/src/namgeometry.h b/src/namgeometry.h index e53c57c..58c8beb 100644 --- a/src/namgeometry.h +++ b/src/namgeometry.h @@ -12,6 +12,9 @@ #include "pluginterfaces/vst/vsttypes.h" +#include +#include + namespace NAMix { namespace geo @@ -21,6 +24,21 @@ namespace geo constexpr int kWinW = 600; constexpr int kWinH = 400; +// The editor uses logical 600x400 coordinates. The host window and Cairo +// device space are enlarged together; NAMIX_UI_SCALE can override this for +// HiDPI setups (valid range 1.0..3.0). +constexpr float kDefaultUiScale = 1.5f; +inline float uiScale() +{ + const char *value = std::getenv("NAMIX_UI_SCALE"); + if (!value || !*value) + return kDefaultUiScale; + char *end = nullptr; + const float scale = std::strtof(value, &end); + return end != value && *end == '\0' && std::isfinite(scale) && scale >= 1.0f && scale <= 3.0f + ? scale + : kDefaultUiScale; +} // Palette (geometry.sh; from the original Colors.h). 0xRRGGBB. constexpr uint32_t kBgColor = 0x1D1A1F; // raisin-black backdrop constexpr uint32_t kAzure = 0x5085E8; // theme accent diff --git a/src/namview.cpp b/src/namview.cpp index 44c2526..17172c5 100644 --- a/src/namview.cpp +++ b/src/namview.cpp @@ -92,7 +92,9 @@ const char *baseName(const std::string &path) NamEditorView::NamEditorView(NamController *controller) : X11PlugView(static_cast(controller)), mController(controller) { - ViewRect size(0, 0, geo::kWinW, geo::kWinH); + mUiScale = geo::uiScale(); + ViewRect size(0, 0, static_cast(std::lround(geo::kWinW * mUiScale)), + static_cast(std::lround(geo::kWinH * mUiScale))); setRect(size); } @@ -111,6 +113,18 @@ void NamEditorView::onAttached() mResourcesLoaded = true; } +// Fit the 3:2 logical canvas into the current host allocation without +// distorting the panel. Any extra area becomes a clean black letterbox. +void NamEditorView::onResize(int width, int height) +{ + if (width <= 0 || height <= 0) + return; + mUiScale = std::min(static_cast(width) / geo::kWinW, + static_cast(height) / geo::kWinH); + mUiOffsetX = (width - geo::kWinW * mUiScale) * 0.5f; + mUiOffsetY = (height - geo::kWinH * mUiScale) * 0.5f; +} + // The window is going: drop an open field without applying it, and hand the // keyboard back while the window it was given to still exists. A drag cannot // outlive the editor either, so its edit gesture is closed for the host. @@ -165,6 +179,8 @@ bool NamEditorView::hitCircle(float x, float y, float cx, float cy, float r) //------------------------------------------------------------------------ void NamEditorView::onDraw(cairo_t *cr) { + cairo_translate(cr, mUiOffsetX, mUiOffsetY); + cairo_scale(cr, mUiScale, mUiScale); Canvas c(cr, &mFonts, geo::kWinW, geo::kWinH); compose(c); } @@ -636,7 +652,8 @@ void NamEditorView::onMouseDown(int x, int y, int button, unsigned /*mods*/, boo { if (button != 1) // left button only, as on the original return; - const float fx = static_cast(x), fy = static_cast(y); + const float fx = (static_cast(x) - mUiOffsetX) / mUiScale; + const float fy = (static_cast(y) - mUiOffsetY) / mUiScale; mMouseX = fx; mMouseY = fy; @@ -742,11 +759,11 @@ void NamEditorView::onMouseDown(int x, int y, int button, unsigned /*mods*/, boo // the way back. void NamEditorView::onMouseMove(int x, int y, unsigned mods) { - mMouseX = static_cast(x); - mMouseY = static_cast(y); + mMouseX = (static_cast(x) - mUiOffsetX) / mUiScale; + mMouseY = (static_cast(y) - mUiOffsetY) / mUiScale; if (!mDragParam || !mController) return; - const float fy = static_cast(y); + const float fy = (static_cast(y) - mUiOffsetY) / mUiScale; const float range = mDragRange * (isFine(mods) ? kFineDragFactor : 1.0f); mDragNorm += static_cast(mDragLastY - fy) / range; mDragNorm = mDragNorm < 0.0 ? 0.0 : (mDragNorm > 1.0 ? 1.0 : mDragNorm); @@ -770,7 +787,8 @@ void NamEditorView::onMouseUp(int /*x*/, int /*y*/, int button) // Wheel adjusts whatever is under the cursor (wheel up = increase). void NamEditorView::onMouseWheel(int x, int y, int delta, unsigned mods) { - const float fx = static_cast(x), fy = static_cast(y); + const float fx = (static_cast(x) - mUiOffsetX) / mUiScale; + const float fy = (static_cast(y) - mUiOffsetY) / mUiScale; const bool fine = isFine(mods); const double step = delta * (fine ? kFineWheelStep : kWheelStep); diff --git a/src/namview.h b/src/namview.h index 48b2a06..b47ef07 100644 --- a/src/namview.h +++ b/src/namview.h @@ -52,6 +52,7 @@ class NamEditorView : public Steinberg::X11PlugView //---from X11PlugView------------- void onAttached() SMTG_OVERRIDE; + void onResize(int width, int height) SMTG_OVERRIDE; void onRemoved() SMTG_OVERRIDE; void onDraw(cairo_t *cr) SMTG_OVERRIDE; void onMouseDown(int x, int y, int button, unsigned mods, bool doubleClick) SMTG_OVERRIDE; @@ -123,6 +124,8 @@ class NamEditorView : public Steinberg::X11PlugView } NamController *mController = nullptr; + float mUiScale = geo::kDefaultUiScale; + float mUiOffsetX = 0, mUiOffsetY = 0; FontStack mFonts; ImageCache mImages; diff --git a/src/platform/x11plugview.cpp b/src/platform/x11plugview.cpp index 24226ea..b4ed559 100644 --- a/src/platform/x11plugview.cpp +++ b/src/platform/x11plugview.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -260,6 +261,41 @@ tresult PLUGIN_API X11PlugView::attached(void *parent, FIDString type) return isWindowOpen() ? kResultOk : kResultFalse; } +//------------------------------------------------------------------------ +tresult PLUGIN_API X11PlugView::onSize(ViewRect *newSize) +{ + if (!newSize || newSize->getWidth() <= 0 || newSize->getHeight() <= 0) + return kInvalidArgument; + const tresult result = Vst::EditorView::onSize(newSize); + if (result != kResultTrue && result != kResultOk) + return result; + resizeSurfaces(static_cast(newSize->getWidth()), + static_cast(newSize->getHeight())); + return kResultTrue; +} + +// Keep the panel's 3:2 layout intact while allowing the host to resize the +// editor. Whichever dimension changed less determines the resulting size. +tresult PLUGIN_API X11PlugView::checkSizeConstraint(ViewRect *requested) +{ + if (!requested) + return kInvalidArgument; + constexpr double aspect = 1.5; + const int width = requested->getWidth(); + const int height = requested->getHeight(); + if (width <= 0 || height <= 0) + return kResultFalse; + + const int constrainedWidth = static_cast(std::lround(height * aspect)); + const int constrainedHeight = static_cast(std::lround(width / aspect)); + const ViewRect ¤t = getRect(); + if (std::abs(width - current.getWidth()) >= std::abs(height - current.getHeight())) + requested->right = requested->left + constrainedWidth; + else + requested->bottom = requested->top + constrainedHeight; + return kResultTrue; +} + //------------------------------------------------------------------------ void X11PlugView::trace(const char *fmt, ...) const { @@ -502,6 +538,9 @@ bool X11PlugView::openWindow(::Window parent) closeWindow(); return false; } + mSurfaceWidth = width; + mSurfaceHeight = height; + onResize(static_cast(width), static_cast(height)); return true; } @@ -714,6 +753,16 @@ void X11PlugView::drainEvents() continue; switch (event.type) { + case ConfigureNotify: + if (event.xconfigure.width > 0 && event.xconfigure.height > 0 && + (static_cast(event.xconfigure.width) != mSurfaceWidth || + static_cast(event.xconfigure.height) != mSurfaceHeight)) { + resizeSurfaces(static_cast(event.xconfigure.width), + static_cast(event.xconfigure.height)); + mDirty = true; + } + break; + case Expose: // Coalesce: only the last expose in a burst needs a repaint, // and the repaint itself happens on the next tick. @@ -876,6 +925,35 @@ void X11PlugView::ensureMapped() mapWindow(); } +//------------------------------------------------------------------------ +void X11PlugView::resizeSurfaces(unsigned width, unsigned height) +{ + if (!mDisplay || !mWindow || width == 0 || height == 0 || + (width == mSurfaceWidth && height == mSurfaceHeight)) + return; + + // The host has already resized the embedded X window before onSize() or + // ConfigureNotify arrives. Sending another XResizeWindow here feeds a + // second resize back into the host while it is dragging the frame and can + // leave stale/interleaved ConfigureNotify events behind. + if (mTarget) + cairo_xlib_surface_set_size(mTarget, static_cast(width), static_cast(height)); + if (mBuffer) + cairo_surface_destroy(mBuffer); + mBuffer = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, static_cast(width), + static_cast(height)); + if (cairo_surface_status(mBuffer) != CAIRO_STATUS_SUCCESS) { + fprintf(stderr, "NAMix: cannot resize the editor drawing buffer\n"); + cairo_surface_destroy(mBuffer); + mBuffer = nullptr; + return; + } + mSurfaceWidth = width; + mSurfaceHeight = height; + onResize(static_cast(width), static_cast(height)); + mDirty = true; +} + //------------------------------------------------------------------------ void X11PlugView::redraw() { @@ -891,8 +969,13 @@ void X11PlugView::redraw() // Compose into the offscreen buffer... cairo_t *cr = cairo_create(mBuffer); - if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) + if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) { + cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); + cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); + cairo_paint(cr); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); onDraw(cr); + } cairo_destroy(cr); // ...then blit it to the window in one operation, so no partially drawn diff --git a/src/platform/x11plugview.h b/src/platform/x11plugview.h index 371a5a2..158565c 100644 --- a/src/platform/x11plugview.h +++ b/src/platform/x11plugview.h @@ -60,6 +60,9 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public //---from CPluginView------------- tresult PLUGIN_API isPlatformTypeSupported(FIDString type) SMTG_OVERRIDE; tresult PLUGIN_API attached(void *parent, FIDString type) SMTG_OVERRIDE; + tresult PLUGIN_API onSize(ViewRect *newSize) SMTG_OVERRIDE; + tresult PLUGIN_API canResize() SMTG_OVERRIDE { return kResultTrue; } + tresult PLUGIN_API checkSizeConstraint(ViewRect *rect) SMTG_OVERRIDE; void attachedToParent() SMTG_OVERRIDE; void removedFromParent() SMTG_OVERRIDE; @@ -92,6 +95,12 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public // Paint the whole editor. `cr` targets the offscreen buffer. virtual void onDraw(cairo_t *cr) = 0; + // The host changed the embedded window's physical pixel dimensions. + virtual void onResize(int width, int height) + { + (void)width, (void)height; + } + // Pointer input. Buttons are X button numbers (1 = left, 2 = middle, // 3 = right). Wheel steps arrive as delta +1 (up) or -1 (down). `mods` // is a PointerMod mask. `doubleClick` is set on the second press of the @@ -173,6 +182,7 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public void redraw(); void mapWindow(); void ensureMapped(); + void resizeSurfaces(unsigned width, unsigned height); // Diagnostics, enabled by setting NAMIX_X11_TRACE in the environment. They // exist because the embedding contract is host-specific and only partly @@ -191,6 +201,7 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public Atom mXEmbedInfoAtom = None; cairo_surface_t *mTarget = nullptr; // xlib surface for mWindow cairo_surface_t *mBuffer = nullptr; // offscreen image surface + unsigned mSurfaceWidth = 0, mSurfaceHeight = 0; IPtr mRunLoop; bool mEventHandlerRegistered = false; diff --git a/tools/namix_ttlgen.cpp b/tools/namix_ttlgen.cpp index 2dd97f1..6fd9184 100644 --- a/tools/namix_ttlgen.cpp +++ b/tools/namix_ttlgen.cpp @@ -321,10 +321,9 @@ bool writePluginTtl(const std::string &path, const std::vector &ports) "# capabilities and the plug-in's state to the editor.\n" "#\n" "# ui:resize is deprecated in the specification and is still the only way a UI can\n" - "# state its size, so it is declared OPTIONAL: the editor asks once at startup and\n" - "# carries on without it. ui:noUserResize IS declared, because this editor is a fixed\n" - "# size — X11PlugView does not override canResize, so the VST3 build tells a host the\n" - "# same thing.\n", + "# state its preferred initial size, so it is declared OPTIONAL: the editor asks once at\n" + "# startup and carries on without it. User resizing is supported, so ui:noUserResize is\n" + "# intentionally omitted.\n", kPortFeedbackFirst, kPortFeedbackFirst + kFeedbackCount - 1, kPortAtomOut); fprintf(f, "<%s>\n\ta ui:X11UI ;\n\tlv2:binary ;\n\tui:binary ;\n" @@ -339,8 +338,7 @@ bool writePluginTtl(const std::string &path, const std::vector &ports) "\t\tui:protocol ui:floatProtocol\n\t]", kPluginUri, kPortFeedbackFirst + static_cast(i)); } - fprintf(f, " ;\n\tlv2:extensionData ui:idleInterface ;\n" - "\tlv2:portProperty ui:noUserResize .\n\n"); + fprintf(f, " ;\n\tlv2:extensionData ui:idleInterface .\n\n"); // --- the plug-in ----------------------------------------------------- // diff --git a/tools/panelrender.cpp b/tools/panelrender.cpp index 5053e0f..b159293 100644 --- a/tools/panelrender.cpp +++ b/tools/panelrender.cpp @@ -128,9 +128,12 @@ int main(int argc, char **argv) SvgCache svgs; svgs.setResourceDir(res); - cairo_surface_t *surface = - cairo_image_surface_create(CAIRO_FORMAT_ARGB32, geo::kWinW, geo::kWinH); + const float scale = geo::uiScale(); + const int width = static_cast(std::lround(geo::kWinW * scale)); + const int height = static_cast(std::lround(geo::kWinH * scale)); + cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); cairo_t *cr = cairo_create(surface); + cairo_scale(cr, scale, scale); Canvas c(cr, &fonts, geo::kWinW, geo::kWinH); // --- backdrop + line overlay --- @@ -267,7 +270,7 @@ int main(int argc, char **argv) if (!fontsOk) ++missing; - printf("panelrender: wrote %s (%dx%d)\n", outPath, geo::kWinW, geo::kWinH); + printf("panelrender: wrote %s (%dx%d)\n", outPath, width, height); if (missing) { fprintf(stderr, "panelrender: %d asset(s) failed to load\n", missing); return 1;