Example #1
0
        internal static bool Execute(string actionName, CommandHint hint, params object[] args)
        {
            // Call some command/shortcut actions to execute the current action
            actionName = actionName.ToLower();
            var executeHandlers = GetModeDataSection(currentIndex, ModeDescriptor.ExecuteHandlersKey) as JSONObject;

            if (executeHandlers == null)
            {
                return(false);
            }

            if (!executeHandlers.Contains(actionName))
            {
                return(false);
            }

            string commandId = (string)executeHandlers[actionName];

            if (!CommandService.Exists(commandId))
            {
                return(false);
            }

            var result = CommandService.Execute(commandId, hint, args);

            return(result == null || (bool)result);
        }
Example #2
0
        internal static MenuItemScriptCommand InitializeFromCommand(string fullMenuName, int priority, string commandId, string validateCommandId)
        {
            var menuItem = new MenuItemScriptCommand()
            {
                name           = fullMenuName,
                priority       = priority,
                commandExecute = new Action(() => CommandService.Execute(commandId, CommandHint.Menu))
            };

            if (!string.IsNullOrEmpty(validateCommandId))
            {
                menuItem.commandValidate = new Func <bool>(() => (bool)CommandService.Execute(commandId, CommandHint.Menu | CommandHint.Validate));
            }

            return(menuItem);
        }
Example #3
0
        private static void OpenQuickSearch()
        {
            const string k_QuickSearchPackageId = "com.unity.quicksearch";

            // If the quick search package is installed it will execute the follow command.
            // Otherwise we will ask the user if he wants to install the package.
            if (CommandService.Exists(nameof(OpenQuickSearch)))
            {
                CommandService.Execute(nameof(OpenQuickSearch), CommandHint.Menu);
            }
            else
            {
                // Search for the latest version of the package.

                var searchLatestRequest = PackageManager.Client.Search(k_QuickSearchPackageId);
                if (!WaitForRequest(searchLatestRequest, "Searching for latest version of Quick Search..."))
                {
                    Debug.LogError($"Cannot find the Quick Search package ({searchLatestRequest.Status}).");
                    return;
                }

                var quickSearchPackage = searchLatestRequest.Result.FirstOrDefault(p => p.name == k_QuickSearchPackageId);
                if (quickSearchPackage != null && EditorUtility.DisplayDialog(
                        $"Shoot! {quickSearchPackage.displayName} is not installed yet!",
                        $"Do you want to install {quickSearchPackage.displayName} ({quickSearchPackage.versions.latest}) and be more productive?" +
                        $"\r\n\r\nPackage Description: {quickSearchPackage.description}", "Yes", "No"))
                {
                    // Install a token that will be read by the quick search package once
                    // installed so it can launch itself automatically the first time.
                    var quickSearchFirstUseTokenPath = Utils.Paths.Combine(Application.dataPath, "..", "Library", "~quicksearch.new");
                    File.Create(Path.GetFullPath(quickSearchFirstUseTokenPath)).Dispose();

                    // Add quick search package entry. the added package will
                    // be compiled and a domain reload will occur.
                    var packageIdToInstall    = $"{quickSearchPackage.name}@{quickSearchPackage.versions.latest}";
                    var addQuickSearchRequest = PackageManager.Client.Add(packageIdToInstall);
                    if (!WaitForRequest(addQuickSearchRequest, $"Installing {quickSearchPackage.displayName}..."))
                    {
                        Debug.LogError($"Failed to install {packageIdToInstall}");
                    }
                }
            }
        }
Example #4
0
        private static void LoadMenu(IList menus, string prefix = "", int priority = 100)
        {
            const string k_MenuKeyName              = "name";
            const string k_MenuKeyItemId            = "menu_item_id";
            const string k_MenuKeyCommandId         = "command_id";
            const string k_MenuKeyValidateCommandId = "validate_command_id";
            const string k_MenuKeyChildren          = "children";
            const string k_MenuKeyPriority          = "priority";
            const string k_MenuKeyInternal          = "internal";
            const string k_MenuKeyShortcut          = "shortcut";
            const string k_MenuKeyChecked           = "checked";
            const string k_MenuKeyPlatform          = "platform";
            const string k_MenuKeyRename            = "rename";

            if (menus == null)
            {
                return;
            }

            foreach (var menuData in menus)
            {
                if (menuData != null)
                {
                    var menu = menuData as JSONObject;
                    if (menu == null)
                    {
                        continue;
                    }
                    var isInternal = JsonUtils.JsonReadBoolean(menu, k_MenuKeyInternal);
                    if (isInternal && !Unsupported.IsDeveloperMode())
                    {
                        continue;
                    }
                    var menuName            = JsonUtils.JsonReadString(menu, k_MenuKeyName);
                    var fullMenuName        = prefix + menuName;
                    var platform            = JsonUtils.JsonReadString(menu, k_MenuKeyPlatform);
                    var hasExplicitPriority = menu.Contains(k_MenuKeyPriority);
                    priority = JsonUtils.JsonReadInt(menu, k_MenuKeyPriority, priority + 1);

                    // Check the menu item platform
                    if (!String.IsNullOrEmpty(platform) && !Application.platform.ToString().ToLowerInvariant().StartsWith(platform.ToLowerInvariant()))
                    {
                        continue;
                    }

                    // Check if we are a submenu
                    if (menu.Contains(k_MenuKeyChildren))
                    {
                        if (menu[k_MenuKeyChildren] is IList)
                        {
                            LoadMenu(menu[k_MenuKeyChildren] as IList, fullMenuName + "/", priority);
                        }
                        else if (menu[k_MenuKeyChildren] is string && (string)menu[k_MenuKeyChildren] == "*")
                        {
                            var whitelistedItems = Menu.ExtractSubmenus(fullMenuName);
                            var renamedTo        = prefix + JsonUtils.JsonReadString(menu, k_MenuKeyRename, menuName);
                            foreach (var wi in whitelistedItems)
                            {
                                Menu.AddExistingMenuItem(wi.Replace(fullMenuName, renamedTo), wi, hasExplicitPriority ? priority : -1);
                            }
                        }
                    }
                    else
                    {
                        var commandId = JsonUtils.JsonReadString(menu, k_MenuKeyCommandId);
                        if (String.IsNullOrEmpty(commandId))
                        {
                            // We are re-using a default menu item
                            var menuItemId = JsonUtils.JsonReadString(menu, k_MenuKeyItemId, fullMenuName);
                            if (fullMenuName.Contains('/'))
                            {
                                Menu.AddExistingMenuItem(fullMenuName, menuItemId, priority);
                            }
                        }
                        else if (CommandService.Exists(commandId))
                        {
                            // Create a new menu item pointing to a command handler
                            var shortcut = JsonUtils.JsonReadString(menu, k_MenuKeyShortcut);
                            var @checked = JsonUtils.JsonReadBoolean(menu, k_MenuKeyChecked);

                            Func <bool> validateHandler   = null;
                            var         validateCommandId = JsonUtils.JsonReadString(menu, k_MenuKeyValidateCommandId);
                            if (!String.IsNullOrEmpty(validateCommandId))
                            {
                                validateHandler = () => (bool)CommandService.Execute(validateCommandId, CommandHint.Menu | CommandHint.Validate);
                            }

                            Menu.AddMenuItem(fullMenuName, shortcut, @checked, priority, () => CommandService.Execute(commandId, CommandHint.Menu), validateHandler);
                        }
                    }
                }
                else
                {
                    priority += 100;
                }
            }
        }