Skip to content

Commit 95e3d0c

Browse files
Merge pull request #1974 from alcomposer/hide-decimal
Hide decimal point for all draggable numbers if there is no fractiona…
2 parents 7d8c5aa + 3583a3c commit 95e3d0c

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Source/Components/DraggableNumber.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,30 @@ class DraggableNumber : public Label
366366
nvgTextLetterSpacing(nvg, 0.275f);
367367
nvgTextAlign(nvg, NVG_ALIGN_MIDDLE | NVG_ALIGN_LEFT);
368368
nvgFillColor(nvg, NVGComponent::convertColour(textColour));
369-
nvgText(nvg, textArea.getX(), textArea.getCentreY() + 1.5f, numberText.toRawUTF8(), nullptr);
369+
370+
// NOTE: We could simply do `String(numberText.getDoubleValue(), 0)` but using string manipulation
371+
// bypasses any potential issues with precision
372+
auto removeDecimalNumString = [](String& numString) {
373+
if (numString.contains(".")) {
374+
// Split the string into the integer and fractional parts
375+
StringArray parts = StringArray::fromTokens(numString, ".", "");
376+
377+
// If the fractional part is "0" or empty, remove the decimal point
378+
if (parts[1] == "0" || parts[1].isEmpty()) {
379+
return parts[0]; // Just keep the integer part
380+
} else {
381+
return numString; // Keep the full string (with decimal point & fractional part)
382+
}
383+
} else {
384+
// If there’s no decimal point, leave the string as it is
385+
return numString;
386+
}
387+
};
388+
389+
// Only display the decimal point if fractional exists, but make sure to show it as a user hovers over the fractional decimal places
390+
auto formatedNumber = isMouseOverOrDragging() && hoveredDecimal > 0 ? numberText : removeDecimalNumString(numberText);
391+
392+
nvgText(nvg, textArea.getX(), textArea.getCentreY() + 1.5f, formatedNumber.toRawUTF8(), nullptr);
370393

371394
if (dragMode == Regular) {
372395
textArea = textArea.withTrimmedLeft(numberTextLength);

0 commit comments

Comments
 (0)