ソースを参照

[Avalonia] Update setting wallpaper to include all settings

Ruben 1 年間 前
コミット
6b3016ba83

+ 77 - 2
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -14,6 +14,7 @@ using PicView.Avalonia.ImageTransformations;
 using PicView.Avalonia.Interfaces;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
+using PicView.Avalonia.Wallpaper;
 using PicView.Avalonia.WindowBehavior;
 using PicView.Core.Calculations;
 using PicView.Core.Config;
@@ -503,6 +504,10 @@ public class MainViewModel : ViewModelBase
     public ReactiveCommand<int, Unit>? SlideshowCommand { get; }
     
     public ReactiveCommand<string, Unit>? SetAsWallpaperCommand { get; }
+    public ReactiveCommand<string, Unit>? SetAsWallpaperFilledCommand { get; }
+    public ReactiveCommand<string, Unit>? SetAsWallpaperStretchedCommand { get; }
+    public ReactiveCommand<string, Unit>? SetAsWallpaperTiledCommand { get; }
+    public ReactiveCommand<string, Unit>? SetAsWallpaperCenteredCommand { get; }
     
     public ReactiveCommand<string, Unit>? SetAsLockScreenCommand { get; }
     
@@ -1565,7 +1570,7 @@ public class MainViewModel : ViewModelBase
         });
     }
     
-    private async Task SetAsWallpaperTask(string path)
+    public async Task SetAsWallpaperTask(string path)
     {
         if (string.IsNullOrWhiteSpace(path))
         {
@@ -1577,7 +1582,71 @@ public class MainViewModel : ViewModelBase
         }
         await Task.Run(() =>
         {
-            PlatformService?.SetAsWallpaper(path, 4);
+            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Fit));
+        });
+    }
+    
+    public async Task SetAsWallpaperFilledTask(string path)
+    {
+        if (string.IsNullOrWhiteSpace(path))
+        {
+            return;
+        }
+        if (PlatformService is null)
+        {
+            return;
+        }
+        await Task.Run(() =>
+        {
+            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Fill));
+        });
+    }
+    
+    public async Task SetAsWallpaperTiledTask(string path)
+    {
+        if (string.IsNullOrWhiteSpace(path))
+        {
+            return;
+        }
+        if (PlatformService is null)
+        {
+            return;
+        }
+        await Task.Run(() =>
+        {
+            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Tile));
+        });
+    }
+    
+    public async Task SetAsWallpaperStretchedTask(string path)
+    {
+        if (string.IsNullOrWhiteSpace(path))
+        {
+            return;
+        }
+        if (PlatformService is null)
+        {
+            return;
+        }
+        await Task.Run(() =>
+        {
+            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Stretch));
+        });
+    }
+    
+    public async Task SetAsWallpaperCenteredTask(string path)
+    {
+        if (string.IsNullOrWhiteSpace(path))
+        {
+            return;
+        }
+        if (PlatformService is null)
+        {
+            return;
+        }
+        await Task.Run(() =>
+        {
+            PlatformService?.SetAsWallpaper(path, WallpaperManager.GetWallpaperStyle(WallpaperStyle.Center));
         });
     }
     
@@ -1610,6 +1679,7 @@ public class MainViewModel : ViewModelBase
         process.Start();
         await TooltipHelper.ShowTooltipMessageAsync(TranslationHelper.Translation.Applying, true);
         await process.WaitForExitAsync();
+        IsLoading = false;
     }
 
     public async Task GalleryItemStretchTask(string value)
@@ -1834,6 +1904,11 @@ public class MainViewModel : ViewModelBase
         LocateOnDiskCommand = ReactiveCommand.CreateFromTask<string>(LocateOnDiskTask);
         
         SetAsWallpaperCommand = ReactiveCommand.CreateFromTask<string>(SetAsWallpaperTask);
+        SetAsWallpaperTiledCommand = ReactiveCommand.CreateFromTask<string>(SetAsWallpaperTiledTask);
+        SetAsWallpaperStretchedCommand = ReactiveCommand.CreateFromTask<string>(SetAsWallpaperStretchedTask);
+        SetAsWallpaperCenteredCommand = ReactiveCommand.CreateFromTask<string>(SetAsWallpaperCenteredTask);
+        SetAsWallpaperFilledCommand = ReactiveCommand.CreateFromTask<string>(SetAsWallpaperFilledTask);
+        
         SetAsLockScreenCommand = ReactiveCommand.CreateFromTask<string>(SetAsLockScreenTask);
 
         #endregion File commands

+ 27 - 0
src/PicView.Avalonia/ViewModels/ViewModelBase.cs

@@ -228,10 +228,37 @@ public class ViewModelBase : ReactiveObject
         ShowUI = TranslationHelper.Translation.ShowUI;
         HideUI = TranslationHelper.Translation.HideUI;
         HideBottomToolbar = TranslationHelper.Translation.HideBottomToolbar;
+        Center = TranslationHelper.Translation.Center;
+        Tile = TranslationHelper.Translation.Tile;
+        Fit = TranslationHelper.Translation.Fit;
     }
 
     #region Strings
     
+    private string? _fit;
+    
+    public string? Fit
+    {
+        get => _fit;
+        set => this.RaiseAndSetIfChanged(ref _fit, value);
+    }
+    
+    private string? _tile;
+
+    public string? Tile
+    {
+        get => _tile;
+        set => this.RaiseAndSetIfChanged(ref _tile, value);
+    }
+
+    private string? _center;
+    
+    public string? Center
+    {
+        get => _center;
+        set => this.RaiseAndSetIfChanged(ref _center, value);
+    }
+    
     private string? _hideBottomToolbar;
     
     public string? HideBottomToolbar

+ 83 - 8
src/PicView.Avalonia/Views/MainView.axaml

@@ -422,12 +422,7 @@
 
             <Separator />
 
-            <MenuItem
-                Command="{CompiledBinding SetAsWallpaperCommand}"
-                CommandParameter="{CompiledBinding FileInfo.FullName,
-                                                   FallbackValue=''}"
-                Header="{CompiledBinding SetAsWallpaper,
-                                         Mode=OneWay}">
+            <MenuItem Header="{CompiledBinding SetAsWallpaper, Mode=OneWay}" PointerReleased="SetWallpaperClick">
                 <MenuItem.Icon>
                     <Path
                         Data="{StaticResource PanoramaGeometry}"
@@ -436,14 +431,94 @@
                         Stretch="Fill"
                         Width="13" />
                 </MenuItem.Icon>
+
+                <MenuItem
+                    Command="{CompiledBinding SetAsWallpaperCommand}"
+                    CommandParameter="{CompiledBinding FileInfo.FullName,
+                                                       FallbackValue=''}"
+                    Header="{CompiledBinding Fit,
+                                             Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Path
+                            Data="{StaticResource PanoramaGeometry}"
+                            Fill="{StaticResource Brush0}"
+                            Height="10.40"
+                            Stretch="Fill"
+                            Width="13" />
+                    </MenuItem.Icon>
+                </MenuItem>
+
+                <MenuItem
+                    Command="{CompiledBinding SetAsWallpaperStretchedCommand}"
+                    CommandParameter="{CompiledBinding FileInfo.FullName,
+                                                       FallbackValue=''}"
+                    Header="{CompiledBinding Stretch,
+                                             Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Path
+                            Data="{StaticResource PanoramaGeometry}"
+                            Fill="{StaticResource Brush0}"
+                            Height="10.40"
+                            Stretch="Fill"
+                            Width="13" />
+                    </MenuItem.Icon>
+                </MenuItem>
+
+                <MenuItem
+                    Command="{CompiledBinding SetAsWallpaperFilledCommand}"
+                    CommandParameter="{CompiledBinding FileInfo.FullName,
+                                                       FallbackValue=''}"
+                    Header="{CompiledBinding Fill,
+                                             Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Path
+                            Data="{StaticResource PanoramaGeometry}"
+                            Fill="{StaticResource Brush0}"
+                            Height="10.40"
+                            Stretch="Fill"
+                            Width="13" />
+                    </MenuItem.Icon>
+                </MenuItem>
+
+                <MenuItem
+                    Command="{CompiledBinding SetAsWallpaperCenteredCommand}"
+                    CommandParameter="{CompiledBinding FileInfo.FullName,
+                                                       FallbackValue=''}"
+                    Header="{CompiledBinding Center,
+                                             Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Path
+                            Data="{StaticResource PanoramaGeometry}"
+                            Fill="{StaticResource Brush0}"
+                            Height="10.40"
+                            Stretch="Fill"
+                            Width="13" />
+                    </MenuItem.Icon>
+                </MenuItem>
+
+                <MenuItem
+                    Command="{CompiledBinding SetAsWallpaperTiledCommand}"
+                    CommandParameter="{CompiledBinding FileInfo.FullName,
+                                                       FallbackValue=''}"
+                    Header="{CompiledBinding Tile,
+                                             Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Path
+                            Data="{StaticResource PanoramaGeometry}"
+                            Fill="{StaticResource Brush0}"
+                            Height="10.40"
+                            Stretch="Fill"
+                            Width="13" />
+                    </MenuItem.Icon>
+                </MenuItem>
+
             </MenuItem>
             <MenuItem
                 Command="{CompiledBinding SetAsLockScreenCommand}"
                 CommandParameter="{CompiledBinding FileInfo.FullName,
                                                    FallbackValue=''}"
                 Header="{CompiledBinding SetAsLockScreenImage,
-                                         Mode=OneWay}"
-                IsEnabled="False">
+                                         Mode=OneWay}">
                 <MenuItem.Icon>
                     <Path
                         Data="{StaticResource PanoramaGeometry}"

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

@@ -2,6 +2,7 @@
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.Media;
 using PicView.Avalonia.Keybindings;
 using PicView.Avalonia.Navigation;
@@ -186,4 +187,9 @@ public partial class MainView : UserControl
     {
         DragAndDropHelper.DragLeave(e, this);
     }
+
+    private void SetWallpaperClick(object? sender, RoutedEventArgs e)
+    {
+        Task.Run(FunctionsHelper.SetAsWallpaper);
+    }
 }

+ 75 - 0
src/PicView.Avalonia/Wallpaper/WallpaperManager.cs

@@ -0,0 +1,75 @@
+using System.Runtime.InteropServices;
+
+namespace PicView.Avalonia.Wallpaper;
+
+public enum WallpaperStyle
+{
+    Tile,
+    Center,
+    Stretch,
+    Fit,
+    Fill
+}
+
+public static class WallpaperManager
+{
+    public static int GetWallpaperStyle(WallpaperStyle style)
+    {
+        switch (style)
+        {
+            case WallpaperStyle.Tile:
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+                {
+                    return -1;
+                }
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                {
+                    return 0;
+                }
+                break;
+            case WallpaperStyle.Center:
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+                {
+                    return -1;
+                }
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                {
+                    return 1;
+                }
+                break;
+            case WallpaperStyle.Stretch:
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+                {
+                    return -1;
+                }
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                {
+                    return 2;
+                }
+                break;
+            case WallpaperStyle.Fit:
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+                {
+                    return -1;
+                }
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                {
+                    return 3;
+                }
+                break;
+            case WallpaperStyle.Fill:
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+                {
+                    return -1;
+                }
+                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+                {
+                    return 4;
+                }
+                break;
+            default:
+                return 3;
+        }
+        return 0;
+    }
+}