Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3608,4 +3608,7 @@
<data name="FailedToRestore" xml:space="preserve">
<value>Failed to restore items from Recycle Bin</value>
</data>
<data name="FailedToSetBackground" xml:space="preserve">
<value>Failed to set the background wallpaper</value>
</data>
</root>
57 changes: 43 additions & 14 deletions src/Files.App/Utils/Global/WallpaperHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls;
using Vanara.PInvoke;
using Windows.Foundation.Metadata;
using Windows.Storage;
using Windows.System.UserProfile;

Expand All @@ -14,12 +13,20 @@ public static class WallpaperHelpers
{
public static async Task SetAsBackgroundAsync(WallpaperType type, string filePath)
{

if (type == WallpaperType.Desktop)
{
// Set the desktop background
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
wallpaper.SetWallpaper(monitorId, filePath);
try
{
// Set the desktop background
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
wallpaper.SetWallpaper(monitorId, filePath);
}
catch (Exception ex)
{
ShowErrorPrompt(ex.Message);
}
}
else if (type == WallpaperType.LockScreen)
{
Expand All @@ -34,15 +41,37 @@ public static void SetSlideshow(string[] filePaths)
if (filePaths is null || !filePaths.Any())
return;

var idList = filePaths.Select(Shell32.IntILCreateFromPath).ToArray();
Shell32.SHCreateShellItemArrayFromIDLists((uint)idList.Length, idList.ToArray(), out var shellItemArray);
try
{
var idList = filePaths.Select(Shell32.IntILCreateFromPath).ToArray();
Shell32.SHCreateShellItemArrayFromIDLists((uint)idList.Length, idList.ToArray(), out var shellItemArray);

// Set SlideShow
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
wallpaper.SetSlideshow(shellItemArray);

// Set wallpaper to fill desktop.
wallpaper.SetPosition(Shell32.DESKTOP_WALLPAPER_POSITION.DWPOS_FILL);
}
catch (Exception ex)
{
ShowErrorPrompt(ex.Message);
}
}

private static async void ShowErrorPrompt(string exception)
{
var errorDialog = new ContentDialog()
{
Title = "FailedToSetBackground".GetLocalizedResource(),
Content = exception,
PrimaryButtonText = "OK".GetLocalizedResource(),
};

// Set SlideShow
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
wallpaper.SetSlideshow(shellItemArray);
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;

// Set wallpaper to fill desktop.
wallpaper.SetPosition(Shell32.DESKTOP_WALLPAPER_POSITION.DWPOS_FILL);
await errorDialog.TryShowAsync();
}
}
}