private MenuItem PopulateMenuItem(ScriptLI li) { MenuItem item = new MenuItem(); if (!li.IsListItem) { item.Text = li.Text; } else { ScreenViewer.API.Elements.LinkController controller = new ScreenViewer.API.Elements.LinkController(); var scriptLink = controller.GetScriptLink(Convert.ToDecimal(li.ElementID)); var linkResponse = scriptLink as OkNegotiatedContentResult <Data.ScriptLink>; if (linkResponse.Content.LinkType == "Web") { item.Text = linkResponse.Content.LinkDesc; item.Url = linkResponse.Content.LinkURL; if (linkResponse.Content.LinkNewWindow.Equals(true)) { item.LinkHtmlAttributes.Add("target", "_blank"); } } if (linkResponse.Content.LinkType == "Workflow") { item.Text = linkResponse.Content.LinkDesc; item.Action("DisplayByUniqueName", "Workflow", new { id = linkResponse.Content.LinkTypeID }); if (linkResponse.Content.LinkNewWindow.Equals(true)) { item.HtmlAttributes.Add("target", "_blank"); } //item.HtmlAttributes.Add("onclick", "$('#myForm #btnSave').click();"); } if (linkResponse.Content.LinkType == "Section") { item.Text = linkResponse.Content.LinkDesc; item.Action("DisplayByUniqueName", "Section", new { id = linkResponse.Content.LinkTypeID }); if (linkResponse.Content.LinkNewWindow.Equals(true)) { item.HtmlAttributes.Add("target", "_blank"); } } } if (li.theUL != null && li.theUL.LIArray.Length > 0) { foreach (ScriptLI cli in li.theUL.LIArray) { item.Items.Add(PopulateMenuItem(cli)); } } return(item); }
private void AddExtensionItems() { extensionsItem.Items.Clear(); AddMenuChild(extensionsItem.Items, "LOCReloadScripts", mainModel.ReloadScriptsCommand); extensionsItem.Items.Add(new Separator()); foreach (var function in mainModel.Extensions.ExportedFunctions) { var item = new MenuItem { Header = function.Name, Command = mainModel.InvokeExtensionFunctionCommand, CommandParameter = function }; extensionsItem.Items.Add(item); } var args = new GetMainMenuItemsArgs(); var toAdd = new List <MainMenuItem>(); foreach (var plugin in mainModel.Extensions.Plugins.Values) { try { var items = plugin.Plugin.GetMainMenuItems(args); if (items.HasItems()) { toAdd.AddRange(items); } } catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors) { logger.Error(e, $"Failed to get menu items from plugin {plugin.Description.Name}"); } } foreach (var script in mainModel.Extensions.Scripts) { if (script.SupportedMenus.Contains(Scripting.SupportedMenuMethods.MainMenu)) { try { var items = script.GetMainMenuItems(args); if (items.HasItems()) { foreach (var item in items) { var newItem = MainMenuItem.FromScriptMainMenuItem(item); newItem.Action = (a) => { script.InvokeFunction(item.FunctionName, new List <object> { new ScriptMainMenuItemActionArgs() }); }; toAdd.Add(newItem); } } } catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors) { logger.Error(e, $"Failed to get menu items from script {script.Name}"); } } } if (toAdd.Count > 0) { var menuItems = new Dictionary <string, MenuItem>(); var menuExtensionItems = new Dictionary <string, MenuItem>(); foreach (var item in toAdd) { var newItem = new MenuItem() { Header = item.Description, Icon = MenuHelpers.GetIcon(item.Icon) }; if (item.Action != null) { newItem.Click += (_, __) => { try { item.Action(new MainMenuItemActionArgs()); } catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors) { logger.Error(e, "Main menu extension action failed."); Dialogs.ShowErrorMessage( ResourceProvider.GetString("LOCMenuActionExecError") + Environment.NewLine + Environment.NewLine + e.Message, ""); } }; } var startIndex = Items.IndexOf(extensionsItem) + 1; if (item.MenuSection.IsNullOrEmpty()) { Items.Insert(startIndex, newItem); } else { if (item.MenuSection == "@") { extensionsItem.Items.Add(newItem); } else if (item.MenuSection.StartsWith("@")) { var parent = MenuHelpers.GenerateMenuParents(menuExtensionItems, item.MenuSection.Substring(1), extensionsItem.Items); parent?.Items.Add(newItem); } else { var parent = MenuHelpers.GenerateMenuParents(menuItems, item.MenuSection, Items, startIndex); parent?.Items.Add(newItem); } } } } }