2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-07-01 16:33:43 -04:00

[Framework + Core] Feature: Add support for adding multiple grid items (#105)

For plugin developers: Adds a new method to the IObservatoryCore interface to add multiple grid items in 1 call -- which only invokes the UI thread once (and potentially only scrolls to bottom once) providing a small performance improvement and providing some modest convenience.
This commit is contained in:
F K
2023-03-01 14:52:20 -05:00
committed by GitHub
parent 7e401adb6f
commit 3f2f11bdf3
2 changed files with 42 additions and 12 deletions

View File

@ -105,21 +105,28 @@ namespace Observatory.PluginManagement
//Hacky removal of original empty object if one was used to populate columns
if (worker.PluginUI.DataGrid.Count == 2)
{
bool allNull = true;
Type itemType = worker.PluginUI.DataGrid[0].GetType();
foreach (var property in itemType.GetProperties())
{
if (property.GetValue(worker.PluginUI.DataGrid[0], null) != null)
{
allNull = false;
break;
}
}
if (allNull)
if (FirstRowIsAllNull(worker))
worker.PluginUI.DataGrid.RemoveAt(0);
}
});
}
/// <summary>
/// Adds multiple items to the datagrid on UI thread to ensure visual update.
/// </summary>
/// <param name="worker"></param>
/// <param name="items"></param>
public void AddGridItems(IObservatoryWorker worker, IEnumerable<object> items)
{
Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
var cleanEmptyRow = worker.PluginUI.DataGrid.Count == 1 && FirstRowIsAllNull(worker) && items.Count() > 0;
foreach (var item in items)
{
worker.PluginUI.DataGrid.Add(item);
}
if (cleanEmptyRow)
worker.PluginUI.DataGrid.RemoveAt(0);
});
}
@ -175,5 +182,21 @@ namespace Observatory.PluginManagement
{
NativePopup.CloseAll();
}
private static bool FirstRowIsAllNull(IObservatoryWorker worker)
{
bool allNull = true;
Type itemType = worker.PluginUI.DataGrid[0].GetType();
foreach (var property in itemType.GetProperties())
{
if (property.GetValue(worker.PluginUI.DataGrid[0], null) != null)
{
allNull = false;
break;
}
}
return allNull;
}
}
}