Пример #1
0
 public AppThemeChangedMsg(
     object sender,
     AppThemeType appTheme,
     string accentColor,
     bool useDarkAmoledTheme,
     bool restartActivity = true) : base(sender)
 {
     AppTheme           = appTheme;
     AccentColor        = accentColor;
     UseDarkAmoledTheme = useDarkAmoledTheme;
     RestartActivity    = restartActivity;
 }
Пример #2
0
        /// <summary>
        /// Changes the app theme and the accent color
        /// </summary>
        /// <param name="appTheme">App theme</param>
        /// <param name="hexAccentColor">Accent color in hex</param>
        public static void ChangeCurrentTheme(AppThemeType appTheme, string hexAccentColor)
        {
            Color accentColor;

            if (!string.IsNullOrEmpty(hexAccentColor))
            {
                accentColor = GetColor(hexAccentColor);
            }
            else
            {
                accentColor = GetSystemAccentColor();
            }

            var tb = ApplicationView.GetForCurrentView().TitleBar;

            tb.BackgroundColor                           =
                tb.ButtonBackgroundColor                 =
                    tb.InactiveBackgroundColor           =
                        tb.ButtonInactiveBackgroundColor = accentColor;

            var systemColors = new List <string>
            {
                "SystemAccentColor", "SystemColorHighlightColor", "SystemAccentColorLight2",
                "SystemAccentColorLight3", "SystemAccentColorDark2", "SystemAccentColorDark3"
            };

            foreach (var systemColor in systemColors)
            {
                Application.Current.Resources[systemColor] = accentColor;
            }


            var brush = new SolidColorBrush(accentColor);

            (Application.Current.Resources["SystemControlHighlightAccentBrush"] as SolidColorBrush).Color        = accentColor;
            (Application.Current.Resources["SystemControlHighlightListAccentLowBrush"] as SolidColorBrush).Color = accentColor;
            Application.Current.Resources["SystemControlHighlightListAccentHighBrush"]  = brush;
            Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] = brush;
            //Application.Current.Resources["SystemControlHighlightAltBaseHighBrush"] = accentColor;
            //Application.Current.Resources["SystemControlForegroundBaseHighBrush"] = accentColor;
            //Application.Current.Resources["SystemControlHighlightListMediumBrush"] = brush;
            //Application.Current.Resources["SystemControlHighlightListLowBrush"] = brush;
            //(Application.Current.Resources["SystemControlHighlightAccent3RevealAccent2BackgroundBrush"] as RevealBackgroundBrush).Color = accentColor;
            //(Application.Current.Resources["SystemControlHighlightAccent3RevealBackgroundBrush"] as RevealBackgroundBrush).Color = accentColor;
            //(Application.Current.Resources["SystemControlHighlightAccent2RevealBackgroundBrush"] as RevealBackgroundBrush).Color = accentColor;

            // Set theme for window root.
            if (Window.Current.Content is FrameworkElement frameworkElement)
            {
                frameworkElement.RequestedTheme = (ElementTheme)appTheme;
            }
        }
Пример #3
0
        public static void ChangeTheme(AppThemeType appTheme, string hexAccentColor)
        {
            var    baseTheme = appTheme == AppThemeType.Dark ? Theme.Dark : Theme.Light;
            ITheme theme     = System.Windows.Application.Current.Resources.GetTheme();

            float lightBy = appTheme == AppThemeType.Dark ? 0.2f : 0.5f;
            float midBy   = appTheme == AppThemeType.Dark ? 0.15f : 0.2f;

            var accentColor  = hexAccentColor.ToColor();
            var primaryLight = accentColor.LerpLight(lightBy).ToHexString().ToMediaColor();
            var primaryMid   = accentColor.LerpLight(midBy).ToHexString().ToMediaColor();
            var primaryDark  = hexAccentColor.ToMediaColor();
            var secondary    = hexAccentColor.ToMediaColor();

            theme.PrimaryLight = new ColorPair(primaryLight, primaryLight);
            theme.PrimaryMid   = new ColorPair(primaryMid, primaryMid);
            theme.PrimaryDark  = new ColorPair(primaryDark, primaryDark);

            theme.SecondaryDark          =
                theme.SecondaryMid       =
                    theme.SecondaryLight = new ColorPair(secondary, secondary);
            theme.SetBaseTheme(baseTheme);
            System.Windows.Application.Current.Resources.SetTheme(theme);

            const string darkTheme  = "/XamlResources/DarkTheme.xaml";
            const string lightTheme = "/XamlResources/LightTheme.xaml";

            var uri             = new Uri(appTheme == AppThemeType.Dark ? darkTheme : lightTheme, UriKind.RelativeOrAbsolute);
            var uriToRemove     = new Uri(appTheme == AppThemeType.Dark ? lightTheme : darkTheme, UriKind.RelativeOrAbsolute);
            var themeDictionary = System.Windows.Application.Current.Resources.MergedDictionaries.Last();

            var currentTheme = themeDictionary.MergedDictionaries
                               .FirstOrDefault(r => r.Source == uriToRemove);

            if (!themeDictionary.MergedDictionaries.Any(r => r.Source == uri))
            {
                themeDictionary.MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = uri
                });
            }

            if (currentTheme != null)
            {
                themeDictionary.MergedDictionaries.Remove(currentTheme);
            }
        }
Пример #4
0
        public void SetAppTheme(
            AppThemeType appTheme,
            string appHexAccentColor,
            bool useDarkAmoled,
            bool restartActivity = false)
        {
            appHexAccentColor = appHexAccentColor.ToLower();
            if (appTheme == AppThemeType.DARK)
            {
                switch (appHexAccentColor)
                {
                case AppConstants.AccentColorLightBlue:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_LightBlue);
                    break;

                case AppConstants.AccentColorLimeGreen:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_LimeGreen);
                    break;

                case AppConstants.AccentColorDarkOrange:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_DarkOrange);
                    break;

                case AppConstants.AccentColorVividRed:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_VividRed);
                    break;

                case AppConstants.AccentColorDarkCyan:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_DarkCyan);
                    break;

                case AppConstants.AccentColorDarkGreen:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_DarkGreen);
                    break;

                case AppConstants.AccentColorDarkMagenta:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_DarkMagenta);
                    break;

                case AppConstants.AccentColorMagenta:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_Magenta);
                    break;

                case AppConstants.AccentColorDarkGray:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_DarkGray);
                    break;

                case AppConstants.AccentColorOrange:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_Orange);
                    break;

                case AppConstants.AccentColorYellow:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_Yellow);
                    break;

                case AppConstants.AccentColorDarkBlue:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_DarkBlue);
                    break;

                case AppConstants.AccentColorViolet:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_Violet);
                    break;

                case AppConstants.AccentColorLightGrey:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Dark_LightGrey);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(appHexAccentColor), appHexAccentColor, "Invalid accent theme");
                }
            }
            else
            {
                switch (appHexAccentColor)
                {
                case AppConstants.AccentColorLightBlue:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_LightBlue);
                    break;

                case AppConstants.AccentColorLimeGreen:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_LimeGreen);
                    break;

                case AppConstants.AccentColorDarkOrange:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_DarkOrange);
                    break;

                case AppConstants.AccentColorVividRed:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_VividRed);
                    break;

                case AppConstants.AccentColorDarkCyan:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_DarkCyan);
                    break;

                case AppConstants.AccentColorDarkGreen:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_DarkGreen);
                    break;

                case AppConstants.AccentColorDarkMagenta:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_DarkMagenta);
                    break;

                case AppConstants.AccentColorMagenta:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_Magenta);
                    break;

                case AppConstants.AccentColorDarkGray:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_DarkGray);
                    break;

                case AppConstants.AccentColorOrange:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_Orange);
                    break;

                case AppConstants.AccentColorYellow:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_Yellow);
                    break;

                case AppConstants.AccentColorDarkBlue:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_DarkBlue);
                    break;

                case AppConstants.AccentColorViolet:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_Violet);
                    break;

                case AppConstants.AccentColorLightGrey:
                    SetTheme(Resource.Style.Theme_MiraiNotes_Light_LightGrey);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(appHexAccentColor), appHexAccentColor, "Invalid accent theme");
                }
            }

            if (useDarkAmoled && appTheme == AppThemeType.DARK)
            {
                Theme.ApplyStyle(Resource.Style.BlackTheme, true);
            }

            if (!restartActivity)
            {
                return;
            }

            RestartActivity();
        }