mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Data model conditions - Simplified list VM code
Data model conditions - When selecting a different type, recheck if a new preferred operator is available
This commit is contained in:
parent
13df88ca40
commit
e1eb03667e
@ -93,18 +93,6 @@ namespace Artemis.Core
|
||||
|
||||
#region Modification
|
||||
|
||||
/// <summary>
|
||||
/// Updates the left side of the predicate
|
||||
/// </summary>
|
||||
/// <param name="path">The path pointing to the left side value inside the list</param>
|
||||
public override void UpdateLeftSide(DataModelPath? path)
|
||||
{
|
||||
if (DataModelConditionList.IsPrimitiveList)
|
||||
throw new ArtemisCoreException("Cannot apply a left side to a predicate inside a primitive list");
|
||||
|
||||
base.UpdateLeftSide(path);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Type? GetPreferredRightSideType()
|
||||
{
|
||||
|
||||
@ -15,8 +15,6 @@ namespace Artemis.UI.Shared
|
||||
{
|
||||
DataModel = ListPredicateWrapperDataModel.Create(listType);
|
||||
ListType = listType;
|
||||
|
||||
IsRootViewModel = false;
|
||||
}
|
||||
|
||||
public int Index
|
||||
|
||||
@ -44,31 +44,7 @@ namespace Artemis.UI.Shared
|
||||
}
|
||||
|
||||
public BindableCollection<DataModelVisualizationViewModel> ListChildren { get; set; }
|
||||
|
||||
public DataModelPropertiesViewModel GetListTypeViewModel(IDataModelUIService dataModelUIService)
|
||||
{
|
||||
Type listType = DataModelPath.GetPropertyType()?.GetGenericEnumerableType();
|
||||
if (listType == null)
|
||||
return null;
|
||||
|
||||
// Create a property VM describing the type of the list
|
||||
DataModelVisualizationViewModel viewModel = CreateListChild(dataModelUIService, listType);
|
||||
viewModel.Update(dataModelUIService);
|
||||
|
||||
// Put an empty value into the list type property view model
|
||||
if (viewModel is DataModelListPropertiesViewModel dataModelListClassViewModel) return dataModelListClassViewModel;
|
||||
|
||||
if (viewModel is DataModelListPropertyViewModel dataModelListPropertyViewModel)
|
||||
{
|
||||
dataModelListPropertyViewModel.DisplayValue = Activator.CreateInstance(dataModelListPropertyViewModel.ListType);
|
||||
DataModelPropertiesViewModel wrapper = new DataModelPropertiesViewModel(null, null, null);
|
||||
wrapper.Children.Add(dataModelListPropertyViewModel);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public override void Update(IDataModelUIService dataModelUIService)
|
||||
{
|
||||
if (Parent != null && !Parent.IsVisualizationExpanded)
|
||||
|
||||
@ -142,41 +142,6 @@ namespace Artemis.UI.Shared
|
||||
IsMatchingFilteredTypes = filteredTypes.Any(t => t == type || t == typeof(Enum) && type.IsEnum);
|
||||
}
|
||||
|
||||
public DataModelVisualizationViewModel GetChildByPath(Guid dataModelGuid, string propertyPath)
|
||||
{
|
||||
if (!IsRootViewModel)
|
||||
{
|
||||
if (DataModel.PluginInfo.Guid != dataModelGuid)
|
||||
return null;
|
||||
if (propertyPath == null)
|
||||
return null;
|
||||
if (Path != null && Path.StartsWith(propertyPath, StringComparison.OrdinalIgnoreCase))
|
||||
return null;
|
||||
}
|
||||
|
||||
// Ensure children are populated by requesting an update
|
||||
if (!IsVisualizationExpanded)
|
||||
{
|
||||
IsVisualizationExpanded = true;
|
||||
RequestUpdate();
|
||||
IsVisualizationExpanded = false;
|
||||
}
|
||||
|
||||
foreach (DataModelVisualizationViewModel child in Children)
|
||||
{
|
||||
// Try the child itself first
|
||||
if (child.Path == propertyPath)
|
||||
return child;
|
||||
|
||||
// Try a child on the child next, this will go recursive
|
||||
DataModelVisualizationViewModel match = child.GetChildByPath(dataModelGuid, propertyPath);
|
||||
if (match != null)
|
||||
return match;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal virtual int GetChildDepth()
|
||||
{
|
||||
return 0;
|
||||
@ -187,7 +152,7 @@ namespace Artemis.UI.Shared
|
||||
if (IsRootViewModel && DataModel == null)
|
||||
return;
|
||||
|
||||
Type modelType = IsRootViewModel ? DataModel.GetType() : DataModelPath?.GetPropertyType() ?? DataModel.GetType();
|
||||
Type modelType = IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType();
|
||||
|
||||
// Add missing static children
|
||||
foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(t => t.MetadataToken))
|
||||
|
||||
@ -99,7 +99,11 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
Operators.Clear();
|
||||
Operators.AddRange(_conditionOperatorService.GetConditionOperatorsForType(leftSideType ?? typeof(object), ConditionParameterSide.Left));
|
||||
if (DataModelConditionPredicate.Operator == null)
|
||||
DataModelConditionPredicate.UpdateOperator(Operators.FirstOrDefault(o => o.SupportsType(leftSideType ?? typeof(object), ConditionParameterSide.Left)));
|
||||
DataModelConditionPredicate.UpdateOperator(Operators.FirstOrDefault());
|
||||
// The core doesn't care about best matches so if there is a new preferred operator, use that instead
|
||||
else if (!Operators.Contains(DataModelConditionPredicate.Operator))
|
||||
DataModelConditionPredicate.UpdateOperator(Operators.FirstOrDefault(o => o.Description == DataModelConditionPredicate.Operator.Description) ?? Operators.FirstOrDefault());
|
||||
|
||||
SelectedOperator = DataModelConditionPredicate.Operator;
|
||||
|
||||
// Without a selected operator or one that supports a right side, leave the right side input empty
|
||||
|
||||
@ -24,13 +24,10 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
_profileEditorService = profileEditorService;
|
||||
_dataModelUIService = dataModelUIService;
|
||||
_dataModelConditionsVmFactory = dataModelConditionsVmFactory;
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public DataModelConditionEvent DataModelConditionEvent => (DataModelConditionEvent) Model;
|
||||
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
LeftSideSelectionViewModel = _dataModelUIService.GetDynamicSelectionViewModel(_profileEditorService.GetCurrentModule());
|
||||
@ -40,8 +37,15 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
List<Type> supportedInputTypes = editors.Select(e => e.SupportedType).ToList();
|
||||
supportedInputTypes.AddRange(editors.Where(e => e.CompatibleConversionTypes != null).SelectMany(e => e.CompatibleConversionTypes));
|
||||
supportedInputTypes.Add(typeof(IEnumerable<>));
|
||||
LeftSideSelectionViewModel.FilterTypes = supportedInputTypes.ToArray();
|
||||
|
||||
// Events are only supported in the root group enforce that here
|
||||
if (Parent is DataModelConditionGroupViewModel groupViewModel && groupViewModel.IsRootGroup)
|
||||
{
|
||||
supportedInputTypes.Add(typeof(DataModelEvent));
|
||||
supportedInputTypes.Add(typeof(DataModelEvent<>));
|
||||
}
|
||||
|
||||
LeftSideSelectionViewModel.FilterTypes = supportedInputTypes.ToArray();
|
||||
LeftSideSelectionViewModel.ButtonBrush = new SolidColorBrush(Color.FromRgb(185, 164, 10));
|
||||
LeftSideSelectionViewModel.Placeholder = "Select an event";
|
||||
|
||||
@ -91,6 +95,12 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
Update();
|
||||
}
|
||||
|
||||
protected override void OnInitialActivate()
|
||||
{
|
||||
Initialize();
|
||||
base.OnInitialActivate();
|
||||
}
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void LeftSideSelectionViewModelOnPropertySelected(object? sender, DataModelInputDynamicEventArgs e)
|
||||
|
||||
@ -26,8 +26,6 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
_profileEditorService = profileEditorService;
|
||||
_dataModelUIService = dataModelUIService;
|
||||
_dataModelConditionsVmFactory = dataModelConditionsVmFactory;
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public DataModelConditionList DataModelConditionList => (DataModelConditionList) Model;
|
||||
@ -74,8 +72,15 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
List<Type> supportedInputTypes = editors.Select(e => e.SupportedType).ToList();
|
||||
supportedInputTypes.AddRange(editors.Where(e => e.CompatibleConversionTypes != null).SelectMany(e => e.CompatibleConversionTypes));
|
||||
supportedInputTypes.Add(typeof(IEnumerable<>));
|
||||
LeftSideSelectionViewModel.FilterTypes = supportedInputTypes.ToArray();
|
||||
|
||||
// Events are only supported in the root group enforce that here
|
||||
if (Parent is DataModelConditionGroupViewModel groupViewModel && groupViewModel.IsRootGroup)
|
||||
{
|
||||
supportedInputTypes.Add(typeof(DataModelEvent));
|
||||
supportedInputTypes.Add(typeof(DataModelEvent<>));
|
||||
}
|
||||
|
||||
LeftSideSelectionViewModel.FilterTypes = supportedInputTypes.ToArray();
|
||||
LeftSideSelectionViewModel.ButtonBrush = new SolidColorBrush(Color.FromRgb(71, 108, 188));
|
||||
LeftSideSelectionViewModel.Placeholder = "Select a list";
|
||||
|
||||
@ -88,14 +93,13 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
bool converted = ConvertIfRequired(newType);
|
||||
if (converted)
|
||||
return;
|
||||
|
||||
|
||||
DataModelConditionList.UpdateList(LeftSideSelectionViewModel.DataModelPath);
|
||||
_profileEditorService.UpdateSelectedProfileElement();
|
||||
|
||||
Update();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
LeftSideSelectionViewModel.ChangeDataModelPath(DataModelConditionList.ListPath);
|
||||
@ -127,6 +131,12 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
childViewModel.Update();
|
||||
}
|
||||
|
||||
protected override void OnInitialActivate()
|
||||
{
|
||||
Initialize();
|
||||
base.OnInitialActivate();
|
||||
}
|
||||
|
||||
private void LeftSideSelectionViewModelOnPropertySelected(object? sender, DataModelInputDynamicEventArgs e)
|
||||
{
|
||||
ApplyList();
|
||||
|
||||
@ -12,7 +12,6 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
public class DataModelConditionEventPredicateViewModel : DataModelConditionPredicateViewModel
|
||||
{
|
||||
private readonly IDataModelUIService _dataModelUIService;
|
||||
private readonly IProfileEditorService _profileEditorService;
|
||||
|
||||
public DataModelConditionEventPredicateViewModel(DataModelConditionEventPredicate dataModelConditionEventPredicate,
|
||||
IProfileEditorService profileEditorService,
|
||||
@ -21,11 +20,9 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
ISettingsService settingsService)
|
||||
: base(dataModelConditionEventPredicate, profileEditorService, dataModelUIService, conditionOperatorService, settingsService)
|
||||
{
|
||||
_profileEditorService = profileEditorService;
|
||||
_dataModelUIService = dataModelUIService;
|
||||
|
||||
LeftSideColor = new SolidColorBrush(Color.FromRgb(185, 164, 10));
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public DataModelConditionEventPredicate DataModelConditionEventPredicate => (DataModelConditionEventPredicate) Model;
|
||||
@ -38,6 +35,12 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
LeftSideSelectionViewModel.ChangeDataModel(eventDataModel);
|
||||
}
|
||||
|
||||
protected override void OnInitialActivate()
|
||||
{
|
||||
base.OnInitialActivate();
|
||||
Initialize();
|
||||
}
|
||||
|
||||
protected override List<Type> GetSupportedInputTypes()
|
||||
{
|
||||
IReadOnlyCollection<DataModelVisualizationRegistration> editors = _dataModelUIService.RegisteredDataModelEditors;
|
||||
|
||||
@ -40,7 +40,7 @@
|
||||
<materialDesign:PackIcon Kind="Close" Width="18" Height="18" />
|
||||
</Button>
|
||||
|
||||
<!-- Left side, may be null for primitive lists -->
|
||||
<!-- Left side, always a property -->
|
||||
<ContentControl Grid.Row="0"
|
||||
Grid.Column="1"
|
||||
s:View.Model="{Binding LeftSideSelectionViewModel}"
|
||||
|
||||
@ -4,7 +4,6 @@ using System.Linq;
|
||||
using System.Windows.Media;
|
||||
using Artemis.Core;
|
||||
using Artemis.Core.Services;
|
||||
using Artemis.UI.Exceptions;
|
||||
using Artemis.UI.Shared;
|
||||
using Artemis.UI.Shared.Services;
|
||||
|
||||
@ -13,8 +12,6 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
public class DataModelConditionListPredicateViewModel : DataModelConditionPredicateViewModel
|
||||
{
|
||||
private readonly IDataModelUIService _dataModelUIService;
|
||||
private readonly IProfileEditorService _profileEditorService;
|
||||
private bool _isPrimitiveList;
|
||||
|
||||
public DataModelConditionListPredicateViewModel(DataModelConditionListPredicate dataModelConditionListPredicate,
|
||||
IProfileEditorService profileEditorService,
|
||||
@ -23,11 +20,9 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
ISettingsService settingsService)
|
||||
: base(dataModelConditionListPredicate, profileEditorService, dataModelUIService, conditionOperatorService, settingsService)
|
||||
{
|
||||
_profileEditorService = profileEditorService;
|
||||
_dataModelUIService = dataModelUIService;
|
||||
|
||||
LeftSideColor = new SolidColorBrush(Color.FromRgb(71, 108, 188));
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public DataModelConditionListPredicate DataModelConditionListPredicate => (DataModelConditionListPredicate) Model;
|
||||
@ -37,14 +32,20 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
base.Initialize();
|
||||
|
||||
DataModelPropertiesViewModel listDataModel = GetListDataModel();
|
||||
if (listDataModel.Children.Count == 1 && listDataModel.Children.First() is DataModelListPropertyViewModel)
|
||||
_isPrimitiveList = true;
|
||||
else
|
||||
_isPrimitiveList = false;
|
||||
LeftSideSelectionViewModel.ChangeDataModel(listDataModel);
|
||||
|
||||
// Get the data models
|
||||
if (!_isPrimitiveList)
|
||||
LeftSideSelectionViewModel.ChangeDataModel(listDataModel);
|
||||
// If this is a primitive list the user doesn't have much to choose, so preselect the list item for them
|
||||
if (DataModelConditionListPredicate.DataModelConditionList.IsPrimitiveList && DataModelConditionListPredicate.LeftPath == null)
|
||||
{
|
||||
DataModelConditionListPredicate.UpdateLeftSide(listDataModel.Children.FirstOrDefault()?.DataModelPath);
|
||||
Update();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnInitialActivate()
|
||||
{
|
||||
base.OnInitialActivate();
|
||||
Initialize();
|
||||
}
|
||||
|
||||
protected override List<Type> GetSupportedInputTypes()
|
||||
@ -72,16 +73,11 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
|
||||
|
||||
private DataModelPropertiesViewModel GetListDataModel()
|
||||
{
|
||||
if (DataModelConditionListPredicate.DataModelConditionList.ListPath?.DataModelGuid == null)
|
||||
throw new ArtemisUIException("Failed to retrieve the list data model VM for this list predicate because it has no list path");
|
||||
|
||||
DataModelPropertiesViewModel dataModel = _dataModelUIService.GetPluginDataModelVisualization(_profileEditorService.GetCurrentModule(), true);
|
||||
DataModelListViewModel listDataModel = (DataModelListViewModel) dataModel.GetChildByPath(
|
||||
DataModelConditionListPredicate.DataModelConditionList.ListPath.DataModelGuid.Value,
|
||||
DataModelConditionListPredicate.DataModelConditionList.ListPath.Path
|
||||
ListPredicateWrapperDataModel wrapper = ListPredicateWrapperDataModel.Create(
|
||||
DataModelConditionListPredicate.DataModelConditionList.ListType
|
||||
);
|
||||
|
||||
return listDataModel.GetListTypeViewModel(_dataModelUIService);
|
||||
return wrapper.CreateViewModel(_dataModelUIService);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user