1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Display conditions - Some work on inputs supporting multiple types

This commit is contained in:
SpoinkyNL 2020-07-07 22:28:32 +02:00
parent 3639185171
commit 92dfbb354a
16 changed files with 112 additions and 49 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using Artemis.Core.Plugins.Models;
@ -6,9 +7,42 @@ namespace Artemis.Core
{
public static class Constants
{
/// <summary>
/// The full path to the Artemis application folder
/// </summary>
public static readonly string ApplicationFolder = Path.GetDirectoryName(typeof(Constants).Assembly.Location);
/// <summary>
/// The full path to the Artemis data folder
/// </summary>
public static readonly string DataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Artemis\\";
/// <summary>
/// The connection string used to connect to the database
/// </summary>
public static readonly string ConnectionString = $"FileName={DataFolder}\\database.db";
/// <summary>
/// The plugin info used by core components of Artemis
/// </summary>
public static readonly PluginInfo CorePluginInfo = new PluginInfo {Guid = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff"), Name = "Artemis Core"};
/// <summary>
/// A read-only collection containing all primitive number types
/// </summary>
public static IReadOnlyCollection<Type> NumberTypes = new List<Type>
{
typeof(sbyte),
typeof(byte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(decimal)
};
}
}

View File

@ -10,24 +10,6 @@ namespace Artemis.Core.Models.Profile.Conditions
{
public abstract class DisplayConditionOperator
{
/// <summary>
/// A read-only collection containing all primitive number types
/// </summary>
protected static IReadOnlyCollection<Type> NumberTypes = new List<Type>
{
typeof(sbyte),
typeof(byte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(decimal)
};
private IDataModelService _dataModelService;
private bool _registered;

View File

@ -6,7 +6,7 @@ namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class GreaterThanConditionOperator : DisplayConditionOperator
{
public override IReadOnlyCollection<Type> CompatibleTypes => NumberTypes;
public override IReadOnlyCollection<Type> CompatibleTypes => Constants.NumberTypes;
public override string Description => "Is greater than";
public override string Icon => "GreaterThan";

View File

@ -6,7 +6,7 @@ namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class GreaterThanOrEqualConditionOperator : DisplayConditionOperator
{
public override IReadOnlyCollection<Type> CompatibleTypes => NumberTypes;
public override IReadOnlyCollection<Type> CompatibleTypes => Constants.NumberTypes;
public override string Description => "Is greater than or equal to";
public override string Icon => "GreaterThanOrEqual";

View File

@ -6,7 +6,7 @@ namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class LessThanConditionOperator : DisplayConditionOperator
{
public override IReadOnlyCollection<Type> CompatibleTypes => NumberTypes;
public override IReadOnlyCollection<Type> CompatibleTypes => Constants.NumberTypes;
public override string Description => "Is less than";
public override string Icon => "LessThan";

View File

@ -6,7 +6,7 @@ namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class LessThanOrEqualConditionOperator : DisplayConditionOperator
{
public override IReadOnlyCollection<Type> CompatibleTypes => NumberTypes;
public override IReadOnlyCollection<Type> CompatibleTypes => Constants.NumberTypes;
public override string Description => "Is less than or equal to";
public override string Icon => "LessThanOrEqual";

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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);

View File

@ -3,8 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Artemis.UI.DataModelVisualization.Input"
xmlns:System="clr-namespace:System;assembly=System.Runtime"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:Artemis.UI.Shared.Behaviors;assembly=Artemis.UI.Shared"
xmlns:s="https://github.com/canton7/Stylet"

View File

@ -2,6 +2,7 @@
using System.Windows.Input;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.UI.Shared.DataModelVisualization;
using FluentValidation.TestHelper;
namespace Artemis.UI.DataModelVisualization.Input
{

View File

@ -3,8 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Artemis.UI.DataModelVisualization.Input"
xmlns:System="clr-namespace:System;assembly=System.Runtime"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:Artemis.UI.Shared.Behaviors;assembly=Artemis.UI.Shared"
xmlns:s="https://github.com/canton7/Stylet"

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
using System.Windows.Input;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.UI.Shared.DataModelVisualization;
@ -16,5 +17,10 @@ namespace Artemis.UI.DataModelVisualization.Input
var regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
protected override int ConvertToSupportedType(object source)
{
return Convert.ToInt32(source);
}
}
}

View File

@ -3,8 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Artemis.UI.DataModelVisualization.Input"
xmlns:System="clr-namespace:System;assembly=System.Runtime"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:Artemis.UI.Shared.Behaviors;assembly=Artemis.UI.Shared"
mc:Ignorable="d"

View File

@ -37,9 +37,9 @@ namespace Artemis.UI.Services
if (_registeredBuiltInDataModelInputs)
return;
_dataModelVisualizationService.RegisterDataModelInput<StringDataModelInputViewModel>(Constants.CorePluginInfo);
_dataModelVisualizationService.RegisterDataModelInput<IntDataModelInputViewModel>(Constants.CorePluginInfo);
_dataModelVisualizationService.RegisterDataModelInput<DoubleDataModelInputViewModel>(Constants.CorePluginInfo);
_dataModelVisualizationService.RegisterDataModelInput<StringDataModelInputViewModel>(Constants.CorePluginInfo, Constants.NumberTypes);
_dataModelVisualizationService.RegisterDataModelInput<IntDataModelInputViewModel>(Constants.CorePluginInfo, Constants.NumberTypes);
_dataModelVisualizationService.RegisterDataModelInput<DoubleDataModelInputViewModel>(Constants.CorePluginInfo, Constants.NumberTypes);
_registeredBuiltInDataModelInputs = true;
}

View File

@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;
using MaterialDesignExtensions.Controls;
namespace Artemis.UI.Utilities
@ -22,8 +23,8 @@ namespace Artemis.UI.Utilities
public void ApplyToWindow(MaterialWindow window)
{
window.Top = Top;
window.Left = Left;
window.Top = Math.Max(0, Top);
window.Left = Math.Max(0, Left);
window.Height = Height;
window.Width = Width;
window.WindowState = IsMaximized ? WindowState.Maximized : WindowState.Normal;