Example #1
0
 //--------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance of the <see cref="MenuManager"/> class.
 /// </summary>
 public MenuManager()
 {
     _coerceVisibilityAction = CoerceVisibility;
     Nodes = new MergeableNodeCollection<ICommandItem>();
     CommandItems = new List<ICommandItem>();
     Menu = new MenuItemViewModelCollection();
 }
Example #2
0
        //--------------------------------------------------------------
        #region Creation & Cleanup
        //--------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="TextDocumentViewModel"/> class.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="document"/> is <see langword="null"/>.
        /// </exception>
        public TextDocumentViewModel(TextDocument document)
            : base(document)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var editor = document.Editor;

            // Mandatory services.
            _textService   = editor.Services.GetInstance <ITextService>().ThrowIfMissing();
            _windowService = editor.Services.GetInstance <IWindowService>().ThrowIfMissing();

            // Optional services.
            _searchService       = editor.Services.GetInstance <ISearchService>().WarnIfMissing();
            _highlightingService = editor.Services.GetInstance <IHighlightingService>().WarnIfMissing();

            _textContextMenu      = _textService.ContextMenu;
            PrintPreviewCommand   = new DelegateCommand(ShowPrintPreview);
            PrintCommand          = new DelegateCommand(Print);
            FindCommand           = new DelegateCommand(Find, CanFind);
            FindAndReplaceCommand = new DelegateCommand(FindAndReplace);
            CancelCommand         = new DelegateCommand(Cancel);
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MenuManager"/> class.
 /// </summary>
 public MenuManager()
 {
     _coerceVisibilityAction = CoerceVisibility;
     Nodes        = new MergeableNodeCollection <ICommandItem>();
     CommandItems = new List <ICommandItem>();
     Menu         = new MenuItemViewModelCollection();
 }
Example #4
0
        protected override void OnActivated(ActivationEventArgs eventArgs)
        {
            if (eventArgs.Opened)
            {
                // We cannot do this in the ctor because the output view model is created before
                // all other services are registered.
                // --> To improve this, let the OutputExtension implement IOutputService.
                // Or do not register an instance in the service container - only the type which is
                // then created by the service container on-demand.

                // We can use the context menu of the text service, but then we also have menu items
                // for syntax highlighting and formatting.
                //TextContextMenu = _editor?.Services.GetInstance<ITextService>()?.ContextMenu;

                var commandExtension = _editor.Extensions.OfType <CommandExtension>().ThrowIfMissing().First();
                TextContextMenu = new MenuItemViewModelCollection
                {
                    commandExtension.CommandItems["Cut"].CreateMenuItem(),
                    commandExtension.CommandItems["Copy"].CreateMenuItem(),
                    commandExtension.CommandItems["Paste"].CreateMenuItem(),
                    commandExtension.CommandItems["Delete"].CreateMenuItem(),
                    new CommandSeparator("ClipboardSeparator").CreateMenuItem(),
                    commandExtension.CommandItems["SelectAll"].CreateMenuItem(),
                    new CommandSeparator("SelectSeparator").CreateMenuItem()
                };
            }

            base.OnActivated(eventArgs);
        }
Example #5
0
        private static void OutputMenuNodes(IOutputService outputService, MenuItemViewModelCollection menuItems, int level = 0)
        {
            if (menuItems == null || menuItems.Count == 0)
            {
                return;
            }

            var indent = Indent(level);

            foreach (var menuItem in menuItems)
            {
                outputService.WriteLine(Invariant($"{indent}\"{menuItem.CommandItem.Name}\""), NodesView);
                OutputMenuNodes(outputService, menuItem.Submenu, level + 1);
            }
        }
Example #6
0
        //--------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="TextDocumentViewModel"/> class.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="document"/> is <see langword="null"/>.
        /// </exception>
        public TextDocumentViewModel(TextDocument document)
            : base(document)
        {
            if (document == null)
                throw new ArgumentNullException(nameof(document));

            var editor = document.Editor;

            // Mandatory services.
            _textService = editor.Services.GetInstance<ITextService>().ThrowIfMissing();
            _windowService = editor.Services.GetInstance<IWindowService>().ThrowIfMissing();

            // Optional services.
            _searchService = editor.Services.GetInstance<ISearchService>().WarnIfMissing();
            _highlightingService = editor.Services.GetInstance<IHighlightingService>().WarnIfMissing();

            _textContextMenu = _textService.ContextMenu;
            PrintPreviewCommand = new DelegateCommand(ShowPrintPreview);
            PrintCommand = new DelegateCommand(Print);
            FindCommand = new DelegateCommand(Find, CanFind);
            FindAndReplaceCommand = new DelegateCommand(FindAndReplace);
            CancelCommand = new DelegateCommand(Cancel);
        }
Example #7
0
        private static void CreateMenuItems(MergeableNodeCollection<ICommandItem> nodes, MenuItemViewModelCollection menuItems)
        {
            foreach (var node in nodes)
            {
                if (node?.Content == null)
                    continue;

                var menuItem = node.Content.CreateMenuItem();
                menuItems.Add(menuItem);

                if (node.Children != null && node.Children.Count > 0)
                {
                    menuItem.Submenu = new MenuItemViewModelCollection();
                    CreateMenuItems(node.Children, menuItem.Submenu);
                }
            }
        }
Example #8
0
        private static void CoerceVisibility(MenuItemViewModelCollection menuItems)
        {
            if (menuItems == null || menuItems.Count == 0)
                return;

            // ----- Hide empty sub-menus.
            for (int i = 0; i < menuItems.Count; i++)
            {
                var menuItem = menuItems[i];
                if (menuItem is MenuSeparatorViewModel)
                    continue;

                // Recursively coerce visibility in children.
                CoerceVisibility(menuItem.Submenu);

                menuItem.IsVisible = menuItem.CommandItem.IsVisible
                                     && (menuItem.Submenu == null || menuItem.Submenu.Any(mi => mi.IsVisible));
            }

            // ----- Hide unnecessary separators.
            bool hasVisiblePredecessor = false;
            for (int i = 0; i < menuItems.Count; i++)
            {
                var menuItem = menuItems[i];

                var menuSeparator = menuItem as MenuSeparatorViewModel;
                if (menuSeparator == null)
                {
                    // Menu item is not a separator.
                    if (menuItem.IsVisible)
                        hasVisiblePredecessor = true;

                    continue;
                }

                // Menu item is a separator.
                // A separator is only visible if there are visible items before and after.
                if (!hasVisiblePredecessor)
                {
                    menuSeparator.IsVisible = false;
                    continue;
                }

                bool hasVisibleSuccessor = false;
                for (int j = i + 1; j < menuItems.Count; j++)
                {
                    var successor = menuItems[j];

                    // Skip separators.
                    if (successor.CommandItem is CommandSeparator)
                        continue;

                    if (!successor.IsVisible)
                        continue;

                    hasVisibleSuccessor = true;
                    break;
                }

                if (!hasVisibleSuccessor)
                {
                    menuSeparator.IsVisible = false;
                    continue;
                }

                menuSeparator.IsVisible = true;
                hasVisiblePredecessor = false;
            }
        }
Example #9
0
        protected override void OnActivated(ActivationEventArgs eventArgs)
        {
            if (eventArgs.Opened)
            {
                // We cannot do this in the ctor because the output view model is created before
                // all other services are registered.
                // --> To improve this, let the OutputExtension implement IOutputService.
                // Or do not register an instance in the service container - only the type which is
                // then created by the service container on-demand.

                // We can use the context menu of the text service, but then we also have menu items
                // for syntax highlighting and formatting.
                //TextContextMenu = _editor?.Services.GetInstance<ITextService>()?.ContextMenu;

                var commandExtension = _editor.Extensions.OfType<CommandExtension>().ThrowIfMissing().First();
                TextContextMenu = new MenuItemViewModelCollection
                {
                    commandExtension.CommandItems["Cut"].CreateMenuItem(),
                    commandExtension.CommandItems["Copy"].CreateMenuItem(),
                    commandExtension.CommandItems["Paste"].CreateMenuItem(),
                    commandExtension.CommandItems["Delete"].CreateMenuItem(),
                    new CommandSeparator("ClipboardSeparator").CreateMenuItem(),
                    commandExtension.CommandItems["SelectAll"].CreateMenuItem(),
                    new CommandSeparator("SelectSeparator").CreateMenuItem()
                };
            }

            base.OnActivated(eventArgs);
        }
Example #10
0
        private static void CoerceVisibility(MenuItemViewModelCollection menuItems)
        {
            if (menuItems == null || menuItems.Count == 0)
            {
                return;
            }

            // ----- Hide empty sub-menus.
            for (int i = 0; i < menuItems.Count; i++)
            {
                var menuItem = menuItems[i];
                if (menuItem is MenuSeparatorViewModel)
                {
                    continue;
                }

                // Recursively coerce visibility in children.
                CoerceVisibility(menuItem.Submenu);

                menuItem.IsVisible = menuItem.CommandItem.IsVisible &&
                                     (menuItem.Submenu == null || menuItem.Submenu.Any(mi => mi.IsVisible));
            }

            // ----- Hide unnecessary separators.
            bool hasVisiblePredecessor = false;

            for (int i = 0; i < menuItems.Count; i++)
            {
                var menuItem = menuItems[i];

                var menuSeparator = menuItem as MenuSeparatorViewModel;
                if (menuSeparator == null)
                {
                    // Menu item is not a separator.
                    if (menuItem.IsVisible)
                    {
                        hasVisiblePredecessor = true;
                    }

                    continue;
                }

                // Menu item is a separator.
                // A separator is only visible if there are visible items before and after.
                if (!hasVisiblePredecessor)
                {
                    menuSeparator.IsVisible = false;
                    continue;
                }

                bool hasVisibleSuccessor = false;
                for (int j = i + 1; j < menuItems.Count; j++)
                {
                    var successor = menuItems[j];

                    // Skip separators.
                    if (successor.CommandItem is CommandSeparator)
                    {
                        continue;
                    }

                    if (!successor.IsVisible)
                    {
                        continue;
                    }

                    hasVisibleSuccessor = true;
                    break;
                }

                if (!hasVisibleSuccessor)
                {
                    menuSeparator.IsVisible = false;
                    continue;
                }

                menuSeparator.IsVisible = true;
                hasVisiblePredecessor   = false;
            }
        }
Example #11
0
        private static void CreateMenuItems(MergeableNodeCollection <ICommandItem> nodes, MenuItemViewModelCollection menuItems)
        {
            foreach (var node in nodes)
            {
                if (node?.Content == null)
                {
                    continue;
                }

                var menuItem = node.Content.CreateMenuItem();
                menuItems.Add(menuItem);

                if (node.Children != null && node.Children.Count > 0)
                {
                    menuItem.Submenu = new MenuItemViewModelCollection();
                    CreateMenuItems(node.Children, menuItem.Submenu);
                }
            }
        }
Example #12
0
        private static void OutputMenuNodes(IOutputService outputService, MenuItemViewModelCollection menuItems, int level = 0)
        {
            if (menuItems == null || menuItems.Count == 0)
                return;

            var indent = Indent(level);
            foreach (var menuItem in menuItems)
            {
                outputService.WriteLine(Invariant($"{indent}\"{menuItem.CommandItem.Name}\""), NodesView);
                OutputMenuNodes(outputService, menuItem.Submenu, level + 1);
            }
        }