Skip to content

Commit

Permalink
Fix TwoWay binding
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Nov 13, 2024
1 parent 1f4be22 commit 23d3308
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
6 changes: 4 additions & 2 deletions swGUI/Core/System/DataBinding/BindingsList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ ReturnResult BindingsList::AddBinding ( BindingPtr binding )
//
void BindingsList::AddBindingLink ( const BindingInfoPtr& binding )
{
auto sourcePropInfo = FindBinding( binding->GetProperty() );
// We are the source of the Binding soe our BIndingInfo object should be reverse of the target.
auto sourceProperty = binding->GetBinding()->GetSourceProperty();
auto sourcePropInfo = FindBinding( sourceProperty );

// If BindingInfo object didn't exist, we create new one with empty binding.
if( sourcePropInfo == nullptr )
sourcePropInfo = std::make_shared< BindingInfo >( binding->GetProperty() );
sourcePropInfo = std::make_shared< BindingInfo >( sourceProperty );

sourcePropInfo->AddBindingLink( binding );
m_bindings.push_back( sourcePropInfo );
Expand Down
6 changes: 5 additions & 1 deletion swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ RTTR_REGISTRATION
{
rttr::registration::class_< sw::gui::DependencyPropsClass >( "sw::gui::DependencyPropsClass" )
.property( "NumberItems", &sw::gui::DependencyPropsClass::GetNumberItems, &sw::gui::DependencyPropsClass::SetNumberItems )
.property( "ContainerName", &sw::gui::DependencyPropsClass::GetContainerName, &sw::gui::DependencyPropsClass::SetContainerName );
.property( "ContainerName", &sw::gui::DependencyPropsClass::GetContainerName, &sw::gui::DependencyPropsClass::SetContainerName )
.property( "RandomName", &sw::gui::DependencyPropsClass::GetRandomName, &sw::gui::DependencyPropsClass::SetRandomName );
;
}


Expand All @@ -22,6 +24,7 @@ namespace gui

DependencyProperty DependencyPropsClass::sNumberItemsProperty = DependencyProperty::Register( "NumberItems", TypeID::get< DependencyPropsClass >() );
DependencyProperty DependencyPropsClass::sContainerNameProperty = DependencyProperty::Register( "ContainerName", TypeID::get< DependencyPropsClass >() );
DependencyProperty DependencyPropsClass::sRandomNameProperty = DependencyProperty::Register( "RandomName", TypeID::get< DependencyPropsClass >() );



Expand All @@ -30,6 +33,7 @@ DependencyProperty DependencyPropsClass::sContainerNameProperty = DependencyProp
DependencyPropsClass::DependencyPropsClass()
: m_numberItems( 3 )
, m_containerName( "Name" )
, m_randomName( "Blaaa" )
{}


Expand Down
5 changes: 5 additions & 0 deletions swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ class DependencyPropsClass : public DependencyObject

static DependencyProperty sNumberItemsProperty;
static DependencyProperty sContainerNameProperty;
static DependencyProperty sRandomNameProperty;

private:

uint32 m_numberItems;
std::string m_containerName;
std::string m_randomName;

public:

Expand All @@ -45,6 +47,9 @@ class DependencyPropsClass : public DependencyObject

const std::string& GetContainerName () const { return m_containerName; }
void SetContainerName ( const std::string& value ) { DependencyObject::SetValue( sContainerNameProperty, value, &DependencyPropsClass::m_containerName ); }

const std::string& GetRandomName () const { return m_randomName; }
void SetRandomName ( const std::string& value ) { DependencyObject::SetValue( sRandomNameProperty, value, &DependencyPropsClass::m_randomName ); }
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ TEST_CASE( "Binding_DependencyObject_TwoWayBinding", "[GUI][BindingSystem]" )
CHECK( child->GetContainerName() == "Value number 2" );
}

// ================================ //
//
TEST_CASE( "Binding.DependencyObject.TwoWayBinding.DifferentName", "[GUI][BindingSystem]" )
{
std::unique_ptr< gui::DependencyPropsClass > root = std::make_unique< gui::DependencyPropsClass >();
std::unique_ptr< gui::DependencyPropsClass > child = std::make_unique< gui::DependencyPropsClass >();

root->SetDataContext( child.get() );
auto initContainerName = root->GetContainerName();
auto initRandomName = child->GetRandomName();

auto binding = gui::Binding::Create( "RandomName", root.get(), "ContainerName", gui::BindingMode::TwoWay );
REQUIRE( root->AddBinding( binding ).IsValid() );

// To target direction.
CHECK_NOTHROW( child->SetContainerName( "Newly set value" ) );
CHECK( root->GetRandomName() == "Newly set value" );
CHECK( root->GetContainerName() == initContainerName ); // Shouldn't change.
CHECK( child->GetContainerName() == "Newly set value" );
CHECK( child->GetRandomName() == initRandomName );

// To source direction.
CHECK_NOTHROW( root->SetRandomName( "Value number 2" ) );
CHECK( root->GetRandomName() == "Value number 2" );
CHECK( root->GetContainerName() == initContainerName ); // Shouldn't change.
CHECK( child->GetContainerName() == "Value number 2" );
CHECK( child->GetRandomName() == initRandomName );
}

// ================================ //
// Circular dependancies shouldn't cause dead lock.
Expand Down Expand Up @@ -172,4 +200,3 @@ TEST_CASE( "Binding_DependencyObject_CircularDependencies_TwoWay", "[GUI][Bindin
CHECK( child3->GetNumberItems() == 394 );
CHECK( child4->GetNumberItems() == 394 );
}

0 comments on commit 23d3308

Please sign in to comment.