Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys committed Nov 1, 2024
1 parent 906620e commit c1e72de
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
38 changes: 22 additions & 16 deletions src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/TextCell.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reactive.Subjects;
using Avalonia.Data;
Expand All @@ -12,11 +10,11 @@ namespace Avalonia.Controls.Models.TreeDataGrid
public class TextCell<T> : NotifyingBase, ITextCell, IDisposable, IEditableObject
{
private readonly ISubject<BindingValue<T>>? _binding;
private readonly ITextCellOptions? _options;
private readonly IDisposable? _subscription;
[AllowNull] private T? _value;
[AllowNull] private T? _cancelValue;
private string? _editText;
private T? _value;
private bool _isEditing;
private ITextCellOptions? _options;

public TextCell(T? value)
{
Expand Down Expand Up @@ -51,21 +49,29 @@ public string? Text
{
get
{
if (_options?.StringFormat is { } format)
if (_isEditing)
return _editText;
else if (_options?.StringFormat is { } format)
return string.Format(_options.Culture ?? CultureInfo.CurrentCulture, format, _value);
else
return _value?.ToString();
}

set
{
if (string.IsNullOrEmpty(value))
if (_isEditing)
{
Value = default(T?);
_editText = value;
}
else
{
Value = (T?)Convert.ChangeType(value, typeof(T));
try
{
Value = (T?)Convert.ChangeType(value, typeof(T));
}
catch
{
// TODO: Data validation errors.
}
}
}
}
Expand All @@ -87,27 +93,27 @@ public void BeginEdit()
if (!_isEditing && !IsReadOnly)
{
_isEditing = true;
_cancelValue = Value;
_editText = Text;
}
}

public void CancelEdit()
{
if (_isEditing)
{
Value = _cancelValue;
_isEditing = false;
_cancelValue = default;
_editText = null;
}
}

public void EndEdit()
{
if (_isEditing && !EqualityComparer<T>.Default.Equals(_value, _cancelValue))
if (_isEditing)
{
var text = _editText;
_isEditing = false;
_cancelValue = default;
_binding!.OnNext(_value!);
_editText = null;
Text = text;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ protected void CancelEdit()
protected void EndEdit()
{
if (EndEditCore() && Model is IEditableObject editable)
{
editable.EndEdit();
UpdateValue();
}
}

protected void SubscribeToModelChanges()
Expand All @@ -108,6 +111,10 @@ protected void UnsubscribeFromModelChanges()
inpc.PropertyChanged -= OnModelPropertyChanged;
}

protected virtual void UpdateValue()
{
}

protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
_treeDataGrid = this.FindLogicalAncestorOfType<TreeDataGrid>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using Avalonia.Media;
Expand Down Expand Up @@ -85,6 +86,11 @@ public override void Unrealize()
base.Unrealize();
}

protected override void UpdateValue()
{
Value = (Model as ITextCell)?.Text;
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Modified_Value_Is_Written_To_Binding_On_EndEdit()
target.Text = "new";

Assert.Equal("new", target.Text);
Assert.Equal("new", target.Value);
Assert.Equal("initial", target.Value);
Assert.Equal(new[] { "initial"}, result);

target.EndEdit();
Expand All @@ -86,7 +86,7 @@ public void Modified_Value_Is_Not_Written_To_Binding_On_CancelEdit()
target.Text = "new";

Assert.Equal("new", target.Text);
Assert.Equal("new", target.Value);
Assert.Equal("initial", target.Value);
Assert.Equal(new[] { "initial" }, result);

target.CancelEdit();
Expand All @@ -104,15 +104,15 @@ public void Initial_Int_Value_Is_Formatted()
var binding = new BehaviorSubject<BindingValue<int>>(42);
var target = new TextCell<int>(binding, true, GetOptions());

Assert.Equal("foo42", target.Text);
Assert.Equal("42.00", target.Text);
Assert.Equal(42, target.Value);
}

[AvaloniaFact(Timeout = 10000)]
public void Int_Value_Is_Formatted_After_Editing()
{
var binding = new BehaviorSubject<BindingValue<int>>(42);
var target = new TextCell<int>(binding, false);
var target = new TextCell<int>(binding, false, GetOptions());
var result = new List<int>();

binding.Subscribe(x => result.Add(x.Value));
Expand All @@ -121,17 +121,17 @@ public void Int_Value_Is_Formatted_After_Editing()
target.Text = "43";

Assert.Equal("43", target.Text);
Assert.Equal(43, target.Value);
Assert.Equal(42, target.Value);
Assert.Equal(new[] { 42 }, result);

target.EndEdit();

Assert.Equal("43.0", target.Text);
Assert.Equal("43.00", target.Text);
Assert.Equal(43, target.Value);
Assert.Equal(new[] { 42, 43 }, result);
}

private ITextCellOptions? GetOptions(string format = "foo{0}")
private ITextCellOptions? GetOptions(string format = "{0:n2}")
{
return new TextColumnOptions<int> { StringFormat = format };
}
Expand Down

0 comments on commit c1e72de

Please sign in to comment.