Example #1
0
        static void UpdateMenuItem(AToolbar toolbar,
                                   ToolbarItem item,
                                   int?menuItemIndex,
                                   IMauiContext mauiContext,
                                   Color?tintColor,
                                   PropertyChangedEventHandler toolbarItemChanged,
                                   List <IMenuItem> previousMenuItems,
                                   List <ToolbarItem> previousToolBarItems,
                                   Action <Context, IMenuItem, ToolbarItem>?updateMenuItemIcon = null)
        {
            var context = mauiContext.Context ??
                          throw new ArgumentNullException($"{nameof(mauiContext.Context)}");

            IMenu menu = toolbar.Menu;

            item.PropertyChanged -= toolbarItemChanged;
            item.PropertyChanged += toolbarItemChanged;

            IMenuItem menuitem;

            Java.Lang.ICharSequence?newTitle = null;

            if (!String.IsNullOrWhiteSpace(item.Text))
            {
                if (item.Order != ToolbarItemOrder.Secondary && tintColor != null && tintColor != null)
                {
                    var             color       = item.IsEnabled ? tintColor.ToPlatform() : tintColor.MultiplyAlpha(0.302f).ToPlatform();
                    SpannableString titleTinted = new SpannableString(item.Text);
                    titleTinted.SetSpan(new ForegroundColorSpan(color), 0, titleTinted.Length(), 0);
                    newTitle = titleTinted;
                }
                else
                {
                    newTitle = new Java.Lang.String(item.Text);
                }
            }
            else
            {
                newTitle = new Java.Lang.String();
            }

            if (menuItemIndex == null || menuItemIndex >= previousMenuItems?.Count)
            {
                menuitem = menu.Add(0, AView.GenerateViewId(), 0, newTitle) ??
                           throw new InvalidOperationException($"Failed to create menuitem: {newTitle}");
                previousMenuItems?.Add(menuitem);
            }
            else
            {
                if (previousMenuItems == null || previousMenuItems.Count < menuItemIndex.Value)
                {
                    return;
                }

                menuitem = previousMenuItems[menuItemIndex.Value];

                if (!menuitem.IsAlive())
                {
                    return;
                }

                menuitem.SetTitle(newTitle);
            }

            menuitem.SetEnabled(item.IsEnabled);
            menuitem.SetTitleOrContentDescription(item);

            if (updateMenuItemIcon != null)
            {
                updateMenuItemIcon(context, menuitem, item);
            }
            else
            {
                UpdateMenuItemIcon(mauiContext, menuitem, item, tintColor);
            }

            if (item.Order != ToolbarItemOrder.Secondary)
            {
                menuitem.SetShowAsAction(ShowAsAction.Always);
            }

            menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate));

            if (item.Order != ToolbarItemOrder.Secondary && !PlatformVersion.IsAtLeast(26) && (tintColor != null && tintColor != null))
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.ToPlatform());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.MultiplyAlpha(0.302f).ToPlatform());
                    }
                }
            }
        }