Skip to content

Commit

Permalink
Update valuelist when library changes
Browse files Browse the repository at this point in the history
  • Loading branch information
visose committed Jan 21, 2022
1 parent 1cd99f0 commit e4bfa25
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 81 deletions.
4 changes: 2 additions & 2 deletions RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

- version: 1.0.2
changes:
- Add warning when multifile input is used with UR.
- Added warning when multifile input is used with UR.
- Fixed robot system value list didn't update if robot library changed.
- Fixed some custom parameters would show an error when hovering the input.
- Additional cleanup.

- version: 1.0.1
changes:
- This and newer versions will be available only through Yak package manager.
- This and newer versions will be available only through Yak package manager.
- Large refactor and project restructure, some bugs might have crept in.
- Robot system can be loaded without meshes if not needed for improved performance (API only).
- Fixed rounding PulseDO value to 3 decimal places.
Expand Down
2 changes: 1 addition & 1 deletion build/Robots.Build/Robots.Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RhinoPackager" Version="1.0.1" />
<PackageReference Include="RhinoPackager" Version="1.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Eto.Drawing;
using Eto.Forms;
using Rhino.Resources;
using static Robots.Grasshopper.LibrariesForm;
using static Robots.Grasshopper.LibraryForm;
using EtoCommand = Eto.Forms.Command;

namespace Robots.Grasshopper;
Expand All @@ -22,7 +22,7 @@ public LibraryCell()
}
}

class LibrariesForm : ComponentForm
class LibraryForm : ComponentForm
{
// static

Expand All @@ -42,61 +42,25 @@ public static Label NewLabel(Expression<Func<LibraryItem, string>> bindText, Tex
return label;
}

public static string Icons(LibraryItem item)
public static string Icons(LibraryItem item) => item switch
{
if (item.IsLocal && item.IsDownloaded)
return "❕📁";

if (item.IsLocal)
return "📁";

if (item.IsDownloaded && item.IsUpdateAvailable)
return "⬆✔";

if (item.IsDownloaded)
return "✔";

if (item.IsOnline)
return "💾";

return "⚠";
}

static string Description(LibraryItem item)
{
if (item.IsLocal && item.IsDownloaded)
return "❕📁 Local override, installed";

if (item.IsLocal && item.IsOnline)
return "📁 Local, available online";

if (item.IsLocal)
return "📁 Local";

if (item.IsDownloaded && item.IsUpdateAvailable)
return "⬆✔ Installed, update available";

if (item.IsDownloaded && !item.IsOnline)
return "✔⚠ Installed, online missing";

if (item.IsDownloaded)
return "✔ Installed";

if (item.IsOnline)
return "💾 Available online";

return "⚠ Unknown error";
}
{ IsLocal: true, IsDownloaded: true } => "❕📁",
{ IsLocal: true } => "📁",
{ IsDownloaded: true, IsUpdateAvailable: true } => "⬆✔",
{ IsDownloaded: true } => "✔",
{ IsOnline: true } => "💾",
_ => "⚠"
};

// instance

readonly OnlineLibrary _library;
readonly GridView _grid;
readonly StackLayout _detailView;

public LibrariesForm()
public LibraryForm(OnlineLibrary library)
{
_library = new OnlineLibrary();
_library = library;

Title = "Robot libraries";
BackgroundColor = Colors.White;
Expand All @@ -116,17 +80,25 @@ public LibrariesForm()
_grid.SelectedRowsChanged += (s, e) => _detailView.DataContext = _grid.SelectedItem;
}

async Task UpdateAsync()
async Task RefreshAsync()
{
await _library.UpdateLibraryAsync();

var values = _library.Libraries.Values;
var ordered = values.OrderBy(i => i.Name);
var ordered = values.OrderBy(i => i.Name).ToList();

var selected = _grid.SelectedItem as LibraryItem;
_grid.DataStore = null;
_grid.DataStore = ordered;

if (values.Any())
_grid.SelectRow(0);
if (!ordered.Any())
return;

int index = selected is null
? 0 : ordered.FindIndex(i => selected.Name.ToLowerInvariant() == i.Name.ToLowerInvariant());
index = Math.Max(index, 0);

_grid.ScrollToRow(index);
_grid.SelectRow(index);
}

async Task DownloadAsync()
Expand Down Expand Up @@ -191,7 +163,7 @@ async Task DownloadAsync()
VerticalContentAlignment = VerticalAlignment.Bottom,
Items =
{
new StackLayoutItem(NewAsyncButton(UpdateAsync, label: "Refresh list", runOnce: true), true),
new StackLayoutItem(NewAsyncButton(RefreshAsync, label: "Refresh list", runOnce: true), true),
new LinkButton
{
Text = "Help",
Expand All @@ -208,12 +180,24 @@ async Task DownloadAsync()
Items =
{
NewLabel(i => i.Name, font: EtoFonts.BoldHeadingFont),
NewLabel(i => Description(i), font: EtoFonts.NormalFont),
NewLabel(i => Description(i)),
new StackLayoutItem(null, true),
NewDetailButton()
}
};

string Description(LibraryItem item) => item switch
{
{ IsLocal: true, IsDownloaded: true } => "❕📁 Installed, local override",
{ IsLocal: true, IsOnline: true } => "📁 Local, available online",
{ IsLocal: true } => "📁 Local",
{ IsDownloaded: true, IsUpdateAvailable: true } => "⬆✔ Installed, update available",
{ IsDownloaded: true, IsOnline: false } => "✔⚠ Installed, online missing",
{ IsDownloaded: true } => "✔ Installed",
{ IsOnline: true } => "💾 Available online",
_ => "⚠ Unknown error"
};

StackLayout NewDetailButton()
{
var detailButton = NewAsyncButton(DownloadAsync);
Expand All @@ -223,7 +207,7 @@ StackLayout NewDetailButton()
return detailButton;
}

static StackLayout NewAsyncButton(Func<Task> actionAsync, string? label = null, bool runOnce = false)
StackLayout NewAsyncButton(Func<Task> actionAsync, string? label = null, bool runOnce = false)
{
Button button = new()
{
Expand Down
61 changes: 39 additions & 22 deletions src/Robots.Grasshopper/RobotSystem/LoadRobotSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Robots.Grasshopper;

public class LoadRobotSystem : GH_Component
{
LibrariesForm? _form;
LibraryForm? _form;
GH_ValueList? _valueList = null;
IGH_Param? _parameter = null;

Expand Down Expand Up @@ -37,26 +37,49 @@ protected override void BeforeSolveInstance()
if (_valueList is not null)
return;

var inputValueList = _parameter.Sources.FirstOrDefault(s => s is GH_ValueList) as GH_ValueList;
_valueList = inputValueList ?? new GH_ValueList();

if (inputValueList is null)
if (_parameter.Sources.FirstOrDefault(s => s is GH_ValueList) is not GH_ValueList inputValueList)
{
_valueList = new GH_ValueList();
_valueList.CreateAttributes();
_valueList.Attributes.Pivot = new System.Drawing.PointF(Attributes.Pivot.X - 180, Attributes.Pivot.Y - 31);
AddRobotsToValueList(_valueList);
_valueList.Attributes.Pivot = new System.Drawing.PointF(Attributes.Pivot.X - 240, Attributes.Pivot.Y - 21);
UpdateValueList();
Instances.ActiveCanvas.Document.AddObject(_valueList, false);
_parameter.AddSource(_valueList);
_parameter.CollectData();
}
else
{
AddRobotsToValueList(_valueList);
_valueList = inputValueList;
UpdateValueList();
}
}

protected override void SolveInstance(IGH_DataAccess DA)
{
string? name = null;
GH_Plane? basePlane = null;

if (!DA.GetData(0, ref name) || name is null) { return; }
if (!DA.GetData(1, ref basePlane) || basePlane is null) { return; }

try
{
var robotSystem = FileIO.LoadRobotSystem(name, basePlane.Value);
DA.SetData(0, new GH_RobotSystem(robotSystem));
}
catch (Exception e)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message);
}
}

void AddRobotsToValueList(GH_ValueList valueList)
void UpdateValueList()
{
if (_valueList is null)
return;

var valueList = _valueList;

var selected = valueList.FirstSelectedItem;
var robotSystems = FileIO.ListRobotSystems();

Expand All @@ -77,18 +100,6 @@ void AddRobotsToValueList(GH_ValueList valueList)
valueList.SelectItem(selectedIndex);
}

protected override void SolveInstance(IGH_DataAccess DA)
{
string? name = null;
GH_Plane? basePlane = null;

if (!DA.GetData(0, ref name) || name is null) { return; }
if (!DA.GetData(1, ref basePlane) || basePlane is null) { return; }

var robotSystem = FileIO.LoadRobotSystem(name, basePlane.Value);
DA.SetData(0, new GH_RobotSystem(robotSystem));
}

// form

public override void CreateAttributes()
Expand All @@ -106,7 +117,13 @@ public override void RemovedFromDocument(GH_Document document)

void ToggleForm()
{
_form ??= new LibrariesForm();
if (_form is null)
{
var library = new OnlineLibrary();
library.LibraryChanged += UpdateValueList;
_form = new LibraryForm(library);
}

_form.Visible = !_form.Visible;
}
}
3 changes: 3 additions & 0 deletions src/Robots/IO/OnlineLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class OnlineLibrary
{
readonly HttpClient _http = new();
public Dictionary<string, LibraryItem> Libraries { get; } = new();
public event Action? LibraryChanged;

public OnlineLibrary()
{
Expand Down Expand Up @@ -51,6 +52,7 @@ public async Task<bool> TryDownloadLibraryAsync(LibraryItem library)
return false;

library.DownloadedSha = sha;
LibraryChanged?.Invoke();
return true;
}

Expand All @@ -71,6 +73,7 @@ public bool TryRemoveDownloadedLibrary(LibraryItem item)
}

item.DownloadedSha = null;
LibraryChanged?.Invoke();
return true;
}

Expand Down

0 comments on commit e4bfa25

Please sign in to comment.