Esempio n. 1
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 = LoadContextMenuFromActive();

            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.
            AddPluginMenuToContextMenu(out Dictionary <List <MenuItem?>, string> FullPluginMenuList, out int LargePluginMenuCount);

            // 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.
            SelectPreferredPluginContextMenu(Items);

            AddExitMenuToContextMenu(Items);

            Logger.AddLog("Menu created");

            return(Result);
        }
Esempio n. 2
0
        private void AddExitMenuToContextMenu(ItemCollection items)
        {
            // Add a separator above the exit menu if appropriate.
            if (items.Count > 0 && items[items.Count - 1] is not Separator)
            {
                items.Add(new Separator());
            }

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

            TaskbarIcon.PrepareMenuItem(ExitMenu, true, true);
            items.Add(ExitMenu);
        }
Esempio n. 3
0
        private static void AddPluginMenu(List <ICommand> fullPluginCommandList, string pluginName, Dictionary <List <MenuItem?>, string> fullPluginMenuList, ref int largePluginMenuCount)
        {
            List <MenuItem?> PluginMenuList = new List <MenuItem?>();
            int VisiblePluginMenuCount      = 0;

            foreach (ICommand Command in fullPluginCommandList)
            {
                if (PluginManager.IsSeparatorCommand(Command))
                {
                    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);
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
0
 private static void AddContextMenu(ContextMenu menu, MenuItem item, bool isVisible, bool isEnabled)
 {
     TaskbarIcon.PrepareMenuItem(item, isVisible, isEnabled);
     menu.Items.Add(item);
 }
Esempio n. 6
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);
        }