Skip to content

Commit 916b4ff

Browse files
authored
Merge pull request #4312 from Flow-Launcher/Theme_Enhancement
Improve code quality
2 parents 7a46fac + dad8f3e commit 916b4ff

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 35 additions & 35 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 == Control.BackgroundProperty));
677677
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(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 == Control.BackgroundProperty));
682682
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, Brushes.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);
@@ -826,20 +826,16 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
826826
if (mainWindow == null) return;
827827

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

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

834834
// Check the user's ColorScheme setting
835-
string colorScheme = _settings.ColorScheme;
836-
837-
// Check system dark mode setting (read AppsUseLightTheme value)
838-
int themeValue = (int)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1);
839-
bool isSystemDark = themeValue == 0;
835+
var colorScheme = _settings.ColorScheme;
840836

841837
// Final decision on whether to use dark mode
842-
bool useDarkMode = false;
838+
var useDarkMode = false;
843839

844840
// If systemBG is not "Auto", prioritize it over ColorScheme and set the mode based on systemBG value
845841
if (systemBG == "Dark")
@@ -854,11 +850,20 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
854850
{
855851
// If systemBG is "Auto", decide based on ColorScheme
856852
if (colorScheme == "Dark")
853+
{
857854
useDarkMode = true;
855+
}
858856
else if (colorScheme == "Light")
857+
{
859858
useDarkMode = false;
859+
}
860860
else
861+
{
862+
// Check system dark mode setting (read AppsUseLightTheme value)
863+
var themeValue = (int)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1);
864+
var isSystemDark = themeValue == 0;
861865
useDarkMode = isSystemDark; // Auto (based on system setting)
866+
}
862867
}
863868

864869
// Apply DWM Dark Mode
@@ -900,7 +905,7 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
900905
else
901906
{
902907
// Only set the background to transparent if the theme supports blur
903-
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
908+
if (backdropType is BackdropTypes.Mica or BackdropTypes.MicaAlt)
904909
{
905910
mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0));
906911
}
@@ -911,14 +916,9 @@ private void ColorizeWindow(string theme, BackdropTypes backdropType)
911916
}
912917
}
913918

914-
private static bool IsBlurTheme()
919+
private static bool IsThemeBlurEnabled(ResourceDictionary dict)
915920
{
916-
if (!Win32Helper.IsBackdropSupported()) // Windows 11 미만이면 무조건 false
917-
return false;
918-
919-
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");
920-
921-
return resource is bool b && b;
921+
return dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool enabled && enabled;
922922
}
923923

924924
#endregion

0 commit comments

Comments
 (0)