2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
4
using CommunityToolkit . WinUI . Helpers ;
5
+ using Microsoft . UI . Windowing ;
5
6
using Microsoft . UI . Xaml ;
7
+ using Microsoft . UI . Xaml . Media ;
8
+ using System . Windows . Input ;
9
+ using Windows . Storage . Pickers ;
10
+ using Windows . UI . WindowManagement ;
6
11
7
12
namespace Files . App . ViewModels . Settings
8
13
{
@@ -15,8 +20,17 @@ public sealed class AppearanceViewModel : ObservableObject
15
20
public List < string > Themes { get ; private set ; }
16
21
public Dictionary < BackdropMaterialType , string > BackdropMaterialTypes { get ; private set ; } = [ ] ;
17
22
23
+ public Dictionary < Stretch , string > ImageStretchTypes { get ; private set ; } = [ ] ;
24
+
25
+ public Dictionary < VerticalAlignment , string > ImageVerticalAlignmentTypes { get ; private set ; } = [ ] ;
26
+
27
+ public Dictionary < HorizontalAlignment , string > ImageHorizontalAlignmentTypes { get ; private set ; } = [ ] ;
28
+
18
29
public ObservableCollection < AppThemeResourceItem > AppThemeResources { get ; }
19
30
31
+ public ICommand SelectImageCommand { get ; }
32
+ public ICommand RemoveImageCommand { get ; }
33
+
20
34
public AppearanceViewModel ( IUserSettingsService userSettingsService , IResourcesService resourcesService )
21
35
{
22
36
UserSettingsService = userSettingsService ;
@@ -42,7 +56,64 @@ public AppearanceViewModel(IUserSettingsService userSettingsService, IResourcesS
42
56
43
57
AppThemeResources = AppThemeResourceFactory . AppThemeResources ;
44
58
59
+
60
+ // Background image fit options
61
+ ImageStretchTypes . Add ( Stretch . None , "None" . GetLocalizedResource ( ) ) ;
62
+ ImageStretchTypes . Add ( Stretch . Fill , "Fill" . GetLocalizedResource ( ) ) ;
63
+ ImageStretchTypes . Add ( Stretch . Uniform , "Uniform" . GetLocalizedResource ( ) ) ;
64
+ ImageStretchTypes . Add ( Stretch . UniformToFill , "UniformToFill" . GetLocalizedResource ( ) ) ;
65
+ SelectedImageStretchType = ImageStretchTypes [ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageFit ] ;
66
+
67
+ // Background image allignment options
68
+
69
+ // VerticalAlignment
70
+ ImageVerticalAlignmentTypes . Add ( VerticalAlignment . Top , "Top" . GetLocalizedResource ( ) ) ;
71
+ ImageVerticalAlignmentTypes . Add ( VerticalAlignment . Center , "Center" . GetLocalizedResource ( ) ) ;
72
+ ImageVerticalAlignmentTypes . Add ( VerticalAlignment . Bottom , "Bottom" . GetLocalizedResource ( ) ) ;
73
+ ImageVerticalAlignmentTypes . Add ( VerticalAlignment . Stretch , "Stretch" . GetLocalizedResource ( ) ) ;
74
+ SelectedImageVerticalAlignmentType = ImageVerticalAlignmentTypes [ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageVerticalAlignment ] ;
75
+
76
+ // HorizontalAlignment
77
+ ImageHorizontalAlignmentTypes . Add ( HorizontalAlignment . Left , "Top" . GetLocalizedResource ( ) ) ;
78
+ ImageHorizontalAlignmentTypes . Add ( HorizontalAlignment . Center , "Center" . GetLocalizedResource ( ) ) ;
79
+ ImageHorizontalAlignmentTypes . Add ( HorizontalAlignment . Right , "Bottom" . GetLocalizedResource ( ) ) ;
80
+ ImageHorizontalAlignmentTypes . Add ( HorizontalAlignment . Stretch , "Stretch" . GetLocalizedResource ( ) ) ;
81
+ SelectedImageHorizontalAlignmentType = ImageHorizontalAlignmentTypes [ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageHorizontalAlignment ] ;
82
+
45
83
UpdateSelectedResource ( ) ;
84
+
85
+ SelectImageCommand = new AsyncRelayCommand ( SelectBackgroundImage ) ;
86
+ RemoveImageCommand = new RelayCommand ( RemoveBackgroundImage ) ;
87
+ }
88
+
89
+ /// <summary>
90
+ /// Opens a file picker to select a background image
91
+ /// </summary>
92
+ private async Task SelectBackgroundImage ( )
93
+ {
94
+ var filePicker = new FileOpenPicker
95
+ {
96
+ ViewMode = PickerViewMode . Thumbnail ,
97
+ SuggestedStartLocation = PickerLocationId . PicturesLibrary ,
98
+ FileTypeFilter = { ".jpg" , ".jpeg" , ".png" , ".bmp" , ".gif" , ".webp" }
99
+ } ;
100
+
101
+ // WINUI3: Create and initialize new window
102
+ var parentWindowId = MainWindow . Instance . AppWindow . Id ;
103
+ var handle = Microsoft . UI . Win32Interop . GetWindowFromWindowId ( parentWindowId ) ;
104
+ WinRT . Interop . InitializeWithWindow . Initialize ( filePicker , handle ) ;
105
+
106
+ var file = await filePicker . PickSingleFileAsync ( ) ;
107
+ if ( file is not null )
108
+ AppThemeBackgroundImageSource = file . Path ;
109
+ }
110
+
111
+ /// <summary>
112
+ /// Clears the current background image
113
+ /// </summary>
114
+ private void RemoveBackgroundImage ( )
115
+ {
116
+ AppThemeBackgroundImageSource = string . Empty ;
46
117
}
47
118
48
119
/// <summary>
@@ -143,5 +214,71 @@ public string SelectedBackdropMaterial
143
214
}
144
215
}
145
216
217
+ public string AppThemeBackgroundImageSource
218
+ {
219
+ get => UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageSource ;
220
+ set
221
+ {
222
+ if ( value != UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageSource )
223
+ {
224
+ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageSource = value ;
225
+
226
+ OnPropertyChanged ( ) ;
227
+ }
228
+ }
229
+ }
230
+
231
+ private string selectedImageStretchType ;
232
+ public string SelectedImageStretchType
233
+ {
234
+ get => selectedImageStretchType ;
235
+ set
236
+ {
237
+ if ( SetProperty ( ref selectedImageStretchType , value ) )
238
+ {
239
+ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageFit = ImageStretchTypes . First ( e => e . Value == value ) . Key ;
240
+ }
241
+ }
242
+ }
243
+
244
+ public float AppThemeBackgroundImageOpacity
245
+ {
246
+ get => UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageOpacity ;
247
+ set
248
+ {
249
+ if ( value != UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageOpacity )
250
+ {
251
+ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageOpacity = value ;
252
+
253
+ OnPropertyChanged ( ) ;
254
+ }
255
+ }
256
+ }
257
+
258
+ private string selectedImageVerticalAlignmentType ;
259
+ public string SelectedImageVerticalAlignmentType
260
+ {
261
+ get => selectedImageVerticalAlignmentType ;
262
+ set
263
+ {
264
+ if ( SetProperty ( ref selectedImageVerticalAlignmentType , value ) )
265
+ {
266
+ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageVerticalAlignment = ImageVerticalAlignmentTypes . First ( e => e . Value == value ) . Key ;
267
+ }
268
+ }
269
+ }
270
+
271
+ private string selectedImageHorizontalAlignmentType ;
272
+ public string SelectedImageHorizontalAlignmentType
273
+ {
274
+ get => selectedImageHorizontalAlignmentType ;
275
+ set
276
+ {
277
+ if ( SetProperty ( ref selectedImageHorizontalAlignmentType , value ) )
278
+ {
279
+ UserSettingsService . AppearanceSettingsService . AppThemeBackgroundImageHorizontalAlignment = ImageHorizontalAlignmentTypes . First ( e => e . Value == value ) . Key ;
280
+ }
281
+ }
282
+ }
146
283
}
147
284
}
0 commit comments