整理代码

This commit is contained in:
艾竹
2023-06-28 22:11:24 +08:00
parent e20d1f7e59
commit 3ec68b7b63
10 changed files with 10 additions and 10 deletions

View File

@@ -5,11 +5,11 @@ using System.Text;
namespace AIStudio.Wpf.DiagramDesigner
{
public class SelectionService
public class DiagramSelectionService
{
private IDiagramViewModel DiagramViewModel;
public SelectionService(IDiagramViewModel diagramViewModel)
public DiagramSelectionService(IDiagramViewModel diagramViewModel)
{
this.DiagramViewModel = diagramViewModel;
}

View File

@@ -40,13 +40,13 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private SelectionService selectionService;
public SelectionService SelectionService
private DiagramSelectionService selectionService;
public DiagramSelectionService SelectionService
{
get
{
if (selectionService == null)
selectionService = new SelectionService(this);
selectionService = new DiagramSelectionService(this);
return selectionService;
}

View File

@@ -1,179 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace AIStudio.Wpf.DiagramDesigner
{
/// <summary>
/// Implementation of <see cref="INotifyPropertyChanged"/> to simplify models.
/// </summary>
public abstract class BindableBase : INotifyPropertyChanged
{
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private Dictionary<string, object> _oldDic = new Dictionary<string, object>();
public void SetOldValue<T>(T value, string propertyName, string guid = null)
{
_oldDic[propertyName + guid ?? string.Empty] = value;
}
public T GetOldValue<T>(string propertyName, string guid = null)
{
if (_oldDic.ContainsKey(propertyName + guid ?? string.Empty))
{
var old = (T)_oldDic[propertyName + guid ?? string.Empty];
return old;
}
else
{
return default(T);
}
}
public void ClearOldValue<T>(string propertyName, string guid = null)
{
if (_oldDic.ContainsKey(propertyName + guid ?? string.Empty))
{
_oldDic.Remove(propertyName + guid ?? string.Empty);
}
}
/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
/// notifies listeners only when necessary.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to a property with both getter and setter.</param>
/// <param name="value">Desired value for the property.</param>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers that
/// support CallerMemberName.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
return false;
if (propertyName == "IsSelected")
{
}
else if (this.ContainsProperty("IsReadOnly"))
{
if (object.Equals(this.GetPropertyValue("IsReadOnly"), true))
return false;
}
var old = storage;
storage = value;
RaisePropertyChanged(propertyName, old, value);
return true;
}
/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
/// notifies listeners only when necessary.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to a property with both getter and setter.</param>
/// <param name="value">Desired value for the property.</param>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers that
/// support CallerMemberName.</param>
/// <param name="onChanged">Action that is called after the property value has been changed.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
if (propertyName == "IsSelected")
{
}
else if (this.ContainsProperty("IsReadOnly"))
{
if (object.Equals(this.GetPropertyValue("IsReadOnly"), true))
return false;
}
var old = storage;
storage = value;
onChanged?.Invoke();
RaisePropertyChanged(propertyName, old, value);
return true;
}
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute"/>.</param>
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null, object oldvalue = null, object newvalue = null)
{
OnPropertyChanged(new ValuePropertyChangedEventArgs(propertyName, oldvalue, newvalue));
}
protected void RaisePropertyChanged(object sender, [CallerMemberName] string propertyName = null, object oldvalue = null, object newvalue = null)
{
PropertyChanged?.Invoke(sender, new ValuePropertyChangedEventArgs(propertyName, oldvalue, newvalue));
}
protected void RaisePropertyChanged(object sender, PropertyChangedEventArgs args)
{
PropertyChanged?.Invoke(sender, args);
}
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="args">The PropertyChangedEventArgs</param>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
PropertyChanged?.Invoke(this, args);
}
public IObservable<string> WhenPropertyChanged
{
get
{
return Observable
.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => this.PropertyChanged += h,
h => this.PropertyChanged -= h)
.Select(x => x.EventArgs.PropertyName);
}
}
}
public class ValuePropertyChangedEventArgs : PropertyChangedEventArgs
{
public ValuePropertyChangedEventArgs(string propertyName, object oldValue, object newValue) : base(propertyName)
{
OldValue = oldValue;
NewValue = newValue;
}
public object OldValue { get; set; }
public object NewValue { get; set; }
}
}

View File

@@ -28,7 +28,7 @@ namespace AIStudio.Wpf.DiagramDesigner
{
get;
}
SelectionService SelectionService
DiagramSelectionService SelectionService
{
get;
}

View File

@@ -1,66 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace AIStudio.Wpf.DiagramDesigner
{
public class SimpleCommand : ICommand
{
/// <summary>
/// Gets or sets the Predicate to execute when the CanExecute of the command gets called
/// </summary>
public Predicate<object> CanExecuteDelegate { get; set; }
/// <summary>
/// Gets or sets the action to be called when the Execute method of the command gets called
/// </summary>
public Action<object> ExecuteDelegate { get; set; }
public SimpleCommand(Predicate<object> canExecuteDelegate, Action<object> executeDelegate)
{
CanExecuteDelegate = canExecuteDelegate;
ExecuteDelegate = executeDelegate;
}
public SimpleCommand(Action<object> executeDelegate)
{
ExecuteDelegate = executeDelegate;
}
#region ICommand Members
/// <summary>
/// Checks if the command Execute method can run
/// </summary>
/// <param name="parameter">THe command parameter to be passed</param>
/// <returns>Returns true if the command can execute. By default true is returned so that if the user of SimpleCommand does not specify a CanExecuteCommand delegate the command still executes.</returns>
public bool CanExecute(object parameter)
{
if (CanExecuteDelegate != null)
return CanExecuteDelegate(parameter);
return true;// if there is no can execute default to true
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Executes the actual command
/// </summary>
/// <param name="parameter">THe command parameter to be passed</param>
public void Execute(object parameter)
{
if (ExecuteDelegate != null)
ExecuteDelegate(parameter);
}
#endregion
}
}