Skip to content

Commit 6064cfd

Browse files
committed
Fix
1 parent 8e3757b commit 6064cfd

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

src/Files.App/Data/Contracts/IWindowsWallpaperService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public interface IWindowsWallpaperService
99

1010
bool SetDesktopSlideshow(string[] aszPaths);
1111

12-
bool SetLockScreenWallpaper(string szPath);
12+
Task<bool> SetLockScreenWallpaper(string szPath);
1313
}
1414
}
Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.Runtime.CompilerServices;
4+
using Microsoft.Extensions.Logging;
5+
using Windows.Storage;
6+
using Windows.System.UserProfile;
57
using Windows.Win32;
68
using Windows.Win32.Foundation;
79
using Windows.Win32.System.Com;
@@ -16,57 +18,64 @@ public sealed class WindowsWallpaperService : IWindowsWallpaperService
1618
/// <inheritdoc/>
1719
public unsafe bool SetDesktopWallpaper(string szPath)
1820
{
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
2822
{
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);
3030

31-
for (uint dwIndex = 0; dwIndex < dwMonitorCount; dwIndex++)
31+
fixed (char* pszPath = szPath)
3232
{
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+
}
3540
}
41+
42+
return true;
3643
}
44+
catch (Exception ex)
45+
{
46+
App.Logger.LogWarning(ex, ex.Message);
3747

38-
// win32metadata bug: SetWallpaper should return HRESULT
39-
return true;
48+
return false;
49+
}
4050
}
4151

42-
/// <inheritdoc/>
52+
/// <inheritdoc/>
4353
public unsafe bool SetDesktopSlideshow(string[] aszPaths)
4454
{
45-
PInvoke.CoCreateInstance(
46-
typeof(DesktopWallpaper).GUID,
47-
null,
48-
CLSCTX.CLSCTX_INPROC_SERVER,
49-
out IDesktopWallpaper desktopWallpaper);
50-
5155
try
5256
{
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);
5462

55-
foreach (var szPath in aszPaths)
56-
{
57-
var id = PInvoke.ILCreateFromPath(szPath);
58-
idList.Append(*id);
59-
}
63+
var dwCount = (uint)aszPaths.Length;
6064

61-
fixed (ITEMIDLIST** idListPointers = idList)
65+
fixed (ITEMIDLIST** idList = new ITEMIDLIST*[dwCount])
6266
{
67+
for (uint dwIndex = 0u; dwIndex < dwCount; dwIndex++)
68+
{
69+
var id = PInvoke.ILCreateFromPath(aszPaths[dwIndex]);
70+
idList[dwIndex] = id;
71+
}
6372

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);
6875

69-
desktopWallpaper.SetSlideshow(shellItemArray);
76+
// Set slideshow
77+
desktopWallpaper.SetSlideshow(shellItemArray);
78+
}
7079

7180
// Set wallpaper to fill desktop.
7281
desktopWallpaper.SetPosition(DESKTOP_WALLPAPER_POSITION.DWPOS_FILL);
@@ -76,15 +85,28 @@ public unsafe bool SetDesktopSlideshow(string[] aszPaths)
7685
}
7786
catch (Exception ex)
7887
{
88+
App.Logger.LogWarning(ex, ex.Message);
89+
7990
return false;
8091
}
8192
}
8293

8394
/// <inheritdoc/>
84-
public bool SetLockScreenWallpaper(string szPath)
95+
public async Task<bool> SetLockScreenWallpaper(string szPath)
8596
{
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+
}
88110
}
89111
}
90112
}

0 commit comments

Comments
 (0)