@@ -24,8 +24,6 @@ public class Theme
24
24
{
25
25
#region Properties & Fields
26
26
27
- public string CurrentTheme => _settings . Theme ;
28
-
29
27
public bool BlurEnabled { get ; set ; }
30
28
31
29
private const string ThemeMetadataNamePrefix = "Name:" ;
@@ -78,6 +76,11 @@ public Theme(IPublicAPI publicAPI, Settings settings)
78
76
79
77
#region Theme Resources
80
78
79
+ public string GetCurrentTheme ( )
80
+ {
81
+ return _settings . Theme ;
82
+ }
83
+
81
84
private void MakeSureThemeDirectoriesExist ( )
82
85
{
83
86
foreach ( var dir in _themeDirectories . Where ( dir => ! Directory . Exists ( dir ) ) )
@@ -183,7 +186,7 @@ private ResourceDictionary GetResourceDictionary(string theme)
183
186
184
187
private ResourceDictionary GetCurrentResourceDictionary ( )
185
188
{
186
- return GetResourceDictionary ( _settings . Theme ) ;
189
+ return GetResourceDictionary ( GetCurrentTheme ( ) ) ;
187
190
}
188
191
189
192
private ThemeData GetThemeDataFromPath ( string path )
@@ -253,9 +256,10 @@ public List<ThemeData> LoadAvailableThemes()
253
256
return themes . OrderBy ( o => o . Name ) . ToList ( ) ;
254
257
}
255
258
256
- public bool ChangeTheme ( string theme )
259
+ public bool ChangeTheme ( string theme = null )
257
260
{
258
- const string defaultTheme = Constant . DefaultTheme ;
261
+ if ( string . IsNullOrEmpty ( theme ) )
262
+ theme = GetCurrentTheme ( ) ;
259
263
260
264
string path = GetThemePath ( theme ) ;
261
265
try
@@ -270,7 +274,7 @@ public bool ChangeTheme(string theme)
270
274
_settings . Theme = theme ;
271
275
272
276
//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 )
274
278
{
275
279
_oldTheme = Path . GetFileNameWithoutExtension ( _oldResource . Source . AbsolutePath ) ;
276
280
}
@@ -284,20 +288,20 @@ public bool ChangeTheme(string theme)
284
288
catch ( DirectoryNotFoundException )
285
289
{
286
290
Log . Error ( $ "|Theme.ChangeTheme|Theme <{ theme } > path can't be found") ;
287
- if ( theme != defaultTheme )
291
+ if ( theme != Constant . DefaultTheme )
288
292
{
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 ) ;
291
295
}
292
296
return false ;
293
297
}
294
298
catch ( XamlParseException )
295
299
{
296
300
Log . Error ( $ "|Theme.ChangeTheme|Theme <{ theme } > fail to parse") ;
297
- if ( theme != defaultTheme )
301
+ if ( theme != Constant . DefaultTheme )
298
302
{
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 ) ;
301
305
}
302
306
return false ;
303
307
}
@@ -416,21 +420,24 @@ private static void SetResizeBoarderThickness(Thickness? effectMargin)
416
420
/// </summary>
417
421
public async Task RefreshFrameAsync ( )
418
422
{
419
- await Application . Current . Dispatcher . InvokeAsync ( async ( ) =>
423
+ await Application . Current . Dispatcher . InvokeAsync ( ( ) =>
420
424
{
425
+ // Get the actual backdrop type and drop shadow effect settings
426
+ var ( backdropType , useDropShadowEffect ) = GetActualValue ( ) ;
427
+
421
428
// Remove OS minimizing/maximizing animation
422
429
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
423
-
430
+
424
431
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
425
432
if ( BlurEnabled )
426
433
{
427
- AutoDropShadow ( ) ;
434
+ AutoDropShadow ( useDropShadowEffect ) ;
428
435
}
429
- await SetBlurForWindowAsync ( ) ;
436
+ SetBlurForWindow ( GetCurrentTheme ( ) , backdropType ) ;
430
437
431
438
if ( ! BlurEnabled )
432
439
{
433
- AutoDropShadow ( ) ;
440
+ AutoDropShadow ( useDropShadowEffect ) ;
434
441
}
435
442
} , DispatcherPriority . Normal ) ;
436
443
}
@@ -440,61 +447,87 @@ await Application.Current.Dispatcher.InvokeAsync(async () =>
440
447
/// </summary>
441
448
public async Task SetBlurForWindowAsync ( )
442
449
{
443
- await Application . Current . Dispatcher . InvokeAsync ( async ( ) =>
450
+ await Application . Current . Dispatcher . InvokeAsync ( ( ) =>
444
451
{
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 ( ) ;
448
454
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
+ }
452
458
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 ;
456
466
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
+ }
463
472
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 ;
477
495
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 ) ) ) ) ;
481
505
}
482
- else
506
+ else if ( backdropType == BackdropTypes . Acrylic )
483
507
{
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 ) ) ) ;
487
510
}
488
511
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 ) ;
491
524
}
492
525
493
- private void AutoDropShadow ( )
526
+ private void AutoDropShadow ( bool useDropShadowEffect )
494
527
{
495
528
SetWindowCornerPreference ( "Default" ) ;
496
529
RemoveDropShadowEffectFromCurrentTheme ( ) ;
497
- if ( _settings . UseDropShadowEffect )
530
+ if ( useDropShadowEffect )
498
531
{
499
532
if ( BlurEnabled && Win32Helper . IsBackdropSupported ( ) )
500
533
{
@@ -530,9 +563,9 @@ private static void SetWindowCornerPreference(string cornerType)
530
563
531
564
// Get Background Color from WindowBorderStyle when there not color for BG.
532
565
// for theme has not "LightBG" or "DarkBG" case.
533
- private Color GetWindowBorderStyleBackground ( )
566
+ private Color GetWindowBorderStyleBackground ( string theme )
534
567
{
535
- var Resources = GetThemeResourceDictionary ( _settings . Theme ) ;
568
+ var Resources = GetThemeResourceDictionary ( theme ) ;
536
569
var windowBorderStyle = ( Style ) Resources [ "WindowBorderStyle" ] ;
537
570
538
571
var backgroundSetter = windowBorderStyle . Setters
@@ -605,9 +638,9 @@ private void ApplyPreviewBackground(Color? bgColor = null)
605
638
Application . Current . Resources [ "PreviewWindowBorderStyle" ] = previewStyle ;
606
639
}
607
640
608
- private void ColorizeWindow ( )
641
+ private void ColorizeWindow ( string theme , BackdropTypes backdropType )
609
642
{
610
- var dict = GetThemeResourceDictionary ( _settings . Theme ) ;
643
+ var dict = GetThemeResourceDictionary ( theme ) ;
611
644
if ( dict == null ) return ;
612
645
613
646
var mainWindow = Application . Current . MainWindow ;
@@ -658,11 +691,11 @@ private void ColorizeWindow()
658
691
// Retrieve LightBG value (fallback to WindowBorderStyle background color if not found)
659
692
try
660
693
{
661
- LightBG = dict . Contains ( "LightBG" ) ? ( Color ) dict [ "LightBG" ] : GetWindowBorderStyleBackground ( ) ;
694
+ LightBG = dict . Contains ( "LightBG" ) ? ( Color ) dict [ "LightBG" ] : GetWindowBorderStyleBackground ( theme ) ;
662
695
}
663
696
catch ( Exception )
664
697
{
665
- LightBG = GetWindowBorderStyleBackground ( ) ;
698
+ LightBG = GetWindowBorderStyleBackground ( theme ) ;
666
699
}
667
700
668
701
// Retrieve DarkBG value (fallback to LightBG if not found)
@@ -688,7 +721,7 @@ private void ColorizeWindow()
688
721
else
689
722
{
690
723
// 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 )
692
725
{
693
726
mainWindow . Background = new SolidColorBrush ( Color . FromArgb ( 1 , 0 , 0 , 0 ) ) ;
694
727
}
0 commit comments