mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Display conditions - Some work on inputs supporting multiple types
This commit is contained in:
1 parent
3639185171
commit
92dfbb354a
16 files changed
+112
-49
No files matched your search
@@ -1,9 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
|
||||
using Artemis.UI.Shared.Exceptions;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.Shared.DataModelVisualization
|
||||
@@ -38,11 +41,29 @@ namespace Artemis.UI.Shared.DataModelVisualization
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed override void Cancel()
|
||||
public sealed override void Cancel()
|
||||
{
|
||||
OnCancel();
|
||||
UpdateCallback(InputValue, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// May be called to convert an object to one of the types defined in CompatibleConversionTypes
|
||||
/// </summary>
|
||||
/// <param name="source">The object to convert, will always be of a type contained in CompatibleConversionTypes</param>
|
||||
/// <returns>The converted value</returns>
|
||||
protected virtual T ConvertToSupportedType(object source)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
internal override object InternalConvertToSupportedType(object source)
|
||||
{
|
||||
if (CompatibleConversionTypes != null && CompatibleConversionTypes.Contains(source.GetType()))
|
||||
return ConvertToSupportedType(source);
|
||||
|
||||
throw new ArtemisSharedUIException($"Cannot convert source of type {source.GetType().Name} because the data model input view model does not support it.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -58,6 +79,11 @@ namespace Artemis.UI.Shared.DataModelVisualization
|
||||
|
||||
internal Action<object, bool> UpdateCallback { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the types this input view model can support through type conversion
|
||||
/// </summary>
|
||||
internal IReadOnlyCollection<Type> CompatibleConversionTypes { get; set; }
|
||||
|
||||
public void AttachView(UIElement view)
|
||||
{
|
||||
if (View != null)
|
||||
@@ -98,5 +124,7 @@ namespace Artemis.UI.Shared.DataModelVisualization
|
||||
protected virtual void OnCancel()
|
||||
{
|
||||
}
|
||||
|
||||
internal abstract object InternalConvertToSupportedType(object source);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Core;
|
||||
using Artemis.Core.Plugins.Models;
|
||||
using Artemis.UI.Shared.Services;
|
||||
@@ -30,6 +31,8 @@ namespace Artemis.UI.Shared.DataModelVisualization
|
||||
public Type SupportedType { get; }
|
||||
public Type ViewModelType { get; }
|
||||
|
||||
public IReadOnlyCollection<Type> CompatibleConversionTypes { get; internal set; }
|
||||
|
||||
internal void Unsubscribe()
|
||||
{
|
||||
if (PluginInfo != Constants.CorePluginInfo)
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace Artemis.UI.Shared.Services
|
||||
return _dataModelService.GetPluginExtendsDataModel(plugin);
|
||||
}
|
||||
|
||||
public DataModelVisualizationRegistration RegisterDataModelInput<T>(PluginInfo pluginInfo) where T : DataModelInputViewModel
|
||||
public DataModelVisualizationRegistration RegisterDataModelInput<T>(PluginInfo pluginInfo, IReadOnlyCollection<Type> compatibleConversionTypes) where T : DataModelInputViewModel
|
||||
{
|
||||
var viewModelType = typeof(T);
|
||||
lock (_registeredDataModelEditors)
|
||||
@@ -80,7 +80,14 @@ namespace Artemis.UI.Shared.Services
|
||||
}
|
||||
|
||||
_kernel.Bind(viewModelType).ToSelf();
|
||||
var registration = new DataModelVisualizationRegistration(this, RegistrationType.Input, pluginInfo, supportedType, viewModelType);
|
||||
|
||||
// Create the registration
|
||||
var registration = new DataModelVisualizationRegistration(this, RegistrationType.Input, pluginInfo, supportedType, viewModelType)
|
||||
{
|
||||
// Apply the compatible conversion types to the registration
|
||||
CompatibleConversionTypes = compatibleConversionTypes
|
||||
};
|
||||
|
||||
_registeredDataModelEditors.Add(registration);
|
||||
return registration;
|
||||
}
|
||||
@@ -154,16 +161,7 @@ namespace Artemis.UI.Shared.Services
|
||||
var match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType == propertyType);
|
||||
if (match != null)
|
||||
{
|
||||
// The view models expecting value types shouldn't be given null, avoid that
|
||||
if (initialValue == null && propertyType.IsValueType)
|
||||
initialValue = Activator.CreateInstance(propertyType);
|
||||
|
||||
var parameters = new IParameter[]
|
||||
{
|
||||
new ConstructorArgument("description", description),
|
||||
new ConstructorArgument("initialValue", initialValue)
|
||||
};
|
||||
var viewModel = (DataModelInputViewModel) _kernel.Get(match.ViewModelType, parameters);
|
||||
var viewModel = InstantiateDataModelInputViewModel(match, description, initialValue);
|
||||
viewModel.UpdateCallback = updateCallback;
|
||||
return viewModel;
|
||||
}
|
||||
@@ -171,6 +169,22 @@ namespace Artemis.UI.Shared.Services
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private DataModelInputViewModel InstantiateDataModelInputViewModel(DataModelVisualizationRegistration registration, DataModelPropertyAttribute description, object initialValue)
|
||||
{
|
||||
// The view models expecting value types shouldn't be given null, avoid that
|
||||
if (initialValue == null && registration.SupportedType.IsValueType)
|
||||
initialValue = Activator.CreateInstance(registration.SupportedType);
|
||||
|
||||
var parameters = new IParameter[]
|
||||
{
|
||||
new ConstructorArgument("description", description),
|
||||
new ConstructorArgument("initialValue", initialValue)
|
||||
};
|
||||
var viewModel = (DataModelInputViewModel) _kernel.Get(registration.ViewModelType, parameters);
|
||||
viewModel.CompatibleConversionTypes = registration.CompatibleConversionTypes;
|
||||
return viewModel;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IDataModelVisualizationService : IArtemisSharedUIService
|
||||
@@ -185,7 +199,7 @@ namespace Artemis.UI.Shared.Services
|
||||
/// <returns></returns>
|
||||
bool GetPluginExtendsDataModel(Plugin plugin);
|
||||
|
||||
DataModelVisualizationRegistration RegisterDataModelInput<T>(PluginInfo pluginInfo) where T : DataModelInputViewModel;
|
||||
DataModelVisualizationRegistration RegisterDataModelInput<T>(PluginInfo pluginInfo, IReadOnlyCollection<Type> compatibleConversionTypes) where T : DataModelInputViewModel;
|
||||
DataModelVisualizationRegistration RegisterDataModelDisplay<T>(PluginInfo pluginInfo) where T : DataModelDisplayViewModel;
|
||||
void RemoveDataModelInput(DataModelVisualizationRegistration registration);
|
||||
void RemoveDataModelDisplay(DataModelVisualizationRegistration registration);
|
||||
|
||||
Reference in new issue
Block a user