Esempio n. 1
0
 private void SelectPrevious()
 {
     if (SelectedItem == null)
     {
         SelectedItem = root.Items.OfType <ConsoleMenuItem>().LastOrDefault();
     }
     else
     {
         var previousItem = SelectedItem.Previous();
         if (previousItem != null)
         {
             if (previousItem == root)
             {
                 if (CircularSelection)
                 {
                     SelectedItem = root.Items.OfType <ConsoleMenuItem>().LastOrDefault();
                 }
             }
             else
             {
                 SelectedItem = previousItem;
             }
         }
     }
 }
Esempio n. 2
0
 private bool IsSelected(ConsoleMenuItem menuItem)
 {
     if (menuItem == SelectedItem)
     {
         return(true);
     }
     return(false);
 }
Esempio n. 3
0
        private void OnMouseClicked(object sender, MouseEventArgs e)
        {
            var mouseOverElement = GetMouseOverElement(e);

            if (mouseOverElement != null)
            {
                SelectedItem = mouseOverElement.MenuItem as ConsoleMenuItem;
            }
        }
Esempio n. 4
0
        private static string DisabledHint(ConsoleMenuItem menuItem)
        {
            if (menuItem.DisabledHint != null)
            {
                return(menuItem.DisabledHint);
            }

            return("[Item is disabled]");
        }
Esempio n. 5
0
        private void OnMouseMoved(object sender, MouseEventArgs e)
        {
            var mouseOverElement = GetMouseOverElement(e);

            if (MouseMode == MouseMode.Hover)
            {
                UpdateMouseOver(mouseOverElement);
            }
            else if (MouseMode == MouseMode.Select)
            {
                if (mouseOverElement != null)
                {
                    SelectedItem = mouseOverElement.MenuItem as ConsoleMenuItem;
                }
            }
        }
Esempio n. 6
0
        public void Add(PrintableItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (SelectedItem == null)
            {
                selectedItem = item as ConsoleMenuItem;
            }

            item.Parent = root;
            item.Menu   = this;
            root.Items.Add(item);
        }
Esempio n. 7
0
        private void SwapExpand(ConsoleMenuItem sender)
        {
            if (Parent == null)
            {
                return;
            }

            if (IsExpanded)
            {
                Collapse();
            }
            else
            {
                Expand(false);
            }
        }
Esempio n. 8
0
        private void RefreshMenuItem(ConsoleMenuItem itemToUpdate, bool isSelected, bool showHint = false)
        {
            if (itemToUpdate == null)
            {
                return;
            }

            ElementInfo elementToUpdate;

            if (elements.TryGetValue(itemToUpdate, out elementToUpdate))
            {
                elementToUpdate.IsSelected = isSelected;
                Console.CursorTop          = elementToUpdate.Line;
                Console.CursorLeft         = 0;

                PrintSelector(elementToUpdate);
                PrintElement(elementToUpdate);
                PrintHint(elementToUpdate, showHint);
            }
        }
Esempio n. 9
0
        private void Execute(ConsoleMenuItem menuItem)
        {
            if (!menuItem.CanExecute())
            {
                RefreshMenuItem(menuItem, true, true);
                return;
            }

            var consoleWasCleared = ClearOnExecution;

            if (consoleWasCleared)
            {
                Console.Clear();
            }

            try
            {
                menuItem.Execute();

                if (consoleWasCleared)
                {
                    RefreshMenu();
                }
            }
            catch (Exception ex)
            {
                var handler = ExecutionError;
                if (handler == null)
                {
                    throw;
                }

                var args = new ExceptionEventArgs(ex);
                handler(this, args);
                if (!args.Handled)
                {
                    throw;
                }
            }
        }
Esempio n. 10
0
 private void SelectNext()
 {
     if (SelectedItem == null)
     {
         SelectedItem = root.Items.OfType <ConsoleMenuItem>().FirstOrDefault();
     }
     else
     {
         var nextItem = SelectedItem.Next();
         if (nextItem != null)
         {
             SelectedItem = nextItem;
         }
         else
         {
             if (CircularSelection)
             {
                 SelectedItem = root.Items.OfType <ConsoleMenuItem>().FirstOrDefault();
             }
         }
     }
 }
Esempio n. 11
0
        private void Collapse(bool selectParentWhenCollapsed)
        {
            if (SelectedItem == null)
            {
                return;
            }

            if (!SelectedItem.IsExpanded)
            {
                if (SelectedItem.Parent == root)
                {
                    return;
                }

                if (selectParentWhenCollapsed)
                {
                    SelectedItem = (ConsoleMenuItem)SelectedItem.Parent;
                }
            }
            else
            {
                SelectedItem.Collapse();
            }
        }
Esempio n. 12
0
 private bool CanExecute(ConsoleMenuItem menuItem)
 {
     return(menuItem.CanExecute());
 }
Esempio n. 13
0
 public void Insert(int index, ConsoleMenuItem item)
 {
     root.Items.Insert(index, item);
     item.Parent = root;
     item.Menu   = this;
 }
Esempio n. 14
0
 public bool Contains(ConsoleMenuItem item)
 {
     return(root.Items.Contains(item));
 }
Esempio n. 15
0
        private void UpdateSelection(ConsoleKeyInfo lastKey, string input)
        {
            switch (lastKey.Key)
            {
            case ConsoleKey.F5:
                RefreshMenu();
                return;

            case ConsoleKey.DownArrow:
                SelectNext();
                return;

            case ConsoleKey.UpArrow:
                SelectPrevious();
                return;

            case ConsoleKey.RightArrow:
                Expand(true, false);
                return;

            case ConsoleKey.Add:
                Expand(false, false);
                break;

            case ConsoleKey.Multiply:
                Expand(false, true);
                break;

            case ConsoleKey.LeftArrow:
                Collapse(true);
                return;

            case ConsoleKey.Subtract:
                Collapse(false);
                break;

            case ConsoleKey.Backspace:
                if (SelectedItem == null || SelectedItem.Parent == root)
                {
                    return;
                }

                SelectedItem = (ConsoleMenuItem)SelectedItem.Parent;
                break;

            case ConsoleKey.Home:
                SelectedItem = root.Items.OfType <ConsoleMenuItem>().FirstOrDefault();
                return;

            case ConsoleKey.End:
                SelectedItem = elements.Values.LastOrDefault()?.MenuItem as ConsoleMenuItem;
                ;
                return;
            }

            if (!IndexMenuItems)
            {
                return;
            }

            ElementInfo element;

            if (indexMap.TryGetValue(input, out element))
            {
                SelectedItem = element.MenuItem as ConsoleMenuItem;
                ;

                if (ExecuteOnIndexSelection && SelectedItem != null)
                {
                    if (SelectedItem.IsExpanded)
                    {
                        SelectedItem.Collapse();
                    }

                    Execute(SelectedItem);
                }
            }
        }