编写「IContainerElement」类型的辅助扩展方法

This commit is contained in:
zengwenjie
2025-09-18 18:22:58 +08:00
parent 83fbda7f6d
commit a8a172b00c
2 changed files with 90 additions and 68 deletions

View File

@@ -11,78 +11,13 @@ namespace Deedy.Activity
{ {
public void Append(IElement element) => this.Elements.Add(element); public void Append(IElement element) => this.Elements.Add(element);
public void Remove(IElement element) => this.Elements.Remove(element); public void Remove(IElement element) => this.Elements.Remove(element);
public void Insert(IElement element, uint index) public void Insert(IElement element, uint index) => this.Help_Insert(element, index);
{ public void MoveTo(IElement element, uint index) => this.Help_MoveTo(element, index);
if (element == null) return;
if (index < this.Elements.Count) this.Elements.Insert((int)index, element);
if (index >= this.Elements.Count) this.Elements.Add(element);
}
public void MoveTo(IElement element, uint index)
{
if (element == null) return;
int newIndex = (index >= this.Elements.Count) ? this.Elements.Count - 1 : (int)index;
int oldIndex = this.Elements.IndexOf(element);
if (oldIndex < 0) this.Elements.Insert(newIndex, element);
else this.Elements.Move(oldIndex, newIndex);
}
/// <summary> /// <summary>
/// 如果类型上没有组装规则,则认为不允许组装,否则按照组装规则处理 /// 如果类型上没有组装规则,则认为不允许组装,否则按照组装规则处理
/// </summary> /// </summary>
/// <param name="element">要进行组装检查的单元实例</param> /// <param name="element">要进行组装检查的单元实例</param>
/// <returns>是否允许组装</returns> /// <returns>是否允许组装</returns>
public bool CombineChecking(IElement element, out DragDropEffects effects) public bool CombineChecking(IElement element, out DragDropEffects effects) => this.Help_CombineChecking(element, out effects);
{
DragDropEffects resultEffects = DragDropEffects.None;
List<CombineRule_AllowTypeAttribute> _AllowTypes = [];
List<CombineRule_RejectTypeAttribute> _RejectTypes = [];
this.GetCombineRules(ref _AllowTypes, ref _RejectTypes);
if (element == null)
{
effects = resultEffects;
return false;
}
// 如果没有组装规则则直接返回
if (_AllowTypes.Count == 0 && _RejectTypes.Count == 0)
{
effects = resultEffects;
return false;
}
Type uType = element.GetType();
bool allow = false;
foreach (var aRule in _AllowTypes)
{
if (aRule.BaseType.IsAssignableFrom(uType))
{
if (!aRule.Excluded.Contains(uType))
{
allow = true;
break;
}
}
}
if (!allow)
{
effects = resultEffects;
return allow; // 如果没有匹配的放行规则则直接返回
}
resultEffects |= DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link;
foreach (var rRule in _RejectTypes)
{
if (rRule.BaseType.IsAssignableFrom(uType))
{
if (!rRule.Excluded.Contains(uType))
{
resultEffects &= DragDropEffects.None;
allow = false;
break;
}
}
}
effects = resultEffects;
return allow;
}
} }
} }

View File

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Deedy.Activity
{
public static partial class Helper
{
/// <summary>
/// 如果类型上没有组装规则,则认为不允许组装,否则按照组装规则处理
/// </summary>
/// <param name="element">要进行组装检查的单元实例</param>
/// <returns>是否允许组装</returns>
public static bool Help_CombineChecking(this IContainerElement container, IElement element, out DragDropEffects effects)
{
DragDropEffects resultEffects = DragDropEffects.None;
List<CombineRule_AllowTypeAttribute> _AllowTypes = [];
List<CombineRule_RejectTypeAttribute> _RejectTypes = [];
GetCombineRules(container, ref _AllowTypes, ref _RejectTypes);
if (element == null)
{
effects = resultEffects;
return false;
}
// 如果没有组装规则则直接返回
if (_AllowTypes.Count == 0 && _RejectTypes.Count == 0)
{
effects = resultEffects;
return false;
}
Type uType = element.GetType();
bool allow = false;
foreach (var aRule in _AllowTypes)
{
if (aRule.BaseType.IsAssignableFrom(uType))
{
if (!aRule.Excluded.Contains(uType))
{
allow = true;
break;
}
}
}
if (!allow)
{
effects = resultEffects;
return allow; // 如果没有匹配的放行规则则直接返回
}
resultEffects |= DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link;
foreach (var rRule in _RejectTypes)
{
if (rRule.BaseType.IsAssignableFrom(uType))
{
if (!rRule.Excluded.Contains(uType))
{
resultEffects &= DragDropEffects.None;
allow = false;
break;
}
}
}
effects = resultEffects;
return allow;
}
public static void Help_Insert(this IContainerElement @this, IElement element, uint index)
{
if (element == null) return;
if (index < @this.Elements.Count) @this.Elements.Insert((int)index, element);
if (index >= @this.Elements.Count) @this.Elements.Add(element);
}
public static void Help_MoveTo(this IContainerElement @this, IElement element, uint index)
{
if (element == null) return;
int newIndex = (index >= @this.Elements.Count) ? @this.Elements.Count - 1 : (int)index;
int oldIndex = @this.Elements.IndexOf(element);
if (oldIndex < 0) @this.Elements.Insert(newIndex, element);
else @this.Elements.Move(oldIndex, newIndex);
}
}
}