11using System . Windows ;
22using System . Windows . Interop ;
33using System . Windows . Media ;
4+ using System . Windows . Media . Imaging ;
45using Wpf . Ui . Appearance ;
56using Wpf . Ui . Controls ;
67using Wpf . Ui . Mvvm . Contracts ;
@@ -26,55 +27,26 @@ public void ApplyTheme()
2627 _themeService . SetTheme ( finalTheme == Enums . Theme . Light ? ThemeType . Light : ThemeType . Dark ) ;
2728 _themeService . SetSystemAccent ( ) ;
2829
29- if ( finalTheme == Enums . Theme . Custom )
30- {
31- Application . Current . Resources [ "WindowBackgroundGradient" ] = null ;
32-
33- double angle = App . Settings . Prop . GradientAngle ;
34- double angleRad = angle * Math . PI / 180.0 ;
30+ Application . Current . Resources [ "ApplicationBackground" ] = null ;
3531
36- double startX = 0.5 + 0.5 * Math . Cos ( angleRad + Math . PI ) ;
37- double startY = 0.5 + 0.5 * Math . Sin ( angleRad + Math . PI ) ;
38- double endX = 0.5 + 0.5 * Math . Cos ( angleRad ) ;
39- double endY = 0.5 + 0.5 * Math . Sin ( angleRad ) ;
32+ this . Background = null ;
4033
41- var customBrush = new LinearGradientBrush
34+ if ( finalTheme == Enums . Theme . Custom )
35+ {
36+ if ( App . Settings . Prop . BackgroundType == BackgroundMode . Gradient )
4237 {
43- StartPoint = new Point ( startX , startY ) ,
44- EndPoint = new Point ( endX , endY )
45- } ;
46-
47- foreach ( var stop in App . Settings . Prop . CustomGradientStops . OrderBy ( s => s . Offset ) )
38+ ApplyGradientBackground ( ) ;
39+ }
40+ else if ( App . Settings . Prop . BackgroundType == BackgroundMode . Image )
4841 {
49- try
50- {
51- var color = ( Color ) ColorConverter . ConvertFromString ( stop . Color ) ;
52- customBrush . GradientStops . Add ( new GradientStop ( color , stop . Offset ) ) ;
53- }
54- catch { }
42+ ApplyImageBackground ( ) ;
5543 }
5644
57- Application . Current . Resources [ "WindowBackgroundGradient" ] = customBrush ;
58-
59- Application . Current . Resources [ "NewTextEditorBackground" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#59000000" ) ) ;
60- Application . Current . Resources [ "NewTextEditorForeground" ] = new SolidColorBrush ( Colors . White ) ;
61- Application . Current . Resources [ "NewTextEditorLink" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#3A9CEA" ) ) ;
62- Application . Current . Resources [ "PrimaryBackgroundColor" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#19000000" ) ) ;
63- Application . Current . Resources [ "NormalDarkAndLightBackground" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#0FFFFFFF" ) ) ;
64- Application . Current . Resources [ "ControlFillColorDefault" ] = ( Color ) ColorConverter . ConvertFromString ( "#19000000" ) ;
45+ ApplyCustomThemeResources ( ) ;
6546 }
6647 else
6748 {
68- var dict = new ResourceDictionary { Source = new Uri ( $ "pack://application:,,,/UI/Style/{ Enum . GetName ( finalTheme ) } .xaml") } ;
69- Application . Current . Resources . MergedDictionaries [ customThemeIndex ] = dict ;
70-
71- Application . Current . Resources [ "WindowBackgroundGradient" ] = null ;
72- Application . Current . Resources . Remove ( "NewTextEditorBackground" ) ;
73- Application . Current . Resources . Remove ( "NewTextEditorForeground" ) ;
74- Application . Current . Resources . Remove ( "NewTextEditorLink" ) ;
75- Application . Current . Resources . Remove ( "PrimaryBackgroundColor" ) ;
76- Application . Current . Resources . Remove ( "NormalDarkAndLightBackground" ) ;
77- Application . Current . Resources . Remove ( "ControlFillColorDefault" ) ;
49+ ApplyStandardTheme ( finalTheme , customThemeIndex ) ;
7850 }
7951
8052#if QA_BUILD
@@ -83,6 +55,99 @@ public void ApplyTheme()
8355#endif
8456 }
8557
58+ private void ApplyGradientBackground ( )
59+ {
60+ double angle = App . Settings . Prop . GradientAngle ;
61+ double angleRad = angle * Math . PI / 180.0 ;
62+
63+ double startX = 0.5 + 0.5 * Math . Cos ( angleRad + Math . PI ) ;
64+ double startY = 0.5 + 0.5 * Math . Sin ( angleRad + Math . PI ) ;
65+ double endX = 0.5 + 0.5 * Math . Cos ( angleRad ) ;
66+ double endY = 0.5 + 0.5 * Math . Sin ( angleRad ) ;
67+
68+ var customBrush = new LinearGradientBrush
69+ {
70+ StartPoint = new Point ( startX , startY ) ,
71+ EndPoint = new Point ( endX , endY )
72+ } ;
73+
74+ foreach ( var stop in App . Settings . Prop . CustomGradientStops . OrderBy ( s => s . Offset ) )
75+ {
76+ try
77+ {
78+ var color = ( Color ) ColorConverter . ConvertFromString ( stop . Color ) ;
79+ customBrush . GradientStops . Add ( new GradientStop ( color , stop . Offset ) ) ;
80+ }
81+ catch { }
82+ }
83+
84+ Application . Current . Resources [ "ApplicationBackground" ] = customBrush ;
85+ }
86+
87+ private void ApplyImageBackground ( )
88+ {
89+ if ( string . IsNullOrEmpty ( App . Settings . Prop . BackgroundImagePath ) || ! File . Exists ( App . Settings . Prop . BackgroundImagePath ) )
90+ {
91+ App . Settings . Prop . BackgroundType = BackgroundMode . Gradient ;
92+ ApplyTheme ( ) ;
93+ return ;
94+ }
95+
96+ try
97+ {
98+ var imageSource = new BitmapImage ( ) ;
99+ imageSource . BeginInit ( ) ;
100+ imageSource . CacheOption = BitmapCacheOption . OnLoad ;
101+ imageSource . UriSource = new Uri ( App . Settings . Prop . BackgroundImagePath ) ;
102+ imageSource . EndInit ( ) ;
103+ imageSource . Freeze ( ) ;
104+
105+ var imageBrush = new ImageBrush
106+ {
107+ ImageSource = imageSource ,
108+ Stretch = App . Settings . Prop . BackgroundStretch switch
109+ {
110+ BackgroundStretch . None => Stretch . None ,
111+ BackgroundStretch . Fill => Stretch . Fill ,
112+ BackgroundStretch . Uniform => Stretch . Uniform ,
113+ BackgroundStretch . UniformToFill => Stretch . UniformToFill ,
114+ _ => Stretch . UniformToFill
115+ } ,
116+ Opacity = App . Settings . Prop . BackgroundOpacity
117+ } ;
118+
119+ Application . Current . Resources [ "ApplicationBackground" ] = imageBrush ;
120+ }
121+ catch ( Exception )
122+ {
123+ App . Settings . Prop . BackgroundType = BackgroundMode . Gradient ;
124+ ApplyTheme ( ) ;
125+ }
126+ }
127+
128+ private void ApplyCustomThemeResources ( )
129+ {
130+ Application . Current . Resources [ "NewTextEditorBackground" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#59000000" ) ) ;
131+ Application . Current . Resources [ "NewTextEditorForeground" ] = new SolidColorBrush ( Colors . White ) ;
132+ Application . Current . Resources [ "NewTextEditorLink" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#3A9CEA" ) ) ;
133+ Application . Current . Resources [ "PrimaryBackgroundColor" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#19000000" ) ) ;
134+ Application . Current . Resources [ "NormalDarkAndLightBackground" ] = new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#0FFFFFFF" ) ) ;
135+ Application . Current . Resources [ "ControlFillColorDefault" ] = ( Color ) ColorConverter . ConvertFromString ( "#19000000" ) ;
136+ }
137+
138+ private void ApplyStandardTheme ( Enums . Theme finalTheme , int customThemeIndex )
139+ {
140+ var dict = new ResourceDictionary { Source = new Uri ( $ "pack://application:,,,/UI/Style/{ Enum . GetName ( finalTheme ) } .xaml") } ;
141+ Application . Current . Resources . MergedDictionaries [ customThemeIndex ] = dict ;
142+
143+ Application . Current . Resources . Remove ( "NewTextEditorBackground" ) ;
144+ Application . Current . Resources . Remove ( "NewTextEditorForeground" ) ;
145+ Application . Current . Resources . Remove ( "NewTextEditorLink" ) ;
146+ Application . Current . Resources . Remove ( "PrimaryBackgroundColor" ) ;
147+ Application . Current . Resources . Remove ( "NormalDarkAndLightBackground" ) ;
148+ Application . Current . Resources . Remove ( "ControlFillColorDefault" ) ;
149+ }
150+
86151 protected override void OnSourceInitialized ( EventArgs e )
87152 {
88153 base . OnSourceInitialized ( e ) ;
0 commit comments