Skip to content

Commit

Permalink
Merge pull request #1974 from alcomposer/hide-decimal
Browse files Browse the repository at this point in the history
Hide decimal point for all draggable numbers if there is no fractiona…
  • Loading branch information
timothyschoen authored Nov 26, 2024
2 parents 7d8c5aa + 3583a3c commit 95e3d0c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Source/Components/DraggableNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,30 @@ class DraggableNumber : public Label
nvgTextLetterSpacing(nvg, 0.275f);
nvgTextAlign(nvg, NVG_ALIGN_MIDDLE | NVG_ALIGN_LEFT);
nvgFillColor(nvg, NVGComponent::convertColour(textColour));
nvgText(nvg, textArea.getX(), textArea.getCentreY() + 1.5f, numberText.toRawUTF8(), nullptr);

// NOTE: We could simply do `String(numberText.getDoubleValue(), 0)` but using string manipulation
// bypasses any potential issues with precision
auto removeDecimalNumString = [](String& numString) {
if (numString.contains(".")) {
// Split the string into the integer and fractional parts
StringArray parts = StringArray::fromTokens(numString, ".", "");

// If the fractional part is "0" or empty, remove the decimal point
if (parts[1] == "0" || parts[1].isEmpty()) {
return parts[0]; // Just keep the integer part
} else {
return numString; // Keep the full string (with decimal point & fractional part)
}
} else {
// If there’s no decimal point, leave the string as it is
return numString;
}
};

// Only display the decimal point if fractional exists, but make sure to show it as a user hovers over the fractional decimal places
auto formatedNumber = isMouseOverOrDragging() && hoveredDecimal > 0 ? numberText : removeDecimalNumString(numberText);

nvgText(nvg, textArea.getX(), textArea.getCentreY() + 1.5f, formatedNumber.toRawUTF8(), nullptr);

if (dragMode == Regular) {
textArea = textArea.withTrimmedLeft(numberTextLength);
Expand Down

0 comments on commit 95e3d0c

Please sign in to comment.