Skip to content

Commit

Permalink
Refactor adding of source link to allow for Presistent menu as well a…
Browse files Browse the repository at this point in the history
…s volatile menu
  • Loading branch information
IsakNaslundBh authored and Fraser Greenroyd committed Sep 16, 2021
1 parent b74ac10 commit 251f1d4
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions BHoM_UI/Components/oM/CreateData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ protected override void SetComponentDetails()
Choices = BH.Engine.Library.Query.Library(FileName).ToList<object>();
Name = FileName.Split(new char[] { '\\' }).Last();
Description = BH.Engine.Library.Query.SourceAndDisclaimer(FileName);
if (m_menu != null)
AddSourceLinkToMenu(m_menu as dynamic);
}
}

Expand All @@ -103,16 +105,26 @@ public override List<string> GetChoiceNames()

/*************************************/

public override void AddToMenu(ToolStripDropDown menu)
public override void AddToMenu(object menu)
{
base.AddToMenu(menu);
m_menu = menu; //Store the menu to be able to add source link in case of presistent menu
AddSourceLinkToMenu(menu as dynamic); //Try add link now for volatile menu
}

if (FileName != null)
/*************************************/
/**** Private methods ****/
/*************************************/

private void AddSourceLinkToMenu(ToolStripDropDown menu)
{
if (FileName != null && !menu.Items.ContainsKey("SourceLink"))
{
string sourceLink = Engine.Library.Query.Source(FileName)?.First()?.SourceLink;
if (!string.IsNullOrWhiteSpace(sourceLink))
{
ToolStripLabel linkLabel = new ToolStripLabel("Source link");
linkLabel.Name = "SourceLink";
linkLabel.IsLink = true;
linkLabel.Click += (object sender, EventArgs e) => System.Diagnostics.Process.Start(sourceLink);
menu.Items.Add(linkLabel);
Expand All @@ -122,24 +134,36 @@ public override void AddToMenu(ToolStripDropDown menu)

/*************************************/

public override void AddToMenu(System.Windows.Controls.ContextMenu menu)
private void AddSourceLinkToMenu(System.Windows.Controls.ContextMenu menu)
{
base.AddToMenu(menu);

if (FileName != null)
if (FileName != null && !menu.Items.SourceCollection.OfType<MenuItem>().Any(x => x.Name == "SourceLink"))
{
string sourceLink = Engine.Library.Query.Source(FileName)?.First()?.SourceLink;
if (!string.IsNullOrWhiteSpace(sourceLink))
{
System.Windows.Controls.MenuItem linkLabel = new System.Windows.Controls.MenuItem();
linkLabel.Header = "Source Link";
linkLabel.Name = "SourceLink";
linkLabel.Click += (object sender, RoutedEventArgs e) => System.Diagnostics.Process.Start(sourceLink);
menu.Items.Add(linkLabel);
}
}
}

/*************************************/

private void AddSourceLinkToMenu(object menu)
{
//fallback method
}

/*************************************/
/**** Private fields ****/
/*************************************/

private object m_menu = null;

/*************************************/
}
}

Expand Down

0 comments on commit 251f1d4

Please sign in to comment.