Skip to content

Commit

Permalink
reworking things in Function Mgr so it does not call Document.ExpireS…
Browse files Browse the repository at this point in the history
…olution
  • Loading branch information
andyopayne committed Aug 29, 2024
1 parent 45e4e6b commit 5baf8e2
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 131 deletions.
122 changes: 120 additions & 2 deletions src/hops/HopsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
using System.IO;
using Grasshopper.Kernel.Types;
using Grasshopper.Kernel.Data;
using System.Linq;
using Rhino;
using System.Drawing;
using Grasshopper;

namespace Hops
{
Expand Down Expand Up @@ -458,7 +458,7 @@ public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
tsi.Enabled = !_showPathInput;
menu.Items.Add(tsi);

tsi = HopsFunctionMgr.AddFunctionMgrControl(this);
tsi = AddFunctionMgrControl();
if (tsi != null)
menu.Items.Add(tsi);

Expand Down Expand Up @@ -532,6 +532,124 @@ public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
restAPITsi.DropDownItems.Add(tsi);
}

public ToolStripMenuItem AddFunctionMgrControl()
{
HopsAppSettings.InitFunctionSources();
if (HopsAppSettings.FunctionSources.Count <= 0)
return null;
ToolStripMenuItem mainMenu = new ToolStripMenuItem("Available Functions", null, null, "Available Functions");
mainMenu.DropDownItems.Clear();
foreach (var row in HopsAppSettings.FunctionSources)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(row.SourceName, null, null, row.SourceName);
GenerateFunctionPathMenu(menuItem, row);
if (menuItem.DropDownItems.Count > 0)
mainMenu.DropDownItems.Add(menuItem);
}
//InitThumbnailViewer();
return mainMenu;
}

private void GenerateFunctionPathMenu(ToolStripMenuItem menu, FunctionSourceRow row)
{
if (String.IsNullOrEmpty(row.SourceName) || String.IsNullOrEmpty(row.SourcePath))
return;
if (row.SourcePath.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
try
{
var getTask = HopsFunctionMgr.HttpClient.GetAsync(row.SourcePath);
if (getTask != null)
{
var responseMessage = getTask.Result;
var remoteSolvedData = responseMessage.Content;
var stringResult = remoteSolvedData.ReadAsStringAsync().Result;
if (string.IsNullOrEmpty(stringResult))
{
//invalid URL
return;
}
else
{
var response = JsonConvert.DeserializeObject<FunctionMgr_Schema[]>(stringResult);
if (response != null)
{
UriFunctionPathInfo functionPaths = new UriFunctionPathInfo(row.SourcePath, true);
functionPaths.isRoot = true;
functionPaths.RootURL = row.SourcePath;
if (!String.IsNullOrEmpty(response[0].Uri))
{
//If the Schema Uri exists, then the response is likely from the ghhops_server.
//Otherwise, let's assume the response is from the appserver
foreach (FunctionMgr_Schema obj in response)
{
HopsFunctionMgr.SeekFunctionMenuDirs(functionPaths, obj.Uri, obj.Uri, row);
}
}
else if (!String.IsNullOrEmpty(response[0].Name))
{
foreach (FunctionMgr_Schema obj in response)
{
HopsFunctionMgr.SeekFunctionMenuDirs(functionPaths, "/" + obj.Name, "/" + obj.Name, row);
}
}
if (functionPaths.Paths.Count != 0)
functionPaths.BuildMenus(menu, new MouseEventHandler(tsm_UriClick));
}
}
}
}
catch (Exception)
{
}
}
else if (Directory.Exists(row.SourcePath))
{
FunctionPathInfo functionPaths = new FunctionPathInfo(row.SourcePath, true);
functionPaths.isRoot = true;

HopsFunctionMgr.SeekFunctionMenuDirs(functionPaths);
if (functionPaths.Paths.Count != 0)
{
functionPaths.BuildMenus(menu, tsm_FileClick, HopsFunctionMgr.tsm_HoverEnter, HopsFunctionMgr.tsm_HoverExit);
functionPaths.RemoveEmptyMenuItems(menu, tsm_FileClick, HopsFunctionMgr.tsm_HoverEnter, HopsFunctionMgr.tsm_HoverExit);
}
}
}

private void tsm_FileClick(object sender, MouseEventArgs e)
{
if (!(sender is ToolStripItem))
return;
ToolStripItem ti = sender as ToolStripItem;

switch (e.Button)
{
case MouseButtons.Left:
RemoteDefinitionLocation = ti.Name;
this.ExpireSolution(true);
break;
case MouseButtons.Right:
try
{
Instances.DocumentEditor.ScriptAccess_OpenDocument(ti.Name);
}
catch (Exception) { }
break;
}

}

private void tsm_UriClick(object sender, MouseEventArgs e)
{
if (!(sender is ToolStripItem))
return;
ToolStripItem ti = sender as ToolStripItem;
RemoteDefinitionLocation = ti.Tag as string;
this.ExpireSolution(true);
}


/// <summary>
/// Used for supporting double click on the component.
/// </summary>
Expand Down
132 changes: 3 additions & 129 deletions src/hops/HopsFunctionMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,9 @@ namespace Hops
{
public static class HopsFunctionMgr
{
private static HopsComponent Parent { get; set; }
static ThumbnailViewer Viewer { get; set; }
public static ToolStripMenuItem AddFunctionMgrControl(HopsComponent _parent)
{
HopsAppSettings.InitFunctionSources();
if (HopsAppSettings.FunctionSources.Count <= 0)
return null;
Parent = _parent;
ToolStripMenuItem mainMenu = new ToolStripMenuItem("Available Functions", null, null, "Available Functions");
mainMenu.DropDownItems.Clear();
foreach(var row in HopsAppSettings.FunctionSources)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(row.SourceName, null, null, row.SourceName);
GenerateFunctionPathMenu(menuItem, row);
if(menuItem.DropDownItems.Count > 0)
mainMenu.DropDownItems.Add(menuItem);
}
InitThumbnailViewer();
return mainMenu;
}

private static void InitThumbnailViewer()
static HopsFunctionMgr()
{
if (Viewer == null)
Viewer = new ThumbnailViewer();
Expand All @@ -47,73 +28,6 @@ private static void InitThumbnailViewer()
Viewer.Visible = false;
}

private static void GenerateFunctionPathMenu(ToolStripMenuItem menu, FunctionSourceRow row)
{
if (String.IsNullOrEmpty(row.SourceName) || String.IsNullOrEmpty(row.SourcePath))
return;
if (row.SourcePath.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
try
{
var getTask = HttpClient.GetAsync(row.SourcePath);
if (getTask != null)
{
var responseMessage = getTask.Result;
var remoteSolvedData = responseMessage.Content;
var stringResult = remoteSolvedData.ReadAsStringAsync().Result;
if (string.IsNullOrEmpty(stringResult))
{
//invalid URL
return;
}
else
{
var response = JsonConvert.DeserializeObject<FunctionMgr_Schema[]>(stringResult);
if(response != null)
{
UriFunctionPathInfo functionPaths = new UriFunctionPathInfo(row.SourcePath, true);
functionPaths.isRoot = true;
functionPaths.RootURL = row.SourcePath;
if (!String.IsNullOrEmpty(response[0].Uri))
{
//If the Schema Uri exists, then the response is likely from the ghhops_server.
//Otherwise, let's assume the response is from the appserver
foreach (FunctionMgr_Schema obj in response)
{
SeekFunctionMenuDirs(functionPaths, obj.Uri, obj.Uri, row);
}
}
else if(!String.IsNullOrEmpty(response[0].Name))
{
foreach (FunctionMgr_Schema obj in response)
{
SeekFunctionMenuDirs(functionPaths, "/" + obj.Name, "/" + obj.Name, row);
}
}
if (functionPaths.Paths.Count != 0)
functionPaths.BuildMenus(menu, new MouseEventHandler(tsm_UriClick));
}
}
}
}
catch (Exception)
{
}
}
else if (Directory.Exists(row.SourcePath))
{
FunctionPathInfo functionPaths = new FunctionPathInfo(row.SourcePath, true);
functionPaths.isRoot = true;

SeekFunctionMenuDirs(functionPaths);
if (functionPaths.Paths.Count != 0)
{
functionPaths.BuildMenus(menu, new MouseEventHandler(tsm_FileClick), new EventHandler(tsm_HoverEnter), new EventHandler(tsm_HoverExit));
functionPaths.RemoveEmptyMenuItems(menu, tsm_FileClick, tsm_HoverEnter, tsm_HoverExit);
}
}
}

public static void SeekFunctionMenuDirs(UriFunctionPathInfo path, string uri, string fullpath, FunctionSourceRow row)
{
if (path == null)
Expand Down Expand Up @@ -165,7 +79,7 @@ public static void SeekFunctionMenuDirs(FunctionPathInfo path)
}
}

static void tsm_HoverEnter(object sender, EventArgs e)
internal static void tsm_HoverEnter(object sender, EventArgs e)
{
if (!(sender is ToolStripMenuItem))
return;
Expand All @@ -181,54 +95,14 @@ static void tsm_HoverEnter(object sender, EventArgs e)
}
}

static void tsm_HoverExit(object sender, EventArgs e)
internal static void tsm_HoverExit(object sender, EventArgs e)
{
if (Viewer != null && Viewer.Visible)
{
Viewer.Hide();
}
}

static void tsm_FileClick(object sender, MouseEventArgs e)
{
if (!(sender is ToolStripItem))
return;
ToolStripItem ti = sender as ToolStripItem;

if(Parent != null)
{
switch (e.Button)
{
case MouseButtons.Left:
Parent.RemoteDefinitionLocation = ti.Name;
if (Instances.ActiveCanvas.Document != null)
Instances.ActiveCanvas.Document.ExpireSolution();
break;
case MouseButtons.Right:
try
{
Instances.DocumentEditor.ScriptAccess_OpenDocument(ti.Name);
}
catch (Exception) { }
break;
}
}
}

static void tsm_UriClick(object sender, MouseEventArgs e)
{
if (!(sender is ToolStripItem))
return;
ToolStripItem ti = sender as ToolStripItem;

if (Parent != null)
{
Parent.RemoteDefinitionLocation = ti.Tag as string;
if (Instances.ActiveCanvas.Document != null)
Instances.ActiveCanvas.Document.ExpireSolution();
}
}

static Image _funcMgr24Icon;
static Image _funcMgr48Icon;
static Image _deleteIcon;
Expand Down

0 comments on commit 5baf8e2

Please sign in to comment.