internal static void UpdateMenuItemIcon(Context context, IMenuItem menuItem, ToolbarItem toolBarItem, Color tintColor) { _ = context.ApplyDrawableAsync(toolBarItem, MenuItem.IconImageSourceProperty, baseDrawable => { 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.ToAndroid(Colors.White), FilterMode.SrcAtop); } if (!menuItem.IsEnabled) { iconDrawable.Mutate().SetAlpha(DefaultDisabledToolbarAlpha); } menuItem.SetIcon(iconDrawable); } } }); }
void UpdateBarBackgroundColor() { if (IsDisposed) { return; } if (IsBottomTabPlacement) { Color tintColor = Element.BarBackgroundColor; if (tintColor == null) { _bottomNavigationView.SetBackground(null); } else if (tintColor != null) { _bottomNavigationView.SetBackgroundColor(tintColor.ToAndroid()); } } else { Color tintColor = Element.BarBackgroundColor; if (tintColor == null) { _tabLayout.BackgroundTintMode = null; } else { _tabLayout.BackgroundTintMode = PorterDuff.Mode.Src; _tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid()); } } }
protected virtual ColorStateList GetItemTextColorStates() { if (IsDisposed) { return(null); } 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.ToAndroid().ToArgb(); defaultColor = checkedColor; } else { defaultColor = barItemColor.ToAndroid().ToArgb(); if (barItemColor == null && _originalTabTextColors != null) { defaultColor = _originalTabTextColors.DefaultColor; } checkedColor = defaultColor; if (barSelectedItemColor != null) { checkedColor = barSelectedItemColor.ToAndroid().ToArgb(); } } _newTabTextColors = GetColorStateList(defaultColor, checkedColor); return(_newTabTextColors); }
void UpdateBackgroundColor() { if (_disposed) { return; } Color bgColor = Element.BackgroundColor; _backgroundDrawable.SetColor(bgColor?.ToAndroid() ?? AColor.White); }
protected virtual ColorStateList GetItemIconTintColorState() { if (IsDisposed) { return(null); } 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.ToAndroid().ToArgb(); if (barItemColor == null && _orignalTabIconColors != null) { defaultColor = _orignalTabIconColors.DefaultColor; } int checkedColor = defaultColor; if (barSelectedItemColor != null) { checkedColor = barSelectedItemColor.ToAndroid().ToArgb(); } _newTabIconColors = GetColorStateList(defaultColor, checkedColor); return(_newTabIconColors); }
void UpdateBorderColor() { if (_disposed) { return; } Color borderColor = Element.BorderColor; if (borderColor == null) { _backgroundDrawable.SetStroke(0, AColor.Transparent); } else { _backgroundDrawable.SetStroke(3, borderColor.ToAndroid()); } }
void UpdateToolbar() { if (_disposed) { return; } Context context = Context; AToolbar bar = _toolbar; ActionBarDrawerToggle toggle = _drawerToggle; if (bar == null) { return; } bool isNavigated = NavigationPageController.StackDepth > 1; bar.NavigationIcon = null; Page currentPage = Element.CurrentPage; if (isNavigated) { if (NavigationPage.GetHasBackButton(currentPage)) { if (toggle != null) { toggle.DrawerIndicatorEnabled = false; toggle.SyncState(); } var icon = new DrawerArrowDrawable(context.GetThemedContext()); icon.Progress = 1; bar.NavigationIcon = icon; var prevPage = Element.Peek(1); var backButtonTitle = NavigationPage.GetBackButtonTitle(prevPage); _defaultNavigationContentDescription = backButtonTitle != null ? bar.SetNavigationContentDescription(prevPage, backButtonTitle) : bar.SetNavigationContentDescription(prevPage, _defaultNavigationContentDescription); } else if (toggle != null && _flyoutPage != null) { toggle.DrawerIndicatorEnabled = _flyoutPage.ShouldShowToolbarButton(); toggle.SyncState(); } } else { if (toggle != null && _flyoutPage != null) { toggle.DrawerIndicatorEnabled = _flyoutPage.ShouldShowToolbarButton(); toggle.SyncState(); } } Color tintColor = Element.BarBackgroundColor; if (tintColor == null) { bar.BackgroundTintMode = null; } else { bar.BackgroundTintMode = PorterDuff.Mode.Src; bar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid()); } Brush barBackground = Element.BarBackground; bar.UpdateBackground(barBackground); Color textColor = Element.BarTextColor; if (textColor != null) { bar.SetTitleTextColor(textColor.ToAndroid().ToArgb()); } Color navIconColor = NavigationPage.GetIconColor(Current); if (navIconColor != null && bar.NavigationIcon != null) { bar.NavigationIcon.SetColorFilter(navIconColor, FilterMode.SrcAtop); } bar.Title = currentPage?.Title ?? string.Empty; if (_toolbar.NavigationIcon != null && textColor != null) { var icon = _toolbar.NavigationIcon as DrawerArrowDrawable; if (icon != null) { icon.Color = textColor.ToAndroid().ToArgb(); } } UpdateTitleIcon(); UpdateTitleView(); }
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 != null) { var color = item.IsEnabled ? tintColor.ToAndroid() : tintColor.MultiplyAlpha(0.302f).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, AppCompat.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 != null)) { var view = toolbar.FindViewById(menuitem.ItemId); if (view is ATextView textView) { if (item.IsEnabled) { textView.SetTextColor(tintColor.ToAndroid()); } else { textView.SetTextColor(tintColor.MultiplyAlpha(0.302f).ToAndroid()); } } } }
void UpdateBarBackgroundColor() { if (IsDisposed) { return; } if (IsBottomTabPlacement) { Color tintColor = Element.BarBackgroundColor; if (tintColor == null) { _bottomNavigationView.SetBackground(null); } else if (tintColor != null) { _bottomNavigationView.SetBackgroundColor(tintColor.ToAndroid()); } } else { Color tintColor = Element.BarBackgroundColor; if (Forms.IsLollipopOrNewer) { if (tintColor == null) { _tabLayout.BackgroundTintMode = null; } else { _tabLayout.BackgroundTintMode = PorterDuff.Mode.Src; _tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid()); } } else { if (tintColor == null && _backgroundDrawable != null) { _tabLayout.SetBackground(_backgroundDrawable); } else if (tintColor != null) { // if you don't create a new drawable then SetBackgroundColor // just sets the color on the background drawable that's saved // it doesn't create a new one if (_backgroundDrawable == null && _tabLayout.Background != null) { _backgroundDrawable = _tabLayout.Background; _wrappedBackgroundDrawable = ADrawableCompat.Wrap(_tabLayout.Background).Mutate(); } if (_wrappedBackgroundDrawable != null) { _tabLayout.Background = _wrappedBackgroundDrawable; } _tabLayout.SetBackgroundColor(tintColor.ToAndroid()); } } } }
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?.ToAndroid() ?? Colors.Transparent.ToAndroid()); } 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); } }