Display conditions - Implemented most of static input

This commit is contained in:
SpoinkyNL committed 2020-07-06 23:41:41 +02:00
1 parent f944a675f1
commit 7af70e9ba4
14 files changed
+248 -30

No files matched your search

@@ -34,6 +34,7 @@
<PackageReference Include="Humanizer.Core" Version="2.8.26" />
<PackageReference Include="MaterialDesignExtensions" Version="3.1.0" />
<PackageReference Include="MaterialDesignThemes" Version="3.1.3" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.19" />
<PackageReference Include="Ninject" Version="3.3.4" />
<PackageReference Include="Ninject.Extensions.Conventions" Version="3.3.0" />
<PackageReference Include="SkiaSharp" Version="1.68.3" />
@@ -0,0 +1,34 @@
using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors;
namespace Artemis.UI.Shared.Behaviors
{
public class PutCursorAtEndTextBoxBehavior : Behavior<UIElement>
{
private TextBox _textBox;
protected override void OnAttached()
{
base.OnAttached();
_textBox = AssociatedObject as TextBox;
if (_textBox == null) return;
_textBox.GotFocus += TextBoxGotFocus;
}
protected override void OnDetaching()
{
if (_textBox == null) return;
_textBox.GotFocus -= TextBoxGotFocus;
base.OnDetaching();
}
private void TextBoxGotFocus(object sender, RoutedEventArgs routedEventArgs)
{
_textBox.CaretIndex = _textBox.Text.Length;
}
}
}
@@ -1,4 +1,6 @@
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using System;
using System.Windows;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Stylet;
namespace Artemis.UI.Shared.DataModelVisualization
@@ -22,20 +24,66 @@ namespace Artemis.UI.Shared.DataModelVisualization
public DataModelPropertyAttribute Description { get; }
internal override object InternalGuard { get; } = null;
protected void Submit()
/// <inheritdoc />
public sealed override void Submit()
{
OnSubmit();
UpdateCallback(InputValue, true);
}
/// <inheritdoc />
public sealed override void Cancel()
{
OnCancel();
UpdateCallback(InputValue, false);
}
}
/// <summary>
/// For internal use only, implement <see cref="DataModelInputViewModel{T}" /> instead.
/// </summary>
public abstract class DataModelInputViewModel : PropertyChangedBase
public abstract class DataModelInputViewModel : PropertyChangedBase, IViewAware
{
/// <summary>
/// Prevents this type being implemented directly, implement <see cref="DataModelInputViewModel{T}" /> instead.
/// </summary>
// ReSharper disable once UnusedMember.Global
internal abstract object InternalGuard { get; }
internal Action<object, bool> UpdateCallback { get; set; }
public void AttachView(UIElement view)
{
if (View != null)
throw new InvalidOperationException(string.Format("Tried to attach View {0} to ViewModel {1}, but it already has a view attached", view.GetType().Name, GetType().Name));
View = view;
}
public UIElement View { get; set; }
/// <summary>
/// Submits the input value and removes this view model
/// </summary>
public abstract void Submit();
/// <summary>
/// Discards changes to the input value and removes this view model
/// </summary>
public abstract void Cancel();
/// <summary>
/// Called before the current value is submitted
/// </summary>
protected virtual void OnSubmit()
{
}
/// <summary>
/// Called before the current value is discarded
/// </summary>
protected virtual void OnCancel()
{
}
}
}
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
@@ -9,6 +10,8 @@ using Artemis.UI.Shared.DataModelVisualization;
using Artemis.UI.Shared.DataModelVisualization.Shared;
using Artemis.UI.Shared.Services.Interfaces;
using Ninject;
using Ninject.Parameters;
using Stylet;
namespace Artemis.UI.Shared.Services
{
@@ -142,6 +145,27 @@ namespace Artemis.UI.Shared.Services
return null;
}
}
public DataModelInputViewModel GetDataModelInputViewModel(Type propertyType, DataModelPropertyAttribute description, object initialValue, Action<object, bool> updateCallback)
{
lock (_registeredDataModelEditors)
{
var match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType.IsAssignableFrom(propertyType));
if (match != null)
{
var parameters = new IParameter[]
{
new ConstructorArgument("description", description),
new ConstructorArgument("initialValue", initialValue)
};
var viewModel = (DataModelInputViewModel) _kernel.Get(match.ViewModelType, parameters);
viewModel.UpdateCallback = updateCallback;
return viewModel;
}
return null;
}
}
}
public interface IDataModelVisualizationService : IArtemisSharedUIService
@@ -162,6 +186,7 @@ namespace Artemis.UI.Shared.Services
void RemoveDataModelDisplay(DataModelVisualizationRegistration registration);
DataModelDisplayViewModel GetDataModelDisplayViewModel(Type propertyType);
DataModelInputViewModel GetDataModelInputViewModel(Type propertyType, DataModelPropertyAttribute description, object initialValue, Action<object, bool> updateCallback);
IReadOnlyCollection<DataModelVisualizationRegistration> RegisteredDataModelEditors { get; }
IReadOnlyCollection<DataModelVisualizationRegistration> RegisteredDataModelDisplays { get; }
}