1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added a parameter to ValueTree::getPropertyAsValue() to allow synchronous updates

This commit is contained in:
jules 2017-10-31 12:17:00 +00:00
parent 1e3e4ab195
commit 4d375d0bc2
2 changed files with 23 additions and 21 deletions

View file

@ -32,21 +32,21 @@ class ValueTree::SharedObject : public ReferenceCountedObject
public: public:
typedef ReferenceCountedObjectPtr<SharedObject> Ptr; typedef ReferenceCountedObjectPtr<SharedObject> Ptr;
explicit SharedObject (const Identifier& t) noexcept : type (t) explicit SharedObject (const Identifier& t) noexcept : type (t) {}
{
}
SharedObject (const SharedObject& other) SharedObject (const SharedObject& other)
: ReferenceCountedObject(), type (other.type), properties (other.properties) : ReferenceCountedObject(), type (other.type), properties (other.properties)
{ {
for (int i = 0; i < other.children.size(); ++i) for (auto* c : other.children)
{ {
auto child = new SharedObject (*other.children.getObjectPointerUnchecked(i)); auto child = new SharedObject (*c);
child->parent = this; child->parent = this;
children.add (child); children.add (child);
} }
} }
SharedObject& operator= (const SharedObject&) = delete;
~SharedObject() ~SharedObject()
{ {
jassert (parent == nullptr); // this should never happen unless something isn't obeying the ref-counting! jassert (parent == nullptr); // this should never happen unless something isn't obeying the ref-counting!
@ -98,7 +98,6 @@ public:
void sendPropertyChangeMessage (const Identifier& property, ValueTree::Listener* listenerToExclude = nullptr) void sendPropertyChangeMessage (const Identifier& property, ValueTree::Listener* listenerToExclude = nullptr)
{ {
ValueTree tree (this); ValueTree tree (this);
callListenersForAllParents ([&] (ListenerList<Listener>& list) { list.callExcluding (listenerToExclude, &ValueTree::Listener::valueTreePropertyChanged, tree, property); }); callListenersForAllParents ([&] (ListenerList<Listener>& list) { list.callExcluding (listenerToExclude, &ValueTree::Listener::valueTreePropertyChanged, tree, property); });
} }
@ -144,11 +143,13 @@ public:
if (auto* existingValue = properties.getVarPointer (name)) if (auto* existingValue = properties.getVarPointer (name))
{ {
if (*existingValue != newValue) if (*existingValue != newValue)
undoManager->perform (new SetPropertyAction (this, name, newValue, *existingValue, false, false, listenerToExclude)); undoManager->perform (new SetPropertyAction (this, name, newValue, *existingValue,
false, false, listenerToExclude));
} }
else else
{ {
undoManager->perform (new SetPropertyAction (this, name, newValue, {}, true, false, listenerToExclude)); undoManager->perform (new SetPropertyAction (this, name, newValue, {},
true, false, listenerToExclude));
} }
} }
} }
@ -402,7 +403,7 @@ public:
} }
else else
{ {
output.writeString (String()); output.writeString ({});
output.writeCompressedInt (0); output.writeCompressedInt (0);
output.writeCompressedInt (0); output.writeCompressedInt (0);
} }
@ -451,7 +452,7 @@ public:
{ {
if (! (isAddingNewProperty || isDeletingProperty)) if (! (isAddingNewProperty || isDeletingProperty))
{ {
if (SetPropertyAction* const next = dynamic_cast<SetPropertyAction*> (nextAction)) if (auto* next = dynamic_cast<SetPropertyAction*> (nextAction))
if (next->target == target && next->name == name if (next->target == target && next->name == name
&& ! (next->isAddingNewProperty || next->isDeletingProperty)) && ! (next->isAddingNewProperty || next->isDeletingProperty))
return new SetPropertyAction (target, name, next->newValue, oldValue, false, false); return new SetPropertyAction (target, name, next->newValue, oldValue, false, false);
@ -571,8 +572,6 @@ public:
SortedSet<ValueTree*> valueTreesWithListeners; SortedSet<ValueTree*> valueTreesWithListeners;
SharedObject* parent = nullptr; SharedObject* parent = nullptr;
private:
SharedObject& operator= (const SharedObject&);
JUCE_LEAK_DETECTOR (SharedObject) JUCE_LEAK_DETECTOR (SharedObject)
}; };
@ -779,12 +778,11 @@ int ValueTree::getReferenceCount() const noexcept
} }
//============================================================================== //==============================================================================
class ValueTreePropertyValueSource : public Value::ValueSource, struct ValueTreePropertyValueSource : public Value::ValueSource,
private ValueTree::Listener private ValueTree::Listener
{ {
public: ValueTreePropertyValueSource (const ValueTree& vt, const Identifier& prop, UndoManager* um, bool sync)
ValueTreePropertyValueSource (const ValueTree& vt, const Identifier& prop, UndoManager* um) : tree (vt), property (prop), undoManager (um), updateSynchronously (sync)
: tree (vt), property (prop), undoManager (um)
{ {
tree.addListener (this); tree.addListener (this);
} }
@ -801,11 +799,12 @@ private:
ValueTree tree; ValueTree tree;
const Identifier property; const Identifier property;
UndoManager* const undoManager; UndoManager* const undoManager;
const bool updateSynchronously;
void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override
{ {
if (tree == changedTree && property == changedProperty) if (tree == changedTree && property == changedProperty)
sendChangeMessage (false); sendChangeMessage (updateSynchronously);
} }
void valueTreeChildAdded (ValueTree&, ValueTree&) override {} void valueTreeChildAdded (ValueTree&, ValueTree&) override {}
@ -816,9 +815,9 @@ private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource)
}; };
Value ValueTree::getPropertyAsValue (const Identifier& name, UndoManager* const undoManager) Value ValueTree::getPropertyAsValue (const Identifier& name, UndoManager* const undoManager, bool updateSynchronously)
{ {
return Value (new ValueTreePropertyValueSource (*this, name, undoManager)); return Value (new ValueTreePropertyValueSource (*this, name, undoManager, updateSynchronously));
} }
//============================================================================== //==============================================================================

View file

@ -209,8 +209,11 @@ public:
The Value object will maintain a reference to this tree, and will use the undo manager when The Value object will maintain a reference to this tree, and will use the undo manager when
it needs to change the value. Attaching a Value::Listener to the value object will provide it needs to change the value. Attaching a Value::Listener to the value object will provide
callbacks whenever the property changes. callbacks whenever the property changes.
If shouldUpdateSynchronously is true the Value::Listener will be updated synchronously.
@see ValueSource::sendChangeMessage (bool)
*/ */
Value getPropertyAsValue (const Identifier& name, UndoManager* undoManager); Value getPropertyAsValue (const Identifier& name, UndoManager* undoManager,
bool shouldUpdateSynchronously = false);
/** Overwrites all the properties in this tree with the properties of the source tree. /** Overwrites all the properties in this tree with the properties of the source tree.
Any properties that already exist will be updated; and new ones will be added, and Any properties that already exist will be updated; and new ones will be added, and