浏览代码

Update setting as wallpaper and disable lockscreen image

Ruben 8 月之前
父节点
当前提交
977cc32c5d

+ 2 - 2
src/PicView.Avalonia.Win32/App.axaml.cs

@@ -20,7 +20,6 @@ using PicView.Core.Localization;
 using PicView.Core.ProcessHandling;
 using PicView.Core.ProcessHandling;
 using PicView.WindowsNT;
 using PicView.WindowsNT;
 using PicView.WindowsNT.FileHandling;
 using PicView.WindowsNT.FileHandling;
-using PicView.WindowsNT.Lockscreen;
 using PicView.WindowsNT.Taskbar;
 using PicView.WindowsNT.Taskbar;
 using PicView.WindowsNT.Wallpaper;
 using PicView.WindowsNT.Wallpaper;
 using Dispatcher = Avalonia.Threading.Dispatcher;
 using Dispatcher = Avalonia.Threading.Dispatcher;
@@ -478,7 +477,8 @@ public partial class App : Application, IPlatformSpecificService
     
     
     public bool SetAsLockScreen(string path)
     public bool SetAsLockScreen(string path)
     {
     {
-        return LockscreenHelper.SetLockScreenImage(path);
+        return false;
+        // return LockscreenHelper.SetLockScreenImage(path);
     }
     }
 
 
     public bool CopyFile(string path)
     public bool CopyFile(string path)

+ 30 - 1
src/PicView.Avalonia/ImageHandling/ImageHelper.cs

@@ -18,8 +18,35 @@ public static class ImageHelper
         return frames > 1;
         return frames > 1;
     }
     }
     
     
-    public static async Task<string> ConvertToCommonSupportedFormatAsync(string path)
+    public static async Task<string> ConvertToCommonSupportedFormatAsync(string path, MainViewModel vm)
     {
     {
+        if (NavigationHelper.CanNavigate(vm) && vm.EffectConfig is not null && !string.IsNullOrEmpty(path))
+        {
+            Bitmap? source = null;
+            if (path == vm.FileInfo.FullName)
+            {
+                if (vm.ImageSource is Bitmap bmp)
+                {
+                    source = bmp;
+                }
+            }
+            else
+            {
+                var preloadValue = await vm.ImageIterator.GetPreLoadValueAsync(vm.ImageIterator.ImagePaths.IndexOf(path))
+                    .ConfigureAwait(false);
+                if (preloadValue?.ImageModel.Image is Bitmap bitmap)
+                {
+                    source = bitmap;
+                }
+            }
+
+            if (source is not null)
+            {
+                var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + "bmp");
+                source.Save(tempPath);
+                return tempPath;
+            }
+        }
         var url = path.GetURL();
         var url = path.GetURL();
         if (!string.IsNullOrWhiteSpace(url))
         if (!string.IsNullOrWhiteSpace(url))
         {
         {
@@ -57,6 +84,8 @@ public static class ImageHelper
                 var success = await SaveImageFileHelper.SaveImageAsync(null, path, tempPath, null, null, null, ".png");
                 var success = await SaveImageFileHelper.SaveImageAsync(null, path, tempPath, null, null, null, ".png");
                 return !success ? string.Empty : tempPath;
                 return !success ? string.Empty : tempPath;
         }
         }
+        
+
     }
     }
     
     
     
     

+ 15 - 3
src/PicView.Avalonia/StartUp/StartUpHelper.cs

@@ -39,10 +39,22 @@ public static class StartUpHelper
             {
             {
                 HandleMultipleInstances(args);
                 HandleMultipleInstances(args);
             }
             }
-            else if (args.Length > 1 && args[1].Equals("update", StringComparison.InvariantCultureIgnoreCase))
+            else if (args.Length > 1)
             {
             {
-                Task.Run(async () => await UpdateManager.UpdateCurrentVersion(vm));
-                return;
+                var arg = args[1];
+                if (arg.Equals("update", StringComparison.InvariantCultureIgnoreCase))
+                {
+                    Task.Run(async () => await UpdateManager.UpdateCurrentVersion(vm));
+                    return;
+                }
+                if (arg.StartsWith("lockscreen", StringComparison.InvariantCultureIgnoreCase))
+                {
+                    // var path = arg[(arg.LastIndexOf(',') + 1)..];
+                    // path = Path.GetFullPath(path);
+                    // vm.PlatformService.SetAsLockScreen(path);
+                    // Environment.Exit(0);
+                    return;
+                }
             }
             }
         }
         }
         
         

+ 56 - 50
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -1364,14 +1364,7 @@ public class MainViewModel : ViewModelBase
         {
         {
             return;
             return;
         }
         }
-        if (PlatformService is null)
-        {
-            return;
-        }
-        await Task.Run(() =>
-        {
-            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Fit));
-        });
+        await SetAsWallpaperTask(path, WallpaperStyle.Fit).ConfigureAwait(false);
     }
     }
     
     
     public async Task SetAsWallpaperFilledTask(string path)
     public async Task SetAsWallpaperFilledTask(string path)
@@ -1380,14 +1373,7 @@ public class MainViewModel : ViewModelBase
         {
         {
             return;
             return;
         }
         }
-        if (PlatformService is null)
-        {
-            return;
-        }
-        await Task.Run(() =>
-        {
-            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Fill));
-        });
+        await SetAsWallpaperTask(path, WallpaperStyle.Fill).ConfigureAwait(false);
     }
     }
     
     
     public async Task SetAsWallpaperTiledTask(string path)
     public async Task SetAsWallpaperTiledTask(string path)
@@ -1396,14 +1382,7 @@ public class MainViewModel : ViewModelBase
         {
         {
             return;
             return;
         }
         }
-        if (PlatformService is null)
-        {
-            return;
-        }
-        await Task.Run(() =>
-        {
-            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Tile));
-        });
+        await SetAsWallpaperTask(path, WallpaperStyle.Tile).ConfigureAwait(false);
     }
     }
     
     
     public async Task SetAsWallpaperStretchedTask(string path)
     public async Task SetAsWallpaperStretchedTask(string path)
@@ -1412,14 +1391,7 @@ public class MainViewModel : ViewModelBase
         {
         {
             return;
             return;
         }
         }
-        if (PlatformService is null)
-        {
-            return;
-        }
-        await Task.Run(() =>
-        {
-            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Stretch));
-        });
+        await SetAsWallpaperTask(path, WallpaperStyle.Stretch).ConfigureAwait(false);
     }
     }
     
     
     public async Task SetAsWallpaperCenteredTask(string path)
     public async Task SetAsWallpaperCenteredTask(string path)
@@ -1428,14 +1400,35 @@ public class MainViewModel : ViewModelBase
         {
         {
             return;
             return;
         }
         }
+        await SetAsWallpaperTask(path, WallpaperStyle.Center).ConfigureAwait(false);
+    }
+    
+    public async Task SetAsWallpaperTask(string path, WallpaperStyle style)
+    {
+
         if (PlatformService is null)
         if (PlatformService is null)
         {
         {
             return;
             return;
         }
         }
-        await Task.Run(() =>
+        
+        IsLoading = true;
+        try
         {
         {
-            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Center));
-        });
+            var file = await ImageHelper.ConvertToCommonSupportedFormatAsync(path, this).ConfigureAwait(false);
+
+            PlatformService?.SetAsWallpaper(file, WallpaperManager.GetWallpaperStyle(style));
+        }
+        catch (Exception e)
+        {
+            await TooltipHelper.ShowTooltipMessageAsync(e.Message, true);
+#if DEBUG
+            Console.WriteLine(e);   
+#endif
+        }
+        finally
+        {
+            IsLoading = false;
+        }
     }
     }
     
     
     private async Task SetAsLockScreenTask(string path)
     private async Task SetAsLockScreenTask(string path)
@@ -1451,23 +1444,36 @@ public class MainViewModel : ViewModelBase
         
         
         IsLoading = true;
         IsLoading = true;
 
 
-        var file = await ImageHelper.ConvertToCommonSupportedFormatAsync(path).ConfigureAwait(false);
-
-        var process = new Process
+        try
         {
         {
-            StartInfo = new ProcessStartInfo
+            var file = await ImageHelper.ConvertToCommonSupportedFormatAsync(path, this).ConfigureAwait(false);
+
+            var process = new Process
             {
             {
-                Verb = "runas",
-                UseShellExecute = true,
-                FileName = "PicView.exe",
-                Arguments = "lockscreen," + file,
-                WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory
-            }
-        };
-        process.Start();
-        await TooltipHelper.ShowTooltipMessageAsync(TranslationHelper.Translation.Applying, true);
-        await process.WaitForExitAsync();
-        IsLoading = false;
+                StartInfo = new ProcessStartInfo
+                {
+                    Verb = "runas",
+                    UseShellExecute = true,
+                    FileName = "PicView.exe",
+                    Arguments = "lockscreen," + file,
+                    WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory
+                }
+            };
+            process.Start();
+            await TooltipHelper.ShowTooltipMessageAsync(TranslationHelper.Translation.Applying, true);
+            await process.WaitForExitAsync();
+        }
+        catch (Exception e)
+        {
+            await TooltipHelper.ShowTooltipMessageAsync(e.Message, true);
+#if DEBUG
+         Console.WriteLine(e);   
+#endif
+        }
+        finally
+        {
+            IsLoading = false;
+        }
     }
     }
 
 
     public async Task GalleryItemStretchTask(string value)
     public async Task GalleryItemStretchTask(string value)

+ 13 - 15
src/PicView.Avalonia/Views/MainView.axaml

@@ -541,11 +541,7 @@
             <Separator />
             <Separator />
 
 
             <!--  Set as wallpaper  -->
             <!--  Set as wallpaper  -->
-            <MenuItem
-                Header="{CompiledBinding SetAsWallpaper,
-                                         Mode=OneWay}"
-                PointerReleased="SetWallpaperClick"
-                x:Name="WallpaperMenuItem">
+            <MenuItem Header="{CompiledBinding SetAsWallpaper, Mode=OneWay}" x:Name="WallpaperMenuItem">
                 <MenuItem.Icon>
                 <MenuItem.Icon>
                     <Path
                     <Path
                         Data="{StaticResource PanoramaGeometry}"
                         Data="{StaticResource PanoramaGeometry}"
@@ -554,13 +550,13 @@
                         Stretch="Fill"
                         Stretch="Fill"
                         Width="13" />
                         Width="13" />
                 </MenuItem.Icon>
                 </MenuItem.Icon>
-
-                <!--  Set as wallpaper fit  -->
+                
+                <!--  Set as wallpaper filled  -->
                 <MenuItem
                 <MenuItem
-                    Command="{CompiledBinding SetAsWallpaperCommand}"
+                    Command="{CompiledBinding SetAsWallpaperFilledCommand}"
                     CommandParameter="{CompiledBinding FileInfo.FullName,
                     CommandParameter="{CompiledBinding FileInfo.FullName,
                                                        FallbackValue=''}"
                                                        FallbackValue=''}"
-                    Header="{CompiledBinding Fit,
+                    Header="{CompiledBinding Fill,
                                              Mode=OneWay}">
                                              Mode=OneWay}">
                     <MenuItem.Icon>
                     <MenuItem.Icon>
                         <Path
                         <Path
@@ -572,12 +568,12 @@
                     </MenuItem.Icon>
                     </MenuItem.Icon>
                 </MenuItem>
                 </MenuItem>
 
 
-                <!--  Set as wallpaper stretched  -->
+                <!--  Set as wallpaper fit  -->
                 <MenuItem
                 <MenuItem
-                    Command="{CompiledBinding SetAsWallpaperStretchedCommand}"
+                    Command="{CompiledBinding SetAsWallpaperCommand}"
                     CommandParameter="{CompiledBinding FileInfo.FullName,
                     CommandParameter="{CompiledBinding FileInfo.FullName,
                                                        FallbackValue=''}"
                                                        FallbackValue=''}"
-                    Header="{CompiledBinding Stretch,
+                    Header="{CompiledBinding Fit,
                                              Mode=OneWay}">
                                              Mode=OneWay}">
                     <MenuItem.Icon>
                     <MenuItem.Icon>
                         <Path
                         <Path
@@ -589,12 +585,12 @@
                     </MenuItem.Icon>
                     </MenuItem.Icon>
                 </MenuItem>
                 </MenuItem>
 
 
-                <!--  Set as wallpaper filled  -->
+                <!--  Set as wallpaper stretched  -->
                 <MenuItem
                 <MenuItem
-                    Command="{CompiledBinding SetAsWallpaperFilledCommand}"
+                    Command="{CompiledBinding SetAsWallpaperStretchedCommand}"
                     CommandParameter="{CompiledBinding FileInfo.FullName,
                     CommandParameter="{CompiledBinding FileInfo.FullName,
                                                        FallbackValue=''}"
                                                        FallbackValue=''}"
-                    Header="{CompiledBinding Fill,
+                    Header="{CompiledBinding Stretch,
                                              Mode=OneWay}">
                                              Mode=OneWay}">
                     <MenuItem.Icon>
                     <MenuItem.Icon>
                         <Path
                         <Path
@@ -606,6 +602,8 @@
                     </MenuItem.Icon>
                     </MenuItem.Icon>
                 </MenuItem>
                 </MenuItem>
 
 
+
+
                 <!--  Set as wallpaper centered  -->
                 <!--  Set as wallpaper centered  -->
                 <MenuItem
                 <MenuItem
                     Command="{CompiledBinding SetAsWallpaperCenteredCommand}"
                     Command="{CompiledBinding SetAsWallpaperCenteredCommand}"

+ 0 - 7
src/PicView.Avalonia/Views/MainView.axaml.cs

@@ -3,7 +3,6 @@ using System.Runtime.InteropServices;
 using Avalonia;
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls;
 using Avalonia.Input;
 using Avalonia.Input;
-using Avalonia.Interactivity;
 using Avalonia.Media;
 using Avalonia.Media;
 using PicView.Avalonia.Crop;
 using PicView.Avalonia.Crop;
 using PicView.Avalonia.DragAndDrop;
 using PicView.Avalonia.DragAndDrop;
@@ -200,10 +199,4 @@ public partial class MainView : UserControl
     {
     {
         DragAndDropHelper.DragLeave(e, this);
         DragAndDropHelper.DragLeave(e, this);
     }
     }
-
-    private void SetWallpaperClick(object? sender, RoutedEventArgs e)
-    {
-        Task.Run(FunctionsHelper.SetAsWallpaper);
-        MainContextMenu.Close();
-    }
 }
 }

+ 46 - 46
src/PicView.WindowsNT/Lockscreen/LockscreenHelper.cs

@@ -1,46 +1,46 @@
-using System.Runtime.InteropServices;
-using Microsoft.Win32;
-
-namespace PicView.WindowsNT.Lockscreen;
-
-public static partial class LockscreenHelper
-{
-    [LibraryImport("kernel32.dll", SetLastError = true)]
-    [return: MarshalAs(UnmanagedType.Bool)]
-    public static partial bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
-
-    public static bool SetLockScreenImage(string path)
-    {
-        const string personalizationcsp =
-            @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP";
-        // const string enforceLockScreenAndLogonImage =
-        //     @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PolicyManager\default\DeviceLock\EnforceLockScreenAndLogonImage";
-        // const string enforcelockscreenprovider =
-        //     @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PolicyManager\default\DeviceLock\EnforceLockScreenProvider";
-        var ptr = new IntPtr();
-        Wow64DisableWow64FsRedirection(ref ptr);
-
-        try
-        {
-            Registry.SetValue(personalizationcsp, "LockScreenImageStatus", 1, RegistryValueKind.DWord);
-            Registry.SetValue(personalizationcsp, "LockScreenImagePath", path, RegistryValueKind.String);
-            Registry.SetValue(personalizationcsp, "LockScreenImageUrl", path, RegistryValueKind.String);
-            
-            // Registry.SetValue(enforceLockScreenAndLogonImage, "policytype", 0, RegistryValueKind.DWord);
-            // Registry.SetValue(enforcelockscreenprovider, "policytype", 0, RegistryValueKind.DWord);
-            
-            // Seems to only work once, and then have to restart the machine to make it work again. 
-            // It seems to disable setting the lock screen image in the settings app with the text:
-            // *Some settings are managed by your organization
-            // enforceLockScreenAndLogonImage and enforcelockscreenprovider are both set to 0 to try to disable it,
-            // but it doesn't seem to work.
-            // Need to investigate further and disable it for now.
-        }
-        catch
-        {
-            return false;
-        }
-
-        return true;
-    }
-}
+// using System.Runtime.InteropServices;
+// using Microsoft.Win32;
+//
+// namespace PicView.WindowsNT.Lockscreen;
+//
+// public static partial class LockscreenHelper
+// {
+//     [LibraryImport("kernel32.dll", SetLastError = true)]
+//     [return: MarshalAs(UnmanagedType.Bool)]
+//     public static partial bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
+//
+//     public static bool SetLockScreenImage(string path)
+//     {
+//         const string personalizationcsp =
+//             @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP";
+//         // const string enforceLockScreenAndLogonImage =
+//         //     @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PolicyManager\default\DeviceLock\EnforceLockScreenAndLogonImage";
+//         // const string enforcelockscreenprovider =
+//         //     @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PolicyManager\default\DeviceLock\EnforceLockScreenProvider";
+//         var ptr = new IntPtr();
+//         Wow64DisableWow64FsRedirection(ref ptr);
+//
+//         try
+//         {
+//             Registry.SetValue(personalizationcsp, "LockScreenImageStatus", 1, RegistryValueKind.DWord);
+//             Registry.SetValue(personalizationcsp, "LockScreenImagePath", path, RegistryValueKind.String);
+//             Registry.SetValue(personalizationcsp, "LockScreenImageUrl", path, RegistryValueKind.String);
+//             
+//             // Registry.SetValue(enforceLockScreenAndLogonImage, "policytype", 0, RegistryValueKind.DWord);
+//             // Registry.SetValue(enforcelockscreenprovider, "policytype", 0, RegistryValueKind.DWord);
+//             
+//             // Seems to only work once, and then have to restart the machine to make it work again. 
+//             // It seems to disable setting the lock screen image in the settings app with the text:
+//             // *Some settings are managed by your organization
+//             // enforceLockScreenAndLogonImage and enforcelockscreenprovider are both set to 0 to try to disable it,
+//             // but it doesn't seem to work.
+//             // Need to investigate further and disable it for now.
+//         }
+//         catch
+//         {
+//             return false;
+//         }
+//
+//         return true;
+//     }
+// }