Exemplo n.º 1
0
        public static async Task SetCommandBarFlyoutWithExtraContextMenuItems(this ListViewBase ListControl, CommandBarFlyout Flyout, Windows.Foundation.Point?ShowAt = null)
        {
            if (Flyout == null)
            {
                throw new ArgumentNullException(nameof(Flyout), "Argument could not be null");
            }

            if (FullTrustProcessController.Current.IsNowHasAnyActionExcuting)
            {
                return;
            }

            try
            {
                ListControl.ContextFlyout = null;

                string SelectedPath;

                if (ListControl.SelectedItems.Count <= 1)
                {
                    if (ListControl.SelectedItem is FileSystemStorageItemBase Selected)
                    {
                        SelectedPath = Selected.Path;
                    }
                    else if (ListControl.FindParentOfType <FileControl>() is FileControl Control)
                    {
                        if (!string.IsNullOrEmpty(Control.CurrentFolder?.Path))
                        {
                            SelectedPath = Control.CurrentFolder.Path;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }

                    List <ContextMenuItem> ExtraMenuItems = await FullTrustProcessController.Current.GetContextMenuItemsAsync(SelectedPath, Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down)).ConfigureAwait(true);

                    if (ExtraMenuItems.Count > 0)
                    {
                        if (Flyout.SecondaryCommands.OfType <AppBarElementContainer>().FirstOrDefault() is AppBarElementContainer ExistsContainer)
                        {
                            StackPanel InnerPanel = ExistsContainer.Content as StackPanel;

                            List <ContextMenuItem> MenuExistItems = InnerPanel.Children.Select((Btn) => (Btn as Button).Tag as ContextMenuItem).ToList();

                            foreach (ContextMenuItem AddItem in ExtraMenuItems.Except(MenuExistItems))
                            {
                                Button Btn = await AddItem.GenerateUIButton().ConfigureAwait(true);

                                Btn.Click += async(s, e) =>
                                {
                                    Flyout?.Hide();

                                    if (((Button)s)?.Tag is ContextMenuItem MenuItem)
                                    {
                                        await MenuItem.Invoke().ConfigureAwait(true);
                                    }
                                };

                                InnerPanel.Children.Add(Btn);
                            }

                            foreach (ContextMenuItem RemoveItem in MenuExistItems.Except(ExtraMenuItems))
                            {
                                if (InnerPanel.Children.OfType <Button>().FirstOrDefault((Item) => (Item.Tag as ContextMenuItem) == RemoveItem) is Button Btn)
                                {
                                    InnerPanel.Children.Remove(Btn);
                                }
                            }

                            foreach (ContextMenuItem UpdateItem in MenuExistItems.Where((Item) => ExtraMenuItems.Any((Extra) => Extra.Equals(Item))))
                            {
                                UpdateItem.UpdateBelonging(SelectedPath);
                            }
                        }
                        else
                        {
                            StackPanel Panel = new StackPanel
                            {
                                HorizontalAlignment = HorizontalAlignment.Stretch
                            };

                            foreach (ContextMenuItem Item in ExtraMenuItems)
                            {
                                Button Btn = await Item.GenerateUIButton().ConfigureAwait(true);

                                Btn.Click += async(s, e) =>
                                {
                                    Flyout?.Hide();

                                    if (((Button)s)?.Tag is ContextMenuItem MenuItem)
                                    {
                                        await MenuItem.Invoke().ConfigureAwait(true);
                                    }
                                };

                                Panel.Children.Add(Btn);
                            }

                            AppBarElementContainer Container = new AppBarElementContainer
                            {
                                HorizontalContentAlignment = HorizontalAlignment.Stretch,
                                Content = Panel
                            };

                            List <int> SeparatorGroup = Flyout.SecondaryCommands.Select((Item, Index) => (Index, Item)).Where((Group) => Group.Item is AppBarSeparator).Select((Group) => Group.Index).ToList();

                            if (SeparatorGroup.Count > 0)
                            {
                                Flyout.SecondaryCommands.Insert(SeparatorGroup[0] + 1, new AppBarSeparator());
                                Flyout.SecondaryCommands.Insert(SeparatorGroup[0] + 1, Container);
                            }
                            else
                            {
                                Flyout.SecondaryCommands.Insert(0, new AppBarSeparator());
                                Flyout.SecondaryCommands.Insert(0, Container);
                            }
                        }
                    }
                    else
                    {
                        foreach (AppBarElementContainer ExistContainer in Flyout.SecondaryCommands.OfType <AppBarElementContainer>())
                        {
                            Flyout.SecondaryCommands.Remove(ExistContainer);
                        }

                        List <int> SeparatorGroup = Flyout.SecondaryCommands.Select((Item, Index) => (Index, Item)).Where((Group) => Group.Item is AppBarSeparator).Select((Group) => Group.Index).ToList();

                        if (SeparatorGroup.Count == 1)
                        {
                            if (SeparatorGroup[0] == 0)
                            {
                                Flyout.SecondaryCommands.RemoveAt(0);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < SeparatorGroup.Count - 1; i++)
                            {
                                if (Math.Abs(SeparatorGroup[i] - SeparatorGroup[i + 1]) == 1)
                                {
                                    Flyout.SecondaryCommands.RemoveAt(SeparatorGroup[i]);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogTracer.Log(ex);
            }
            finally
            {
                if (ShowAt != null)
                {
                    try
                    {
                        FlyoutShowOptions Option = new FlyoutShowOptions
                        {
                            Position  = ShowAt,
                            Placement = FlyoutPlacementMode.RightEdgeAlignedTop
                        };

                        Flyout?.ShowAt(ListControl, Option);
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "An exception was threw when trying show flyout");
                    }
                }
                else
                {
                    ListControl.ContextFlyout = Flyout;
                }
            }
        }
Exemplo n.º 2
0
        public static async Task SetCommandBarFlyoutWithExtraContextMenuItems(this ListViewBase ListControl, CommandBarFlyout Flyout, Point ShowAt)
        {
            if (Flyout == null)
            {
                throw new ArgumentNullException(nameof(Flyout), "Argument could not be null");
            }

            if (Interlocked.Exchange(ref ContextMenuLockResource, 1) == 0)
            {
                try
                {
                    if (ApplicationData.Current.LocalSettings.Values["ContextMenuExtSwitch"] is bool IsExt && !IsExt)
                    {
                        foreach (AppBarButton ExtraButton in Flyout.SecondaryCommands.OfType <AppBarButton>().Where((Btn) => Btn.Name == "ExtraButton").ToArray())
                        {
                            Flyout.SecondaryCommands.Remove(ExtraButton);
                        }

                        foreach (AppBarSeparator Separator in Flyout.SecondaryCommands.OfType <AppBarSeparator>().Where((Sep) => Sep.Name == "CustomSep").ToArray())
                        {
                            Flyout.SecondaryCommands.Remove(Separator);
                        }
                    }
                    else
                    {
                        string[] SelectedPathArray = null;

                        if (ListControl.SelectedItems.Count <= 1)
                        {
                            if (ListControl.SelectedItem is FileSystemStorageItemBase Selected)
                            {
                                SelectedPathArray = new string[] { Selected.Path };
                            }
                            else if (ListControl.FindParentOfType <FileControl>() is FileControl Control && !string.IsNullOrEmpty(Control.CurrentPresenter.CurrentFolder?.Path))
                            {
                                SelectedPathArray = new string[] { Control.CurrentPresenter.CurrentFolder.Path };
                            }
                        }
                        else
                        {
                            SelectedPathArray = ListControl.SelectedItems.OfType <FileSystemStorageItemBase>().Select((Item) => Item.Path).ToArray();
                        }

                        if (SelectedPathArray != null)
                        {
                            using (FullTrustProcessController.ExclusiveUsage Exclusive = await FullTrustProcessController.GetAvailableController())
                            {
                                List <ContextMenuItem> ExtraMenuItems = await Exclusive.Controller.GetContextMenuItemsAsync(SelectedPathArray, Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down)).ConfigureAwait(true);

                                foreach (AppBarButton ExtraButton in Flyout.SecondaryCommands.OfType <AppBarButton>().Where((Btn) => Btn.Name == "ExtraButton").ToArray())
                                {
                                    Flyout.SecondaryCommands.Remove(ExtraButton);
                                }

                                foreach (AppBarSeparator Separator in Flyout.SecondaryCommands.OfType <AppBarSeparator>().Where((Sep) => Sep.Name == "CustomSep").ToArray())
                                {
                                    Flyout.SecondaryCommands.Remove(Separator);
                                }

                                if (ExtraMenuItems.Count > 0)
                                {
                                    async void ClickHandler(object sender, RoutedEventArgs args)
                                    {
                                        if (sender is FrameworkElement Btn)
                                        {
                                            if (Btn.Tag is ContextMenuItem MenuItem)
                                            {
                                                Flyout.Hide();

                                                if (!await MenuItem.InvokeAsync().ConfigureAwait(true))
                                                {
                                                    QueueContentDialog Dialog = new QueueContentDialog
                                                    {
                                                        Title           = Globalization.GetString("Common_Dialog_ErrorTitle"),
                                                        Content         = Globalization.GetString("QueueDialog_InvokeContextMenuError_Content"),
                                                        CloseButtonText = Globalization.GetString("Common_Dialog_CloseButton")
                                                    };

                                                    await Dialog.ShowAsync().ConfigureAwait(true);
                                                }
                                            }
                                        }
                                    }

                                    short ShowExtNum = Convert.ToInt16(Math.Max(9 - Flyout.SecondaryCommands.Count((Item) => Item is AppBarButton), 0));

                                    int Index = Flyout.SecondaryCommands.IndexOf(Flyout.SecondaryCommands.OfType <AppBarSeparator>().FirstOrDefault()) + 1;

                                    if (ExtraMenuItems.Count > ShowExtNum + 1)
                                    {
                                        Flyout.SecondaryCommands.Insert(Index, new AppBarSeparator {
                                            Name = "CustomSep"
                                        });

                                        foreach (ContextMenuItem AddItem in ExtraMenuItems.Take(ShowExtNum))
                                        {
                                            Flyout.SecondaryCommands.Insert(Index, await AddItem.GenerateUIButtonAsync(ClickHandler).ConfigureAwait(true));
                                        }

                                        AppBarButton MoreItem = new AppBarButton
                                        {
                                            Label    = Globalization.GetString("CommandBarFlyout_More_Item"),
                                            Icon     = new SymbolIcon(Symbol.More),
                                            Name     = "ExtraButton",
                                            MinWidth = 250
                                        };

                                        MenuFlyout MoreFlyout = new MenuFlyout();

                                        await ContextMenuItem.GenerateSubMenuItemsAsync(MoreFlyout.Items, ExtraMenuItems.Skip(ShowExtNum).ToArray(), ClickHandler).ConfigureAwait(true);

                                        MoreItem.Flyout = MoreFlyout;

                                        Flyout.SecondaryCommands.Insert(Index + ShowExtNum, MoreItem);
                                    }
                                    else
                                    {
                                        foreach (ContextMenuItem AddItem in ExtraMenuItems)
                                        {
                                            Flyout.SecondaryCommands.Insert(Index, await AddItem.GenerateUIButtonAsync(ClickHandler).ConfigureAwait(true));
                                        }

                                        Flyout.SecondaryCommands.Insert(Index + ExtraMenuItems.Count, new AppBarSeparator {
                                            Name = "CustomSep"
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
Exemplo n.º 3
0
        public static async Task SetCommandBarFlyoutWithExtraContextMenuItems(this ListViewBase ListControl, CommandBarFlyout Flyout, Point ShowAt)
        {
            if (Flyout == null)
            {
                throw new ArgumentNullException(nameof(Flyout), "Argument could not be null");
            }

            if (Interlocked.Exchange(ref ContextMenuLockResource, 1) == 0)
            {
                try
                {
                    if (ApplicationData.Current.LocalSettings.Values["ContextMenuExtSwitch"] is bool IsExt && !IsExt)
                    {
                        foreach (AppBarElementContainer ExistContainer in Flyout.SecondaryCommands.OfType <AppBarElementContainer>())
                        {
                            Flyout.SecondaryCommands.Remove(ExistContainer);
                        }

                        List <int> SeparatorGroup = Flyout.SecondaryCommands.Select((Item, Index) => (Index, Item)).Where((Group) => Group.Item is AppBarSeparator).Select((Group) => Group.Index).ToList();

                        if (SeparatorGroup.Count == 1)
                        {
                            if (SeparatorGroup[0] == 0)
                            {
                                Flyout.SecondaryCommands.RemoveAt(0);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < SeparatorGroup.Count - 1; i++)
                            {
                                if (Math.Abs(SeparatorGroup[i] - SeparatorGroup[i + 1]) == 1)
                                {
                                    Flyout.SecondaryCommands.RemoveAt(SeparatorGroup[i]);
                                }
                            }
                        }
                    }
                    else
                    {
                        string SelectedPath;

                        if (ListControl.SelectedItems.Count <= 1)
                        {
                            if (ListControl.SelectedItem is FileSystemStorageItemBase Selected)
                            {
                                SelectedPath = Selected.Path;
                            }
                            else if (ListControl.FindParentOfType <FileControl>() is FileControl Control)
                            {
                                if (!string.IsNullOrEmpty(Control.CurrentPresenter.CurrentFolder?.Path))
                                {
                                    SelectedPath = Control.CurrentPresenter.CurrentFolder.Path;
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else
                            {
                                return;
                            }

                            using (FullTrustProcessController.ExclusiveUsage Exclusive = await FullTrustProcessController.GetAvailableController())
                            {
                                List <ContextMenuItem> ExtraMenuItems = await Exclusive.Controller.GetContextMenuItemsAsync(SelectedPath, Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down)).ConfigureAwait(true);

                                foreach (AppBarButton ExtraButton in Flyout.SecondaryCommands.OfType <AppBarButton>().Where((Btn) => Btn.Name == "ExtraButton").ToArray())
                                {
                                    Flyout.SecondaryCommands.Remove(ExtraButton);
                                }

                                foreach (AppBarSeparator Separator in Flyout.SecondaryCommands.OfType <AppBarSeparator>().Where((Sep) => Sep.Name == "CustomSep").ToArray())
                                {
                                    Flyout.SecondaryCommands.Remove(Separator);
                                }

                                if (ExtraMenuItems.Count > 0)
                                {
                                    async void ClickHandler(object sender, RoutedEventArgs args)
                                    {
                                        if (sender is FrameworkElement Btn)
                                        {
                                            if (Btn.Tag is ContextMenuItem MenuItem)
                                            {
                                                Flyout.Hide();
                                                await MenuItem.InvokeAsync().ConfigureAwait(true);
                                            }
                                        }
                                    }

                                    const short ShowExtNum = 2;

                                    int Index = Flyout.SecondaryCommands.IndexOf(Flyout.SecondaryCommands.OfType <AppBarSeparator>().FirstOrDefault()) + 1;

                                    if (ExtraMenuItems.Count > ShowExtNum)
                                    {
                                        Flyout.SecondaryCommands.Insert(Index, new AppBarSeparator {
                                            Name = "CustomSep"
                                        });

                                        foreach (ContextMenuItem AddItem in ExtraMenuItems.Take(ShowExtNum))
                                        {
                                            Flyout.SecondaryCommands.Insert(Index, await AddItem.GenerateUIButtonAsync(ClickHandler).ConfigureAwait(true));
                                        }

                                        AppBarButton MoreItem = new AppBarButton
                                        {
                                            Label    = Globalization.GetString("CommandBarFlyout_More_Item"),
                                            Icon     = new SymbolIcon(Symbol.More),
                                            Name     = "ExtraButton",
                                            MinWidth = 250
                                        };

                                        MenuFlyout MoreFlyout = new MenuFlyout();

                                        await ContextMenuItem.GenerateSubMenuItemsAsync(MoreFlyout.Items, ExtraMenuItems.Skip(ShowExtNum).ToArray(), ClickHandler).ConfigureAwait(true);

                                        MoreItem.Flyout = MoreFlyout;

                                        Flyout.SecondaryCommands.Insert(Index + ShowExtNum, MoreItem);
                                    }
                                    else
                                    {
                                        foreach (ContextMenuItem AddItem in ExtraMenuItems)
                                        {
                                            Flyout.SecondaryCommands.Insert(Index, await AddItem.GenerateUIButtonAsync(ClickHandler).ConfigureAwait(true));
                                        }

                                        Flyout.SecondaryCommands.Insert(Index + ExtraMenuItems.Count, new AppBarSeparator {
                                            Name = "CustomSep"
                                        });
                                    }
                                }
                            }
                        }
                    }
                }