mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[ImageResizer]Fix: Deleting an Image Resizer preset deletes the wrong preset (#38476)
* [ImageResizer]Fix: Deleting an Image Resizer preset deletes the wrong preset * update the helper * sort items
This commit is contained in:
51
src/common/ManagedCommon/IdRecoveryHelper.cs
Normal file
51
src/common/ManagedCommon/IdRecoveryHelper.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ManagedCommon
|
||||
{
|
||||
public static class IdRecoveryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Fixes invalid IDs in the given list by assigning unique values.
|
||||
/// It ensures that all IDs are non-empty and unique, correcting any duplicates or empty IDs.
|
||||
/// </summary>
|
||||
/// <param name="items">The list of items that may contain invalid IDs.</param>
|
||||
public static void RecoverInvalidIds<T>(IEnumerable<T> items)
|
||||
where T : class, IHasId
|
||||
{
|
||||
var idSet = new HashSet<int>();
|
||||
int newId = 0;
|
||||
var sortedItems = items.OrderBy(i => i.Id).ToList(); // Sort items by ID for consistent processing
|
||||
|
||||
// Iterate through the list and fix invalid IDs
|
||||
foreach (var item in sortedItems)
|
||||
{
|
||||
// If the ID is invalid or already exists in the set (duplicate), assign a new unique ID
|
||||
if (!idSet.Add(item.Id))
|
||||
{
|
||||
// Find the next available unique ID
|
||||
while (idSet.Contains(newId))
|
||||
{
|
||||
newId++;
|
||||
}
|
||||
|
||||
item.Id = newId;
|
||||
idSet.Add(newId); // Add the newly assigned ID to the set
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IHasId
|
||||
{
|
||||
int Id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,11 @@ using System.Text.Json.Serialization;
|
||||
|
||||
using ImageResizer.Helpers;
|
||||
using ImageResizer.Properties;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace ImageResizer.Models
|
||||
{
|
||||
public class ResizeSize : Observable
|
||||
public class ResizeSize : Observable, IHasId
|
||||
{
|
||||
private static readonly Dictionary<string, string> _tokens = new Dictionary<string, string>
|
||||
{
|
||||
@@ -24,6 +25,7 @@ namespace ImageResizer.Models
|
||||
["$phone$"] = Resources.Phone,
|
||||
};
|
||||
|
||||
private int _id;
|
||||
private string _name;
|
||||
private ResizeFit _fit = ResizeFit.Fit;
|
||||
private double _width;
|
||||
@@ -31,8 +33,9 @@ namespace ImageResizer.Models
|
||||
private bool _showHeight = true;
|
||||
private ResizeUnit _unit = ResizeUnit.Pixel;
|
||||
|
||||
public ResizeSize(string name, ResizeFit fit, double width, double height, ResizeUnit unit)
|
||||
public ResizeSize(int id, string name, ResizeFit fit, double width, double height, ResizeUnit unit)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Fit = fit;
|
||||
Width = width;
|
||||
@@ -44,6 +47,13 @@ namespace ImageResizer.Models
|
||||
{
|
||||
}
|
||||
|
||||
[JsonPropertyName("Id")]
|
||||
public int Id
|
||||
{
|
||||
get => _id;
|
||||
set => Set(ref _id, value);
|
||||
}
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public virtual string Name
|
||||
{
|
||||
|
||||
@@ -19,6 +19,7 @@ using System.Threading;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
using ImageResizer.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace ImageResizer.Properties
|
||||
{
|
||||
@@ -63,10 +64,10 @@ namespace ImageResizer.Properties
|
||||
FileName = "%1 (%2)";
|
||||
Sizes = new ObservableCollection<ResizeSize>
|
||||
{
|
||||
new ResizeSize("$small$", ResizeFit.Fit, 854, 480, ResizeUnit.Pixel),
|
||||
new ResizeSize("$medium$", ResizeFit.Fit, 1366, 768, ResizeUnit.Pixel),
|
||||
new ResizeSize("$large$", ResizeFit.Fit, 1920, 1080, ResizeUnit.Pixel),
|
||||
new ResizeSize("$phone$", ResizeFit.Fit, 320, 568, ResizeUnit.Pixel),
|
||||
new ResizeSize(0, "$small$", ResizeFit.Fit, 854, 480, ResizeUnit.Pixel),
|
||||
new ResizeSize(1, "$medium$", ResizeFit.Fit, 1366, 768, ResizeUnit.Pixel),
|
||||
new ResizeSize(2, "$large$", ResizeFit.Fit, 1920, 1080, ResizeUnit.Pixel),
|
||||
new ResizeSize(3, "$phone$", ResizeFit.Fit, 320, 568, ResizeUnit.Pixel),
|
||||
};
|
||||
KeepDateModified = false;
|
||||
FallbackEncoder = new System.Guid("19e4a5aa-5662-4fc5-a0c0-1758028e1057");
|
||||
@@ -480,6 +481,9 @@ namespace ImageResizer.Properties
|
||||
{
|
||||
Sizes.Clear();
|
||||
Sizes.AddRange(jsonSettings.Sizes);
|
||||
|
||||
// Ensure Ids are unique and handle missing Ids
|
||||
IdRecoveryHelper.RecoverInvalidIds(Sizes);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using ManagedCommon;
|
||||
using Settings.UI.Library.Resources;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
public partial class ImageSize : INotifyPropertyChanged
|
||||
public partial class ImageSize : INotifyPropertyChanged, IHasId
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
|
||||
@@ -67,6 +67,7 @@ public partial class ImageResizerViewModel : Observable
|
||||
try
|
||||
{
|
||||
Settings = _settingsUtils.GetSettings<ImageResizerSettings>(ModuleName);
|
||||
IdRecoveryHelper.RecoverInvalidIds(Settings.Properties.ImageresizerSizes.Value);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user