Skip to content

Commit

Permalink
Implemented First Approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Pretasoc committed Dec 19, 2017
1 parent ff93150 commit 32a7db2
Show file tree
Hide file tree
Showing 32 changed files with 1,400 additions and 83 deletions.
91 changes: 76 additions & 15 deletions phirSOFT.FluentRegionAdapters/ComposedPositionContraint.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace phirSOFT.FluentRegionAdapters
{
public abstract class ComposedPositionContraint<T> : PositionConstraint<T>, ICollection<PositionConstraint<T>>
public class ComposedPositionContraint : PositionConstraint, IList<PositionConstraint>, IList
{
private ICollection<PositionConstraint<T>> _collectionImplementation;
private readonly Collection<PositionConstraint> _collectionImplementation =
new Collection<PositionConstraint>();

public IEnumerator<PositionConstraint<T>> GetEnumerator()
public int Add(object value)
{
return ((IList) _collectionImplementation).Add(value);
}

public bool Contains(object value)
{
return ((IList) _collectionImplementation).Contains(value);
}

public int IndexOf(object value)
{
return ((IList) _collectionImplementation).IndexOf(value);
}

public void Insert(int index, object value)
{
((IList) _collectionImplementation).Insert(index, value);
}

public void Remove(object value)
{
((IList) _collectionImplementation).Remove(value);
}

public void CopyTo(Array array, int index)
{
((ICollection) _collectionImplementation).CopyTo(array, index);
}

public object SyncRoot => ((ICollection) _collectionImplementation).SyncRoot;

public bool IsSynchronized => ((ICollection) _collectionImplementation).IsSynchronized;

public bool IsFixedSize => ((IList) _collectionImplementation).IsFixedSize;

object IList.this[int index]
{
get => ((IList) _collectionImplementation)[index];
set => ((IList) _collectionImplementation)[index] = value;
}

public IEnumerator<PositionConstraint> GetEnumerator()
{
return _collectionImplementation.GetEnumerator();
}
Expand All @@ -18,7 +64,7 @@ IEnumerator IEnumerable.GetEnumerator()
return ((IEnumerable) _collectionImplementation).GetEnumerator();
}

public void Add(PositionConstraint<T> item)
public void Add(PositionConstraint item)
{
_collectionImplementation.Add(item);
}
Expand All @@ -28,40 +74,55 @@ public void Clear()
_collectionImplementation.Clear();
}

public bool Contains(PositionConstraint<T> item)
public bool Contains(PositionConstraint item)
{
return _collectionImplementation.Contains(item);
}

public void CopyTo(PositionConstraint<T>[] array, int arrayIndex)
public void CopyTo(PositionConstraint[] array, int arrayIndex)
{
_collectionImplementation.CopyTo(array, arrayIndex);
}

public bool Remove(PositionConstraint<T> item)
public bool Remove(PositionConstraint item)
{
return _collectionImplementation.Remove(item);
}

public int Count
public int Count => _collectionImplementation.Count;

public bool IsReadOnly => false;

public int IndexOf(PositionConstraint item)
{
get { return _collectionImplementation.Count; }
return _collectionImplementation.IndexOf(item);
}

public bool IsReadOnly
public void Insert(int index, PositionConstraint item)
{
get { return _collectionImplementation.IsReadOnly; }
_collectionImplementation.Insert(index, item);
}

protected override bool CanCompare(T left, T right)
public void RemoveAt(int index)
{
return this.Any(c => CanCompare(c, left, right));
_collectionImplementation.RemoveAt(index);
}

public override int Compare(T x, T y)
public PositionConstraint this[int index]
{
return this.First(c => CanCompare(c, x, y)).Compare(x, y);
get => _collectionImplementation[index];
set => _collectionImplementation[index] = value;
}

public override bool CanCompare(DependencyObject left, DependencyObject right)
{
return this.Any(c => c.CanCompare(left, right));
}

public override int Compare(DependencyObject x, DependencyObject y)
{
var result = 0;
return _collectionImplementation.Any(comparer => comparer.CanCompare(x, y) && (result = comparer.Compare(x, y)) != 0) ? result : 0;
}
}
}
93 changes: 93 additions & 0 deletions phirSOFT.FluentRegionAdapters/ContraintedRegionAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections;
using System.Collections.Specialized;
using System.Windows;
using phirSOFT.TopologicalComparison;
using Prism.Regions;

namespace phirSOFT.FluentRegionAdapters
{
public abstract class ContraintedRegionAdapter<TContainer> : RegionAdapterBase<TContainer>
where TContainer : DependencyObject
{
public static readonly DependencyProperty ConstraintComparerProperty = DependencyProperty.RegisterAttached(
"ConstraintComparer", typeof(ITopologicalComparer), typeof(ContraintedRegionAdapter<TContainer>),
new PropertyMetadata(new DefaultComparer()));

protected ContraintedRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
{
}

public static void SetConstraintComparer(DependencyObject element, ITopologicalComparer value)
{
element.SetValue(ConstraintComparerProperty, value);
}

public static ITopologicalComparer GetConstraintComparer(DependencyObject element)
{
return (ITopologicalComparer) element.GetValue(ConstraintComparerProperty);
}


protected override void Adapt(IRegion region, TContainer regionTarget)
{
region.Views.CollectionChanged += (sender, args) => UpdateRegion(sender, args, regionTarget);
//region.ActiveViews.CollectionChanged += (sender, args) => ActivateView(sender, args);
}


protected virtual void UpdateRegion(object sender, NotifyCollectionChangedEventArgs args,
TContainer regionTarget)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (DependencyObject item in args.NewItems)
{
var list = GetItemCollection(regionTarget, item);
list.Insert(item, GetConstraintComparer(regionTarget));
}

break;
case NotifyCollectionChangedAction.Remove:
foreach (DependencyObject item in args.NewItems)
{
var list = GetItemCollection(regionTarget, item);
list.Remove(item);
}
break;
case NotifyCollectionChangedAction.Replace:
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Reset:
break;
}
}


protected abstract IList GetItemCollection(TContainer container);

protected virtual IList GetItemCollection(TContainer container, DependencyObject item)
{
return GetItemCollection(container);
}

protected override IRegion CreateRegion()
{
return new Region();
}

private class DefaultComparer : ITopologicalComparer
{
public int Compare(object x, object y)
{
return 0;
}

public bool CanCompare(object x, object y)
{
return true;
}
}
}
}
34 changes: 34 additions & 0 deletions phirSOFT.FluentRegionAdapters/GroupConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Windows;
using Fluent;

namespace phirSOFT.FluentRegionAdapters
{
public class GroupConstraint : PositionConstraint
{
public static readonly DependencyProperty GroupProperty = DependencyProperty.RegisterAttached(
"Group", typeof(int), typeof(GroupConstraint), new PropertyMetadata(default(int)));

public static void SetGroup(DependencyObject element, int value)
{
element.SetValue(GroupProperty, value);
}

public static int GetGroup(DependencyObject element)
{
return (int) element.GetValue(GroupProperty);
}

public override bool CanCompare(DependencyObject x, DependencyObject y)
{
return x is RibbonTabItem && y is RibbonTabItem;
}

public override int Compare(DependencyObject x, DependencyObject y)
{
var left = GetGroup(x);
var right = GetGroup(y);

return left - right;
}
}
}
18 changes: 13 additions & 5 deletions phirSOFT.FluentRegionAdapters/PositionConstraint.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
using System.Collections.Generic;
using System.Windows;
using phirSOFT.TopologicalComparison;

namespace phirSOFT.FluentRegionAdapters
{
public abstract class PositionConstraint<T> : Comparer<T>
public abstract class PositionConstraint : ITopologicalComparer<DependencyObject>, ITopologicalComparer
{
protected abstract bool CanCompare(T left, T right);
public int Compare(object x, object y)
{
return Compare(x as DependencyObject, y as DependencyObject);
}

protected static bool CanCompare(PositionConstraint<T> comparer, T left, T rigt)
public bool CanCompare(object x, object y)
{
return comparer.CanCompare(left, rigt);
return x is DependencyObject xx && y is DependencyObject yy && CanCompare(xx, yy);
}

public abstract bool CanCompare(DependencyObject x, DependencyObject y);

public abstract int Compare(DependencyObject x, DependencyObject y);
}
}
3 changes: 1 addition & 2 deletions phirSOFT.FluentRegionAdapters/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down Expand Up @@ -33,4 +32,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
53 changes: 37 additions & 16 deletions phirSOFT.FluentRegionAdapters/RelativeContraint.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System;
using System;
using System.Collections.Generic;
using System.Windows;

namespace phirSOFT.FluentRegionAdapters
{
public class RelativeContraint<T> : PositionConstraint<T> where T : DependencyObject
public class RelativeContraint : PositionConstraint
{
public readonly DependencyProperty Before =
DependencyProperty.RegisterAttached("Before", typeof(IList<T>), typeof(T));
public static readonly DependencyProperty Before =
DependencyProperty.RegisterAttached("Before", typeof(IList<DependencyObject>), typeof(DependencyObject),
new PropertyMetadata(null));

public readonly DependencyProperty After =
DependencyProperty.RegisterAttached("Before", typeof(IList<T>), typeof(T));
public static readonly DependencyProperty After =
DependencyProperty.RegisterAttached("After", typeof(IList<DependencyObject>), typeof(DependencyObject),
new PropertyMetadata(null));

protected override bool CanCompare(T left, T right)
public override bool CanCompare(DependencyObject left, DependencyObject right)
{
try
{
Expand All @@ -21,18 +23,38 @@ protected override bool CanCompare(T left, T right)
catch (Exception)
{
return false;

}

}

public override int Compare(T x, T y)
public static IList<DependencyObject> GetBefore(DependencyObject dependencyObject)
{
var xBefore = (IList<T>) x?.GetValue(Before);
var xAfter = (IList<T>)x?.GetValue(After);
var yBefore = (IList<T>) y?.GetValue(Before);
var yAfter = (IList<T>)y?.GetValue(After);

var list = (IList<DependencyObject>) dependencyObject.GetValue(Before);
if (list != null) return list;

list = new List<DependencyObject>();
dependencyObject.SetValue(Before, list);
return list;
}


public static IList<DependencyObject> GetAfter(DependencyObject dependencyObject)
{
var list = (IList<DependencyObject>) dependencyObject.GetValue(After);
if (list != null) return list;

list = new List<DependencyObject>();
dependencyObject.SetValue(After, list);
return list;
}


public override int Compare(DependencyObject x, DependencyObject y)
{
var xBefore = (IList<DependencyObject>) x?.GetValue(Before);
var xAfter = (IList<DependencyObject>) x?.GetValue(After);
var yBefore = (IList<DependencyObject>) y?.GetValue(Before);
var yAfter = (IList<DependencyObject>) y?.GetValue(After);

// x > y
var xGy = xAfter?.Contains(y) ?? false;

Expand All @@ -56,7 +78,6 @@ public override int Compare(T x, T y)
if (xLy)
return -1;
return 0;

}
}
}
Loading

0 comments on commit 32a7db2

Please sign in to comment.