public virtual Theme?GenerateRuntimeThemeFromWindowsSettings(string baseColor, bool isHighContrast, IEnumerable <LibraryThemeProvider> libraryThemeProviders)
        {
            var windowsAccentColor = WindowsThemeHelper.GetWindowsAccentColor();

            if (windowsAccentColor is null)
            {
                return(null);
            }

            var accentColor = windowsAccentColor.Value;

            return(this.GenerateRuntimeTheme(baseColor, accentColor, isHighContrast, libraryThemeProviders));
        }
Esempio n. 2
0
        public void SyncTheme(ThemeSyncMode?syncMode)
        {
            try
            {
                var syncModeToUse = syncMode ?? this.themeSyncMode;

                if (syncModeToUse == ThemeSyncMode.DoNotSync)
                {
                    return;
                }

                if (Application.Current is null)
                {
                    return;
                }

                var detectedTheme = this.DetectTheme();

                string?baseColor = null;
                if (syncModeToUse.HasFlag(ThemeSyncMode.SyncWithAppMode))
                {
                    baseColor = WindowsThemeHelper.GetWindowsBaseColor();
                }
                else
                {
                    baseColor ??= detectedTheme?.BaseColorScheme ?? BaseColorLight;
                }

                string?accentColor;
                if (syncModeToUse.HasFlag(ThemeSyncMode.SyncWithAccent))
                {
                    accentColor = WindowsThemeHelper.GetWindowsAccentColor()?.ToString() ?? detectedTheme?.ColorScheme;
                }
                else
                {
                    // If there was no detected Theme just use the windows accent color.
                    accentColor = detectedTheme?.ColorScheme ?? WindowsThemeHelper.GetWindowsAccentColor()?.ToString();
                }

                if (accentColor is null)
                {
                    throw new Exception("Accent color could not be detected.");
                }

                var isHighContrast = syncModeToUse.HasFlag(ThemeSyncMode.SyncWithHighContrast) &&
                                     WindowsThemeHelper.IsHighContrastEnabled();

                // Check if we previously generated a theme matching the desired settings
                var theme = this.GetTheme(baseColor, accentColor, isHighContrast)
                            ?? RuntimeThemeGenerator.Current.GenerateRuntimeThemeFromWindowsSettings(baseColor, isHighContrast, this.libraryThemeProvidersInternal);

                // Only change the theme if it's not the current already
                if (theme is not null &&
                    theme != detectedTheme)
                {
                    this.ChangeTheme(Application.Current, theme);
                }
            }
            finally
            {
                this.isSyncScheduled = false;
            }
        }