Skip to content

Commit 85a77a5

Browse files
committed
Improve text/number editor: fix interactionFn, fix int showing decimals, clean up number range
1 parent bbccf51 commit 85a77a5

8 files changed

Lines changed: 31 additions & 58 deletions

File tree

Source/Canvas.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,12 @@ void Canvas::performRender(NVGcontext* nvg, Rectangle<int> invalidRegion)
796796
if (canvasSearchHighlight)
797797
canvasSearchHighlight->render(nvg);
798798

799-
if (dimensionsAreBeingEdited)
799+
if (dimensionsAreBeingEdited) {
800+
bool borderWasShown = showBorder;
801+
showBorder = true;
800802
drawBorder(false, true);
803+
showBorder = borderWasShown;
804+
}
801805

802806
if (objectsDistributeResizer)
803807
objectsDistributeResizer->render(nvg);
@@ -2788,8 +2792,8 @@ void Canvas::receiveMessage(t_symbol* symbol, SmallArray<pd::Atom> const& atoms)
27882792
if (atoms.size() >= 4) {
27892793
auto const width = atoms[2].getFloat() - atoms[0].getFloat();
27902794
auto const height = atoms[3].getFloat() - atoms[1].getFloat();
2791-
setValueExcludingListener(patchWidth, width, this);
2792-
setValueExcludingListener(patchHeight, height, this);
2795+
setValueExcludingListener(patchWidth, static_cast<int>(width), this);
2796+
setValueExcludingListener(patchHeight, static_cast<int>(height), this);
27932797
repaint();
27942798
}
27952799

Source/Components/DraggableNumber.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ void DraggableNumber::mouseUp(MouseEvent const& e)
644644
if (editor)
645645
return;
646646

647-
onInteraction(hasKeyboardFocus(false));
647+
onInteraction(false);
648648

649649
repaint();
650650

Source/Components/PropertiesPanel.h

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,17 @@ class PropertiesPanel : public Component {
312312
Value property;
313313
String allowedCharacters = "";
314314
double min, max;
315+
bool limit;
315316

316317
public:
317318
std::unique_ptr<Component> label;
318319

319-
EditableComponent(String const& propertyName, Value const& value, double minimum = 0.0, double maximum = 0.0, std::function<void(bool)> onInteractionFn = nullptr)
320+
EditableComponent(String const& propertyName, Value const& value, bool clip = false, double minimum = 0.0, double maximum = 1<<30, std::function<void(bool)> onInteractionFn = nullptr)
320321
: PropertiesPanelProperty(propertyName)
321322
, property(value)
322323
, min(minimum)
323324
, max(maximum)
325+
, limit(clip)
324326
{
325327
if constexpr (std::is_arithmetic_v<T>) {
326328
auto* draggableNumber = new DraggableNumber(std::is_integral_v<T>);
@@ -331,29 +333,30 @@ class PropertiesPanel : public Component {
331333
draggableNumber->setFont(draggableNumber->getFont().withHeight(14.5f));
332334
draggableNumber->setEditableOnClick(true);
333335

334-
if (minimum != 0.0f)
336+
if(clip)
337+
{
335338
draggableNumber->setMinimum(minimum);
336-
if (maximum != 0.0f)
337339
draggableNumber->setMaximum(maximum);
340+
}
338341

339342
if (onInteractionFn)
340343
draggableNumber->onInteraction = onInteractionFn;
341-
344+
342345
draggableNumber->setPrecision(3);
343346

344347
draggableNumber->onValueChange = [this](double const newValue) {
345-
if (min != 0.0f || max != 0.0f) {
346-
property = std::clamp(newValue, min, max);
348+
if (limit) {
349+
property = std::clamp<T>(newValue, min, max);
347350
} else {
348-
property = newValue;
351+
property = static_cast<T>(newValue);
349352
}
350353
};
351354

352355
draggableNumber->onReturnKey = [this](double const newValue) {
353-
if (min != 0.0f || max != 0.0f) {
354-
property = std::clamp(newValue, min, max);
356+
if (limit) {
357+
property = std::clamp<T>(newValue, min, max);
355358
} else {
356-
property = newValue;
359+
property = static_cast<T>(newValue);
357360
}
358361
};
359362

@@ -382,7 +385,7 @@ class PropertiesPanel : public Component {
382385

383386
labelComp->onEditorHide = [this] {
384387
// synchronise the value to the canvas when updated
385-
if (PluginEditor* pluginEditor = findParentComponentOfClass<PluginEditor>()) {
388+
if (auto* pluginEditor = findParentComponentOfClass<PluginEditor>()) {
386389
if (auto const cnv = pluginEditor->getCurrentCanvas())
387390
cnv->synchronise();
388391
}
@@ -398,8 +401,8 @@ class PropertiesPanel : public Component {
398401
{
399402
if constexpr (std::is_arithmetic_v<T>) {
400403
auto const draggableNumber = reinterpret_cast<DraggableNumber*>(label.get());
401-
auto const value = getValue<double>(v);
402-
if (value != draggableNumber->getValue()) {
404+
auto const value = getValue<T>(v);
405+
if (value != static_cast<T>(draggableNumber->getValue())) {
403406
draggableNumber->setValue(value, dontSendNotification);
404407
}
405408
}
@@ -415,22 +418,6 @@ class PropertiesPanel : public Component {
415418
allowedCharacters = newAllowedCharacters;
416419
}
417420

418-
void setRangeMin(float const minimum)
419-
{
420-
min = minimum;
421-
if constexpr (std::is_arithmetic_v<T>) {
422-
dynamic_cast<DraggableNumber*>(label.get())->setMinimum(minimum);
423-
}
424-
}
425-
426-
void setRangeMax(float const maximum)
427-
{
428-
max = maximum;
429-
if constexpr (std::is_arithmetic_v<T>) {
430-
dynamic_cast<DraggableNumber*>(label.get())->setMaximum(maximum);
431-
}
432-
}
433-
434421
void resized() override
435422
{
436423
label->setBounds(getLocalBounds().removeFromRight(getWidth() / (2 - hideLabel)));

Source/Dialogs/AdvancedSettingsPanel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AdvancedSettingsPanel final : public SettingsDialogPanel
6767
otherProperties.add(new PropertiesPanel::BoolComponent("Enable auto patching", autoPatchingValue, { "No", "Yes" }));
6868

6969
autosaveInterval.referTo(settingsFile->getPropertyAsValue("autosave_interval"));
70-
autosaveProperties.add(new PropertiesPanel::EditableComponent<int>("Auto-save interval (minutes)", autosaveInterval, 1, 60));
70+
autosaveProperties.add(new PropertiesPanel::EditableComponent<int>("Auto-save interval (minutes)", autosaveInterval, true, 1, 60));
7171

7272
autosaveEnabled.referTo(settingsFile->getPropertyAsValue("autosave_enabled"));
7373
autosaveProperties.add(new PropertiesPanel::BoolComponent("Enable autosave", autosaveEnabled, { "No", "Yes" }));
@@ -128,7 +128,7 @@ class AdvancedSettingsPanel final : public SettingsDialogPanel
128128

129129
defaultZoom = settingsFile->getProperty<float>("default_zoom");
130130
defaultZoom.addListener(this);
131-
interfaceProperties.add(new PropertiesPanel::EditableComponent<float>("Default zoom %", defaultZoom));
131+
interfaceProperties.add(new PropertiesPanel::EditableComponent<float>("Default zoom %", defaultZoom, true, 25, 300));
132132

133133
centreResized = settingsFile->getPropertyAsValue("centre_resized_canvas");
134134
centreResized.addListener(this);

Source/Dialogs/AudioSettingsPanel.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,12 @@ class DAWAudioSettingsPanel final : public SettingsDialogPanel
493493

494494
latencyValue = processor->getLatencySamples() - pd::Instance::getBlockSize();
495495

496-
latencyNumberBox = new PropertiesPanel::EditableComponent<int>("Latency (samples)", latencyValue);
496+
latencyNumberBox = new PropertiesPanel::EditableComponent<int>("Latency (samples)", latencyValue, true, 0, 1<<30);
497497
tailLengthNumberBox = new PropertiesPanel::EditableComponent<float>("Tail length (seconds)", tailLengthValue);
498498

499499
dawSettingsPanel.addSection("Audio", { latencyNumberBox, tailLengthNumberBox });
500500

501501
addAndMakeVisible(dawSettingsPanel);
502-
503-
latencyNumberBox->setRangeMin(0);
504-
latencyNumberBox->setRangeMax(1 << 30);
505502
}
506503

507504
PropertiesPanel* getPropertiesPanel() override

Source/Heavy/DaisyExporter.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ class DaisyExporter final : public ExporterBase {
3535
usbMidiProperty = new PropertiesPanel::BoolComponent("USB MIDI", usbMidiValue, { "No", "Yes" });
3636
properties.add(usbMidiProperty);
3737
properties.add(new PropertiesPanel::BoolComponent("Debug printing", debugPrintValue, { "No", "Yes" }));
38-
auto const blocksizeProperty = new PropertiesPanel::EditableComponent<int>("Blocksize", blocksizeValue);
39-
blocksizeProperty->setRangeMin(1);
40-
blocksizeProperty->setRangeMax(256);
38+
auto const blocksizeProperty = new PropertiesPanel::EditableComponent<int>("Blocksize", blocksizeValue, true, 1, 256);
4139
blocksizeProperty->editableOnClick(false);
4240
properties.add(blocksizeProperty);
4341
properties.add(new PropertiesPanel::ComboComponent("Samplerate", samplerateValue, { "8000", "16000", "32000", "48000", "96000" }));

Source/Objects/ArrayObject.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,9 @@ class ArrayListView final : public PropertiesPanel
887887
for (int i = 0; i < numProperties; i++) {
888888
auto& value = *arrayValues.add(new Value(vec[i].w_float));
889889
value.addListener(this);
890-
auto* property = new EditableComponent<float>(String(i), value);
890+
auto* property = new EditableComponent<float>(String(i), value, true, ptr->x_glist->gl_y2, ptr->x_glist->gl_y1);
891891
auto* label = dynamic_cast<DraggableNumber*>(property->label.get());
892892

893-
property->setRangeMin(ptr->x_glist->gl_y2);
894-
property->setRangeMax(ptr->x_glist->gl_y1);
895-
896893
// Only send this after drag end so it doesn't interrupt the drag action
897894
label->dragEnd = [this] {
898895
if (auto p = array.get<t_fake_garray>()) {

Source/Sidebar/Inspector.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,10 @@ class Inspector final : public Component {
123123
case tString:
124124
return new PropertiesPanel::EditableComponent<String>(name, *value);
125125
case tFloat: {
126-
auto* c = new PropertiesPanel::EditableComponent<float>(name, *value);
127-
if(clip) {
128-
c->setRangeMin(min);
129-
c->setRangeMax(max);
130-
}
131-
return c;
126+
return new PropertiesPanel::EditableComponent<float>(name, *value, clip, min, max);
132127
}
133128
case tInt: {
134-
auto* c = new PropertiesPanel::EditableComponent<int>(name, *value);
135-
if(clip) {
136-
c->setRangeMin(min);
137-
c->setRangeMax(max);
138-
}
139-
return c;
129+
return new PropertiesPanel::EditableComponent<int>(name, *value, clip, min, max, onInteractionFn);
140130
}
141131
case tColour:
142132
return new PropertiesPanel::InspectorColourComponent(name, *value);

0 commit comments

Comments
 (0)