Skip to content

Commit f3a5365

Browse files
committed
Merge remote-tracking branch 'origin/250223FluentTest2' into 250223FluentTest2
2 parents 841cc06 + 4d080a9 commit f3a5365

File tree

21 files changed

+1218
-1135
lines changed

21 files changed

+1218
-1135
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 97 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class Theme
2424
{
2525
#region Properties & Fields
2626

27-
public string CurrentTheme => _settings.Theme;
28-
2927
public bool BlurEnabled { get; set; }
3028

3129
private const string ThemeMetadataNamePrefix = "Name:";
@@ -78,6 +76,11 @@ public Theme(IPublicAPI publicAPI, Settings settings)
7876

7977
#region Theme Resources
8078

79+
public string GetCurrentTheme()
80+
{
81+
return _settings.Theme;
82+
}
83+
8184
private void MakeSureThemeDirectoriesExist()
8285
{
8386
foreach (var dir in _themeDirectories.Where(dir => !Directory.Exists(dir)))
@@ -183,7 +186,7 @@ private ResourceDictionary GetResourceDictionary(string theme)
183186

184187
private ResourceDictionary GetCurrentResourceDictionary()
185188
{
186-
return GetResourceDictionary(_settings.Theme);
189+
return GetResourceDictionary(GetCurrentTheme());
187190
}
188191

189192
private ThemeData GetThemeDataFromPath(string path)
@@ -253,9 +256,10 @@ public List<ThemeData> LoadAvailableThemes()
253256
return themes.OrderBy(o => o.Name).ToList();
254257
}
255258

256-
public bool ChangeTheme(string theme)
259+
public bool ChangeTheme(string theme = null)
257260
{
258-
const string defaultTheme = Constant.DefaultTheme;
261+
if (string.IsNullOrEmpty(theme))
262+
theme = GetCurrentTheme();
259263

260264
string path = GetThemePath(theme);
261265
try
@@ -270,7 +274,7 @@ public bool ChangeTheme(string theme)
270274
_settings.Theme = theme;
271275

272276
//always allow re-loading default theme, in case of failure of switching to a new theme from default theme
273-
if (_oldTheme != theme || theme == defaultTheme)
277+
if (_oldTheme != theme || theme == Constant.DefaultTheme)
274278
{
275279
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
276280
}
@@ -284,20 +288,20 @@ public bool ChangeTheme(string theme)
284288
catch (DirectoryNotFoundException)
285289
{
286290
Log.Error($"|Theme.ChangeTheme|Theme <{theme}> path can't be found");
287-
if (theme != defaultTheme)
291+
if (theme != Constant.DefaultTheme)
288292
{
289-
_api.ShowMsgBox(string.Format(InternationalizationManager.Instance.GetTranslation("theme_load_failure_path_not_exists"), theme));
290-
ChangeTheme(defaultTheme);
293+
_api.ShowMsgBox(string.Format(_api.GetTranslation("theme_load_failure_path_not_exists"), theme));
294+
ChangeTheme(Constant.DefaultTheme);
291295
}
292296
return false;
293297
}
294298
catch (XamlParseException)
295299
{
296300
Log.Error($"|Theme.ChangeTheme|Theme <{theme}> fail to parse");
297-
if (theme != defaultTheme)
301+
if (theme != Constant.DefaultTheme)
298302
{
299-
_api.ShowMsgBox(string.Format(InternationalizationManager.Instance.GetTranslation("theme_load_failure_parse_error"), theme));
300-
ChangeTheme(defaultTheme);
303+
_api.ShowMsgBox(string.Format(_api.GetTranslation("theme_load_failure_parse_error"), theme));
304+
ChangeTheme(Constant.DefaultTheme);
301305
}
302306
return false;
303307
}
@@ -416,21 +420,24 @@ private static void SetResizeBoarderThickness(Thickness? effectMargin)
416420
/// </summary>
417421
public async Task RefreshFrameAsync()
418422
{
419-
await Application.Current.Dispatcher.InvokeAsync(async () =>
423+
await Application.Current.Dispatcher.InvokeAsync(() =>
420424
{
425+
// Get the actual backdrop type and drop shadow effect settings
426+
var (backdropType, useDropShadowEffect) = GetActualValue();
427+
421428
// Remove OS minimizing/maximizing animation
422429
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
423-
430+
424431
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
425432
if (BlurEnabled)
426433
{
427-
AutoDropShadow();
434+
AutoDropShadow(useDropShadowEffect);
428435
}
429-
await SetBlurForWindowAsync();
436+
SetBlurForWindow(GetCurrentTheme(), backdropType);
430437

431438
if (!BlurEnabled)
432439
{
433-
AutoDropShadow();
440+
AutoDropShadow(useDropShadowEffect);
434441
}
435442
}, DispatcherPriority.Normal);
436443
}
@@ -440,61 +447,87 @@ await Application.Current.Dispatcher.InvokeAsync(async () =>
440447
/// </summary>
441448
public async Task SetBlurForWindowAsync()
442449
{
443-
await Application.Current.Dispatcher.InvokeAsync(async () =>
450+
await Application.Current.Dispatcher.InvokeAsync(() =>
444451
{
445-
var dict = GetThemeResourceDictionary(_settings.Theme);
446-
if (dict == null)
447-
return;
452+
// Get the actual backdrop type and drop shadow effect settings
453+
var (backdropType, _) = GetActualValue();
448454

449-
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
450-
if (windowBorderStyle == null)
451-
return;
455+
SetBlurForWindow(GetCurrentTheme(), backdropType);
456+
}, DispatcherPriority.Normal);
457+
}
452458

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

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-
}
467+
// When changed non-blur theme, change to backdrop to none
468+
if (!BlurEnabled)
469+
{
470+
backdropType = BackdropTypes.None;
471+
}
463472

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-
}
473+
// Dropshadow on and control disabled.(user can't change dropshadow with blur theme)
474+
if (BlurEnabled)
475+
{
476+
useDropShadowEffect = true;
477+
}
478+
479+
return (backdropType, useDropShadowEffect);
480+
}
481+
482+
private void SetBlurForWindow(string theme, BackdropTypes backdropType)
483+
{
484+
var dict = GetThemeResourceDictionary(theme);
485+
if (dict == null)
486+
return;
487+
488+
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
489+
if (windowBorderStyle == null)
490+
return;
491+
492+
Window mainWindow = Application.Current.MainWindow;
493+
if (mainWindow == null)
494+
return;
477495

478-
// Apply the blur effect
479-
Win32Helper.DWMSetBackdropForWindow(mainWindow, _settings.BackdropType);
480-
ColorizeWindow();
496+
// Check if the theme supports blur
497+
bool hasBlur = dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool b && b;
498+
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
499+
{
500+
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
501+
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
502+
{
503+
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
504+
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
481505
}
482-
else
506+
else if (backdropType == BackdropTypes.Acrylic)
483507
{
484-
// Apply default style when Blur is disabled
485-
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
486-
ColorizeWindow();
508+
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
509+
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
487510
}
488511

489-
UpdateResourceDictionary(dict);
490-
}, DispatcherPriority.Normal);
512+
// Apply the blur effect
513+
Win32Helper.DWMSetBackdropForWindow(mainWindow, backdropType);
514+
ColorizeWindow(theme, backdropType);
515+
}
516+
else
517+
{
518+
// Apply default style when Blur is disabled
519+
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
520+
ColorizeWindow(theme, backdropType);
521+
}
522+
523+
UpdateResourceDictionary(dict);
491524
}
492525

493-
private void AutoDropShadow()
526+
private void AutoDropShadow(bool useDropShadowEffect)
494527
{
495528
SetWindowCornerPreference("Default");
496529
RemoveDropShadowEffectFromCurrentTheme();
497-
if (_settings.UseDropShadowEffect)
530+
if (useDropShadowEffect)
498531
{
499532
if (BlurEnabled && Win32Helper.IsBackdropSupported())
500533
{
@@ -530,9 +563,9 @@ private static void SetWindowCornerPreference(string cornerType)
530563

531564
// Get Background Color from WindowBorderStyle when there not color for BG.
532565
// for theme has not "LightBG" or "DarkBG" case.
533-
private Color GetWindowBorderStyleBackground()
566+
private Color GetWindowBorderStyleBackground(string theme)
534567
{
535-
var Resources = GetThemeResourceDictionary(_settings.Theme);
568+
var Resources = GetThemeResourceDictionary(theme);
536569
var windowBorderStyle = (Style)Resources["WindowBorderStyle"];
537570

538571
var backgroundSetter = windowBorderStyle.Setters
@@ -605,9 +638,9 @@ private void ApplyPreviewBackground(Color? bgColor = null)
605638
Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle;
606639
}
607640

608-
private void ColorizeWindow()
641+
private void ColorizeWindow(string theme, BackdropTypes backdropType)
609642
{
610-
var dict = GetThemeResourceDictionary(_settings.Theme);
643+
var dict = GetThemeResourceDictionary(theme);
611644
if (dict == null) return;
612645

613646
var mainWindow = Application.Current.MainWindow;
@@ -658,11 +691,11 @@ private void ColorizeWindow()
658691
// Retrieve LightBG value (fallback to WindowBorderStyle background color if not found)
659692
try
660693
{
661-
LightBG = dict.Contains("LightBG") ? (Color)dict["LightBG"] : GetWindowBorderStyleBackground();
694+
LightBG = dict.Contains("LightBG") ? (Color)dict["LightBG"] : GetWindowBorderStyleBackground(theme);
662695
}
663696
catch (Exception)
664697
{
665-
LightBG = GetWindowBorderStyleBackground();
698+
LightBG = GetWindowBorderStyleBackground(theme);
666699
}
667700

668701
// Retrieve DarkBG value (fallback to LightBG if not found)
@@ -688,7 +721,7 @@ private void ColorizeWindow()
688721
else
689722
{
690723
// Only set the background to transparent if the theme supports blur
691-
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
724+
if (backdropType == BackdropTypes.Mica || backdropType == BackdropTypes.MicaAlt)
692725
{
693726
mainWindow.Background = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
694727
}

0 commit comments

Comments
 (0)