public static MenuItem AddMenuChild( ItemCollection parent, string locString, RelayCommand command, object commandParameter = null, string icon = null) { var item = new MenuItem { Command = command, CommandParameter = commandParameter, InputGestureText = command?.GestureText }; if (locString.StartsWith("LOC")) { item.SetResourceReference(MenuItem.HeaderProperty, locString); } else { item.Header = locString; } var iconObj = MenuHelpers.GetIcon(icon); if (iconObj != null) { item.Icon = iconObj; } parent.Add(item); return(item); }
private void AddExtensionItems() { var args = new GetGameMenuItemsArgs(); var toAdd = new List <GameMenuItem>(); if (Games != null) { args.Games = Games; } else { args.Games = new List <Game>(1) { Game }; } foreach (var plugin in model.Extensions.Plugins.Values) { try { var items = plugin.Plugin.GetGameMenuItems(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 model.Extensions.Scripts) { if (script.SupportedMenus.Contains(Scripting.SupportedMenuMethods.GameMenu)) { try { var items = script.GetGameMenuItems(args); if (items.HasItems()) { foreach (var item in items) { var newItem = GameMenuItem.FromScriptGameMenuItem(item); newItem.Action = (a) => { script.InvokeFunction(item.FunctionName, new List <object> { new ScriptGameMenuItemActionArgs { Games = a.Games, SourceItem = item } }); }; 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) { Items.Add(new Separator()); var menuItems = new Dictionary <string, MenuItem>(); foreach (var item in toAdd) { object newItem = null; if (item.Description == "-") { newItem = new Separator(); } else { newItem = new MenuItem() { Header = item.Description, Icon = MenuHelpers.GetIcon(item.Icon) }; if (item.Action != null) { ((MenuItem)newItem).Click += (_, __) => { try { item.Action(new GameMenuItemActionArgs { Games = args.Games, SourceItem = item }); } catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors) { logger.Error(e, "Game menu extension action failed."); Dialogs.ShowErrorMessage( ResourceProvider.GetString("LOCMenuActionExecError") + Environment.NewLine + Environment.NewLine + e.Message, ""); } }; } } if (item.MenuSection.IsNullOrEmpty()) { Items.Add(newItem); } else { var parent = MenuHelpers.GenerateMenuParents(menuItems, item.MenuSection, Items); parent?.Items.Add(newItem); } } } }
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); } } } } }
public void InitializeItems() { // Have to load icons as late as possible to make sure ovewritten theme resources are loaded. if (!iconsLoaded) { startIcon = MenuHelpers.GetIcon("PlayIcon"); removeIcon = MenuHelpers.GetIcon("RemoveGameIcon"); linksIcon = MenuHelpers.GetIcon("LinksIcon"); favoriteIcon = MenuHelpers.GetIcon("AddFavoritesIcon"); unFavoriteIcon = MenuHelpers.GetIcon("RemoveFavoritesIcon"); hideIcon = MenuHelpers.GetIcon("HideIcon"); unHideIcon = MenuHelpers.GetIcon("UnHideIcon"); browseIcon = MenuHelpers.GetIcon("OpenFolderIcon"); shortcutIcon = MenuHelpers.GetIcon("DesktopShortcutIcon"); installIcon = MenuHelpers.GetIcon("InstallIcon"); editIcon = MenuHelpers.GetIcon("EditGameIcon"); manualIcon = MenuHelpers.GetIcon("ManualIcon"); iconsLoaded = true; } Items.Clear(); if (Games?.Count == 0 && Game == null) { return; } if (Games != null) { // Create Desktop Shortcut var shortcutItem = new MenuItem() { Header = resources.GetString("LOCCreateDesktopShortcut"), Icon = shortcutIcon, Command = model.CreateDesktopShortcutsCommand, CommandParameter = Games }; Items.Add(shortcutItem); // Set Favorites var favoriteItem = new MenuItem() { Header = resources.GetString("LOCFavoriteGame"), Icon = favoriteIcon, Command = model.SetAsFavoritesCommand, CommandParameter = Games }; Items.Add(favoriteItem); var unFavoriteItem = new MenuItem() { Header = resources.GetString("LOCRemoveFavoriteGame"), Icon = unFavoriteIcon, Command = model.RemoveAsFavoritesCommand, CommandParameter = Games }; Items.Add(unFavoriteItem); // Set Hide var hideItem = new MenuItem() { Header = resources.GetString("LOCHideGame"), Icon = hideIcon, Command = model.SetAsHiddensCommand, CommandParameter = Games }; Items.Add(hideItem); var unHideItem = new MenuItem() { Header = resources.GetString("LOCUnHideGame"), Icon = unHideIcon, Command = model.RemoveAsHiddensCommand, CommandParameter = Games }; Items.Add(unHideItem); // Edit var editItem = new MenuItem() { Header = resources.GetString("LOCEditGame"), Icon = editIcon, Command = model.EditGamesCommand, CommandParameter = Games, InputGestureText = model.EditSelectedGamesCommand.GestureText }; Items.Add(editItem); // Set Category var categoryItem = new MenuItem() { Header = resources.GetString("LOCSetGameCategory"), //Icon = Images.GetEmptyImage(), Command = model.AssignGamesCategoryCommand, CommandParameter = Games }; Items.Add(categoryItem); // Set Completion Status Items.Add(LoadCompletionStatusItem()); // Extensions items AddExtensionItems(); Items.Add(new Separator()); // Remove var removeItem = new MenuItem() { Header = resources.GetString("LOCRemoveGame"), Icon = removeIcon, Command = model.RemoveGamesCommand, CommandParameter = Games, InputGestureText = model.RemoveSelectedGamesCommand.GestureText }; Items.Add(removeItem); } else if (Game != null) { // Play / Install if (ShowStartSection) { bool added = false; if (Game.IsInstalled) { var playItem = new MenuItem() { Header = resources.GetString("LOCPlayGame"), Icon = startIcon, FontWeight = FontWeights.Bold, Command = model.StartGameCommand, CommandParameter = Game, InputGestureText = model.StartSelectedGameCommand.GestureText }; Items.Add(playItem); added = true; } else if (!Game.IsCustomGame) { var installItem = new MenuItem() { Header = resources.GetString("LOCInstallGame"), Icon = installIcon, FontWeight = FontWeights.Bold, Command = model.InstallGameCommand, CommandParameter = Game }; Items.Add(installItem); added = true; } if (added) { Items.Add(new Separator()); } } // Custom Actions var otherActions = Game.GameActions?.Where(a => !a.IsPlayAction).ToList(); if (otherActions.HasItems()) { foreach (var task in otherActions) { var taskItem = new MenuItem() { Header = task.Name }; taskItem.Click += (s, e) => { model.GamesEditor.ActivateAction(Game, task); }; Items.Add(taskItem); } Items.Add(new Separator()); } // Links if (Game.Links?.Any() == true) { var linksItem = new MenuItem() { Header = resources.GetString("LOCLinksLabel"), Icon = linksIcon }; foreach (var link in Game.Links) { if (link != null) { linksItem.Items.Add(new MenuItem() { Header = link.Name, Command = new RelayCommand <Link>((_) => { try { GlobalCommands.NavigateUrl(Game.ExpandVariables(link.Url)); } catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors) { logger.Error(e, "Failed to open url."); } }) }); } } Items.Add(linksItem); Items.Add(new Separator()); } // Open Game Location if (Game.IsInstalled) { var locationItem = new MenuItem() { Header = resources.GetString("LOCOpenGameLocation"), Icon = browseIcon, Command = model.OpenGameLocationCommand, CommandParameter = Game }; Items.Add(locationItem); } // Create Desktop Shortcut var shortcutItem = new MenuItem() { Header = resources.GetString("LOCCreateDesktopShortcut"), Icon = shortcutIcon, Command = model.CreateDesktopShortcutCommand, CommandParameter = Game }; Items.Add(shortcutItem); // Manual if (!Game.Manual.IsNullOrEmpty()) { Items.Add(new MenuItem() { Header = resources.GetString("LOCOpenGameManual"), Icon = manualIcon, Command = model.OpenManualCommand, CommandParameter = Game }); } Items.Add(new Separator()); // Toggle Favorites var favoriteItem = new MenuItem() { Header = Game.Favorite ? resources.GetString("LOCRemoveFavoriteGame") : resources.GetString("LOCFavoriteGame"), Icon = Game.Favorite ? unFavoriteIcon : favoriteIcon, Command = model.ToggleFavoritesCommand, CommandParameter = Game }; Items.Add(favoriteItem); // Toggle Hide var hideItem = new MenuItem() { Header = Game.Hidden ? resources.GetString("LOCUnHideGame") : resources.GetString("LOCHideGame"), Icon = Game.Hidden ? unHideIcon : hideIcon, Command = model.ToggleVisibilityCommand, CommandParameter = Game }; Items.Add(hideItem); // Edit var editItem = new MenuItem() { Header = resources.GetString("LOCEditGame"), Icon = editIcon, Command = model.EditGameCommand, CommandParameter = Game, InputGestureText = model.EditSelectedGamesCommand.GestureText }; Items.Add(editItem); // Set Category var categoryItem = new MenuItem() { Header = resources.GetString("LOCSetGameCategory"), //Icon = Images.GetEmptyImage(), Command = model.AssignGameCategoryCommand, CommandParameter = Game }; Items.Add(categoryItem); // Set Completion Status Items.Add(LoadCompletionStatusItem()); // Extensions items AddExtensionItems(); Items.Add(new Separator()); // Remove var removeItem = new MenuItem() { Header = resources.GetString("LOCRemoveGame"), Icon = removeIcon, Command = model.RemoveGameCommand, CommandParameter = Game, InputGestureText = model.RemoveGameCommand.GestureText }; Items.Add(removeItem); // Uninstall if (!Game.IsCustomGame && Game.IsInstalled) { var uninstallItem = new MenuItem() { Header = resources.GetString("LOCUninstallGame"), //Icon = Images.GetEmptyImage(), Command = model.UninstallGameCommand, CommandParameter = Game }; Items.Add(uninstallItem); } } }