1
1
// Copyright (c) 2024 Files Community
2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
- using System . Runtime . CompilerServices ;
4
+ using Microsoft . Extensions . Logging ;
5
+ using Windows . Storage ;
6
+ using Windows . System . UserProfile ;
5
7
using Windows . Win32 ;
6
8
using Windows . Win32 . Foundation ;
7
9
using Windows . Win32 . System . Com ;
@@ -16,57 +18,64 @@ public sealed class WindowsWallpaperService : IWindowsWallpaperService
16
18
/// <inheritdoc/>
17
19
public unsafe bool SetDesktopWallpaper ( string szPath )
18
20
{
19
- PInvoke . CoCreateInstance (
20
- typeof ( DesktopWallpaper ) . GUID ,
21
- null ,
22
- CLSCTX . CLSCTX_INPROC_SERVER ,
23
- out IDesktopWallpaper desktopWallpaper ) ;
24
-
25
- desktopWallpaper . GetMonitorDevicePathCount ( out var dwMonitorCount ) ;
26
-
27
- fixed ( char * pszPath = szPath )
21
+ try
28
22
{
29
- var pwszPath = new PWSTR ( pszPath ) ;
23
+ PInvoke . CoCreateInstance (
24
+ typeof ( DesktopWallpaper ) . GUID ,
25
+ null ,
26
+ CLSCTX . CLSCTX_INPROC_SERVER ,
27
+ out IDesktopWallpaper desktopWallpaper ) ;
28
+
29
+ desktopWallpaper . GetMonitorDevicePathCount ( out var dwMonitorCount ) ;
30
30
31
- for ( uint dwIndex = 0 ; dwIndex < dwMonitorCount ; dwIndex ++ )
31
+ fixed ( char * pszPath = szPath )
32
32
{
33
- desktopWallpaper . GetMonitorDevicePathAt ( dwIndex , out var pMonitorID ) ;
34
- desktopWallpaper . SetWallpaper ( pMonitorID , pwszPath ) ;
33
+ var pwszPath = new PWSTR ( pszPath ) ;
34
+
35
+ for ( uint dwIndex = 0 ; dwIndex < dwMonitorCount ; dwIndex ++ )
36
+ {
37
+ desktopWallpaper . GetMonitorDevicePathAt ( dwIndex , out var pMonitorID ) ;
38
+ desktopWallpaper . SetWallpaper ( pMonitorID , pwszPath ) ;
39
+ }
35
40
}
41
+
42
+ return true ;
36
43
}
44
+ catch ( Exception ex )
45
+ {
46
+ App . Logger . LogWarning ( ex , ex . Message ) ;
37
47
38
- // win32metadata bug: SetWallpaper should return HRESULT
39
- return true ;
48
+ return false ;
49
+ }
40
50
}
41
51
42
- /// <inheritdoc/>
52
+ /// <inheritdoc/>
43
53
public unsafe bool SetDesktopSlideshow ( string [ ] aszPaths )
44
54
{
45
- PInvoke . CoCreateInstance (
46
- typeof ( DesktopWallpaper ) . GUID ,
47
- null ,
48
- CLSCTX . CLSCTX_INPROC_SERVER ,
49
- out IDesktopWallpaper desktopWallpaper ) ;
50
-
51
55
try
52
56
{
53
- ITEMIDLIST [ ] idList = new ITEMIDLIST [ aszPaths . Length ] ;
57
+ PInvoke . CoCreateInstance (
58
+ typeof ( DesktopWallpaper ) . GUID ,
59
+ null ,
60
+ CLSCTX . CLSCTX_INPROC_SERVER ,
61
+ out IDesktopWallpaper desktopWallpaper ) ;
54
62
55
- foreach ( var szPath in aszPaths )
56
- {
57
- var id = PInvoke . ILCreateFromPath ( szPath ) ;
58
- idList . Append ( * id ) ;
59
- }
63
+ var dwCount = ( uint ) aszPaths . Length ;
60
64
61
- fixed ( ITEMIDLIST * * idListPointers = idList )
65
+ fixed ( ITEMIDLIST * * idList = new ITEMIDLIST * [ dwCount ] )
62
66
{
67
+ for ( uint dwIndex = 0u ; dwIndex < dwCount ; dwIndex ++ )
68
+ {
69
+ var id = PInvoke . ILCreateFromPath ( aszPaths [ dwIndex ] ) ;
70
+ idList [ dwIndex ] = id ;
71
+ }
63
72
64
- }
65
-
66
- var idList = aszPaths . Select ( x => PInvoke . ILCreateFromPath ( x ) ) . ToList ( ) ;
67
- PInvoke . SHCreateShellItemArrayFromIDLists ( ( uint ) idList . cou , [ .. idList ] , out var shellItemArray ) ;
73
+ // Get shell item array from images to use for slideshow
74
+ PInvoke . SHCreateShellItemArrayFromIDLists ( dwCount , idList , out var shellItemArray ) ;
68
75
69
- desktopWallpaper . SetSlideshow ( shellItemArray ) ;
76
+ // Set slideshow
77
+ desktopWallpaper . SetSlideshow ( shellItemArray ) ;
78
+ }
70
79
71
80
// Set wallpaper to fill desktop.
72
81
desktopWallpaper . SetPosition ( DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) ;
@@ -76,15 +85,28 @@ public unsafe bool SetDesktopSlideshow(string[] aszPaths)
76
85
}
77
86
catch ( Exception ex )
78
87
{
88
+ App . Logger . LogWarning ( ex , ex . Message ) ;
89
+
79
90
return false ;
80
91
}
81
92
}
82
93
83
94
/// <inheritdoc/>
84
- public bool SetLockScreenWallpaper ( string szPath )
95
+ public async Task < bool > SetLockScreenWallpaper ( string szPath )
85
96
{
86
- // TODO: Use LockScreen WinRT class
87
- return true ;
97
+ try
98
+ {
99
+ IStorageFile sourceFile = await StorageFile . GetFileFromPathAsync ( szPath ) ;
100
+ await LockScreen . SetImageFileAsync ( sourceFile ) ;
101
+
102
+ return true ;
103
+ }
104
+ catch ( Exception ex )
105
+ {
106
+ App . Logger . LogWarning ( ex , ex . Message ) ;
107
+
108
+ return false ;
109
+ }
88
110
}
89
111
}
90
112
}
0 commit comments