Keep user settings when changing theme

This commit is contained in:
Jack251970 2025-03-16 23:55:15 +08:00
parent bf5591c9a8
commit 0bcc187b5d
6 changed files with 94 additions and 91 deletions

View file

@ -416,21 +416,24 @@ namespace Flow.Launcher.Core.Resource
/// </summary>
public async Task RefreshFrameAsync()
{
await Application.Current.Dispatcher.InvokeAsync(async () =>
await Application.Current.Dispatcher.InvokeAsync(() =>
{
// Get the actual backdrop type and drop shadow effect settings
var (backdropType, useDropShadowEffect) = GetActualValue();
// Remove OS minimizing/maximizing animation
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
if (BlurEnabled)
{
AutoDropShadow();
AutoDropShadow(useDropShadowEffect);
}
await SetBlurForWindowAsync();
SetBlurForWindow(backdropType);
if (!BlurEnabled)
{
AutoDropShadow();
AutoDropShadow(useDropShadowEffect);
}
}, DispatcherPriority.Normal);
}
@ -440,61 +443,87 @@ namespace Flow.Launcher.Core.Resource
/// </summary>
public async Task SetBlurForWindowAsync()
{
await Application.Current.Dispatcher.InvokeAsync(async () =>
await Application.Current.Dispatcher.InvokeAsync(() =>
{
var dict = GetThemeResourceDictionary(_settings.Theme);
if (dict == null)
return;
// Get the actual backdrop type and drop shadow effect settings
var (backdropType, _) = GetActualValue();
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
if (windowBorderStyle == null)
return;
Window mainWindow = Application.Current.MainWindow;
if (mainWindow == null)
return;
// Check if the theme supports blur
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
if (!hasBlur)
{
_settings.BackdropType = BackdropTypes.None;
}
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
{
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
{
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
}
else if (_settings.BackdropType == BackdropTypes.Acrylic)
{
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
}
// Apply the blur effect
Win32Helper.DWMSetBackdropForWindow(mainWindow, _settings.BackdropType);
ColorizeWindow();
}
else
{
// Apply default style when Blur is disabled
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
ColorizeWindow();
}
UpdateResourceDictionary(dict);
SetBlurForWindow(backdropType);
}, DispatcherPriority.Normal);
}
private void AutoDropShadow()
/// <summary>
/// Gets the actual backdrop type and drop shadow effect settings based on the current theme status.
/// </summary>
public (BackdropTypes BackdropType, bool UseDropShadowEffect) GetActualValue()
{
var backdropType = _settings.BackdropType;
var useDropShadowEffect = _settings.UseDropShadowEffect;
// When changed non-blur theme, change to backdrop to none
if (!BlurEnabled)
{
backdropType = BackdropTypes.None;
}
// Dropshadow on and control disabled.(user can't change dropshadow with blur theme)
if (BlurEnabled)
{
useDropShadowEffect = true;
}
return (backdropType, useDropShadowEffect);
}
private void SetBlurForWindow(BackdropTypes backdropType)
{
var dict = GetThemeResourceDictionary(_settings.Theme);
if (dict == null)
return;
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
if (windowBorderStyle == null)
return;
Window mainWindow = Application.Current.MainWindow;
if (mainWindow == null)
return;
// Check if the theme supports blur
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
{
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
{
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
}
else if (backdropType == BackdropTypes.Acrylic)
{
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
}
// Apply the blur effect
Win32Helper.DWMSetBackdropForWindow(mainWindow, backdropType);
ColorizeWindow(backdropType);
}
else
{
// Apply default style when Blur is disabled
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
ColorizeWindow(backdropType);
}
UpdateResourceDictionary(dict);
}
private void AutoDropShadow(bool useDropShadowEffect)
{
SetWindowCornerPreference("Default");
RemoveDropShadowEffectFromCurrentTheme();
if (_settings.UseDropShadowEffect)
if (useDropShadowEffect)
{
if (BlurEnabled && Win32Helper.IsBackdropSupported())
{
@ -605,7 +634,7 @@ namespace Flow.Launcher.Core.Resource
Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle;
}
private void ColorizeWindow()
private void ColorizeWindow(BackdropTypes backdropType)
{
var dict = GetThemeResourceDictionary(_settings.Theme);
if (dict == null) return;
@ -688,7 +717,7 @@ namespace Flow.Launcher.Core.Resource
else
{
// Only set the background to transparent if the theme supports blur
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
{
mainWindow.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
}

View file

@ -76,7 +76,6 @@ namespace Flow.Launcher.Infrastructure.UserSettings
}
}
public bool UseDropShadowEffect { get; set; } = true;
public BackdropTypes BackdropType{ get; set; } = BackdropTypes.None;
/* Appearance Settings. It should be separated from the setting later.*/

View file

@ -137,8 +137,7 @@ namespace Flow.Launcher
await PluginManager.InitializePluginsAsync();
await imageLoadertask;
var mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
var window = new MainWindow(_settings, mainVM);
var window = new MainWindow();
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");

View file

@ -36,6 +36,7 @@ namespace Flow.Launcher
#region Private Fields
private readonly Settings _settings;
private readonly Theme _theme;
private NotifyIcon _notifyIcon;
private readonly ContextMenu contextMenu = new();
private readonly MainViewModel _viewModel;
@ -58,11 +59,12 @@ namespace Flow.Launcher
#region Constructor
public MainWindow(Settings settings, MainViewModel mainVM)
public MainWindow()
{
DataContext = mainVM;
_viewModel = mainVM;
_settings = settings;
_settings = Ioc.Default.GetRequiredService<Settings>();
_theme = Ioc.Default.GetRequiredService<Theme>();
_viewModel = Ioc.Default.GetRequiredService<MainViewModel>();
DataContext = _viewModel;
InitializeComponent();
UpdatePosition(true);
@ -390,7 +392,8 @@ namespace Flow.Launcher
if (_initialHeight != (int)Height)
{
var shadowMargin = 0;
if (_settings.UseDropShadowEffect)
var (_, useDropShadowEffect) = _theme.GetActualValue();
if (useDropShadowEffect)
{
shadowMargin = 32;
}

View file

@ -35,18 +35,6 @@ public partial class SettingsPaneThemeViewModel : BaseModel
_selectedTheme = value;
ThemeManager.Instance.ChangeTheme(value.FileNameWithoutExtension);
// when changed non-blur theme, change to backdrop to none
if (!ThemeManager.Instance.BlurEnabled)
{
Settings.BackdropType = BackdropTypes.None;
}
// dropshadow on and control disabled.(user can't change dropshadow with blur theme)
if (ThemeManager.Instance.BlurEnabled)
{
Settings.UseDropShadowEffect = true;
}
// Update UI state
OnPropertyChanged(nameof(BackdropType));
OnPropertyChanged(nameof(IsBackdropEnabled));
@ -235,8 +223,8 @@ public partial class SettingsPaneThemeViewModel : BaseModel
public BackdropTypes BackdropType
{
get => Enum.IsDefined(typeof(BackdropTypes), Settings.BackdropType)
? Settings.BackdropType
: BackdropTypes.None;
? Settings.BackdropType
: BackdropTypes.None;
set
{
if (!Enum.IsDefined(typeof(BackdropTypes), value))

View file

@ -2,7 +2,6 @@
using System.Linq;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core.Resource;
using FLSettings = Flow.Launcher.Infrastructure.UserSettings.Settings;
namespace Flow.Launcher.Plugin.Sys
{
@ -10,7 +9,6 @@ namespace Flow.Launcher.Plugin.Sys
{
public const string Keyword = "fltheme";
private readonly FLSettings _settings;
private readonly Theme _theme;
private readonly PluginInitContext _context;
@ -27,18 +25,6 @@ namespace Flow.Launcher.Plugin.Sys
_selectedTheme = value;
_theme.ChangeTheme(value.FileNameWithoutExtension);
// when changed non-blur theme, change to backdrop to none
if (!_theme.BlurEnabled)
{
_settings.BackdropType = 0; // Change to 0 instead of BackdropTypes.None
}
// dropshadow on and control disabled.(user can't change dropshadow with blur theme)
if (_theme.BlurEnabled)
{
_settings.UseDropShadowEffect = true;
}
_ = _theme.RefreshFrameAsync();
}
}
@ -51,7 +37,6 @@ namespace Flow.Launcher.Plugin.Sys
{
_context = context;
_theme = Ioc.Default.GetRequiredService<Theme>();
_settings = Ioc.Default.GetRequiredService<FLSettings>();
}
public List<Result> Query(Query query)