Skip to content

Commit 6566871

Browse files
committed
Refactor theme blur and dark mode handling logic
Refactored Theme.cs to improve theme blur and dark mode handling: - Replaced static IsBlurTheme() with IsThemeBlurEnabled(dict) for per-theme blur detection. - Updated all blur checks to use the new method. - Improved code style, readability, and comments. - Moved system dark mode registry check to only run when needed. - Ensured blur and dark mode are applied based on theme and user/system preferences.
1 parent dda9000 commit 6566871

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public void UpdateFonts()
124124
try
125125
{
126126
// Load a ResourceDictionary for the specified theme.
127-
var themeName = _settings.Theme;
128-
var dict = GetThemeResourceDictionary(themeName);
127+
var theme = _settings.Theme;
128+
var dict = GetThemeResourceDictionary(theme);
129129

130130
// Apply font settings to the theme resource.
131131
ApplyFontSettings(dict);
@@ -292,10 +292,10 @@ private ResourceDictionary GetResourceDictionary(string theme)
292292
dict["ItemHotkeyStyle"] is Style resultHotkeyItemStyle &&
293293
dict["ItemHotkeySelectedStyle"] is Style resultHotkeyItemSelectedStyle)
294294
{
295-
Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(_settings.ResultFont));
296-
Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultFontStyle));
297-
Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultFontWeight));
298-
Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultFontStretch));
295+
var fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(_settings.ResultFont));
296+
var fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultFontStyle));
297+
var fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultFontWeight));
298+
var fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultFontStretch));
299299

300300
Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch };
301301
Array.ForEach(
@@ -307,10 +307,10 @@ private ResourceDictionary GetResourceDictionary(string theme)
307307
dict["ItemSubTitleStyle"] is Style resultSubItemStyle &&
308308
dict["ItemSubTitleSelectedStyle"] is Style resultSubItemSelectedStyle)
309309
{
310-
Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(_settings.ResultSubFont));
311-
Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultSubFontStyle));
312-
Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultSubFontWeight));
313-
Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultSubFontStretch));
310+
var fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(_settings.ResultSubFont));
311+
var fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultSubFontStyle));
312+
var fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultSubFontWeight));
313+
var fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultSubFontStretch));
314314

315315
Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch };
316316
Array.ForEach(
@@ -395,7 +395,7 @@ public ThemeData GetCurrentTheme()
395395

396396
public List<ThemeData> GetAvailableThemes()
397397
{
398-
List<ThemeData> themes = new List<ThemeData>();
398+
var themes = new List<ThemeData>();
399399
foreach (var themeDirectory in _themeDirectories)
400400
{
401401
var filePaths = Directory
@@ -410,8 +410,7 @@ public List<ThemeData> GetAvailableThemes()
410410

411411
public bool ChangeTheme(string theme = null)
412412
{
413-
if (string.IsNullOrEmpty(theme))
414-
theme = _settings.Theme;
413+
if (string.IsNullOrEmpty(theme)) theme = _settings.Theme;
415414

416415
string path = GetThemePath(theme);
417416
try
@@ -426,13 +425,14 @@ public bool ChangeTheme(string theme = null)
426425

427426
_settings.Theme = theme;
428427

429-
//always allow re-loading default theme, in case of failure of switching to a new theme from default theme
428+
// Always allow re-loading default theme, in case of failure of switching to a new theme from default theme
430429
if (_oldTheme != theme || theme == Constant.DefaultTheme)
431430
{
432431
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
433432
}
434433

435-
BlurEnabled = IsBlurTheme();
434+
// Check if blur is enabled
435+
BlurEnabled = Win32Helper.IsBackdropSupported() && IsThemeBlurEnabled(resourceDict);
436436

437437
// Apply blur and drop shadow effect so that we do not need to call it again
438438
_ = RefreshFrameAsync();
@@ -667,11 +667,11 @@ private void SetBlurForWindow(string theme, BackdropTypes backdropType)
667667
if (mainWindow == null) return;
668668

669669
// Check if the theme supports blur
670-
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
670+
var hasBlur = IsThemeBlurEnabled(dict);
671671
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
672672
{
673673
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
674-
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
674+
if (backdropType is BackdropTypes.Mica or BackdropTypes.MicaAlt)
675675
{
676676
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
677677
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
@@ -681,15 +681,15 @@ private void SetBlurForWindow(string theme, BackdropTypes backdropType)
681681
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
682682
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
683683
}
684-
684+
685685
// For themes with blur enabled, the window border is rendered by the system, so it's treated as a simple rectangle regardless of thickness.
686686
//(This is to avoid issues when the window is forcibly changed to a rectangular shape during snap scenarios.)
687687
var cornerRadiusSetter = windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property == Border.CornerRadiusProperty);
688688
if (cornerRadiusSetter != null)
689689
cornerRadiusSetter.Value = new CornerRadius(0);
690690
else
691691
windowBorderStyle.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(0)));
692-
692+
693693
// Apply the blur effect
694694
Win32Helper.DWMSetBackdropForWindow(mainWindow, backdropType);
695695
ColorizeWindow(theme, backdropType);
@@ -802,7 +802,7 @@ private void ApplyPreviewBackground(Color? bgColor = null)
802802
}
803803

804804
// Apply background color (remove transparency in color)
805-
Color backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B);
805+
var backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B);
806806
previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(backgroundColor)));
807807

808808
// The blur theme keeps the corner round fixed (applying DWM code to modify it causes rendering issues).
@@ -841,20 +841,16 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
841841
if (mainWindow == null) return;
842842

843843
// Check if the theme supports blur
844-
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
844+
var hasBlur = IsThemeBlurEnabled(dict);
845845

846846
// SystemBG value check (Auto, Light, Dark)
847-
string systemBG = dict.Contains("SystemBG") ? dict["SystemBG"] as string : "Auto"; // 기본값 Auto
847+
var systemBG = dict.Contains("SystemBG") ? dict["SystemBG"] as string : "Auto"; // 기본값 Auto
848848

849849
// Check the user's ColorScheme setting
850-
string colorScheme = _settings.ColorScheme;
851-
852-
// Check system dark mode setting (read AppsUseLightTheme value)
853-
int themeValue = (int)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1);
854-
bool isSystemDark = themeValue == 0;
850+
var colorScheme = _settings.ColorScheme;
855851

856852
// Final decision on whether to use dark mode
857-
bool useDarkMode = false;
853+
var useDarkMode = false;
858854

859855
// If systemBG is not "Auto", prioritize it over ColorScheme and set the mode based on systemBG value
860856
if (systemBG == "Dark")
@@ -869,11 +865,20 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
869865
{
870866
// If systemBG is "Auto", decide based on ColorScheme
871867
if (colorScheme == "Dark")
868+
{
872869
useDarkMode = true;
870+
}
873871
else if (colorScheme == "Light")
872+
{
874873
useDarkMode = false;
874+
}
875875
else
876+
{
877+
// Check system dark mode setting (read AppsUseLightTheme value)
878+
var themeValue = (int)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1);
879+
var isSystemDark = themeValue == 0;
876880
useDarkMode = isSystemDark; // Auto (based on system setting)
881+
}
877882
}
878883

879884
// Apply DWM Dark Mode
@@ -915,7 +920,7 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
915920
else
916921
{
917922
// Only set the background to transparent if the theme supports blur
918-
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
923+
if (backdropType is BackdropTypes.Mica or BackdropTypes.MicaAlt)
919924
{
920925
mainWindow.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
921926
}
@@ -926,14 +931,9 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
926931
}
927932
}
928933

929-
private static bool IsBlurTheme()
934+
private static bool IsThemeBlurEnabled(ResourceDictionary dict)
930935
{
931-
if (!Win32Helper.IsBackdropSupported()) // Windows 11 미만이면 무조건 false
932-
return false;
933-
934-
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");
935-
936-
return resource is bool b && b;
936+
return dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool enabled && enabled;
937937
}
938938

939939
#endregion

0 commit comments

Comments
 (0)