Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI 简易调试界面深色主题 #597

Open
Timothy-Liuxf opened this issue May 25, 2023 · 1 comment
Open

GUI 简易调试界面深色主题 #597

Timothy-Liuxf opened this issue May 25, 2023 · 1 comment
Assignees
Labels
cake Future work enhancement New feature or request

Comments

@Timothy-Liuxf
Copy link
Member

Is your feature request related to a problem? Please describe.
目前 WPF 的 GUI 简易调试界面是浅色主题,底色为纯白,这对深夜写代码和习惯暗色的选手不是特别友好。

Describe the solution you'd like
可以考虑增加深色主题,选手可以选择浅色或深色,或跟随操作系统深浅色设置。

Describe alternatives you've considered
不知道 WPF / UWP / WinUI3 / MAUI 都有没有现成的轮子可以用(个人感觉至少后三者是有的)

Additional context
作为未来的饼吧(

@Timothy-Liuxf Timothy-Liuxf added enhancement New feature or request cake Future work labels May 25, 2023
@tyanyuy3125
Copy link

tyanyuy3125 commented May 29, 2023

颜色主题的实现并没有那么复杂,可以使用两套平行的 ResourceDictionary 。在运行时动态修改 Application.Current.Resources.MergedDictionaries 即可。

关于获取系统深浅色设置,以下是一种可能的实现方案。使用事件驱动的方式可以避免轮询带来的性能下降。(来自我的私有库)

    using System.Management;

...

    private const string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
    private const string RegistryValueName = "AppsUseLightTheme";

    private static ManagementEventWatcher _managementEventWatcher;
    private static bool _watcherStarted;

    #region System Theme

    private static void SwitchThemeInternal(Theme theme)
    {
        var uri = theme switch
        {
            Theme.Light => new Uri(LightRd),
            Theme.Dark => new Uri(DarkRd),
            _ => new Uri(LightRd)
        };
        Application.Current.Resources.MergedDictionaries[0].Source = uri;
    }

    private static void WatchTheme()
    {
        var currentUser = WindowsIdentity.GetCurrent();
        var query = string.Format(
            CultureInfo.InvariantCulture,
            @"SELECT * FROM RegistryValueChangeEvent WHERE Hive = 'HKEY_USERS' AND KeyPath = '{0}\\{1}' AND ValueName = '{2}'",
            currentUser.User.Value,
            RegistryKeyPath.Replace(@"\", @"\\"),
            RegistryValueName);

        try
        {
            _managementEventWatcher = new ManagementEventWatcher(query);
            _managementEventWatcher.EventArrived += (_, _) =>
            {
                var newWindowsTheme = GetWindowsTheme();
                SwitchThemeInternal(newWindowsTheme);
                // React to new theme
            };

            // Start listening for events
            _managementEventWatcher.Start();
            _watcherStarted = true;
        }
        catch (Exception)
        {
            // This can fail on Windows 7
        }

        var initialTheme = GetWindowsTheme();
        SwitchThemeInternal(initialTheme);
    }

    private static Theme GetWindowsTheme()
    {
        using var key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath);
        var registryValueObject = key?.GetValue(RegistryValueName);

        if (registryValueObject == null) return Theme.Light;

        var registryValue = (int)registryValueObject;

        return registryValue > 0 ? Theme.Light : Theme.Dark;
    }

    #endregion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cake Future work enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants