Skip to content

Commit

Permalink
Fix number box expression parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Nov 13, 2024
1 parent b352f0b commit ccff307
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions Source/Components/DraggableNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,11 @@ class DraggableNumber : public Label
void editorHidden(Label*, TextEditor& editor) override
{
auto text = editor.getText();
double newValue;
if(!text.containsOnly("0123456789."))
{
String parseError;
try {
newValue = Expression(text, parseError).evaluate();
} catch (...) {
newValue = 0.0f;
}

if(!parseError.isEmpty())
{
newValue = 0.0f;
}
}
else {
newValue = text.getDoubleValue();
}
double newValue = parseExpression(text);

onInteraction(hasKeyboardFocus(false));
setValue(newValue, dontSendNotification);
editor.setText(getText(), dontSendNotification);
decimalDrag = 0;
dragEnd();
}
Expand Down Expand Up @@ -554,6 +538,41 @@ class DraggableNumber : public Label

return text;
}

double parseExpression(String const& expression)
{
if(expression.containsOnly("0123456789."))
{
return expression.getDoubleValue();
}
else {
String parseError;
try {
return Expression(expression, parseError).evaluate();
} catch (...) {
return 0.0f;
}

if(!parseError.isEmpty())
{
return 0.0f;
}
}

return 0.0f;
}

void textEditorFocusLost (TextEditor& editor) override
{
textEditorReturnKeyPressed(editor);
}

void textEditorReturnKeyPressed(TextEditor& editor) override
{
auto text = editor.getText();
double newValue = parseExpression(text);
setValue(newValue);
}
};

struct DraggableListNumber : public DraggableNumber {
Expand Down

0 comments on commit ccff307

Please sign in to comment.