Skip to content

Commit 0bcc187

Browse files
committed
Keep user settings when changing theme
1 parent bf5591c commit 0bcc187

File tree

6 files changed

+87
-84
lines changed

6 files changed

+87
-84
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -416,21 +416,24 @@ private static void SetResizeBoarderThickness(Thickness? effectMargin)
416416
/// </summary>
417417
public async Task RefreshFrameAsync()
418418
{
419-
await Application.Current.Dispatcher.InvokeAsync(async () =>
419+
await Application.Current.Dispatcher.InvokeAsync(() =>
420420
{
421+
// Get the actual backdrop type and drop shadow effect settings
422+
var (backdropType, useDropShadowEffect) = GetActualValue();
423+
421424
// Remove OS minimizing/maximizing animation
422425
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
423-
426+
424427
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
425428
if (BlurEnabled)
426429
{
427-
AutoDropShadow();
430+
AutoDropShadow(useDropShadowEffect);
428431
}
429-
await SetBlurForWindowAsync();
432+
SetBlurForWindow(backdropType);
430433

431434
if (!BlurEnabled)
432435
{
433-
AutoDropShadow();
436+
AutoDropShadow(useDropShadowEffect);
434437
}
435438
}, DispatcherPriority.Normal);
436439
}
@@ -440,61 +443,87 @@ await Application.Current.Dispatcher.InvokeAsync(async () =>
440443
/// </summary>
441444
public async Task SetBlurForWindowAsync()
442445
{
443-
await Application.Current.Dispatcher.InvokeAsync(async () =>
446+
await Application.Current.Dispatcher.InvokeAsync(() =>
444447
{
445-
var dict = GetThemeResourceDictionary(_settings.Theme);
446-
if (dict == null)
447-
return;
448+
// Get the actual backdrop type and drop shadow effect settings
449+
var (backdropType, _) = GetActualValue();
448450

449-
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
450-
if (windowBorderStyle == null)
451-
return;
451+
SetBlurForWindow(backdropType);
452+
}, DispatcherPriority.Normal);
453+
}
452454

453-
Window mainWindow = Application.Current.MainWindow;
454-
if (mainWindow == null)
455-
return;
455+
/// <summary>
456+
/// Gets the actual backdrop type and drop shadow effect settings based on the current theme status.
457+
/// </summary>
458+
public (BackdropTypes BackdropType, bool UseDropShadowEffect) GetActualValue()
459+
{
460+
var backdropType = _settings.BackdropType;
461+
var useDropShadowEffect = _settings.UseDropShadowEffect;
456462

457-
// Check if the theme supports blur
458-
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
459-
if (!hasBlur)
460-
{
461-
_settings.BackdropType = BackdropTypes.None;
462-
}
463+
// When changed non-blur theme, change to backdrop to none
464+
if (!BlurEnabled)
465+
{
466+
backdropType = BackdropTypes.None;
467+
}
463468

464-
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
465-
{
466-
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
467-
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
468-
{
469-
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
470-
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
471-
}
472-
else if (_settings.BackdropType == BackdropTypes.Acrylic)
473-
{
474-
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
475-
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
476-
}
469+
// Dropshadow on and control disabled.(user can't change dropshadow with blur theme)
470+
if (BlurEnabled)
471+
{
472+
useDropShadowEffect = true;
473+
}
477474

478-
// Apply the blur effect
479-
Win32Helper.DWMSetBackdropForWindow(mainWindow, _settings.BackdropType);
480-
ColorizeWindow();
475+
return (backdropType, useDropShadowEffect);
476+
}
477+
478+
private void SetBlurForWindow(BackdropTypes backdropType)
479+
{
480+
var dict = GetThemeResourceDictionary(_settings.Theme);
481+
if (dict == null)
482+
return;
483+
484+
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
485+
if (windowBorderStyle == null)
486+
return;
487+
488+
Window mainWindow = Application.Current.MainWindow;
489+
if (mainWindow == null)
490+
return;
491+
492+
// Check if the theme supports blur
493+
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
494+
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
495+
{
496+
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
497+
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
498+
{
499+
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
500+
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
481501
}
482-
else
502+
else if (backdropType == BackdropTypes.Acrylic)
483503
{
484-
// Apply default style when Blur is disabled
485-
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
486-
ColorizeWindow();
504+
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
505+
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
487506
}
488507

489-
UpdateResourceDictionary(dict);
490-
}, DispatcherPriority.Normal);
508+
// Apply the blur effect
509+
Win32Helper.DWMSetBackdropForWindow(mainWindow, backdropType);
510+
ColorizeWindow(backdropType);
511+
}
512+
else
513+
{
514+
// Apply default style when Blur is disabled
515+
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
516+
ColorizeWindow(backdropType);
517+
}
518+
519+
UpdateResourceDictionary(dict);
491520
}
492521

493-
private void AutoDropShadow()
522+
private void AutoDropShadow(bool useDropShadowEffect)
494523
{
495524
SetWindowCornerPreference("Default");
496525
RemoveDropShadowEffectFromCurrentTheme();
497-
if (_settings.UseDropShadowEffect)
526+
if (useDropShadowEffect)
498527
{
499528
if (BlurEnabled && Win32Helper.IsBackdropSupported())
500529
{
@@ -605,7 +634,7 @@ private void ApplyPreviewBackground(Color? bgColor = null)
605634
Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle;
606635
}
607636

608-
private void ColorizeWindow()
637+
private void ColorizeWindow(BackdropTypes backdropType)
609638
{
610639
var dict = GetThemeResourceDictionary(_settings.Theme);
611640
if (dict == null) return;
@@ -688,7 +717,7 @@ private void ColorizeWindow()
688717
else
689718
{
690719
// Only set the background to transparent if the theme supports blur
691-
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
720+
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
692721
{
693722
mainWindow.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
694723
}

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public string Theme
7676
}
7777
}
7878
public bool UseDropShadowEffect { get; set; } = true;
79-
8079
public BackdropTypes BackdropType{ get; set; } = BackdropTypes.None;
8180

8281
/* Appearance Settings. It should be separated from the setting later.*/

Flow.Launcher/App.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
137137
await PluginManager.InitializePluginsAsync();
138138
await imageLoadertask;
139139

140-
var mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
141-
var window = new MainWindow(_settings, mainVM);
140+
var window = new MainWindow();
142141

143142
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");
144143

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public partial class MainWindow
3636
#region Private Fields
3737

3838
private readonly Settings _settings;
39+
private readonly Theme _theme;
3940
private NotifyIcon _notifyIcon;
4041
private readonly ContextMenu contextMenu = new();
4142
private readonly MainViewModel _viewModel;
@@ -58,11 +59,12 @@ public partial class MainWindow
5859

5960
#region Constructor
6061

61-
public MainWindow(Settings settings, MainViewModel mainVM)
62+
public MainWindow()
6263
{
63-
DataContext = mainVM;
64-
_viewModel = mainVM;
65-
_settings = settings;
64+
_settings = Ioc.Default.GetRequiredService<Settings>();
65+
_theme = Ioc.Default.GetRequiredService<Theme>();
66+
_viewModel = Ioc.Default.GetRequiredService<MainViewModel>();
67+
DataContext = _viewModel;
6668

6769
InitializeComponent();
6870
UpdatePosition(true);
@@ -390,7 +392,8 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
390392
if (_initialHeight != (int)Height)
391393
{
392394
var shadowMargin = 0;
393-
if (_settings.UseDropShadowEffect)
395+
var (_, useDropShadowEffect) = _theme.GetActualValue();
396+
if (useDropShadowEffect)
394397
{
395398
shadowMargin = 32;
396399
}

Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ public Theme.ThemeData SelectedTheme
3535
_selectedTheme = value;
3636
ThemeManager.Instance.ChangeTheme(value.FileNameWithoutExtension);
3737

38-
// when changed non-blur theme, change to backdrop to none
39-
if (!ThemeManager.Instance.BlurEnabled)
40-
{
41-
Settings.BackdropType = BackdropTypes.None;
42-
}
43-
44-
// dropshadow on and control disabled.(user can't change dropshadow with blur theme)
45-
if (ThemeManager.Instance.BlurEnabled)
46-
{
47-
Settings.UseDropShadowEffect = true;
48-
}
49-
5038
// Update UI state
5139
OnPropertyChanged(nameof(BackdropType));
5240
OnPropertyChanged(nameof(IsBackdropEnabled));
@@ -235,8 +223,8 @@ public void ApplyBackdrop()
235223
public BackdropTypes BackdropType
236224
{
237225
get => Enum.IsDefined(typeof(BackdropTypes), Settings.BackdropType)
238-
? Settings.BackdropType
239-
: BackdropTypes.None;
226+
? Settings.BackdropType
227+
: BackdropTypes.None;
240228
set
241229
{
242230
if (!Enum.IsDefined(typeof(BackdropTypes), value))

Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
using System.Linq;
33
using CommunityToolkit.Mvvm.DependencyInjection;
44
using Flow.Launcher.Core.Resource;
5-
using FLSettings = Flow.Launcher.Infrastructure.UserSettings.Settings;
65

76
namespace Flow.Launcher.Plugin.Sys
87
{
98
public class ThemeSelector
109
{
1110
public const string Keyword = "fltheme";
1211

13-
private readonly FLSettings _settings;
1412
private readonly Theme _theme;
1513
private readonly PluginInitContext _context;
1614

@@ -27,18 +25,6 @@ public Theme.ThemeData SelectedTheme
2725
_selectedTheme = value;
2826
_theme.ChangeTheme(value.FileNameWithoutExtension);
2927

30-
// when changed non-blur theme, change to backdrop to none
31-
if (!_theme.BlurEnabled)
32-
{
33-
_settings.BackdropType = 0; // Change to 0 instead of BackdropTypes.None
34-
}
35-
36-
// dropshadow on and control disabled.(user can't change dropshadow with blur theme)
37-
if (_theme.BlurEnabled)
38-
{
39-
_settings.UseDropShadowEffect = true;
40-
}
41-
4228
_ = _theme.RefreshFrameAsync();
4329
}
4430
}
@@ -51,7 +37,6 @@ public ThemeSelector(PluginInitContext context)
5137
{
5238
_context = context;
5339
_theme = Ioc.Default.GetRequiredService<Theme>();
54-
_settings = Ioc.Default.GetRequiredService<FLSettings>();
5540
}
5641

5742
public List<Result> Query(Query query)

0 commit comments

Comments
 (0)