11// Copyright (c) Files Community 
22// Licensed under the MIT License. 
33
4- using  System . IO ; 
5- using  Vanara . PInvoke ; 
6- using  Vanara . Windows . Shell ; 
74using  Windows . Storage ; 
85using  Windows . System . UserProfile ; 
96using  Windows . Win32 ; 
@@ -16,17 +13,15 @@ namespace Files.App.Services
1613	/// <inheritdoc cref="IWindowsWallpaperService"/> 
1714	public  sealed  class  WindowsWallpaperService  :  IWindowsWallpaperService 
1815	{ 
19- 		private  static   readonly  Ole32 . PROPERTYKEY  PKEY_FilePlaceholderStatus  =  new  Ole32 . PROPERTYKEY ( new  Guid ( "B2F9B9D6-FEC4-4DD5-94D7-8957488C807B" ) ,  2 ) ; 
20- 		private  const  uint  PS_CLOUDFILE_PLACEHOLDER  =  8 ; 
2116		/// <inheritdoc/> 
2217		public  unsafe  void  SetDesktopWallpaper ( string  szPath ) 
2318		{ 
2419			// Instantiate IDesktopWallpaper 
2520			using  ComPtr < IDesktopWallpaper >  pDesktopWallpaper  =  default ; 
26- 			HRESULT  hr  =  pDesktopWallpaper . CoCreateInstance ( CLSID . CLSID_DesktopWallpaper ) . ThrowOnFailure ( ) ; 
21+ 			HRESULT  hr  =  pDesktopWallpaper . CoCreateInstance ( CLSID . CLSID_DesktopWallpaper ) ; 
2722
2823			// Get total count of all available monitors 
29- 			hr  =  pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathCount ( out  var  dwMonitorCount ) . ThrowOnFailure ( ) ; 
24+ 			hr  =  pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathCount ( out  var  dwMonitorCount ) ; 
3025
3126			fixed ( char *  pszPath  =  szPath ) 
3227			{ 
@@ -36,8 +31,8 @@ public unsafe void SetDesktopWallpaper(string szPath)
3631				for  ( uint  dwIndex  =  0u ;  dwIndex  <  dwMonitorCount ;  dwIndex ++ ) 
3732				{ 
3833					// Set the wallpaper 
39- 					hr  =  pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathAt ( dwIndex ,  & pMonitorId ) . ThrowOnFailure ( ) ; 
40- 					hr  =  pDesktopWallpaper . Get ( ) ->SetWallpaper ( pMonitorId ,  pszPath ) . ThrowOnFailure ( ) ; 
34+ 					hr  =  pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathAt ( dwIndex ,  & pMonitorId ) ; 
35+ 					hr  =  pDesktopWallpaper . Get ( ) ->SetWallpaper ( pMonitorId ,  pszPath ) ; 
4136
4237					pMonitorId  =  default ; 
4338				} 
@@ -49,7 +44,7 @@ public unsafe void SetDesktopSlideshow(string[] aszPaths)
4944		{ 
5045			// Instantiate IDesktopWallpaper 
5146			using  ComPtr < IDesktopWallpaper >  pDesktopWallpaper  =  default ; 
52- 			HRESULT  hr  =  pDesktopWallpaper . CoCreateInstance ( CLSID . CLSID_DesktopWallpaper ) . ThrowOnFailure ( ) ; 
47+ 			HRESULT  hr  =  pDesktopWallpaper . CoCreateInstance ( CLSID . CLSID_DesktopWallpaper ) ; 
5348
5449			uint  dwCount  =  ( uint ) aszPaths . Length ; 
5550			ITEMIDLIST * *  ppItemIdList  =  stackalloc  ITEMIDLIST * [ aszPaths . Length ] ; 
@@ -60,52 +55,22 @@ public unsafe void SetDesktopSlideshow(string[] aszPaths)
6055
6156			// Get an IShellItemArray from the array of the PIDL 
6257			using  ComPtr < IShellItemArray >  pShellItemArray  =  default ; 
63- 			hr  =  PInvoke . SHCreateShellItemArrayFromIDLists ( dwCount ,  ppItemIdList ,  pShellItemArray . GetAddressOf ( ) ) . ThrowOnFailure ( ) ; 
58+ 			hr  =  PInvoke . SHCreateShellItemArrayFromIDLists ( dwCount ,  ppItemIdList ,  pShellItemArray . GetAddressOf ( ) ) ; 
6459
6560			// Release the allocated PIDL 
6661			for  ( uint  dwIndex  =  0u ;  dwIndex  <  dwCount ;  dwIndex ++ ) 
6762				PInvoke . CoTaskMemFree ( ( void * ) ppItemIdList [ dwIndex ] ) ; 
6863
6964			// Set the slideshow and its position 
70- 			hr  =  pDesktopWallpaper . Get ( ) ->SetSlideshow ( pShellItemArray . Get ( ) ) . ThrowOnFailure ( ) ; 
71- 			hr  =  pDesktopWallpaper . Get ( ) ->SetPosition ( DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) . ThrowOnFailure ( ) ; 
65+ 			hr  =  pDesktopWallpaper . Get ( ) ->SetSlideshow ( pShellItemArray . Get ( ) ) ; 
66+ 			hr  =  pDesktopWallpaper . Get ( ) ->SetPosition ( DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) ; 
7267		} 
7368
7469		/// <inheritdoc/> 
7570		public  async  Task  SetLockScreenWallpaper ( string  szPath ) 
7671		{ 
77- 			// Verify the file exists on disk 
78- 			if  ( ! File . Exists ( szPath ) ) 
79- 				throw  new  FileNotFoundException ( "The specified file does not exist." ,  szPath ) ; 
80- 
81- 			// Check if the file is a cloud placeholder (online-only file) 
82- 			if  ( IsCloudPlaceholder ( szPath ) ) 
83- 				throw  new  InvalidOperationException ( "The file is stored in the cloud and is not available offline. Please download the file before setting it as a wallpaper." ) ; 
84- 
8572			IStorageFile  sourceFile  =  await  StorageFile . GetFileFromPathAsync ( szPath ) ; 
8673			await  LockScreen . SetImageFileAsync ( sourceFile ) ; 
8774		} 
88- 
89- 		/// <summary> 
90- 		/// Checks if the file is a cloud placeholder (online-only file). 
91- 		/// </summary> 
92- 		/// <param name="path">The path to the file.</param> 
93- 		/// <returns>True if the file is a cloud placeholder; otherwise, false.</returns> 
94- 		private  static   bool  IsCloudPlaceholder ( string  path ) 
95- 		{ 
96- 			try 
97- 			{ 
98- 				using  var  shi  =  new  ShellItem ( path ) ; 
99- 				if  ( shi . Properties . TryGetValue < uint > ( PKEY_FilePlaceholderStatus ,  out  var  value )  &&  value  ==  PS_CLOUDFILE_PLACEHOLDER ) 
100- 					return  true ; 
101- 			} 
102- 			catch 
103- 			{ 
104- 				// If we can't determine the placeholder status, assume it's not a placeholder 
105- 				// and let the subsequent StorageFile.GetFileFromPathAsync call handle any errors 
106- 			} 
107- 
108- 			return  false ; 
109- 		} 
11075	} 
11176} 
0 commit comments