예제 #1
0
        internal static void UpdateMenuItemIcon(IMauiContext mauiContext, IMenuItem menuItem, ToolbarItem toolBarItem, Color tintColor)
        {
            ImageSourceLoader.LoadImage(toolBarItem, mauiContext, result =>
            {
                var baseDrawable = result.Value;
                if (menuItem == null || !menuItem.IsAlive())
                {
                    return;
                }

                if (baseDrawable != null)
                {
                    using (var constant = baseDrawable.GetConstantState())
                        using (var newDrawable = constant.NewDrawable())
                            using (var iconDrawable = newDrawable.Mutate())
                            {
                                if (tintColor != null)
                                {
                                    iconDrawable.SetColorFilter(tintColor.ToNative(Colors.White), FilterMode.SrcAtop);
                                }

                                if (!menuItem.IsEnabled)
                                {
                                    iconDrawable.Mutate().SetAlpha(DefaultDisabledToolbarAlpha);
                                }

                                menuItem.SetIcon(iconDrawable);
                            }
                }
            });
        }
예제 #2
0
        void UpdateBarBackgroundColor()
        {
            if (IsBottomTabPlacement)
            {
                Color tintColor = Element.BarBackgroundColor;

                if (tintColor == null)
                {
                    _bottomNavigationView.SetBackground(null);
                }
                else if (tintColor != null)
                {
                    _bottomNavigationView.SetBackgroundColor(tintColor.ToNative());
                }
            }
            else
            {
                Color tintColor = Element.BarBackgroundColor;

                if (tintColor == null)
                {
                    _tabLayout.BackgroundTintMode = null;
                }
                else
                {
                    _tabLayout.BackgroundTintMode = PorterDuff.Mode.Src;
                    _tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToNative());
                }
            }
        }
예제 #3
0
        public static void UpdateTextColor(this TextView textView, ITextStyle textStyle, Graphics.Color defaultColor)
        {
            var textColor = textStyle.TextColor?.ToNative() ?? defaultColor?.ToNative();

            if (textColor != null)
            {
                textView.SetTextColor(textColor.Value);
            }
        }
예제 #4
0
        public static void SetColorFilter(this ADrawable drawable, Graphics.Color color, FilterMode mode)
        {
            if (drawable == null)
            {
                return;
            }

            drawable.SetColorFilter(color.ToNative(), mode);
        }
예제 #5
0
        public static void UpdateThumbColor(this UISwitch uiSwitch, ISwitch view, UIColor?defaultThumbColor)
        {
            if (view == null)
            {
                return;
            }

            Graphics.Color thumbColor = view.ThumbColor;
            uiSwitch.ThumbTintColor = thumbColor?.ToNative() ?? defaultThumbColor;
        }
예제 #6
0
        protected virtual ColorStateList GetItemTextColorStates()
        {
            if (_originalTabTextColors == null)
            {
                _originalTabTextColors = (IsBottomTabPlacement) ? _bottomNavigationView.ItemTextColor : _tabLayout.TabTextColors;
            }

            Color barItemColor         = BarItemColor;
            Color barTextColor         = Element.BarTextColor;
            Color barSelectedItemColor = BarSelectedItemColor;

            if (barItemColor == null && barTextColor == null && barSelectedItemColor == null)
            {
                return(_originalTabTextColors);
            }

            if (_newTabTextColors != null)
            {
                return(_newTabTextColors);
            }

            int checkedColor;
            int defaultColor;

            if (barTextColor != null)
            {
                checkedColor = barTextColor.ToNative().ToArgb();
                defaultColor = checkedColor;
            }
            else
            {
                defaultColor = barItemColor.ToNative().ToArgb();

                if (barItemColor == null && _originalTabTextColors != null)
                {
                    defaultColor = _originalTabTextColors.DefaultColor;
                }

                checkedColor = defaultColor;

                if (barSelectedItemColor != null)
                {
                    checkedColor = barSelectedItemColor.ToNative().ToArgb();
                }
            }

            _newTabTextColors = GetColorStateList(defaultColor, checkedColor);
            return(_newTabTextColors);
        }
예제 #7
0
        public static void SetColorFilter(this ADrawable drawable, Graphics.Color color, FilterMode mode, AColorFilter?defaultColorFilter)
        {
            if (drawable == null)
            {
                return;
            }

            if (color == null)
            {
                SetColorFilter(drawable, defaultColorFilter);
            }
            else
            {
                drawable.SetColorFilter(color.ToNative(), mode);
            }
        }
        public static void UpdatePlaceholderColor(this AppCompatEditText editText, Graphics.Color placeholderTextColor, ColorStateList?defaultColor)
        {
            if (placeholderTextColor == null)
            {
                editText.SetHintTextColor(defaultColor);
            }
            else
            {
                var androidColor = placeholderTextColor.ToNative();

                if (!editText.HintTextColors.IsOneColor(ColorExtensions.States, androidColor))
                {
                    var acolor = androidColor.ToArgb();
                    editText.SetHintTextColor(new ColorStateList(ColorExtensions.States, new[] { acolor, acolor }));
                }
            }
        }
예제 #9
0
        protected virtual ColorStateList GetItemIconTintColorState()
        {
            if (IsBottomTabPlacement)
            {
                if (_orignalTabIconColors == null)
                {
                    _orignalTabIconColors = _bottomNavigationView.ItemIconTintList;
                }
            }
            // this ensures that existing behavior doesn't change
            else if (!IsBottomTabPlacement && BarSelectedItemColor != null && BarItemColor == null)
            {
                return(null);
            }

            Color barItemColor         = BarItemColor;
            Color barSelectedItemColor = BarSelectedItemColor;

            if (barItemColor == null && barSelectedItemColor == null)
            {
                return(_orignalTabIconColors);
            }

            if (_newTabIconColors != null)
            {
                return(_newTabIconColors);
            }

            int defaultColor = barItemColor.ToNative().ToArgb();

            if (barItemColor == null && _orignalTabIconColors != null)
            {
                defaultColor = _orignalTabIconColors.DefaultColor;
            }

            int checkedColor = defaultColor;

            if (barSelectedItemColor != null)
            {
                checkedColor = barSelectedItemColor.ToNative().ToArgb();
            }

            _newTabIconColors = GetColorStateList(defaultColor, checkedColor);
            return(_newTabIconColors);
        }
예제 #10
0
        public static void UpdateTextColor(this AppCompatEditText editText, Graphics.Color textColor, ColorStateList?defaultColor)
        {
            if (textColor == null)
            {
                if (defaultColor != null)
                {
                    editText.SetTextColor(defaultColor);
                }
            }
            else
            {
                var androidColor = textColor.ToNative();

                if (!editText.TextColors.IsOneColor(ColorStates, androidColor))
                {
                    var acolor = androidColor.ToArgb();
                    editText.SetTextColor(new ColorStateList(ColorStates, new[] { acolor, acolor }));
                }
            }
        }
예제 #11
0
 public static void UpdateTextColor(this TextView textView, ITextStyle textStyle, Graphics.Color defaultColor)
 {
     textView.SetTextColor(textStyle.TextColor?.ToNative() ?? defaultColor.ToNative());
 }
예제 #12
0
        public static void UpdateBackground(this GradientDrawable gradientDrawable, Brush brush, int height, int width)
        {
            if (gradientDrawable == null || brush == null || brush.IsEmpty)
            {
                return;
            }

            if (brush is SolidColorBrush solidColorBrush)
            {
                Color bgColor = solidColorBrush.Color;
                gradientDrawable.SetColor(bgColor?.ToNative() ?? Colors.Transparent.ToNative());
            }

            if (brush is LinearGradientBrush linearGradientBrush)
            {
                var p1 = linearGradientBrush.StartPoint;
                var x1 = (float)p1.X;
                var y1 = (float)p1.Y;

                var p2 = linearGradientBrush.EndPoint;
                var x2 = (float)p2.X;
                var y2 = (float)p2.Y;

                const double Rad2Deg = 180.0 / Math.PI;

                float xDiff = x2 - x1;
                float yDiff = y2 - y1;

                double angle = Math.Atan2(yDiff, xDiff) * Rad2Deg;

                if (angle < 0)
                {
                    angle += 360;
                }

                var gradientBrushData = linearGradientBrush.GetGradientBrushData();
                var colors            = gradientBrushData.Item1;

                if (colors.Length < 2)
                {
                    return;
                }

                gradientDrawable.SetGradientType(GradientType.LinearGradient);
                gradientDrawable.SetColors(colors);
                SetGradientOrientation(gradientDrawable, angle);
            }

            if (brush is RadialGradientBrush radialGradientBrush)
            {
                var   center  = radialGradientBrush.Center;
                float centerX = (float)center.X;
                float centerY = (float)center.Y;
                float radius  = (float)radialGradientBrush.Radius;

                var gradientBrushData = radialGradientBrush.GetGradientBrushData();
                var colors            = gradientBrushData.Item1;

                if (colors.Length < 2)
                {
                    return;
                }

                gradientDrawable.SetGradientType(GradientType.RadialGradient);
                gradientDrawable.SetGradientCenter(centerX, centerY);
                gradientDrawable.SetGradientRadius(Math.Max(height, width) * radius);
                gradientDrawable.SetColors(colors);
            }
        }
예제 #13
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;
            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());
                    }
                }
            }
        }