Skip to content

Commit

Permalink
[Avalonia] Update setting wallpaper to include all settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Oct 18, 2024
1 parent aa498c5 commit 6b3016b
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 10 deletions.
79 changes: 77 additions & 2 deletions src/PicView.Avalonia/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -503,6 +504,10 @@ public bool IsFillSquareMenuChecked
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; }

Expand Down Expand Up @@ -1565,7 +1570,7 @@ await Task.Run(() =>
});
}

private async Task SetAsWallpaperTask(string path)
public async Task SetAsWallpaperTask(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
Expand All @@ -1577,7 +1582,71 @@ private async Task SetAsWallpaperTask(string path)
}
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));
});
}

Expand Down Expand Up @@ -1610,6 +1679,7 @@ private async Task SetAsLockScreenTask(string path)
process.Start();
await TooltipHelper.ShowTooltipMessageAsync(TranslationHelper.Translation.Applying, true);
await process.WaitForExitAsync();
IsLoading = false;
}

public async Task GalleryItemStretchTask(string value)
Expand Down Expand Up @@ -1834,6 +1904,11 @@ public MainViewModel(IPlatformSpecificService? platformSpecificService)
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
Expand Down
27 changes: 27 additions & 0 deletions src/PicView.Avalonia/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,37 @@ public void UpdateLanguage()
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
Expand Down
91 changes: 83 additions & 8 deletions src/PicView.Avalonia/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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}"
Expand Down
6 changes: 6 additions & 0 deletions src/PicView.Avalonia/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,4 +187,9 @@ private void DragLeave(object? sender, DragEventArgs e)
{
DragAndDropHelper.DragLeave(e, this);
}

private void SetWallpaperClick(object? sender, RoutedEventArgs e)
{
Task.Run(FunctionsHelper.SetAsWallpaper);
}
}
75 changes: 75 additions & 0 deletions src/PicView.Avalonia/Wallpaper/WallpaperManager.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 6b3016b

Please sign in to comment.