Skip to content

Commit

Permalink
Merge pull request #124 from ImJimmi/string-widths
Browse files Browse the repository at this point in the history
Handle JUCE 8.0.2 deprecating `Font::getStringWidth()`
  • Loading branch information
sudara authored Oct 12, 2024
2 parents ac5595d + b3622fc commit c0baed5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 6 additions & 5 deletions melatonin/components/overlay.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "../helpers/misc.h"
#include "../lookandfeel.h"

namespace melatonin
{
Expand Down Expand Up @@ -351,7 +352,7 @@ namespace melatonin

void drawDimensionsLabel()
{
int labelWidth = (int) dimensions.getFont().getStringWidthFloat (dimensionsString (selectedBounds)) + 15;
int labelWidth = (int) InspectorLookAndFeel::getStringWidth (dimensions.getFont(), dimensionsString (selectedBounds)) + 15;
int labelHeight = 15;
auto paddingToLabel = 4;
auto labelCenterX = selectedBounds.getX() + selectedBounds.getWidth() / 2;
Expand Down Expand Up @@ -413,7 +414,7 @@ namespace melatonin
// top
if (lineToTopHoveredComponent.getLength() > 0)
{
int labelWidth = (int) distanceToTopHoveredLabel.getFont().getStringWidthFloat (distanceString (lineToTopHoveredComponent)) + 15;
int labelWidth = (int) InspectorLookAndFeel::getStringWidth (distanceToTopHoveredLabel.getFont(), distanceString (lineToTopHoveredComponent)) + 15;

// todo draw on left or right side of line
auto labelCenterY = lineToTopHoveredComponent.getPointAlongLineProportionally (0.5f).getY();
Expand All @@ -429,7 +430,7 @@ namespace melatonin
// bottom
if (lineToBottomHoveredComponent.getLength() > 0)
{
int labelWidth = (int) distanceToBottomHoveredLabel.getFont().getStringWidthFloat (distanceString (lineToBottomHoveredComponent)) + 15;
int labelWidth = (int) InspectorLookAndFeel::getStringWidth (distanceToBottomHoveredLabel.getFont(), distanceString (lineToBottomHoveredComponent)) + 15;

// todo draw on left or right side of line
auto labelCenterY = lineToBottomHoveredComponent.getPointAlongLineProportionally (0.5f).getY();
Expand All @@ -445,7 +446,7 @@ namespace melatonin
// right
if (lineToRightHoveredComponent.getLength() > 0)
{
int labelWidth = (int) distanceToRightHoveredLabel.getFont().getStringWidthFloat (distanceString (lineToRightHoveredComponent)) + 15;
int labelWidth = (int) InspectorLookAndFeel::getStringWidth (distanceToRightHoveredLabel.getFont(), distanceString (lineToRightHoveredComponent)) + 15;

// todo draw on top or bottom side of line
auto labelCenterX = lineToRightHoveredComponent.getPointAlongLineProportionally (0.5f).getX();
Expand All @@ -461,7 +462,7 @@ namespace melatonin
// left
if (lineToLeftHoveredComponent.getLength() > 0)
{
int labelWidth = (int) distanceToLeftHoveredLabel.getFont().getStringWidthFloat (distanceString (lineToLeftHoveredComponent)) + 15;
int labelWidth = (int) InspectorLookAndFeel::getStringWidth (distanceToLeftHoveredLabel.getFont(), distanceString (lineToLeftHoveredComponent)) + 15;

// todo draw on top or bottom side of line
auto labelCenterX = lineToLeftHoveredComponent.getPointAlongLineProportionally (0.5f).getX();
Expand Down
13 changes: 11 additions & 2 deletions melatonin/lookandfeel.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace melatonin
// don't use the target app's font
juce::Font getLabelFont (juce::Label& label) override
{
return getInspectorFont(label.getFont().getHeight(), label.getFont().getStyleFlags());
return getInspectorFont (label.getFont().getHeight(), label.getFont().getStyleFlags());
}

// oh i dream of css resets...
Expand Down Expand Up @@ -164,7 +164,7 @@ namespace melatonin

if (component.getProperties().getWithDefault ("isUserProperty", false))
{
auto textWidth = (float) g.getCurrentFont().getStringWidth (component.getName());
auto textWidth = getStringWidth (g.getCurrentFont(), component.getName());
auto tagBounds = juce::Rectangle<float> (3 + textWidth + 3, 7, 40, 14).toFloat();
g.setColour (colors::panelBackgroundDarker);
g.fillRoundedRectangle (tagBounds, 3);
Expand Down Expand Up @@ -250,5 +250,14 @@ namespace melatonin
#endif

}

static float getStringWidth (const juce::Font& font, const juce::String& text)
{
#if JUCE_MAJOR_VERSION == 8 && JUCE_BUILDNUMBER >= 2
return juce::GlyphArrangement::getStringWidth (font, text);
#else
return font.getStringWidthFloat (text);
#endif
}
};
}

0 comments on commit c0baed5

Please sign in to comment.