コード例 #1
0
        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));
        }
コード例 #2
0
        public static void UpdateWindowEffect(IntPtr windowHandle, bool isWindowActive = false)
        {
            var isDarkTheme = WindowsThemeHelper.AppsUseLightTheme() == false;

            // {
            //     var wtaOptions = new WTA_OPTIONS
            //     {
            //         dwFlags = WTNCA.NODRAWCAPTION | WTNCA.NODRAWICON | WTNCA.NOSYSMENU | WTNCA.NOMIRRORHELP
            //     };
            //     wtaOptions.dwMask = wtaOptions.dwFlags;
            //     NativeMethods.SetWindowThemeAttribute(windowHandle, WINDOWTHEMEATTRIBUTETYPE.WTA_NONCLIENT, ref wtaOptions, (uint)Marshal.SizeOf(typeof(WTA_OPTIONS)));
            // }

            if (OSVersionHelper.IsWindows11_OrGreater)
            {
                EnableMicaEffect(windowHandle, isDarkTheme);
            }
            else
            {
                SetAccentPolicy(windowHandle, isWindowActive, isDarkTheme);
            }
        }
コード例 #3
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;
            }
        }