Skip to content

Commit

Permalink
Update setting as wallpaper and disable lockscreen image
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Jan 23, 2025
1 parent 757510b commit 977cc32
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 124 deletions.
4 changes: 2 additions & 2 deletions src/PicView.Avalonia.Win32/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using PicView.Core.ProcessHandling;
using PicView.WindowsNT;
using PicView.WindowsNT.FileHandling;
using PicView.WindowsNT.Lockscreen;
using PicView.WindowsNT.Taskbar;
using PicView.WindowsNT.Wallpaper;
using Dispatcher = Avalonia.Threading.Dispatcher;
Expand Down Expand Up @@ -478,7 +477,8 @@ public void SetAsWallpaper(string path, int wallpaperStyle)

public bool SetAsLockScreen(string path)
{
return LockscreenHelper.SetLockScreenImage(path);
return false;
// return LockscreenHelper.SetLockScreenImage(path);
}

public bool CopyFile(string path)
Expand Down
31 changes: 30 additions & 1 deletion src/PicView.Avalonia/ImageHandling/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,35 @@ public static bool IsAnimated(FileInfo fileInfo)
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();
if (!string.IsNullOrWhiteSpace(url))
{
Expand Down Expand Up @@ -57,6 +84,8 @@ public static async Task<string> ConvertToCommonSupportedFormatAsync(string path
var success = await SaveImageFileHelper.SaveImageAsync(null, path, tempPath, null, null, null, ".png");
return !success ? string.Empty : tempPath;
}


}


Expand Down
18 changes: 15 additions & 3 deletions src/PicView.Avalonia/StartUp/StartUpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS
{
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;
}
}
}

Expand Down
106 changes: 56 additions & 50 deletions src/PicView.Avalonia/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,7 @@ public async Task SetAsWallpaperTask(string path)
{
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)
Expand All @@ -1380,14 +1373,7 @@ public async Task SetAsWallpaperFilledTask(string path)
{
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)
Expand All @@ -1396,14 +1382,7 @@ public async Task SetAsWallpaperTiledTask(string path)
{
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)
Expand All @@ -1412,14 +1391,7 @@ public async Task SetAsWallpaperStretchedTask(string path)
{
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)
Expand All @@ -1428,14 +1400,35 @@ public async Task SetAsWallpaperCenteredTask(string path)
{
return;
}
await SetAsWallpaperTask(path, WallpaperStyle.Center).ConfigureAwait(false);
}

public async Task SetAsWallpaperTask(string path, WallpaperStyle style)
{

if (PlatformService is null)
{
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)
Expand All @@ -1451,23 +1444,36 @@ private async Task SetAsLockScreenTask(string path)

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)
Expand Down
28 changes: 13 additions & 15 deletions src/PicView.Avalonia/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,7 @@
<Separator />

<!-- 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>
<Path
Data="{StaticResource PanoramaGeometry}"
Expand All @@ -554,13 +550,13 @@
Stretch="Fill"
Width="13" />
</MenuItem.Icon>

<!-- Set as wallpaper fit -->
<!-- Set as wallpaper filled -->
<MenuItem
Command="{CompiledBinding SetAsWallpaperCommand}"
Command="{CompiledBinding SetAsWallpaperFilledCommand}"
CommandParameter="{CompiledBinding FileInfo.FullName,
FallbackValue=''}"
Header="{CompiledBinding Fit,
Header="{CompiledBinding Fill,
Mode=OneWay}">
<MenuItem.Icon>
<Path
Expand All @@ -572,12 +568,12 @@
</MenuItem.Icon>
</MenuItem>

<!-- Set as wallpaper stretched -->
<!-- Set as wallpaper fit -->
<MenuItem
Command="{CompiledBinding SetAsWallpaperStretchedCommand}"
Command="{CompiledBinding SetAsWallpaperCommand}"
CommandParameter="{CompiledBinding FileInfo.FullName,
FallbackValue=''}"
Header="{CompiledBinding Stretch,
Header="{CompiledBinding Fit,
Mode=OneWay}">
<MenuItem.Icon>
<Path
Expand All @@ -589,12 +585,12 @@
</MenuItem.Icon>
</MenuItem>

<!-- Set as wallpaper filled -->
<!-- Set as wallpaper stretched -->
<MenuItem
Command="{CompiledBinding SetAsWallpaperFilledCommand}"
Command="{CompiledBinding SetAsWallpaperStretchedCommand}"
CommandParameter="{CompiledBinding FileInfo.FullName,
FallbackValue=''}"
Header="{CompiledBinding Fill,
Header="{CompiledBinding Stretch,
Mode=OneWay}">
<MenuItem.Icon>
<Path
Expand All @@ -606,6 +602,8 @@
</MenuItem.Icon>
</MenuItem>



<!-- Set as wallpaper centered -->
<MenuItem
Command="{CompiledBinding SetAsWallpaperCenteredCommand}"
Expand Down
7 changes: 0 additions & 7 deletions src/PicView.Avalonia/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using PicView.Avalonia.Crop;
using PicView.Avalonia.DragAndDrop;
Expand Down Expand Up @@ -200,10 +199,4 @@ private void DragLeave(object? sender, DragEventArgs e)
{
DragAndDropHelper.DragLeave(e, this);
}

private void SetWallpaperClick(object? sender, RoutedEventArgs e)
{
Task.Run(FunctionsHelper.SetAsWallpaper);
MainContextMenu.Close();
}
}
Loading

0 comments on commit 977cc32

Please sign in to comment.