public static void UpdateMenuItems(this AToolbar toolbar,
                                           IEnumerable <ToolbarItem> sortedToolbarItems,
                                           Context context,
                                           Color?tintColor,
                                           PropertyChangedEventHandler toolbarItemChanged
                                           )
        {
            if (sortedToolbarItems == null)
            {
                return;
            }

            var menu = toolbar.Menu;

            menu.Clear();

            foreach (var item in sortedToolbarItems)
            {
                item.PropertyChanged -= toolbarItemChanged;
                item.PropertyChanged += toolbarItemChanged;

                using (var title = new Java.Lang.String(item.Text))
                {
                    var menuitem = menu.Add(title);
                    menuitem.SetEnabled(item.IsEnabled);
                    menuitem.SetTitleOrContentDescription(item);
                    UpdateMenuItemIcon(context, menuitem, item, tintColor);

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

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

                    if (tintColor != null && tintColor != Color.Default)
                    {
                        var view = toolbar.FindViewById(menuitem.ItemId);
                        if (view is ATextView textView)
                        {
                            if (item.IsEnabled)
                            {
                                textView.SetTextColor(tintColor.Value.ToAndroid());
                            }
                            else
                            {
                                textView.SetTextColor(tintColor.Value.MultiplyAlpha(0.302).ToAndroid());
                            }
                        }
                    }

                    menuitem.Dispose();
                }
            }
        }
Beispiel #2
0
        internal static void UpdateMenuItem(AToolbar toolbar,
                                            ToolbarItem item,
                                            int?menuItemIndex,
                                            Context context,
                                            Color?tintColor,
                                            PropertyChangedEventHandler toolbarItemChanged,
                                            List <IMenuItem> menuItemsCreated,
                                            List <ToolbarItem> toolbarItemsCreated,
                                            Action <Context, IMenuItem, ToolbarItem> updateMenuItemIcon = null)
        {
            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 != Color.Default)
                {
                    var             color       = item.IsEnabled ? tintColor.Value.ToAndroid() : tintColor.Value.MultiplyAlpha(0.302).ToAndroid();
                    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)
            {
                menuitem = menu.Add(0, Platform.GenerateViewId(), 0, newTitle);
                menuItemsCreated?.Add(menuitem);
                toolbarItemsCreated?.Add(item);
            }
            else
            {
                if (menuItemsCreated == null || menuItemsCreated.Count < menuItemIndex.Value)
                {
                    return;
                }

                menuitem = menuItemsCreated[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(context, menuitem, item, tintColor);
            }

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

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

            if (item.Order != ToolbarItemOrder.Secondary && !Forms.IsOreoOrNewer && (tintColor != null && tintColor != Color.Default))
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.Value.ToAndroid());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.Value.MultiplyAlpha(0.302).ToAndroid());
                    }
                }
            }
        }
Beispiel #3
0
        internal static void UpdateMenuItem(AToolbar toolbar,
                                            ToolbarItem item,
                                            int?menuItemIndex,
                                            Context context,
                                            Color?tintColor,
                                            PropertyChangedEventHandler toolbarItemChanged,
                                            List <IMenuItem> menuItemsCreated,
                                            List <ToolbarItem> toolbarItemsCreated,
                                            Action <Context,
                                                    IMenuItem,
                                                    ToolbarItem> updateMenuItemIcon = null)
        {
            IMenu menu = toolbar.Menu;

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

            IMenuItem menuitem;

            if (menuItemIndex == null)
            {
                menuitem = menu.Add(new Java.Lang.String(item.Text));
                menuItemsCreated?.Add(menuitem);
                toolbarItemsCreated?.Add(item);
            }
            else
            {
                if (menuItemsCreated == null || menuItemsCreated.Count < menuItemIndex.Value)
                {
                    return;
                }

                menuitem = menuItemsCreated[menuItemIndex.Value];

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

                menuitem.SetTitle(new Java.Lang.String(item.Text));
            }

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

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

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

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

            if (tintColor != null && tintColor != Color.Default)
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.Value.ToAndroid());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.Value.MultiplyAlpha(0.302).ToAndroid());
                    }
                }
            }
        }
        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;
            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.ToNative() : tintColor.MultiplyAlpha(0.302f).ToNative();
                    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);
                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 && !NativeVersion.IsAtLeast(26) && (tintColor != null && tintColor != null))
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.ToNative());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.MultiplyAlpha(0.302f).ToNative());
                    }
                }
            }
        }