Beispiel #1
0
        private void UpdateCommandsVisibilityTracking(IEnumerable <ICommandBarElement> sender)
        {
            List <ICommandBarElement> toRemove = new List <ICommandBarElement>();

            foreach (KeyValuePair <ICommandBarElement, long> element in _visibilityTrackedElements)
            {
                var visibilityTrackable = element.Key as FrameworkElement;
                if (visibilityTrackable != null &&
                    !(PrimaryCommands.Contains(element.Key) || SecondaryCommands.Contains(element.Key)))
                {
                    visibilityTrackable.UnregisterPropertyChangedCallback(VisibilityProperty, element.Value);
                }
            }

            foreach (var stale in toRemove)
            {
                _visibilityTrackedElements.Remove(stale);
            }

            foreach (ICommandBarElement element in sender)
            {
                if (element is NavAppBarButton)
                {
                    continue;
                }
                var visibilityTrackable = element as FrameworkElement;
                if (visibilityTrackable != null && !_visibilityTrackedElements.ContainsKey(element))
                {
                    long callbackToken = visibilityTrackable.RegisterPropertyChangedCallback(VisibilityProperty, OnCommandBarElementVisibilityChanged);
                    _visibilityTrackedElements.Add(element, callbackToken);
                }
            }
        }
Beispiel #2
0
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _moreButton            = GetTemplateChild(MoreButton) as Button;
            _overflowPopup         = GetTemplateChild(OverflowPopup) as Popup;
            _primaryItemsControl   = GetTemplateChild(PrimaryItemsControl) as ItemsControl;
            _secondaryItemsControl = GetTemplateChild(SecondaryItemsControl) as ItemsControl;

#if __ANDROID__ || __IOS__
            Presenter = this.FindFirstChild <NativeCommandBarPresenter>();
#endif
            if (_moreButton != null)
            {
                _moreButton.Click += (s, e) =>
                {
                    IsOpen = !IsOpen;
                };
            }

            if (_overflowPopup != null)
            {
                _overflowPopup.Closed += (s, e) =>
                {
                    IsOpen = false;
                };
            }

            PrimaryCommands.VectorChanged   += (s, e) => UpdateCommands();
            SecondaryCommands.VectorChanged += (s, e) => UpdateCommands();

            UpdateCommands();

            this.RegisterPropertyChangedCallback(IsEnabledProperty, (s, e) => UpdateCommonState());
            this.RegisterPropertyChangedCallback(ClosedDisplayModeProperty, (s, e) => UpdateDisplayModeState());
            this.RegisterPropertyChangedCallback(IsOpenProperty, (s, e) =>
            {
                // TODO: Consider the content of _secondaryItemsControl when IsDynamicOverflowEnabled is supported.
                var hasSecondaryItems = SecondaryCommands.Any();
                if (hasSecondaryItems)
                {
                    if (_overflowPopup != null)
                    {
                        _overflowPopup.IsOpen = true;
                    }
                }

                UpdateDisplayModeState();
                UpdateButtonsIsCompact();
            });

            UpdateCommonState();
            UpdateDisplayModeState();
            UpdateButtonsIsCompact();
        }
Beispiel #3
0
 private void TryRemoveSecondarySeparator()
 {
     if (SecondaryCommands.Any(x => x is AppBarSeparator) && (
             SecondaryCommands.Count == 0 ||
             !SecondaryCommands.Any(x => x is NavAppBarButton) ||
             !SecondaryCommands.Any(x => x is ISortableAppBarButton)
             ))
     {
         int separatorIndex = SecondaryCommands.IndexOf(SecondaryCommands.First(x => x is AppBarSeparator));
         SecondaryCommands.RemoveAt(separatorIndex);
     }
 }
Beispiel #4
0
        protected override Size ArrangeOverride(Size finalSize)
        {
            var ret = base.ArrangeOverride(finalSize);

            if (!haveDynamicOverflow && primary.Count != 0)
            {
                uint limit        = (uint)Math.Floor(ret.Width / 68) - 1;
                int  currentCount = PrimaryCommands.CountVisible();

                if (currentCount != limit && !(currentCount == primary.Count && limit > currentCount))
                {
                    reflowing = true;

#if WINDOWS_APP
                    secondaryFlyout = null;
#endif
                    needseparator = false;
                    PrimaryCommands.Clear();
                    SecondaryCommands.Clear();

                    int i = 0;
                    foreach (var item in primary)
                    {
                        if (i < limit)
                        {
                            PrimaryCommands.Add(item);
                        }
                        else
                        {
                            SecondaryCommands.Add(item);
                        }

                        i++;
                    }

                    needseparator = i > limit;

                    foreach (var item in secondary)
                    {
                        SecondaryCommands.Add(item);
                    }

                    reflowing = false;
                }
            }

            return(ret);
        }
Beispiel #5
0
        private void InsertToSecondaryBar(NavAppBarButton buttonToMove)
        {
            buttonToMove.IsSecondaryCommand = true;

            if (SecondaryCommands.Any(x => x is MovableAppBarButton) && !SecondaryCommands.Any(x => x is AppBarSeparator))
            {
                SecondaryCommands.Insert(0, new AppBarSeparator());
            }

            int separatorIndex = -1;

            if (SecondaryCommands.Any(x => x is AppBarSeparator))
            {
                separatorIndex = SecondaryCommands.IndexOf(SecondaryCommands.First(x => x is AppBarSeparator));
            }

            int endIndex = separatorIndex != -1 ? separatorIndex - 1 : SecondaryCommands.Count - 1;

            if (endIndex == -1) //nothing in the SecondaryCommands list yet
            {
                SecondaryCommands.Add(buttonToMove);
            }
            else //we've got at least one other SecondaryCommand, maybe more
            {
                for (int i = 0; i <= endIndex; i++)
                {
                    int currCommandPosition = ((ISortableAppBarButton)SecondaryCommands[i]).Position;
                    if (buttonToMove.Position < currCommandPosition)
                    {
                        SecondaryCommands.Insert(i, buttonToMove);
                        return;
                    }
                }
                //if we get through the entire list and we don't find anything of a lesser priority, the new button belongs at the end
                SecondaryCommands.Insert(endIndex, buttonToMove);
            }
        }
Beispiel #6
0
        private void InsertToSecondaryBar(ICommandBarElement buttonToMove)
        {
            ((ISortableAppBarButton)buttonToMove).IsSecondaryCommand = true;
            if (SecondaryCommands.Any(x => x is NavAppBarButton) && !SecondaryCommands.Any(x => x is AppBarSeparator))
            {
                SecondaryCommands.Add(new AppBarSeparator());
            }

            int separatorIndex = -1;

            if (SecondaryCommands.Any(x => x is AppBarSeparator))
            {
                separatorIndex = SecondaryCommands.IndexOf(SecondaryCommands.First(x => x is AppBarSeparator));
            }

            int startIndex = separatorIndex != -1 ? separatorIndex + 1 : 0;

            if (startIndex == 0)
            {
                SecondaryCommands.Add(buttonToMove);
            }
            else
            {
                for (int i = startIndex; i < SecondaryCommands.Count; i++)
                {
                    int currCommandPosition = ((ISortableAppBarButton)SecondaryCommands[i]).Position;
                    if (((ISortableAppBarButton)buttonToMove).Position < currCommandPosition)
                    {
                        SecondaryCommands.Insert(i, buttonToMove);
                        return;
                    }
                }
                //if we get through the entire list and we don't find anything of a lesser priority, the new button belongs at the end
                SecondaryCommands.Add(buttonToMove);
            }
        }
Beispiel #7
0
            public RichEditBoxSelectionFlyout()
            {
                Opened += RichEditBoxSelectionFlyout_Opened;

                Placement = Windows.UI.Xaml.Controls.Primitives.FlyoutPlacementMode.TopEdgeAlignedLeft;

                ResourceLoader loader = ResourceLoader.GetForCurrentView();

                CutButton = new AppBarButton()
                {
                    Label = loader.GetString("CutButton/Text")
                };
                CutButton.Click += CutButton_Click;
                PrimaryCommands.Add(CutButton);

                CopyButton = new AppBarButton()
                {
                    Label = loader.GetString("CopyButton/Text")
                };
                CopyButton.Click += CopyButton_Click;
                PrimaryCommands.Add(CopyButton);

                PasteButton = new AppBarButton()
                {
                    Label = loader.GetString("PasteButton/Text")
                };
                PasteButton.Click += PasteButton_Click;
                PrimaryCommands.Add(PasteButton);

                SelectAllButton = new AppBarButton()
                {
                    Label = loader.GetString("SelectAllButton/Text")
                };
                SelectAllButton.Click += SelectAllButton_Click;
                SecondaryCommands.Add(SelectAllButton);

                HighlightSeparator = new AppBarSeparator();
                SecondaryCommands.Add(HighlightSeparator);

                HighlightButton = new AppBarButton()
                {
                    Label = loader.GetString("HighlightButton/Text")
                };
                HighlightButton.Click += HighlightButton_Click;
                SecondaryCommands.Add(HighlightButton);

                RemoveHighlightButton = new AppBarButton()
                {
                    Label = loader.GetString("RemoveHighlightButton/Text")
                };
                RemoveHighlightButton.Click += RemoveHighlightButton_Click;
                SecondaryCommands.Add(RemoveHighlightButton);

                CaseSeparator = new AppBarSeparator();
                SecondaryCommands.Add(CaseSeparator);

                UppercaseButton = new AppBarButton()
                {
                    Label = loader.GetString("UppercaseButton/Text")
                };
                UppercaseButton.Click += UppercaseButton_Click;
                SecondaryCommands.Add(UppercaseButton);

                LowercaseButton = new AppBarButton()
                {
                    Label = loader.GetString("LowercaseButton/Text")
                };
                LowercaseButton.Click += LowercaseButton_Click;
                SecondaryCommands.Add(LowercaseButton);

                SearchSeparator = new AppBarSeparator();
                SecondaryCommands.Add(SearchSeparator);

                SearchButton = new AppBarButton()
                {
                    Label = "[search]"
                };
                SearchButton.Click += SearchButton_Click;
                SecondaryCommands.Add(SearchButton);

                CutButton.Icon = new FontIcon {
                    Glyph = "\uE8C6"
                };
                CopyButton.Icon = new FontIcon {
                    Glyph = "\uE8C8"
                };
                PasteButton.Icon = new FontIcon {
                    Glyph = "\uE77F"
                };

                if (ApiInformation.IsTypePresent("Windows.UI.Xaml.Input.KeyboardAccelerator"))
                {
                    KeyboardAccelerator CtrlX = new KeyboardAccelerator();
                    CtrlX.Key           = VirtualKey.X;
                    CtrlX.Modifiers     = VirtualKeyModifiers.Control;
                    CtrlX.IsEnabled     = false;
                    CutButton.AccessKey = "X";
                    CutButton.KeyboardAccelerators.Add(CtrlX);

                    KeyboardAccelerator CtrlC = new KeyboardAccelerator();
                    CtrlC.Key            = VirtualKey.C;
                    CtrlC.Modifiers      = VirtualKeyModifiers.Control;
                    CtrlC.IsEnabled      = false;
                    CopyButton.AccessKey = "C";
                    CopyButton.KeyboardAccelerators.Add(CtrlC);

                    KeyboardAccelerator CtrlV = new KeyboardAccelerator();
                    CtrlV.Key             = VirtualKey.V;
                    CtrlV.Modifiers       = VirtualKeyModifiers.Control;
                    CtrlV.IsEnabled       = false;
                    PasteButton.AccessKey = "V";
                    PasteButton.KeyboardAccelerators.Add(CtrlV);

                    KeyboardAccelerator CtrlA = new KeyboardAccelerator();
                    CtrlA.Key                 = VirtualKey.A;
                    CtrlA.Modifiers           = VirtualKeyModifiers.Control;
                    CtrlA.IsEnabled           = false;
                    SelectAllButton.AccessKey = "A";
                    SelectAllButton.KeyboardAccelerators.Add(CtrlA);
                }
            }
Beispiel #8
0
        private void VectorChanged(Windows.Foundation.Collections.IObservableVector <ICommandBarElement> sender, Windows.Foundation.Collections.IVectorChangedEventArgs args)
        {
            if (args.CollectionChange == Windows.Foundation.Collections.CollectionChange.Reset)
            {
#if WINDOWS_APP
                secondaryFlyout = null;
#endif
                needseparator = false;
                if (!reflowing)
                {
                    primary.Clear();
                    secondary.Clear();
                }
            }
            else if (args.CollectionChange == Windows.Foundation.Collections.CollectionChange.ItemInserted)
            {
                var itembase = sender[(int)args.Index];
                itembase.IsCompact = true;

#if WINDOWS_APP
                if (sender == SecondaryCommands)
                {
                    if (secondaryFlyout == null)
                    {
                        var ellipsis = new AppBarEllipsis
                        {
                            Flyout = secondaryFlyout = new MenuFlyout()
                        };

                        PrimaryCommands.Add(ellipsis);
                        secondaryFlyout.Opening += SecondaryFlyout_Opening;
                        secondaryFlyout.Closed  += SecondaryFlyout_Closed;
                    }

                    if (needseparator)
                    {
                        needseparator = false;
                        secondaryFlyout.Items.Add(new MenuFlyoutSeparator());
                    }

                    if (itembase is AppBarSeparator sep)
                    {
                        secondaryFlyout.Items.Add(new MenuFlyoutSeparator());
                    }
                    else if (itembase is AppBarButton button)
                    {
                        if (!reflowing && !primary.Contains(button))
                        {
                            secondary.Add(button);
                        }

                        var menuitem = new MenuFlyoutItemEx
                        {
                            Text        = button.Label,
                            Command     = button.Command,
                            DataContext = button.DataContext,
                        };
                        if (button.Icon is BitmapIcon bmp)
                        {
                            menuitem.Icon = new BitmapIcon {
                                UriSource = bmp.UriSource
                            }
                        }
                        ;
                        else if (button.Icon is SymbolIcon sym)
                        {
                            menuitem.Icon = new SymbolIcon(sym.Symbol);
                        }

                        if (menuitem.DataContext is Ao3TrackReader.Controls.ToolbarItem)
                        {
                            // Create the binding description.
                            Binding b = new Binding()
                            {
                                Mode      = BindingMode.OneWay,
                                Path      = new PropertyPath("Foreground"),
                                Converter = new ColorConverter()
                            };
                            menuitem.SetBinding(MenuFlyoutItemEx.ForegroundProperty, b);
                        }

                        secondaryFlyout.Items.Add(menuitem);
                    }

                    if (itembase is FrameworkElement elem)
                    {
                        elem.Visibility = Visibility.Collapsed;
                    }

                    return;
                }
#endif

                if (itembase is FrameworkElement e)
                {
                    e.Visibility = Visibility.Visible;
                }

                var item = itembase as AppBarButton;
                if (item == null)
                {
                    return;
                }
                if (AppBarButtonTemplate != null)
                {
                    item.Template = AppBarButtonTemplate;
                }

                var xitem = item.DataContext as Xamarin.Forms.ToolbarItem;
                if (xitem == null)
                {
                    return;
                }

                if (sender == PrimaryCommands)
                {
                    if (!reflowing)
                    {
                        primary.Add(item);

                        if (!haveDynamicOverflow && ActualWidth > 0)
                        {
                            uint limit = (uint)Math.Floor(ActualWidth / 68) - 1;
                            if (args.Index >= limit)
                            {
                                item.Visibility = Visibility.Collapsed;
                                needseparator   = false;
                                var newitem = new AppBarButton
                                {
                                    Label       = item.Label,
                                    Command     = item.Command,
                                    DataContext = item.DataContext
                                };
                                if (item.Icon is BitmapIcon bmp)
                                {
                                    newitem.Icon = new BitmapIcon {
                                        UriSource = bmp.UriSource
                                    }
                                }
                                ;
                                else if (item.Icon is SymbolIcon sym)
                                {
                                    newitem.Icon = new SymbolIcon(sym.Symbol);
                                }
                                SecondaryCommands.Add(newitem);

                                needseparator = true;

                                return;
                            }
                        }
                    }
                }
                else if (sender == SecondaryCommands)
                {
                    if (needseparator)
                    {
                        needseparator = false;
#if !WINDOWS_PHONE_APP
                        SecondaryCommands.Insert((int)args.Index, new AppBarSeparator());
#else
                        SecondaryCommands.Insert((int)args.Index, new AppBarButton {
                            Label     = "\x23AF\x23AF\x23AF\x23AF",
                            IsEnabled = false,
                        });
#endif
                    }
                    if (!reflowing)
                    {
                        secondary.Add(item);
                    }

                    var ti = GetTemplateChild("SecondaryItemsControl");
                }

                item.ClearValue(AppBarButton.IconProperty);
                if (!string.IsNullOrWhiteSpace(xitem.Icon?.File))
                {
#if !WINDOWS_PHONE_APP
                    var uri = new Uri("ms-appx:///" + xitem.Icon.File);
#else
                    var uri = new Uri("ms-appx:///Appbar/" + xitem.Icon.File);
#endif
                    item.Icon = new BitmapIcon()
                    {
                        UriSource = uri
                    };
                }

                if (item.DataContext is Ao3TrackReader.Controls.ToolbarItem)
                {
                    // Create the binding description.
                    Binding b = new Binding()
                    {
                        Mode      = BindingMode.OneWay,
                        Path      = new PropertyPath("Foreground"),
                        Converter = new ColorConverter()
                    };
                    item.SetBinding(AppBarButton.ForegroundProperty, b);
                }
            }
        }