Beispiel #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;
             }
         }
     }
 }
Beispiel #2
0
 private bool IsSelected(ConsoleMenuItem menuItem)
 {
     if (menuItem == SelectedItem)
     {
         return(true);
     }
     return(false);
 }
Beispiel #3
0
        private void OnMouseClicked(object sender, MouseEventArgs e)
        {
            var mouseOverElement = GetMouseOverElement(e);

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

            return("[Item is disabled]");
        }
Beispiel #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;
                }
            }
        }
Beispiel #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);
        }
Beispiel #7
0
        private void SwapExpand(ConsoleMenuItem sender)
        {
            if (Parent == null)
            {
                return;
            }

            if (IsExpanded)
            {
                Collapse();
            }
            else
            {
                Expand(false);
            }
        }
Beispiel #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);
            }
        }
Beispiel #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;
                }
            }
        }
Beispiel #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();
             }
         }
     }
 }
Beispiel #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();
            }
        }
Beispiel #12
0
 private bool CanExecute(ConsoleMenuItem menuItem)
 {
     return(menuItem.CanExecute());
 }
Beispiel #13
0
 public void Insert(int index, ConsoleMenuItem item)
 {
     root.Items.Insert(index, item);
     item.Parent = root;
     item.Menu   = this;
 }
Beispiel #14
0
 public bool Contains(ConsoleMenuItem item)
 {
     return(root.Items.Contains(item));
 }
Beispiel #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);
                }
            }
        }