1
1
using Files . App . Extensions ;
2
+ using Microsoft . UI . Xaml ;
3
+ using Microsoft . UI . Xaml . Markup ;
2
4
using System ;
3
5
using System . Collections . Generic ;
4
6
using System . Diagnostics ;
5
7
using System . IO ;
6
8
using System . Linq ;
7
9
using System . Threading . Tasks ;
8
10
using Windows . Storage ;
9
- using Microsoft . UI . Xaml ;
10
- using Microsoft . UI . Xaml . Markup ;
11
11
12
12
namespace Files . App . Helpers
13
13
{
14
- public class ExternalResourcesHelper
15
- {
16
- public List < AppTheme > Themes = new List < AppTheme > ( )
17
- {
18
- new AppTheme
19
- {
20
- Name = "Default" . GetLocalizedResource ( ) ,
21
- } ,
22
- } ;
23
-
24
- public StorageFolder ThemeFolder { get ; set ; }
25
- public StorageFolder ImportedThemesFolder { get ; set ; }
26
-
27
- public string CurrentThemeResources { get ; set ; }
28
-
29
- public async Task LoadSelectedTheme ( )
30
- {
31
- string bundledThemesPath = Path . Combine ( Windows . ApplicationModel . Package . Current . InstalledLocation . Path , "Files.App" , "Themes" ) ;
32
- ThemeFolder = await StorageFolder . GetFolderFromPathAsync ( bundledThemesPath ) ;
33
- ImportedThemesFolder = await ApplicationData . Current . LocalFolder . CreateFolderAsync ( "Themes" , CreationCollisionOption . OpenIfExists ) ;
34
-
35
- if ( App . AppSettings . SelectedTheme . Path != null )
36
- {
37
- await TryLoadThemeAsync ( App . AppSettings . SelectedTheme ) ;
38
- }
39
- }
40
-
41
- public async Task LoadOtherThemesAsync ( )
42
- {
43
- try
44
- {
45
- await AddThemesAsync ( ThemeFolder ) ;
46
- await AddThemesAsync ( ImportedThemesFolder ) ;
47
- }
48
- catch ( Exception )
49
- {
50
- Debug . WriteLine ( $ "Error loading themes") ;
51
- }
52
- }
53
-
54
- private async Task AddThemesAsync ( StorageFolder folder )
55
- {
56
- foreach ( var file in ( await folder . GetFilesAsync ( ) ) . Where ( x => x . FileType == ".xaml" ) )
57
- {
58
- if ( ! Themes . Exists ( t => t . AbsolutePath == file . Path ) )
59
- {
60
- Themes . Add ( new AppTheme ( )
61
- {
62
- Name = file . Name . Replace ( ".xaml" , "" , StringComparison . Ordinal ) ,
63
- Path = file . Name ,
64
- AbsolutePath = file . Path ,
65
- } ) ;
66
- }
67
- }
68
- }
69
-
70
- public async Task < bool > TryLoadThemeAsync ( AppTheme theme )
71
- {
72
- try
73
- {
74
- var xaml = await TryLoadResourceDictionary ( theme ) ;
75
- if ( xaml != null )
76
- {
77
- App . Current . Resources . MergedDictionaries . Add ( xaml ) ;
78
- return true ;
79
- }
80
- return false ;
81
- }
82
- catch ( Exception ex )
83
- {
84
- App . Logger . Warn ( ex , $ "Error loading theme: { theme ? . Path } ") ;
85
- return false ;
86
- }
87
- }
88
-
89
- public async Task < ResourceDictionary > TryLoadResourceDictionary ( AppTheme theme )
90
- {
91
- StorageFile file ;
92
- if ( theme ? . Path == null )
93
- {
94
- return null ;
95
- }
96
-
97
- if ( theme . AbsolutePath . Contains ( ImportedThemesFolder . Path ) )
98
- {
99
- file = await ImportedThemesFolder . GetFileAsync ( theme . Path ) ;
100
- theme . IsImportedTheme = true ;
101
- }
102
- else
103
- {
104
- file = await ThemeFolder . GetFileAsync ( theme . Path ) ;
105
- theme . IsImportedTheme = false ;
106
- }
107
-
108
- var code = await FileIO . ReadTextAsync ( file ) ;
109
- var xaml = XamlReader . Load ( code ) as ResourceDictionary ;
110
- xaml . Add ( "CustomThemeID" , theme . Key ) ;
111
- return xaml ;
112
- }
113
-
114
- public async Task UpdateTheme ( AppTheme OldTheme , AppTheme NewTheme )
115
- {
116
- if ( OldTheme . Path != null )
117
- {
118
- RemoveTheme ( OldTheme ) ;
119
- }
120
-
121
- if ( NewTheme . Path != null )
122
- {
123
- await TryLoadThemeAsync ( NewTheme ) ;
124
- }
125
- }
126
-
127
- public bool RemoveTheme ( AppTheme theme )
128
- {
129
- try
130
- {
131
- App . Current . Resources . MergedDictionaries . Remove ( App . Current . Resources . MergedDictionaries . FirstOrDefault ( x => x . TryGetValue ( "CustomThemeID" , out var key ) && ( key as string ) == theme . Key ) ) ;
132
- return true ;
133
- }
134
- catch ( Exception )
135
- {
136
- return false ;
137
- }
138
- }
139
- }
140
-
141
- public class AppTheme
142
- {
143
- public string Name { get ; set ; }
144
- public string Path { get ; set ; }
145
- public string AbsolutePath { get ; set ; }
146
- public string Key => $ "{ Name } ";
147
- public bool IsImportedTheme { get ; set ; }
148
- }
14
+ public class ExternalResourcesHelper
15
+ {
16
+ public List < AppTheme > Themes = new List < AppTheme > ( )
17
+ {
18
+ new AppTheme
19
+ {
20
+ Name = "Default" . GetLocalizedResource ( ) ,
21
+ } ,
22
+ } ;
23
+
24
+ public StorageFolder ThemeFolder { get ; set ; }
25
+ public StorageFolder ImportedThemesFolder { get ; set ; }
26
+
27
+ public string CurrentThemeResources { get ; set ; }
28
+
29
+ public async Task LoadSelectedTheme ( )
30
+ {
31
+ string bundledThemesPath = Path . Combine ( Windows . ApplicationModel . Package . Current . InstalledLocation . Path , "Files.App" , "Themes" ) ;
32
+ ThemeFolder = await StorageFolder . GetFolderFromPathAsync ( bundledThemesPath ) ;
33
+ ImportedThemesFolder = await ApplicationData . Current . LocalFolder . CreateFolderAsync ( "Themes" , CreationCollisionOption . OpenIfExists ) ;
34
+
35
+ if ( App . AppSettings . SelectedTheme . Path != null )
36
+ {
37
+ await TryLoadThemeAsync ( App . AppSettings . SelectedTheme ) ;
38
+ }
39
+ }
40
+
41
+ /// <summary>
42
+ /// Overrides xaml resources with custom values
43
+ /// </summary>
44
+ /// <param name="UseCompactSpacing"></param>
45
+ public void OverrideAppResources ( bool UseCompactSpacing )
46
+ {
47
+ if ( UseCompactSpacing )
48
+ {
49
+ Application . Current . Resources [ "ListItemHeight" ] = 28 ;
50
+ Application . Current . Resources [ "NavigationViewItemOnLeftMinHeight" ] = 24 ;
51
+ }
52
+ else
53
+ {
54
+ Application . Current . Resources [ "ListItemHeight" ] = 36 ;
55
+ Application . Current . Resources [ "NavigationViewItemOnLeftMinHeight" ] = 32 ;
56
+ }
57
+ }
58
+
59
+ public async Task LoadOtherThemesAsync ( )
60
+ {
61
+ try
62
+ {
63
+ await AddThemesAsync ( ThemeFolder ) ;
64
+ await AddThemesAsync ( ImportedThemesFolder ) ;
65
+ }
66
+ catch ( Exception )
67
+ {
68
+ Debug . WriteLine ( $ "Error loading themes") ;
69
+ }
70
+ }
71
+
72
+ private async Task AddThemesAsync ( StorageFolder folder )
73
+ {
74
+ foreach ( var file in ( await folder . GetFilesAsync ( ) ) . Where ( x => x . FileType == ".xaml" ) )
75
+ {
76
+ if ( ! Themes . Exists ( t => t . AbsolutePath == file . Path ) )
77
+ {
78
+ Themes . Add ( new AppTheme ( )
79
+ {
80
+ Name = file . Name . Replace ( ".xaml" , "" , StringComparison . Ordinal ) ,
81
+ Path = file . Name ,
82
+ AbsolutePath = file . Path ,
83
+ } ) ;
84
+ }
85
+ }
86
+ }
87
+
88
+ public async Task < bool > TryLoadThemeAsync ( AppTheme theme )
89
+ {
90
+ try
91
+ {
92
+ var xaml = await TryLoadResourceDictionary ( theme ) ;
93
+ if ( xaml != null )
94
+ {
95
+ App . Current . Resources . MergedDictionaries . Add ( xaml ) ;
96
+ return true ;
97
+ }
98
+ return false ;
99
+ }
100
+ catch ( Exception ex )
101
+ {
102
+ App . Logger . Warn ( ex , $ "Error loading theme: { theme ? . Path } ") ;
103
+ return false ;
104
+ }
105
+ }
106
+
107
+ public async Task < ResourceDictionary > TryLoadResourceDictionary ( AppTheme theme )
108
+ {
109
+ StorageFile file ;
110
+ if ( theme ? . Path == null )
111
+ {
112
+ return null ;
113
+ }
114
+
115
+ if ( theme . AbsolutePath . Contains ( ImportedThemesFolder . Path ) )
116
+ {
117
+ file = await ImportedThemesFolder . GetFileAsync ( theme . Path ) ;
118
+ theme . IsImportedTheme = true ;
119
+ }
120
+ else
121
+ {
122
+ file = await ThemeFolder . GetFileAsync ( theme . Path ) ;
123
+ theme . IsImportedTheme = false ;
124
+ }
125
+
126
+ var code = await FileIO . ReadTextAsync ( file ) ;
127
+ var xaml = XamlReader . Load ( code ) as ResourceDictionary ;
128
+ xaml . Add ( "CustomThemeID" , theme . Key ) ;
129
+ return xaml ;
130
+ }
131
+
132
+ public async Task UpdateTheme ( AppTheme OldTheme , AppTheme NewTheme )
133
+ {
134
+ if ( OldTheme . Path != null )
135
+ {
136
+ RemoveTheme ( OldTheme ) ;
137
+ }
138
+
139
+ if ( NewTheme . Path != null )
140
+ {
141
+ await TryLoadThemeAsync ( NewTheme ) ;
142
+ }
143
+ }
144
+
145
+ public bool RemoveTheme ( AppTheme theme )
146
+ {
147
+ try
148
+ {
149
+ App . Current . Resources . MergedDictionaries . Remove ( App . Current . Resources . MergedDictionaries . FirstOrDefault ( x => x . TryGetValue ( "CustomThemeID" , out var key ) && ( key as string ) == theme . Key ) ) ;
150
+ return true ;
151
+ }
152
+ catch ( Exception )
153
+ {
154
+ return false ;
155
+ }
156
+ }
157
+ }
158
+
159
+ public class AppTheme
160
+ {
161
+ public string Name { get ; set ; }
162
+ public string Path { get ; set ; }
163
+ public string AbsolutePath { get ; set ; }
164
+ public string Key => $ "{ Name } ";
165
+ public bool IsImportedTheme { get ; set ; }
166
+ }
149
167
}
0 commit comments