Mind Editoe

This commit is contained in:
艾竹
2023-03-05 21:30:53 +08:00
parent 9061146139
commit 79f4896fbd
41 changed files with 2090 additions and 484 deletions

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AIStudio.Wpf.DiagramDesigner
{
class DoCommandManager
public class DoCommandManager
{
#region Command定义
public class Command
@@ -24,9 +24,18 @@ namespace AIStudio.Wpf.DiagramDesigner
this.clearAction = clearAction;
}
internal void Do() { action(); }
internal void UnDo() { unDoAction(); }
internal void Clear() { if (clearAction != null) clearAction(); }
internal void Do()
{
action();
}
internal void UnDo()
{
unDoAction();
}
internal void Clear()
{
if (clearAction != null) clearAction();
}
public override string ToString()
{
@@ -35,35 +44,57 @@ namespace AIStudio.Wpf.DiagramDesigner
}
#endregion
public Stack<Command> ReDoActionStack { get; private set; }
public Stack<Command> UnDoActionStack { get; private set; }
public Stack<Command> ReDoActionStack
{
get; private set;
}
public Stack<Command> UnDoActionStack
{
get; private set;
}
public int Capacity { get; set; } = 10;
public DoCommandManager()
{
Init();
}
public void Init()
{
ReDoActionStack = new Stack<Command>();
UnDoActionStack = new Stack<Command>();
}
private bool _undoing;
public void DoNewCommand(string name, Action action, Action unDoAction, Action clearAction = null, bool doit = true)
{
if (UnDoActionStack.Count >= Capacity)
{
//清理
var clear = UnDoActionStack.LastOrDefault();
clear.Clear();
UnDoActionStack = new Stack<Command>(UnDoActionStack.Take(Capacity - 1).Reverse());
}
var cmd = new Command(name, action, unDoAction, clearAction);
UnDoActionStack.Push(cmd);
ReDoActionStack.Clear();
if (doit)
if (_undoing == true) return;
try
{
cmd.Do();
_undoing = true;
if (UnDoActionStack.Count >= Capacity)
{
//清理
var clear = UnDoActionStack.LastOrDefault();
clear.Clear();
UnDoActionStack = new Stack<Command>(UnDoActionStack.Take(Capacity - 1).Reverse());
}
var cmd = new Command(name, action, unDoAction, clearAction);
UnDoActionStack.Push(cmd);
ReDoActionStack.Clear();
if (doit)
{
cmd.Do();
}
}
finally
{
_undoing = false;
}
}
@@ -72,9 +103,19 @@ namespace AIStudio.Wpf.DiagramDesigner
if (!CanUnDo)
return;
var cmd = UnDoActionStack.Pop();
ReDoActionStack.Push(cmd);
cmd.UnDo();
if (_undoing == true) return;
try
{
_undoing = true;
var cmd = UnDoActionStack.Pop();
ReDoActionStack.Push(cmd);
cmd.UnDo();
}
finally
{
_undoing = false;
}
}
public void ReDo()
@@ -82,13 +123,34 @@ namespace AIStudio.Wpf.DiagramDesigner
if (!CanReDo)
return;
var cmd = ReDoActionStack.Pop();
UnDoActionStack.Push(cmd);
cmd.Do();
if (_undoing == true) return;
try
{
_undoing = true;
var cmd = ReDoActionStack.Pop();
UnDoActionStack.Push(cmd);
cmd.Do();
}
finally
{
_undoing = false;
}
}
public bool CanUnDo { get { return UnDoActionStack.Count != 0; } }
public bool CanReDo { get { return ReDoActionStack.Count != 0; } }
public bool CanUnDo
{
get
{
return UnDoActionStack.Count != 0;
}
}
public bool CanReDo
{
get
{
return ReDoActionStack.Count != 0;
}
}
//public IEnumerable<Command> Actions { get { return ReDoActionStack.Reverse().Concat(UnDoActionStack); } }
}
}

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AIStudio.Wpf.DiagramDesigner.Helpers
{
public static class IEnumerableExtensions
{
/// <summary>
/// Invokes a transform function on each element of a sequence and returns the minimum Double value
/// if the sequence is not empty; otherwise returns the specified default value.
/// </summary>
/// <typeparam name="TSource"> The type of the elements of source. </typeparam>
/// <param name="source"> A sequence of values to determine the minimum value of. </param>
/// <param name="selector"> A transform function to apply to each element. </param>
/// <param name="defaultValue"> The default value. </param>
/// <returns> The minimum value in the sequence or default value if sequence is empty. </returns>
public static double MinOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector, double defaultValue=default(double))
{
if (source.Any<TSource>())
return source.Min<TSource>(selector);
return defaultValue;
}
/// <summary>
/// Invokes a transform function on each element of a sequence and returns the maximum Double value
/// if the sequence is not empty; otherwise returns the specified default value.
/// </summary>
/// <typeparam name="TSource"> The type of the elements of source. </typeparam>
/// <param name="source"> A sequence of values to determine the maximum value of. </param>
/// <param name="selector"> A transform function to apply to each element. </param>
/// <param name="defaultValue"> The default value. </param>
/// <returns> The maximum value in the sequence or default value if sequence is empty. </returns>
public static double MaxOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector, double defaultValue=default(double))
{
if (source.Any<TSource>())
return source.Max<TSource>(selector);
return defaultValue;
}
public static TResult MinOrDefault<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector, TResult defaultValue)
{
if (source.Any())
{
return source.Min(selector);
}
return defaultValue;
}
public static TResult MaxOrDefault<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector, TResult defaultValue)
{
if (source.Any())
{
return source.Max(selector);
}
return defaultValue;
}
/// <summary>
/// Invokes a transform function on each element of a sequence and returns the minimum Double value
/// if the sequence is not empty; otherwise returns the specified default value.
/// </summary>
/// <typeparam name="TSource"> The type of the elements of source. </typeparam>
/// <param name="source"> A sequence of values to determine the minimum value of. </param>
/// <param name="selector"> A transform function to apply to each element. </param>
/// <param name="defaultValue"> The default value. </param>
/// <returns> The minimum value in the sequence or default value if sequence is empty. </returns>
public static double SumOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector, double defaultValue=default(double))
{
if (source.Any<TSource>())
return source.Sum<TSource>(selector);
return defaultValue;
}
}
}