-
Notifications
You must be signed in to change notification settings - Fork 71
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
Support dark theme on title bar #6
Open
AlpyneDreams
wants to merge
9
commits into
skiff-org:main
Choose a base branch
from
AlpyneDreams:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
35fc725
Add theme change message
AlpyneDreams 550d987
Automatically hook localStorage for theme changes
AlpyneDreams 842425a
Revert "Automatically hook localStorage for theme changes"
AlpyneDreams 1daaeef
Fix bad line endings
AlpyneDreams 32fee1d
Remove EnsureHandle
AlpyneDreams 33b2661
Change "value" to "theme"
AlpyneDreams 16dc4df
Add ThemeWatcher
AlpyneDreams a5d78ba
Merge branch 'main' into main
AlpyneDreams 1957af3
Merge branch 'main' into main
AlpyneDreams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Microsoft.Win32; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Management; | ||
using System.Security.Principal; | ||
|
||
namespace Skiff_Desktop.Utilities | ||
{ | ||
public enum ThemeMode | ||
{ | ||
Light = 0, | ||
Dark = 1, | ||
} | ||
|
||
public class ThemeWatcher | ||
{ | ||
public ThemeMode Theme | ||
{ | ||
get => _theme; | ||
private set { | ||
_theme = value; | ||
ThemeChanged?.Invoke(this, value); | ||
} | ||
} | ||
public event EventHandler<ThemeMode>? ThemeChanged; | ||
|
||
private const string RegistryPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"; | ||
private const string RegistryValueName = "AppsUseLightTheme"; | ||
private ManagementEventWatcher? watcher; | ||
private ThemeMode _theme; | ||
|
||
public void Start() | ||
{ | ||
var currentUser = WindowsIdentity.GetCurrent(); | ||
|
||
// Based on https://stackoverflow.com/a/69604613 | ||
string query = string.Format( | ||
CultureInfo.InvariantCulture, | ||
@"SELECT * FROM RegistryValueChangeEvent WHERE Hive = 'HKEY_USERS' AND KeyPath = '{0}\\{1}' AND ValueName = '{2}'", | ||
currentUser.User?.Value, | ||
RegistryPath.Replace(@"\", @"\\"), | ||
RegistryValueName); | ||
|
||
try | ||
{ | ||
Theme = GetCurrentTheme(); | ||
|
||
// Listen for events | ||
watcher = new ManagementEventWatcher(query); | ||
watcher.EventArrived += (sender, e) => | ||
{ | ||
Theme = GetCurrentTheme(); | ||
Debug.WriteLine("System theme changed: " + Theme); | ||
}; | ||
watcher.Start(); | ||
} | ||
catch (Exception) | ||
{ | ||
// This can fail on Windows 7 | ||
Theme = ThemeMode.Light; | ||
} | ||
} | ||
|
||
public ThemeMode GetCurrentTheme() | ||
{ | ||
ThemeMode theme = ThemeMode.Light; | ||
|
||
try | ||
{ | ||
using (RegistryKey? key = Registry.CurrentUser?.OpenSubKey(RegistryPath)) | ||
{ | ||
object? value = key?.GetValue(RegistryValueName); | ||
if (value == null) | ||
return ThemeMode.Light; | ||
|
||
theme = (int)value > 0 ? ThemeMode.Light : ThemeMode.Dark; | ||
} | ||
|
||
return theme; | ||
} | ||
catch (Exception) | ||
{ | ||
return theme; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opinion: Ideally, rather than write PInvokes by hand, we would wire up CsWin32 here.