Example #1
0
        public ContextMenu CreateContextMenu(CommandBarDefinitionBase contextMenuDefinition, IReadOnlyList <CommandBarGroupDefinition> groups, Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemsFunc)
        {
            var contextMenu = new ContextMenu(contextMenuDefinition);

            CreateRecursive(ref contextMenu, contextMenuDefinition, groups, itemsFunc);
            return(contextMenu);
        }
Example #2
0
 public DummyListMenuItem(CommandBarDefinitionBase commandDefinition, ItemsControl parent) : base(commandDefinition)
 {
     _parent = parent;
     CommandBarItemDefinition = commandDefinition;
     _listItems = new List <ItemsControl>();
     SetValue(VisibilityProperty, Visibility.Collapsed);
 }
Example #3
0
        public MenuItem CreateMenuItem(CommandBarDefinitionBase contextMenuDefinition, IReadOnlyList <CommandBarGroupDefinition> groups, Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemsFunc)
        {
            var menuItem = new MenuItem(contextMenuDefinition);

            CreateRecursive(ref menuItem, contextMenuDefinition, groups, itemsFunc);
            return(menuItem);
        }
Example #4
0
        public IEnumerable <CommandBarItemDefinition> GetSingleSubDefinitions(CommandBarDefinitionBase menuDefinition, IReadOnlyList <CommandBarGroupDefinition> groups,
                                                                              Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > items,
                                                                              CommandBarCreationOptions options = CommandBarCreationOptions.DisplaySeparatorsOnlyIfGroupNotEmpty)
        {
            var list = new List <CommandBarItemDefinition>();

            groups = groups.Where(x => x.Items.Any(y => y.IsVisible))
                     .OrderBy(x => x.SortOrder)
                     .ToList();
            for (var i = 0; i < groups.Count; i++)
            {
                var itemList = items.Invoke(groups[i]).ToList();
                if (i > 0 && i <= groups.Count - 1)
                {
                    if (options == CommandBarCreationOptions.DisplaySeparatorsOnlyIfGroupNotEmpty)
                    {
                        if (itemList.Any(x => x.IsVisible))
                        {
                            var separatorDefinition = CommandBarSeparatorDefinition.SeparatorDefinition;
                            separatorDefinition.Group = groups[i - 1];
                            list.Add(separatorDefinition);
                        }
                    }
                    else
                    {
                        var separatorDefinition = CommandBarSeparatorDefinition.SeparatorDefinition;
                        separatorDefinition.Group = groups[i - 1];
                        list.Add(separatorDefinition);
                    }
                }
                list.AddRange(itemList);
            }
            return(list);
        }
Example #5
0
        public override void CreateRecursive <T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition, IReadOnlyList <CommandBarGroupDefinition> groups,
                                                 Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemFunc)
        {
            var topItem = GetSingleSubDefinitions(itemDefinition, groups, itemFunc);

            if (!(itemDefinition is ToolbarDefinition))
            {
                return;
            }

            foreach (var item in topItem)
            {
                if (item is MenuDefinition)
                {
                    var list = new ObservableCollection <CommandBarItemDefinition>();
                    Fill(ref list, item, itemFunc);
                    item.Items = list;
                    itemsControl.Items.Add(new CommandDefinitionButton(item));
                }
                else
                {
                    itemsControl.Items.Add(new CommandDefinitionButton(item));
                }
            }
        }
Example #6
0
        public override void CreateRecursive <T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition, IReadOnlyList <CommandBarGroupDefinition> groups,
                                                 Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemFunc)
        {
            var host = IoC.Get <ICommandBarDefinitionHost>();

            var topItem = GetSingleSubDefinitions(itemDefinition, groups, itemFunc);

            foreach (var item in topItem)
            {
                MenuItem menuItemControl;
                if (item.CommandDefinition is CommandListDefinition)
                {
                    menuItemControl = new DummyListMenuItem(item, itemsControl);
                }
                else
                {
                    menuItemControl = new MenuItem(item);
                }

                if (item is MenuDefinition)
                {
                    groups = host.ItemGroupDefinitions.Where(x => x.Parent == item)
                             .Where(x => !host.ExcludedItemDefinitions.Contains(x))
                             .Where(x => x.Items.Any(y => y.IsVisible))
                             .OrderBy(x => x.SortOrder)
                             .ToList();
                    CreateRecursive(ref menuItemControl, item, groups, itemFunc);
                }

                itemsControl.Items.Add(menuItemControl);
            }
        }
Example #7
0
        public ContextMenu CreateContextMenu(CommandBarDefinitionBase contextMenuDefinition)
        {
            var contextMenu = new ContextMenu(contextMenuDefinition);

            CreateRecursive(ref contextMenu, contextMenuDefinition);
            return(contextMenu);
        }
Example #8
0
        /// <inheritdoc />
        /// <summary>
        /// Gets the single sub definitions.
        /// </summary>
        /// <param name="menuDefinition">The menu definition.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public IEnumerable <CommandBarItemDefinition> GetSingleSubDefinitions(CommandBarDefinitionBase menuDefinition,
                                                                              CommandBarCreationOptions options = CommandBarCreationOptions.DisplaySeparatorsOnlyIfGroupNotEmpty)
        {
            var host   = IoC.Get <ICommandBarDefinitionHost>();
            var groups = host.ItemGroupDefinitions.Where(x => x.Parent == menuDefinition)
                         .Where(x => !host.ExcludedItemDefinitions.Contains(x))
                         .Where(x => x.Items.Any(y => y.IsVisible))
                         .OrderBy(x => x.SortOrder)
                         .ToList();

            return(GetSingleSubDefinitions(menuDefinition, groups, group =>
            {
                return host.ItemDefinitions.Where(x => x.Group == group)
                .Where(x => !host.ExcludedItemDefinitions.Contains(x))
                .OrderBy(x => x.SortOrder).ToList();
            }));


            //var list = new List<CommandBarItemDefinition>();
            //var host = IoC.Get<ICommandBarDefinitionHost>();

            //var groups = host.ItemGroupDefinitions.Where(x => x.Parent == menuDefinition)
            //    .Where(x => !host.ExcludedItemDefinitions.Contains(x))
            //    .Where(x => x.Items.Any(y => y.IsVisible))
            //    .OrderBy(x => x.SortOrder)
            //    .ToList();
            //for (var i = 0; i < groups.Count; i++)
            //{
            //    var group = groups[i];
            //    var menuItems = host.ItemDefinitions.Where(x => x.Group == group)
            //        .Where(x => !host.ExcludedItemDefinitions.Contains(x))
            //        .OrderBy(x => x.SortOrder);
            //    if (i > 0 && i <= groups.Count - 1)
            //    {
            //        if (options == CommandBarCreationOptions.DisplaySeparatorsOnlyIfGroupNotEmpty)
            //        {
            //            if (menuItems.Any(x => x.IsVisible))
            //            {
            //                var separatorDefinition = CommandBarSeparatorDefinition.SeparatorDefinition;
            //                separatorDefinition.Group = groups[i - 1];
            //                list.Add(separatorDefinition);
            //            }
            //        }
            //        else
            //        {
            //            var separatorDefinition = CommandBarSeparatorDefinition.SeparatorDefinition;
            //            separatorDefinition.Group = groups[i - 1];
            //            list.Add(separatorDefinition);
            //        }
            //    }
            //    list.AddRange(menuItems);
            //}
            //return list;
        }
Example #9
0
        protected void RemoveGapsInGroupSortOrder(CommandBarDefinitionBase parent)
        {
            var groupsToChange = GetAllGroupsInParent(parent);

            uint newSortOrder = 0;

            foreach (var groupDefinition in groupsToChange)
            {
                groupDefinition.SortOrder = newSortOrder++;
            }
        }
Example #10
0
        public IReadOnlyList <CommandBarGroupDefinition> GetSortedGroupsOfDefinition(CommandBarDefinitionBase definition, bool onlyGroupsWithVisibleItems = true)
        {
            var groups = ItemGroupDefinitions.Where(x => x.Parent == definition)
                         .Where(x => !ExcludedItemDefinitions.Contains(x));

            if (onlyGroupsWithVisibleItems)
            {
                groups = groups.Where(x => x.Items.Any(y => y.IsVisible)).ToList();
            }
            return(groups.OrderBy(x => x.SortOrder).ToList());
        }
Example #11
0
        public int Compare(CommandBarDefinitionBase x, CommandBarDefinitionBase y)
        {
            if (!(x is TCommandBarDefinitionBase) || !(y is TCommandBarDefinitionBase))
            {
                throw new ArgumentException("This converter does not support the provided objects");
            }

            var sortOrder1 = x.SortOrder;
            var sortOrder2 = y.SortOrder;

            return(sortOrder1.CompareTo(sortOrder2));
        }
Example #12
0
        public static CommandBarDefinitionBase FindRoot(CommandBarDefinitionBase definition)
        {
            if (!(definition is CommandBarItemDefinition) && !(definition is CommandBarGroupDefinition))
            {
                return(definition);
            }
            if (definition is CommandBarGroupDefinition groupDefinition)
            {
                return(FindRoot(groupDefinition.Parent));
            }
            var itemDefinition = (CommandBarItemDefinition)definition;

            return(FindRoot(itemDefinition.Group.Parent));
        }
Example #13
0
        public void BuildLogical(CommandBarDefinitionBase definition)
        {
            var groups = DefinitionHost.ItemGroupDefinitions.Where(x => x.Parent == definition)
                         .Where(x => !DefinitionHost.ExcludedItemDefinitions.Contains(x))
                         .OrderBy(x => x.SortOrder)
                         .ToList();

            if (definition is MenuDefinition && groups.Count == 0)
            {
                definition.IsEnabled = false;
            }
            else
            {
                definition.IsEnabled = true;
            }

            var  veryFirstItem     = true;
            uint newGroupSortOrder = 0;

            for (var i = 0; i < groups.Count; i++)
            {
                var group = groups[i];
                group.SortOrder = newGroupSortOrder++;
                var menuItems = group.Items
                                .Where(x => !DefinitionHost.ExcludedItemDefinitions.Contains(x))
                                .OrderBy(x => x.SortOrder);


                var precededBySeparator = false;
                if (i > 0 && i <= groups.Count - 1 && menuItems.Any())
                {
                    if (menuItems.Any(menuItemDefinition => menuItemDefinition.IsVisible))
                    {
                        precededBySeparator = true;
                    }
                }
                uint newSortOrder = 0;

                foreach (var menuItemDefinition in menuItems)
                {
                    menuItemDefinition.PrecededBySeparator = precededBySeparator;
                    precededBySeparator = false;
                    BuildLogical(menuItemDefinition);
                    menuItemDefinition.SortOrder   = newSortOrder++;
                    menuItemDefinition.IsVeryFirst = veryFirstItem;
                    veryFirstItem = false;
                }
            }
        }
Example #14
0
        protected void StepwiseMoveDown(CommandBarItemDefinition item, CommandBarDefinitionBase parent)
        {
            var veryLastGroup = GetLastGroupDefinitionInParent(parent);
            var veryLastItem  = veryLastGroup.LastItem;

            if (veryLastItem == item)
            {
                return;
            }

            if (item.CommandDefinition.ControlType == CommandControlTypes.Separator)
            {
                var nextGroup = GetNextGroup(item.Group);
                var nextItem  = nextGroup.FirstItem;

                StepwiseMoveUp(nextItem, parent);
            }
            else
            {
                var lastItemIndex = item.Group.LastItem?.SortOrder;

                if (lastItemIndex == item.SortOrder)
                {
                    //Add to a new Group
                    var nextGroup = GetNextGroup(item.Group);
                    var lastGroup = item.Group;
                    item.Group     = nextGroup;
                    item.SortOrder = 0;
                    AdjustItemsAfterItemInsertedInGroup(item);
                    if (DefinitionHost.ItemDefinitions.All(x => x.Group != lastGroup))
                    {
                        DeleteGroup(lastGroup);
                    }
                }
                else
                {
                    var nextItem = GetNextItemInGroup(item);
                    if (nextItem == null)
                    {
                        return;
                    }
                    nextItem.SortOrder = item.SortOrder;
                    item.SortOrder     = item.SortOrder + 1;
                }
            }
        }
Example #15
0
        public void BuildLogical(CommandBarDefinitionBase definition, IReadOnlyList <CommandBarGroupDefinition> groups, Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemFunc)
        {
            var topGroups = groups.Where(x => x.Parent == definition)
                            .OrderBy(x => x.SortOrder)
                            .ToList();

            if (definition is MenuDefinition && !groups.Any())
            {
                definition.IsEnabled = false;
            }
            else
            {
                definition.IsEnabled = true;
            }

            var  veryFirstItem     = true;
            uint newGroupSortOrder = 0;

            for (var i = 0; i < topGroups.Count; i++)
            {
                var group = groups[i];
                group.SortOrder = newGroupSortOrder++;

                var items = itemFunc(group);

                var precededBySeparator = false;
                if (i > 0 && i <= groups.Count - 1 && items.Any())
                {
                    if (items.Any(menuItemDefinition => menuItemDefinition.IsVisible))
                    {
                        precededBySeparator = true;
                    }
                }
                uint newSortOrder = 0;

                foreach (var menuItemDefinition in items)
                {
                    menuItemDefinition.PrecededBySeparator = precededBySeparator;
                    precededBySeparator = false;
                    BuildLogical(menuItemDefinition, groups, itemFunc);
                    menuItemDefinition.SortOrder   = newSortOrder++;
                    menuItemDefinition.IsVeryFirst = veryFirstItem;
                    veryFirstItem = false;
                }
            }
        }
Example #16
0
        protected void StepwiseMoveUp(CommandBarItemDefinition item, CommandBarDefinitionBase parent)
        {
            if (item.IsVeryFirst)
            {
                return;
            }
            if (item.CommandDefinition.ControlType == CommandControlTypes.Separator)
            {
                var lastItem = item.Group.LastItem;
                StepwiseMoveDown(lastItem, parent);
            }
            else
            {
                if (item.SortOrder == 0)
                {
                    var previousGroup = GetPreviousGroup(item.Group);
                    var lastGroup     = item.Group;
                    item.Group     = previousGroup;
                    item.SortOrder = previousGroup.LastItem.SortOrder + 1;

                    var itemsToChange = lastGroup.Items;
                    if (!itemsToChange.Any())
                    {
                        DeleteGroup(lastGroup);
                    }
                    else
                    {
                        foreach (var itemDefinition in itemsToChange.ToList())
                        {
                            itemDefinition.SortOrder--;
                        }
                    }
                }
                else
                {
                    var previousItem = GetPreviousItemInGroup(item);
                    if (previousItem == null)
                    {
                        return;
                    }
                    previousItem.SortOrder = item.SortOrder;
                    item.SortOrder         = item.SortOrder - 1;
                }
            }
        }
Example #17
0
        public virtual void AddItemDefinition(CommandBarItemDefinition definition, CommandBarDefinitionBase parent,
                                              bool addAboveSeparator)
        {
            //Apparently the current parent is empty so we need to add a group first
            if (definition.Group == null)
            {
                var group = new CommandBarGroupDefinition(parent, uint.MinValue);
                definition.Group = group;
                DefinitionHost.ItemGroupDefinitions.AddSorted(group, new SortOrderComparer <CommandBarDefinitionBase>());
            }

            if (!addAboveSeparator)
            {
                AdjustItemsAfterItemInsertedInGroup(definition);
            }
            DefinitionHost.ItemDefinitions.AddSorted(definition, new SortOrderComparer <CommandBarDefinitionBase>());
            RemoveGapsInGroupSortOrder(definition.Group.Parent);
            BuildLogical(definition);
        }
Example #18
0
        public override void Build(CommandBarDefinitionBase definition)
        {
            if (!(definition is Definitions.ContextMenu.ContextMenuDefinition contextMenuDefinition))
            {
                return;
            }
            BuildLogical(contextMenuDefinition);

            var groups      = DefinitionHost.GetSortedGroupsOfDefinition(definition);
            var contextMenu = IoC.Get <IContextMenuCreator>().CreateContextMenu(definition, groups, DefinitionHost.GetItemsOfGroup);

            if (!_hostedContextMenus.ContainsKey(contextMenuDefinition))
            {
                _hostedContextMenus.Add(contextMenuDefinition, contextMenu);
            }
            else
            {
                _hostedContextMenus[contextMenuDefinition] = contextMenu;
            }
        }
Example #19
0
        protected IEnumerable <CommandBarDefinitionBase> GetSubHeaderMenus(CommandBarDefinitionBase definition)
        {
            var list = new List <CommandBarDefinitionBase>();

            var groups = DefinitionHost.ItemGroupDefinitions.Where(x => x.Parent == definition);

            foreach (var groupDefinition in groups)
            {
                var headerMenus = DefinitionHost.ItemDefinitions.Where(x => x is MenuDefinition)
                                  .Where(x => x.Group == groupDefinition)
                                  .OrderBy(x => x.SortOrder);

                foreach (var headerMenu in headerMenus)
                {
                    list.Add(headerMenu);
                    list.AddRange(GetSubHeaderMenus(headerMenu));
                }
            }
            return(list);
        }
Example #20
0
        public void MoveItem(CommandBarItemDefinition item, int offset, CommandBarDefinitionBase parent)
        {
            var sepcounter = Math.Abs(offset);

            if (offset == 0)
            {
                return;
            }

            for (var i = 0; i < sepcounter; i++)
            {
                if (offset < 0)
                {
                    StepwiseMoveUp(item, parent);
                }
                else
                {
                    StepwiseMoveDown(item, parent);
                }
            }
            BuildLogical(parent);
        }
Example #21
0
        //public override void CreateRecursive<T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition)
        //{
        //    var topItem = GetSingleSubDefinitions(itemDefinition);
        //    if (!(itemDefinition is ToolbarDefinition))
        //        return;
        //    foreach (var item in topItem)
        //    {
        //        if (item is MenuDefinition)
        //        {
        //            var list = new ObservableCollection<CommandBarItemDefinition>();
        //            Fill(ref list, item);
        //            item.Items = list;
        //            itemsControl.Items.Add(new CommandDefinitionButton(item));
        //        }
        //        else
        //            itemsControl.Items.Add(new CommandDefinitionButton(item));
        //    }
        //}

        //public void CreateToolbarDefinition<T>(ref T itemsControl, ToolbarDefinition toolbarDefinition,
        //    Func<IReadOnlyCollection<CommandBarGroupDefinition>> groupAction) where T : ItemsControl
        //{

        //    var groups = groupAction();

        //    var topItem = GetSingleSubDefinitions(toolbarDefinition, groups.ToList(), group => groups.SelectMany(x => x.Items).ToList());

        //    foreach (var item in topItem)
        //    {
        //        if (item is MenuDefinition)
        //        {
        //            var list = new ObservableCollection<CommandBarItemDefinition>();
        //            Fill(ref list, item, );
        //            item.Items = list;
        //            itemsControl.Items.Add(new CommandDefinitionButton(item));
        //        }
        //        else
        //            itemsControl.Items.Add(new CommandDefinitionButton(item));
        //    }
        //}

        private void Fill(ref ObservableCollection <CommandBarItemDefinition> list, CommandBarDefinitionBase item,
                          Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemFunc)
        {
            var host  = IoC.Get <ICommandBarDefinitionHost>();
            var group = host.ItemGroupDefinitions.Where(x => x.Parent == item)
                        .Where(x => !host.ExcludedItemDefinitions.Contains(x))
                        .Where(x => x.Items.Any(y => y.IsVisible))
                        .OrderBy(x => x.SortOrder)
                        .ToList();


            var defs = GetSingleSubDefinitions(item, group, itemFunc);

            foreach (var def in defs)
            {
                list.Add(def);
                if (def is MenuDefinition)
                {
                    var newList = new ObservableCollection <CommandBarItemDefinition>();
                    Fill(ref newList, def, itemFunc);
                    def.Items = newList;
                }
            }
        }
Example #22
0
        /// <summary>
        /// Creates a sub-tree of an <see cref="T:System.Windows.Controls.ItemsControl" /> recursively
        /// </summary>
        /// <typeparam name="T">The type of the <see cref="T:System.Windows.Controls.ItemsControl" /></typeparam>
        /// <param name="itemsControl">The <see cref="T:System.Windows.Controls.ItemsControl" /> that should be filled</param>
        /// <param name="itemDefinition">The data model of the current item</param>
        /// <inheritdoc />
        public override void CreateRecursive <T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition)
        {
            var topItem = GetSingleSubDefinitions(itemDefinition);

            foreach (var item in topItem)
            {
                MenuItem menuItemControl;
                if (item.CommandDefinition is CommandListDefinition)
                {
                    menuItemControl = new DummyListMenuItem(item, itemsControl);
                }
                else
                {
                    menuItemControl = new MenuItem(item);
                }

                if (item is MenuDefinition)
                {
                    CreateRecursive(ref menuItemControl, item);
                }

                itemsControl.Items.Add(menuItemControl);
            }
        }
Example #23
0
 public override void CreateRecursive <T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition)
 {
 }
Example #24
0
 public abstract void Build(CommandBarDefinitionBase definition);
Example #25
0
 public CommandDefinitionButton(CommandBarDefinitionBase definition) : this()
 {
     DataContext = definition;
 }
Example #26
0
 public ToolBar(CommandBarDefinitionBase definition) : this()
 {
     DataContext = definition;
 }
Example #27
0
 public abstract void CreateRecursive <T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition,
                                          IReadOnlyList <CommandBarGroupDefinition> groups, Func <CommandBarGroupDefinition, IReadOnlyList <CommandBarItemDefinition> > itemFunc) where T : ItemsControl;
Example #28
0
 public ExcludeCommandBarElementDefinition(CommandBarDefinitionBase definition)
 {
     ExcludedCommandBarDefinition = definition;
 }
Example #29
0
 public MenuItem(CommandBarDefinitionBase definitionBase)
 {
     DataContext = definitionBase;
 }
Example #30
0
 /// <inheritdoc />
 /// <summary>
 /// Creates a sub-tree of an <see cref="T:System.Windows.Controls.ItemsControl" /> recursively
 /// </summary>
 /// <typeparam name="T">The type of the <see cref="T:System.Windows.Controls.ItemsControl" /></typeparam>
 /// <param name="itemsControl">The <see cref="T:System.Windows.Controls.ItemsControl" /> that should be filled</param>
 /// <param name="itemDefinition">The data model of the current item</param>
 public abstract void CreateRecursive <T>(ref T itemsControl, CommandBarDefinitionBase itemDefinition)
     where T : ItemsControl;