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