Skip to content

Commit

Permalink
Tree Table Layout- fix new rows not always refreshed
Browse files Browse the repository at this point in the history
  • Loading branch information
tzachshabtay committed Feb 14, 2019
1 parent db669a2 commit c6fec3a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Source/AGS.API/UI/Layouts/TreeTable/ITreeTableLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public interface ITreeTableLayout : IDisposable
/// <summary>
/// Once the table layout calculates new column sizes it fires this event, for which all the rows should
/// listen and readjust their columns accordingly.
/// If a specific row is passed, only that row needs to recalculate. If the passed row is null, all rows need to recalculate.
/// </summary>
/// <value>The refresh layout event.</value>
IBlockingEvent OnRefreshLayoutNeeded { get; }
IBlockingEvent<ITreeTableRowLayoutComponent> OnRefreshLayoutNeeded { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using AGS.API;

namespace AGS.Engine
Expand Down Expand Up @@ -106,7 +107,7 @@ private void calculateColumns()
var tree = _tree;
if (tree == null)
{
return;
return;
}
if (tree.TreeNode.ChildrenCount > _columnSizes.Count)
{
Expand Down Expand Up @@ -180,8 +181,12 @@ private void onQueryLayout(QueryLayoutEventArgs args)
}
}

private void onRefreshLayout()
private void onRefreshLayout(ITreeTableRowLayoutComponent specificRow)
{
if (specificRow != null && specificRow != this)
{
return;
}
var table = Table;
var tree = _tree;
if (table == null || tree == null) return;
Expand Down
41 changes: 33 additions & 8 deletions Source/Engine/AGS.Engine/UI/Layouts/TreeTable/TreeTableLayout.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using AGS.API;

namespace AGS.Engine
Expand All @@ -11,13 +13,14 @@ public class TreeTableLayout : ITreeTableLayout

private bool _isDirty;
private int _inUpdate; //For preventing re-entrancy
private ConcurrentQueue<ITreeTableRowLayoutComponent> _newRows = new ConcurrentQueue<ITreeTableRowLayoutComponent>();

public TreeTableLayout(IGameEvents gameEvents)
{
_events = gameEvents;
ColumnSizes = new AGSBindingList<float>(10);
Rows = new AGSBindingList<ITreeTableRowLayoutComponent>(10);
OnRefreshLayoutNeeded = new AGSEvent();
OnRefreshLayoutNeeded = new AGSEvent<ITreeTableRowLayoutComponent>();
OnQueryLayout = new AGSEvent<QueryLayoutEventArgs>();
Rows.OnListChanged.Subscribe(onRowsChanged);
gameEvents.OnRepeatedlyExecute.Subscribe(onRepeatedlyExecute);
Expand All @@ -29,7 +32,7 @@ public float ColumnPadding
set
{
_columnPadding = value;
OnRefreshLayoutNeeded.Invoke();
OnRefreshLayoutNeeded.Invoke(null);
}
}

Expand All @@ -39,15 +42,15 @@ public float StartX
set
{
_startX = value;
OnRefreshLayoutNeeded.Invoke();
OnRefreshLayoutNeeded.Invoke(null);
}
}

public IAGSBindingList<float> ColumnSizes { get; private set; }

public IAGSBindingList<ITreeTableRowLayoutComponent> Rows { get; private set; }

public IBlockingEvent OnRefreshLayoutNeeded { get; private set; }
public IBlockingEvent<ITreeTableRowLayoutComponent> OnRefreshLayoutNeeded { get; private set; }

public IBlockingEvent<QueryLayoutEventArgs> OnQueryLayout { get; private set; }

Expand All @@ -62,8 +65,12 @@ public void PerformLayout()
_isDirty = true;
}

private void onRowsChanged(AGSListChangedEventArgs<ITreeTableRowLayoutComponent> obj)
private void onRowsChanged(AGSListChangedEventArgs<ITreeTableRowLayoutComponent> args)
{
foreach (var row in args.Items)
{
_newRows.Enqueue(row.Item);
}
PerformLayout();
}

Expand All @@ -84,6 +91,15 @@ private void onRepeatedlyExecute()

private void performLayout()
{
List<ITreeTableRowLayoutComponent> newRows = null;
if (_newRows.Count > 0)
{
newRows = new List<ITreeTableRowLayoutComponent>(_newRows.Count);
while (_newRows.TryDequeue(out var row))
{
newRows.Add(row);
}
}
QueryLayoutEventArgs args = new QueryLayoutEventArgs();
OnQueryLayout.Invoke(args);
bool isDirty = false;
Expand All @@ -104,8 +120,17 @@ private void performLayout()
isDirty = true;
ColumnSizes[index] = args.ColumnSizes[index];
}
if (!isDirty) return;
OnRefreshLayoutNeeded.Invoke();
if (isDirty)
{
OnRefreshLayoutNeeded.Invoke(null);
}
else if (newRows != null)
{
foreach (var row in newRows)
{
OnRefreshLayoutNeeded.Invoke(row);
}
}
}
}
}

0 comments on commit c6fec3a

Please sign in to comment.