Skip to content

Commit

Permalink
Fixed an Exception that occured by clicking on the baseitem of a plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenHarenburg committed Aug 3, 2019
1 parent 176b50d commit f183017
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
20 changes: 10 additions & 10 deletions src/TrayKit/Controls/TrayKitContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public TrayKitContextMenu() : base()

private void AddBaseItems()
{
AddItem(ContextMenuBaseItem.OpenTrayKit.ToString(), "Open TrayKit", BASE_ITEM_TAG, null);
AddItem(ContextMenuBaseItem.ExitTrayKit.ToString(), "Exit TrayKit", BASE_ITEM_TAG, null);
AddItem(ContextMenuBaseItem.OpenTrayKit.ToString(), "Open TrayKit", BASE_ITEM_TAG, null, true);
AddItem(ContextMenuBaseItem.ExitTrayKit.ToString(), "Exit TrayKit", BASE_ITEM_TAG, null, true);

// Set to 1 because Plugin-Items should be added after the "Open TrayKit"-Item which is at index 0.
pluginStartingIndex = 1;
}

private ToolStripMenuItem AddItem(string name, string text, object tag, Image image)
private ToolStripMenuItem AddItem(string name, string text, object tag, Image image, bool addClickEventHandler)
{
var newItem = new ToolStripMenuItem()
{
Expand All @@ -49,13 +49,13 @@ private ToolStripMenuItem AddItem(string name, string text, object tag, Image im
Tag = tag,
Image = image
};
newItem.Click += Item_Click;
if (addClickEventHandler) newItem.Click += Item_Click;
Items.Add(newItem);

return newItem;
}

private ToolStripMenuItem AddItem(string name, string text, object tag, Image image, int index)
private ToolStripMenuItem AddItem(string name, string text, object tag, Image image, bool addClickEventHandler, int index)
{
var newItem = new ToolStripMenuItem()
{
Expand All @@ -64,7 +64,7 @@ private ToolStripMenuItem AddItem(string name, string text, object tag, Image im
Tag = tag,
Image = image
};
newItem.Click += Item_Click;
if (addClickEventHandler) newItem.Click += Item_Click;
Items.Insert(index, newItem);

return newItem;
Expand Down Expand Up @@ -108,21 +108,21 @@ where q.Tag.ToString() != BASE_ITEM_TAG
{
newItemIndex = (from ToolStripMenuItem q in Items
where q.Tag.ToString() != BASE_ITEM_TAG
select Items.IndexOf(q)).Max() + 1; // + 1 because the new item has to be placed behind the last plugin-item already in the list.
select Items.IndexOf(q)).Max() + 1; // + 1 because the new item has to be placed behind the last plugin-item already in the list.
}

if (!plugin.Commands.Any())
{
AddItem(plugin.Name, plugin.Name, plugin.Name, plugin.Image, newItemIndex);
AddItem(plugin.Name, plugin.Name, plugin.Name, plugin.Image, true, newItemIndex);
}
else if (plugin.Commands.Count == 1)
{
var command = plugin.Commands[0];
AddItem(command.Name, command.Name, command.GetFullCommandKey(), command.Image, newItemIndex);
AddItem(command.Name, command.Name, command.GetFullCommandKey(), command.Image, true, newItemIndex);
}
else
{
var pluginItem = AddItem(plugin.Name, plugin.Name, plugin.Name, plugin.Image, newItemIndex);
var pluginItem = AddItem(plugin.Name, plugin.Name, plugin.Name, plugin.Image, false, newItemIndex);
foreach (var command in (from q in plugin.Commands
orderby q.SortPosition ascending
select q))
Expand Down
4 changes: 2 additions & 2 deletions src/TrayKit/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]
46 changes: 27 additions & 19 deletions src/TrayKit/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void Plugin_EnabledChanged(object sender, EventArgs e)
ctmNotifyIcon.RemovePlugin(plugin.TrayKitPlugin);
}
}

protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
Expand Down Expand Up @@ -139,39 +139,47 @@ private void InitializeContextMenu()

private void CtmNotifyIcon_MenuItemClicked(object sender, Controls.MenuItemClickedEventArgs e)
{
if (e.Tag == Controls.TrayKitContextMenu.BASE_ITEM_TAG)
try
{
if (e.Name == Controls.ContextMenuBaseItem.OpenTrayKit.ToString())

if (e.Tag == Controls.TrayKitContextMenu.BASE_ITEM_TAG)
{
Show();
WindowState = WindowState.Normal;
if (e.Name == Controls.ContextMenuBaseItem.OpenTrayKit.ToString())
{
Show();
WindowState = WindowState.Normal;
}
else if (e.Name == Controls.ContextMenuBaseItem.ExitTrayKit.ToString())
{
Close();
}
}
else if (e.Name == Controls.ContextMenuBaseItem.ExitTrayKit.ToString())
else
{
Close();
var command = (from p in viewModel.PluginController.Plugins
from cmd in p.TrayKitPlugin.Commands
where cmd.GetFullCommandKey() == e.Tag
select cmd).FirstOrDefault();
if (command == null)
{
throw new Exception($"Command not found: {e.Tag}");
}
command.Execute();
}
}
else
catch (Exception ex)
{
var command = (from p in viewModel.PluginController.Plugins
from cmd in p.TrayKitPlugin.Commands
where cmd.GetFullCommandKey() == e.Tag
select cmd).FirstOrDefault();
if (command == null)
{
throw new Exception($"Command not found: {e.Tag}");
}
command.Execute();
System.Windows.MessageBox.Show(ex.ToString());
}
}

private void AddPluginsToContextMenu()
{
foreach (var plugin in viewModel.PluginController.Plugins)
{
ctmNotifyIcon.ImportPlugin(plugin.TrayKitPlugin);
}
}
}
#endregion
}
}

0 comments on commit f183017

Please sign in to comment.