Skip to content

Commit

Permalink
[NUI] BindableObject creates property dictionary on demand
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyun Yang <[email protected]>
  • Loading branch information
rabbitfor committed Dec 31, 2024
1 parent 76a9232 commit bca7e7e
Showing 1 changed file with 72 additions and 33 deletions.
105 changes: 72 additions & 33 deletions src/Tizen.NUI/src/public/XamlBinding/BindableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceH
BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), default(object), BindingMode.OneWay, null, BindingContextPropertyChanged,
null, null, BindingContextPropertyBindingChanging);

readonly Dictionary<BindableProperty, BindablePropertyContext> properties = new Dictionary<BindableProperty, BindablePropertyContext>(4);
private Dictionary<BindableProperty, BindablePropertyContext> properties;

bool applying;
object inheritedContext;
Expand Down Expand Up @@ -110,18 +110,24 @@ public void CopyBindingRelationShip(BindableObject other)
return;
}

foreach (var property in properties)
if (properties != null)
{
RemoveBinding(property.Key);
foreach (var property in properties)
{
RemoveBinding(property.Key);
}
}

foreach (var property in other.properties)
if (other.properties != null)
{
if (null != property.Value.Binding)
foreach (var property in other.properties)
{
var binding = property.Value.Binding;
other.RemoveBinding(property.Key);
SetBinding(property.Key, binding);
if (null != property.Value.Binding)
{
var binding = property.Value.Binding;
other.RemoveBinding(property.Key);
SetBinding(property.Key, binding);
}
}
}
}
Expand Down Expand Up @@ -445,7 +451,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
=> PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));

/// <summary>
/// Method that is called when a bound property is changed.
/// </summary>
Expand All @@ -459,6 +465,11 @@ protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
[EditorBrowsable(EditorBrowsableState.Never)]
protected void UnapplyBindings()
{
if (properties == null)
{
return;
}

foreach (var context in properties.Values)
{
if (context.Binding == null)
Expand Down Expand Up @@ -487,21 +498,24 @@ internal object[] GetValues(BindableProperty property0, BindableProperty propert
{
var values = new object[2];

foreach (var context in properties.Values)
if (properties != null)
{
if (ReferenceEquals(context.Property, property0))
{
values[0] = context.Value;
property0 = null;
}
else if (ReferenceEquals(context.Property, property1))
foreach (var context in properties.Values)
{
values[1] = context.Value;
property1 = null;
}
if (ReferenceEquals(context.Property, property0))
{
values[0] = context.Value;
property0 = null;
}
else if (ReferenceEquals(context.Property, property1))
{
values[1] = context.Value;
property1 = null;
}

if (property0 == null && property1 == null)
return values;
if (property0 == null && property1 == null)
return values;
}
}

if (!ReferenceEquals(property0, null))
Expand All @@ -523,6 +537,11 @@ internal object[] GetValues(BindableProperty property0, BindableProperty propert
{
var values = new object[3];

if (properties == null)
{
return values;
}

foreach (var context in properties.Values)
{
if (ReferenceEquals(context.Property, property0))
Expand Down Expand Up @@ -756,6 +775,11 @@ internal void SetValueCore(BindableProperty property, object value, SetValueFlag

internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
{
if (properties == null)
{
return;
}

var prop = properties.Values.ToArray();
for (int i = 0, propLength = prop.Length; i < propLength; i++)
{
Expand Down Expand Up @@ -876,12 +900,19 @@ BindablePropertyContext CreateAndAddContext(BindableProperty property)
else
context.Attributes = BindableContextAttributes.IsDefaultValueCreated;

properties.Add(property, context);
(properties ??= new Dictionary<BindableProperty, BindablePropertyContext>(4)).Add(property, context);
return context;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
BindablePropertyContext GetContext(BindableProperty property) => properties.TryGetValue(property, out var result) ? result : null;
BindablePropertyContext GetContext(BindableProperty property)
{
if (properties != null && properties.TryGetValue(property, out var result))
{
return result;
}
return null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
BindablePropertyContext GetOrCreateContext(BindableProperty property)
Expand Down Expand Up @@ -1066,19 +1097,22 @@ internal void ReplaceBindingElement(Dictionary<string, object> oldNameScope, Dic
xElementToNameOfOld.Add(pair.Value, pair.Key);
}

foreach (var property in properties)
if (properties != null)
{
if (property.Value.Binding is Binding binding && null != binding.Source)
foreach (var property in properties)
{
string xName;
xElementToNameOfOld.TryGetValue(binding.Source, out xName);

if (null != xName)
if (property.Value.Binding is Binding binding && null != binding.Source)
{
var newObject = newNameScope[xName];
binding.Unapply();
binding.Source = newObject;
SetBinding(property.Key, binding);
string xName;
xElementToNameOfOld.TryGetValue(binding.Source, out xName);

if (null != xName)
{
var newObject = newNameScope[xName];
binding.Unapply();
binding.Source = newObject;
SetBinding(property.Key, binding);
}
}
}
}
Expand All @@ -1102,6 +1136,11 @@ internal void ReplaceBindingElement(Dictionary<string, object> oldNameScope, Dic
[EditorBrowsable(EditorBrowsableState.Never)]
public void ClearBinding()
{
if (properties == null)
{
return;
}

foreach (var property in properties)
{
if (null != property.Value.Binding)
Expand Down

0 comments on commit bca7e7e

Please sign in to comment.