Skip to content

Commit 047bb7e

Browse files
committed
Added comments & replace
1 parent 6064cfd commit 047bb7e

File tree

8 files changed

+115
-163
lines changed

8 files changed

+115
-163
lines changed

src/Files.App/Actions/Content/Background/BaseSetAsAction.cs

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Microsoft.UI.Xaml.Controls;
5+
using Windows.Foundation.Metadata;
6+
47
namespace Files.App.Actions
58
{
69
internal abstract class BaseSetAsAction : ObservableObject, IAction
@@ -28,6 +31,21 @@ public BaseSetAsAction()
2831

2932
public abstract Task ExecuteAsync(object? parameter = null);
3033

34+
protected async void ShowErrorDialog(string message)
35+
{
36+
var errorDialog = new ContentDialog()
37+
{
38+
Title = "FailedToSetBackground".GetLocalizedResource(),
39+
Content = message,
40+
PrimaryButtonText = "OK".GetLocalizedResource(),
41+
};
42+
43+
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
44+
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
45+
46+
await errorDialog.TryShowAsync();
47+
}
48+
3149
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
3250
{
3351
switch (e.PropertyName)

src/Files.App/Actions/Content/Background/SetAsLockscreenBackgroundAction.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Files.App.Actions
55
{
66
internal sealed class SetAsLockscreenBackgroundAction : BaseSetAsAction
77
{
8+
private readonly IWindowsWallpaperService WindowsWallpaperService = Ioc.Default.GetRequiredService<IWindowsWallpaperService>();
9+
810
public override string Label
911
=> "SetAsLockscreen".GetLocalizedResource();
1012

@@ -20,10 +22,19 @@ public override RichGlyph Glyph
2022

2123
public override Task ExecuteAsync(object? parameter = null)
2224
{
23-
if (context.SelectedItem is not null)
24-
return WallpaperHelpers.SetAsBackgroundAsync(WallpaperType.LockScreen, context.SelectedItem.ItemPath);
25+
if (context.SelectedItem is null)
26+
return Task.CompletedTask;
27+
28+
try
29+
{
30+
return WindowsWallpaperService.SetLockScreenWallpaper(context.SelectedItem.ItemPath);
31+
}
32+
catch (Exception ex)
33+
{
34+
ShowErrorDialog(ex.Message);
2535

26-
return Task.CompletedTask;
36+
return Task.CompletedTask;
37+
}
2738
}
2839
}
2940
}

src/Files.App/Actions/Content/Background/SetAsSlideshowBackgroundAction.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Files.App.Actions
55
{
66
internal sealed class SetAsSlideshowBackgroundAction : BaseSetAsAction
77
{
8+
private readonly IWindowsWallpaperService WindowsWallpaperService = Ioc.Default.GetRequiredService<IWindowsWallpaperService>();
9+
810
public override string Label
911
=> "SetAsSlideshow".GetLocalizedResource();
1012

@@ -21,7 +23,15 @@ public override RichGlyph Glyph
2123
public override Task ExecuteAsync(object? parameter = null)
2224
{
2325
var paths = context.SelectedItems.Select(item => item.ItemPath).ToArray();
24-
WallpaperHelpers.SetSlideshow(paths);
26+
27+
try
28+
{
29+
WindowsWallpaperService.SetDesktopSlideshow(paths);
30+
}
31+
catch (Exception ex)
32+
{
33+
ShowErrorDialog(ex.Message);
34+
}
2535

2636
return Task.CompletedTask;
2737
}

src/Files.App/Actions/Content/Background/SetAsWallpaperBackgroundAction.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Files.App.Actions
55
{
66
internal sealed class SetAsWallpaperBackgroundAction : BaseSetAsAction
77
{
8+
private readonly IWindowsWallpaperService WindowsWallpaperService = Ioc.Default.GetRequiredService<IWindowsWallpaperService>();
9+
810
public override string Label
911
=> "SetAsBackground".GetLocalizedResource();
1012

@@ -20,10 +22,21 @@ public override RichGlyph Glyph
2022

2123
public override Task ExecuteAsync(object? parameter = null)
2224
{
23-
if (context.SelectedItem is not null)
24-
return WallpaperHelpers.SetAsBackgroundAsync(WallpaperType.Desktop, context.SelectedItem.ItemPath);
25+
if (context.SelectedItem is null)
26+
return Task.CompletedTask;
27+
28+
try
29+
{
30+
WindowsWallpaperService.SetDesktopWallpaper(context.SelectedItem.ItemPath);
31+
32+
return Task.CompletedTask;
33+
}
34+
catch (Exception ex)
35+
{
36+
ShowErrorDialog(ex.Message);
2537

26-
return Task.CompletedTask;
38+
return Task.CompletedTask;
39+
}
2740
}
2841
}
2942
}

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@
33

44
namespace Files.App.Data.Contracts
55
{
6+
/// <summary>
7+
/// Provides service for manipulating shell wallpapers on Windows.
8+
/// </summary>
69
public interface IWindowsWallpaperService
710
{
8-
bool SetDesktopWallpaper(string szPath);
11+
/// <summary>
12+
/// Sets desktop wallpaper using the specified image path.
13+
/// </summary>
14+
/// <param name="szPath">The image path to use to set as wallpaper.</param>
15+
void SetDesktopWallpaper(string szPath);
916

10-
bool SetDesktopSlideshow(string[] aszPaths);
17+
/// <summary>
18+
/// Sets desktop slideshow using the specified image paths.
19+
/// </summary>
20+
/// <param name="aszPaths">The image paths to use to set as slideshow.</param>
21+
void SetDesktopSlideshow(string[] aszPaths);
1122

12-
Task<bool> SetLockScreenWallpaper(string szPath);
23+
/// <summary>
24+
/// Gets lock screen wallpaper using the specified image path.
25+
/// </summary>
26+
/// <param name="szPath">The image path to use to set as lock screen wallpaper.</param>
27+
Task SetLockScreenWallpaper(string szPath);
1328
}
1429
}

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public static IHost ConfigureHost()
165165
.AddSingleton<ISidebarContext, SidebarContext>()
166166
// Services
167167
.AddSingleton<IWindowsIniService, WindowsIniService>()
168+
.AddSingleton<IWindowsWallpaperService, WindowsWallpaperService>()
168169
.AddSingleton<IAppThemeModeService, AppThemeModeService>()
169170
.AddSingleton<IDialogService, DialogService>()
170171
.AddSingleton<ICommonDialogService, CommonDialogService>()
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using Microsoft.Extensions.Logging;
54
using Windows.Storage;
65
using Windows.System.UserProfile;
76
using Windows.Win32;
@@ -12,101 +11,67 @@
1211

1312
namespace Files.App.Services
1413
{
15-
/// <inheritdoc cref="IWindowsWallpaperService"/>
14+
/// <inheritdoc cref="IWindowsWallpaperService"/>
1615
public sealed class WindowsWallpaperService : IWindowsWallpaperService
1716
{
18-
/// <inheritdoc/>
19-
public unsafe bool SetDesktopWallpaper(string szPath)
17+
/// <inheritdoc/>
18+
public unsafe void SetDesktopWallpaper(string szPath)
2019
{
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);
3025

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

42-
return true;
43-
}
44-
catch (Exception ex)
28+
fixed (char* pszPath = szPath)
4529
{
46-
App.Logger.LogWarning(ex, ex.Message);
30+
var pwszPath = new PWSTR(pszPath);
4731

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+
}
4937
}
5038
}
5139

5240
/// <inheritdoc/>
53-
public unsafe bool SetDesktopSlideshow(string[] aszPaths)
41+
public unsafe void SetDesktopSlideshow(string[] aszPaths)
5442
{
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);
6248

63-
var dwCount = (uint)aszPaths.Length;
49+
var dwCount = (uint)aszPaths.Length;
6450

65-
fixed (ITEMIDLIST** idList = new ITEMIDLIST*[dwCount])
51+
fixed (ITEMIDLIST** idList = new ITEMIDLIST*[dwCount])
52+
{
53+
for (uint dwIndex = 0u; dwIndex < dwCount; dwIndex++)
6654
{
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;
7857
}
7958

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

83-
// win32metadata bug: SetWallpaper should return HRESULT
84-
return true;
62+
// Set slideshow
63+
desktopWallpaper.SetSlideshow(shellItemArray);
8564
}
86-
catch (Exception ex)
87-
{
88-
App.Logger.LogWarning(ex, ex.Message);
8965

90-
return false;
91-
}
66+
// Set wallpaper to fill desktop.
67+
desktopWallpaper.SetPosition(DESKTOP_WALLPAPER_POSITION.DWPOS_FILL);
9268
}
9369

9470
/// <inheritdoc/>
95-
public async Task<bool> SetLockScreenWallpaper(string szPath)
71+
public async Task SetLockScreenWallpaper(string szPath)
9672
{
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);
11075
}
11176
}
11277
}

src/Files.App/Utils/Global/WallpaperHelpers.cs

-81
This file was deleted.

0 commit comments

Comments
 (0)