Skip to content

Commit 07b0e72

Browse files
committed
Code cleanup
1 parent 29c6f12 commit 07b0e72

File tree

1 file changed

+50
-85
lines changed

1 file changed

+50
-85
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 50 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@
1212
using Flow.Launcher.Infrastructure;
1313
using Flow.Launcher.Infrastructure.Logger;
1414
using Flow.Launcher.Infrastructure.UserSettings;
15-
using System.Windows.Shell;
1615
using static Flow.Launcher.Core.Resource.Theme.ParameterTypes;
1716
using System.Runtime.InteropServices;
1817
using System.Windows.Interop;
19-
using System.Diagnostics;
2018
using Microsoft.Win32;
21-
using CommunityToolkit.Mvvm.DependencyInjection;
2219
using Flow.Launcher.Plugin;
23-
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
2420
using TextBox = System.Windows.Controls.TextBox;
2521
using System.Windows.Threading;
26-
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
2722

2823
namespace Flow.Launcher.Core.Resource
2924
{
@@ -49,8 +44,6 @@ public class Theme
4944

5045
public bool BlurEnabled { get; set; }
5146

52-
private double mainWindowWidth;
53-
5447
public Theme(IPublicAPI publicAPI, Settings settings)
5548
{
5649
_api = publicAPI;
@@ -125,23 +118,16 @@ public static int SetWindowAttribute(IntPtr hwnd, ParameterTypes.DWMWINDOWATTRIB
125118
=> DwmSetWindowAttribute(hwnd, attribute, ref parameter, Marshal.SizeOf<int>());
126119
}
127120

128-
private bool IsBackdropSupported()
129-
{
130-
// Windows 11 (22000) over mica and arcrylic.
131-
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
132-
Environment.OSVersion.Version.Build >= 22000;
133-
}
134-
private System.Windows.Window GetMainWindow()
121+
private Window GetMainWindow()
135122
{
136123
return Application.Current.Dispatcher.Invoke(() => Application.Current.MainWindow);
137124
}
138125

139-
140126
public void RefreshFrame()
141127
{
142128
Application.Current.Dispatcher.Invoke(() =>
143129
{
144-
System.Windows.Window mainWindow = Application.Current.MainWindow;
130+
Window mainWindow = Application.Current.MainWindow;
145131
if (mainWindow == null)
146132
return;
147133

@@ -170,15 +156,13 @@ public void RefreshFrame()
170156
}, DispatcherPriority.Normal);
171157
}
172158

173-
174-
175159
public void AutoDropShadow()
176160
{
177161
SetWindowCornerPreference("Default");
178162
RemoveDropShadowEffectFromCurrentTheme();
179163
if (_settings.UseDropShadowEffect)
180164
{
181-
if (BlurEnabled && IsBackdropSupported())
165+
if (BlurEnabled && Win32Helper.IsBackdropSupported())
182166
{
183167
SetWindowCornerPreference("Round");
184168
}
@@ -190,7 +174,7 @@ public void AutoDropShadow()
190174
}
191175
else
192176
{
193-
if (BlurEnabled && IsBackdropSupported())
177+
if (BlurEnabled && Win32Helper.IsBackdropSupported())
194178
{
195179
SetWindowCornerPreference("Default");
196180
}
@@ -257,7 +241,7 @@ public void SetBlurForWindow()
257241
_ => 0 // None
258242
};
259243

260-
if (BlurEnabled && hasBlur && IsBackdropSupported())
244+
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
261245
{
262246
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
263247
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
@@ -291,9 +275,6 @@ public void SetBlurForWindow()
291275
}, DispatcherPriority.Normal);
292276
}
293277

294-
295-
296-
297278
// Get Background Color from WindowBorderStyle when there not color for BG.
298279
// for theme has not "LightBG" or "DarkBG" case.
299280
private Color GetWindowBorderStyleBackground()
@@ -348,8 +329,7 @@ private void ApplyPreviewBackground(Color? bgColor = null)
348329
var previewStyle = new Style(typeof(Border));
349330
if (Application.Current.Resources.Contains("WindowBorderStyle"))
350331
{
351-
var originalStyle = Application.Current.Resources["WindowBorderStyle"] as Style;
352-
if (originalStyle != null)
332+
if (Application.Current.Resources["WindowBorderStyle"] is Style originalStyle)
353333
{
354334
foreach (var setter in originalStyle.Setters.OfType<Setter>())
355335
{
@@ -375,7 +355,6 @@ private void ApplyPreviewBackground(Color? bgColor = null)
375355
}, DispatcherPriority.Render);
376356
}
377357

378-
379358
public void ColorizeWindow(string Mode)
380359
{
381360
Application.Current.Dispatcher.Invoke(() =>
@@ -452,7 +431,7 @@ public void ColorizeWindow(string Mode)
452431
Color selectedBG = useDarkMode ? DarkBG : LightBG;
453432
ApplyPreviewBackground(selectedBG);
454433

455-
bool isBlurAvailable = hasBlur && IsBackdropSupported(); // Windows 11 미만이면 hasBlur를 강제 false
434+
bool isBlurAvailable = hasBlur && Win32Helper.IsBackdropSupported(); // Windows 11 미만이면 hasBlur를 강제 false
456435

457436
if (!isBlurAvailable)
458437
{
@@ -473,25 +452,24 @@ public void ColorizeWindow(string Mode)
473452
}, DispatcherPriority.Normal);
474453
}
475454

476-
477-
478455
public bool IsBlurTheme()
479456
{
480-
if (!IsBackdropSupported()) // Windows 11 미만이면 무조건 false
457+
if (!Win32Helper.IsBackdropSupported()) // Windows 11 미만이면 무조건 false
481458
return false;
482459

483460
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");
484461

485462
return resource is bool b && b;
486463
}
464+
487465
public string GetSystemBG()
488466
{
489467
if (Environment.OSVersion.Version >= new Version(6, 2))
490468
{
491469
var resource = Application.Current.TryFindResource("SystemBG");
492470

493-
if (resource is string)
494-
return (string)resource;
471+
if (resource is string str)
472+
return str;
495473

496474
return null;
497475
}
@@ -532,7 +510,6 @@ public bool ChangeTheme(string theme)
532510

533511
_settings.Theme = theme;
534512

535-
536513
//always allow re-loading default theme, in case of failure of switching to a new theme from default theme
537514
if (_oldTheme != theme || theme == defaultTheme)
538515
{
@@ -542,9 +519,6 @@ public bool ChangeTheme(string theme)
542519
BlurEnabled = IsBlurTheme();
543520
//if (_settings.UseDropShadowEffect)
544521
// AddDropShadowEffectToCurrentTheme();
545-
546-
547-
548522
//Win32Helper.SetBlurForWindow(Application.Current.MainWindow, BlurEnabled);
549523
SetBlurForWindow();
550524
}
@@ -591,8 +565,6 @@ private ResourceDictionary GetThemeResourceDictionary(string theme)
591565
return dict;
592566
}
593567

594-
private ResourceDictionary CurrentThemeResourceDictionary() => GetThemeResourceDictionary(_settings.Theme);
595-
596568
public ResourceDictionary GetResourceDictionary(string theme)
597569
{
598570
var dict = GetThemeResourceDictionary(theme);
@@ -657,8 +629,7 @@ public ResourceDictionary GetResourceDictionary(string theme)
657629
/* Ignore Theme Window Width and use setting */
658630
var windowStyle = dict["WindowStyle"] as Style;
659631
var width = _settings.WindowSize;
660-
windowStyle.Setters.Add(new Setter(System.Windows.Window.WidthProperty, width));
661-
mainWindowWidth = (double)width;
632+
windowStyle.Setters.Add(new Setter(Window.WidthProperty, width));
662633
return dict;
663634
}
664635

@@ -705,11 +676,11 @@ private ThemeData GetThemeDataFromPath(string path)
705676
}
706677
else if (line.StartsWith(ThemeMetadataIsDarkPrefix, StringComparison.OrdinalIgnoreCase))
707678
{
708-
isDark = bool.Parse(line.Remove(0, ThemeMetadataIsDarkPrefix.Length).Trim());
679+
isDark = bool.Parse(line[ThemeMetadataIsDarkPrefix.Length..].Trim());
709680
}
710681
else if (line.StartsWith(ThemeMetadataHasBlurPrefix, StringComparison.OrdinalIgnoreCase))
711682
{
712-
hasBlur = bool.Parse(line.Remove(0, ThemeMetadataHasBlurPrefix.Length).Trim());
683+
hasBlur = bool.Parse(line[ThemeMetadataHasBlurPrefix.Length..].Trim());
713684
}
714685
}
715686

@@ -732,69 +703,63 @@ private string GetThemePath(string themeName)
732703

733704
public void AddDropShadowEffectToCurrentTheme()
734705
{
735-
736706
var dict = GetCurrentResourceDictionary();
737707

738-
var windowBorderStyle = dict["WindowBorderStyle"] as Style;
708+
var windowBorderStyle = dict["WindowBorderStyle"] as Style;
739709

740-
var effectSetter = new Setter
710+
var effectSetter = new Setter
711+
{
712+
Property = Border.EffectProperty,
713+
Value = new DropShadowEffect
741714
{
742-
Property = Border.EffectProperty,
743-
Value = new DropShadowEffect
744-
{
745-
Opacity = 0.3,
746-
ShadowDepth = 12,
747-
Direction = 270,
748-
BlurRadius = 30
749-
}
750-
};
715+
Opacity = 0.3,
716+
ShadowDepth = 12,
717+
Direction = 270,
718+
BlurRadius = 30
719+
}
720+
};
751721

752-
var marginSetter = windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == Border.MarginProperty) as Setter;
753-
if (marginSetter == null)
722+
if (windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == Border.MarginProperty) is not Setter marginSetter)
723+
{
724+
var margin = new Thickness(ShadowExtraMargin, 12, ShadowExtraMargin, ShadowExtraMargin);
725+
marginSetter = new Setter()
754726
{
755-
var margin = new Thickness(ShadowExtraMargin, 12, ShadowExtraMargin, ShadowExtraMargin);
756-
marginSetter = new Setter()
757-
{
758-
Property = Border.MarginProperty,
759-
Value = margin,
760-
};
761-
windowBorderStyle.Setters.Add(marginSetter);
727+
Property = Border.MarginProperty,
728+
Value = margin,
729+
};
730+
windowBorderStyle.Setters.Add(marginSetter);
762731

763-
SetResizeBoarderThickness(margin);
764-
}
765-
else
766-
{
767-
var baseMargin = (Thickness)marginSetter.Value;
768-
var newMargin = new Thickness(
769-
baseMargin.Left + ShadowExtraMargin,
770-
baseMargin.Top + ShadowExtraMargin,
771-
baseMargin.Right + ShadowExtraMargin,
772-
baseMargin.Bottom + ShadowExtraMargin);
773-
marginSetter.Value = newMargin;
732+
SetResizeBoarderThickness(margin);
733+
}
734+
else
735+
{
736+
var baseMargin = (Thickness)marginSetter.Value;
737+
var newMargin = new Thickness(
738+
baseMargin.Left + ShadowExtraMargin,
739+
baseMargin.Top + ShadowExtraMargin,
740+
baseMargin.Right + ShadowExtraMargin,
741+
baseMargin.Bottom + ShadowExtraMargin);
742+
marginSetter.Value = newMargin;
774743

775-
SetResizeBoarderThickness(newMargin);
776-
}
744+
SetResizeBoarderThickness(newMargin);
745+
}
777746

778-
windowBorderStyle.Setters.Add(effectSetter);
747+
windowBorderStyle.Setters.Add(effectSetter);
779748

780749
UpdateResourceDictionary(dict);
781-
782750
}
783751

784752
public void RemoveDropShadowEffectFromCurrentTheme()
785753
{
786-
787754
var dict = GetCurrentResourceDictionary();
788755
var windowBorderStyle = dict["WindowBorderStyle"] as Style;
789756

790-
var effectSetter = windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == Border.EffectProperty) as Setter;
791-
var marginSetter = windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == Border.MarginProperty) as Setter;
792-
793-
if (effectSetter != null)
757+
if (windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == Border.EffectProperty) is Setter effectSetter)
794758
{
795759
windowBorderStyle.Setters.Remove(effectSetter);
796760
}
797-
if (marginSetter != null)
761+
762+
if (windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == Border.MarginProperty) is Setter marginSetter)
798763
{
799764
var currentMargin = (Thickness)marginSetter.Value;
800765
var newMargin = new Thickness(

0 commit comments

Comments
 (0)