예제 #1
0
        private void AddPluginIcon(MenuItem iconSubmenu, IPluginClient plugin)
        {
            Guid SubmenuGuid = plugin.Guid;

            // Protection against plugins reusing a guid...
            if (!IconSelectionTable.ContainsKey(SubmenuGuid))
            {
                RoutedUICommand SubmenuCommand = new RoutedUICommand();
                SubmenuCommand.Text = PluginManager.GuidToString(SubmenuGuid);

                // The currently preferred plugin will be checked as so.
                string SubmenuHeader    = plugin.Name;
                bool   SubmenuIsChecked = SubmenuGuid == PluginManager.PreferredPluginGuid;
                Bitmap SubmenuIcon      = plugin.SelectionBitmap;

                AddMenuCommand(SubmenuCommand, OnCommandSelectPreferred);
                MenuItem PluginMenu = CreateMenuItem(SubmenuCommand, SubmenuHeader, SubmenuIsChecked, SubmenuIcon);
                TaskbarIcon.PrepareMenuItem(PluginMenu, true, true);
                iconSubmenu.Items.Add(PluginMenu);

                IconSelectionTable.Add(SubmenuGuid, SubmenuCommand);
            }
        }
예제 #2
0
        private ContextMenu LoadContextMenu()
        {
            // Create the taskbar context menu and populate it with menu items, submenus and separators.
            ContextMenu    Result = new ContextMenu();
            ItemCollection Items  = Result.Items;

            MenuItem LoadAtStartup;
            string   ExeName = Assembly.GetExecutingAssembly().Location;

            // Create a menu item for the load at startup/remove from startup command, depending on the current situation.
            // UAC-16.png is the recommended 'shield' icon to indicate administrator mode is required for the operation.
            if (Scheduler.IsTaskActive(ExeName))
            {
                if (IsElevated)
                {
                    LoadAtStartup = CreateMenuItem(LoadAtStartupCommand, LoadAtStartupHeader, true, null);
                }
                else
                {
                    LoadAtStartup = CreateMenuItem(LoadAtStartupCommand, RemoveFromStartupHeader, false, LoadEmbeddedResource <Bitmap>("UAC-16.png"));
                }
            }
            else
            {
                if (IsElevated)
                {
                    LoadAtStartup = CreateMenuItem(LoadAtStartupCommand, LoadAtStartupHeader, false, null);
                }
                else
                {
                    LoadAtStartup = CreateMenuItem(LoadAtStartupCommand, LoadAtStartupHeader, false, LoadEmbeddedResource <Bitmap>("UAC-16.png"));
                }
            }

            TaskbarIcon.PrepareMenuItem(LoadAtStartup, true, true);
            Items.Add(LoadAtStartup);

            // Below load at startup, we add plugin menus.
            // Separate them in two categories, small and large. Small menus are always directly visible in the main context menu.
            // Large plugin menus, if there is more than one, have their own submenu. If there is just one plugin with a large menu we don't bother.

            Dictionary <List <MenuItem>, string> FullPluginMenuList = new Dictionary <List <MenuItem>, string>();
            int LargePluginMenuCount = 0;

            foreach (KeyValuePair <List <ICommand>, string> Entry in PluginManager.FullCommandList)
            {
                List <ICommand> FullPluginCommandList = Entry.Key;
                string          PluginName            = Entry.Value;

                List <MenuItem> PluginMenuList         = new List <MenuItem>();
                int             VisiblePluginMenuCount = 0;

                foreach (ICommand Command in FullPluginCommandList)
                {
                    if (Command == null)
                    {
                        PluginMenuList.Add(null); // This will result in the creation of a separator.
                    }
                    else
                    {
                        string MenuHeader    = PluginManager.GetMenuHeader(Command);
                        bool   MenuIsVisible = PluginManager.GetMenuIsVisible(Command);
                        bool   MenuIsEnabled = PluginManager.GetMenuIsEnabled(Command);
                        bool   MenuIsChecked = PluginManager.GetMenuIsChecked(Command);
                        Bitmap MenuIcon      = PluginManager.GetMenuIcon(Command);

                        MenuItem PluginMenu = CreateMenuItem(Command, MenuHeader, MenuIsChecked, MenuIcon);
                        TaskbarIcon.PrepareMenuItem(PluginMenu, MenuIsVisible, MenuIsEnabled);

                        PluginMenuList.Add(PluginMenu);

                        // Count how many visible items to decide if the menu is large or small.
                        if (MenuIsVisible)
                        {
                            VisiblePluginMenuCount++;
                        }
                    }
                }

                if (VisiblePluginMenuCount > 1)
                {
                    LargePluginMenuCount++;
                }

                FullPluginMenuList.Add(PluginMenuList, PluginName);
            }

            // Add small menus, then large menus.
            AddPluginMenuItems(Items, FullPluginMenuList, false, false);
            AddPluginMenuItems(Items, FullPluginMenuList, true, LargePluginMenuCount > 1);

            // If there are more than one plugin capable of receiving click notification, we must give the user a way to choose which.
            // For this purpose, create a "Icons" menu with a choice of plugins, with their name and preferred icon.
            if (PluginManager.ConsolidatedPluginList.Count > 1)
            {
                Items.Add(new Separator());

                MenuItem IconSubmenu = new MenuItem();
                IconSubmenu.Header = "Icons";

                foreach (IPluginClient Plugin in PluginManager.ConsolidatedPluginList)
                {
                    Guid SubmenuGuid = Plugin.Guid;
                    if (!IconSelectionTable.ContainsKey(SubmenuGuid)) // Protection against plugins reusing a guid...
                    {
                        RoutedUICommand SubmenuCommand = new RoutedUICommand();
                        SubmenuCommand.Text = PluginManager.GuidToString(SubmenuGuid);

                        string SubmenuHeader    = Plugin.Name;
                        bool   SubmenuIsChecked = (SubmenuGuid == PluginManager.PreferredPluginGuid); // The currently preferred plugin will be checked as so.
                        Bitmap SubmenuIcon      = Plugin.SelectionBitmap;

                        AddMenuCommand(SubmenuCommand, OnCommandSelectPreferred);
                        MenuItem PluginMenu = CreateMenuItem(SubmenuCommand, SubmenuHeader, SubmenuIsChecked, SubmenuIcon);
                        TaskbarIcon.PrepareMenuItem(PluginMenu, true, true);
                        IconSubmenu.Items.Add(PluginMenu);

                        IconSelectionTable.Add(SubmenuGuid, SubmenuCommand);
                    }
                }

                // Add this "Icons" menu to the main context menu.
                Items.Add(IconSubmenu);
            }

            // Always add a separator above the exit menu.
            Items.Add(new Separator());

            MenuItem ExitMenu = CreateMenuItem(ExitCommand, "Exit", false, null);

            TaskbarIcon.PrepareMenuItem(ExitMenu, true, true);
            Items.Add(ExitMenu);

            Logger.AddLog("Menu created");

            return(Result);
        }