mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-19 06:53:26 +08:00
[ImageResizer] Default values for new size (#13285)
* Code changes * small fix in unittests * Add new Unit Test * Fix tests * fix typo * remove unused code from unit test * Update resource string name * Add name counter * comment update * Update tests
This commit is contained in:
parent
4bc2de7b7b
commit
f9bb7ba270
@ -250,11 +250,16 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public void AddRow()
|
||||
public void AddRow(string sizeNamePrefix)
|
||||
{
|
||||
// Without validation we get warning CA1062 when unsing the parameter variable sizeNamePrefix
|
||||
string prefixString = string.IsNullOrEmpty(sizeNamePrefix) ? "New Size" : sizeNamePrefix;
|
||||
|
||||
ObservableCollection<ImageSize> imageSizes = Sizes;
|
||||
int maxId = imageSizes.Count > 0 ? imageSizes.OrderBy(x => x.Id).Last().Id : -1;
|
||||
ImageSize newSize = new ImageSize(maxId + 1);
|
||||
string sizeName = GenerateNameForNewSize(imageSizes, prefixString);
|
||||
|
||||
ImageSize newSize = new ImageSize(maxId + 1, sizeName, ResizeFit.Fit, 854, 480, ResizeUnit.Pixel);
|
||||
newSize.PropertyChanged += SizePropertyChanged;
|
||||
imageSizes.Add(newSize);
|
||||
_advancedSizes = imageSizes;
|
||||
@ -371,5 +376,28 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
_advancedSizes = imageSizes;
|
||||
SavesImageSizes(imageSizes);
|
||||
}
|
||||
|
||||
private static string GenerateNameForNewSize(in ObservableCollection<ImageSize> sizesList, in string namePrefix)
|
||||
{
|
||||
int newSizeCounter = 0;
|
||||
|
||||
foreach (ImageSize imgSize in sizesList)
|
||||
{
|
||||
string name = imgSize.Name;
|
||||
|
||||
if (name.StartsWith(namePrefix, StringComparison.InvariantCulture))
|
||||
{
|
||||
if (int.TryParse(name.Substring(namePrefix.Length), out int number))
|
||||
{
|
||||
if (newSizeCounter < number)
|
||||
{
|
||||
newSizeCounter = number;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $"{namePrefix} {++newSizeCounter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -204,7 +204,7 @@ namespace ViewModelTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddRowShouldAddEmptyImageSizeWhenSuccessful()
|
||||
public void AddRowShouldAddNewImageSizeWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
@ -213,12 +213,32 @@ namespace ViewModelTests
|
||||
int sizeOfOriginalArray = viewModel.Sizes.Count;
|
||||
|
||||
// act
|
||||
viewModel.AddRow();
|
||||
viewModel.AddRow("New size");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(sizeOfOriginalArray + 1, viewModel.Sizes.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NewlyAddedImageSizeHasCorrectValues()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, (string name) => name);
|
||||
|
||||
// act
|
||||
viewModel.AddRow("New size");
|
||||
|
||||
// Assert
|
||||
ImageSize newTestSize = viewModel.Sizes.Where<ImageSize>(x => x.Id == 0).First();
|
||||
Assert.AreEqual(newTestSize.Name, "New size 1");
|
||||
Assert.AreEqual(newTestSize.Fit, (int)ResizeFit.Fit);
|
||||
Assert.AreEqual(newTestSize.Width, 854);
|
||||
Assert.AreEqual(newTestSize.Height, 480);
|
||||
Assert.AreEqual(newTestSize.Unit, (int)ResizeUnit.Pixel);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeleteImageSizeShouldDeleteImageSizeWhenSuccessful()
|
||||
{
|
||||
@ -226,7 +246,7 @@ namespace ViewModelTests
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, (string name) => name);
|
||||
viewModel.AddRow();
|
||||
viewModel.AddRow("New Size");
|
||||
int sizeOfOriginalArray = viewModel.Sizes.Count;
|
||||
ImageSize deleteCandidate = viewModel.Sizes.Where<ImageSize>(x => x.Id == 0).First();
|
||||
|
||||
@ -238,6 +258,27 @@ namespace ViewModelTests
|
||||
Assert.IsFalse(viewModel.Sizes.Contains(deleteCandidate));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NameOfNewImageSizesIsIncrementedCorrectly()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, (string name) => name);
|
||||
|
||||
// act
|
||||
viewModel.AddRow("New size"); // Add: "New size 1"
|
||||
viewModel.AddRow("New size"); // Add: "New size 2"
|
||||
viewModel.AddRow("New size"); // Add: "New size 3"
|
||||
viewModel.DeleteImageSize(1); // Delete: "New size 2"
|
||||
viewModel.AddRow("New size"); // Add: "New Size 4"
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(viewModel.Sizes[0].Name, "New size 1");
|
||||
Assert.AreEqual(viewModel.Sizes[1].Name, "New size 3");
|
||||
Assert.AreEqual(viewModel.Sizes[2].Name, "New size 4");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UpdateWidthAndHeightShouldUpdateSizeWhenCorrectValuesAreProvided()
|
||||
{
|
||||
|
@ -875,7 +875,7 @@
|
||||
<data name="ImageResizer_UseOriginalDate.Header" xml:space="preserve">
|
||||
<value>Use original date modified</value>
|
||||
</data>
|
||||
<data name="ImageResizer_UseOriginalDate.Description" xml:space="preserve">
|
||||
<data name="ImageResizer_UseOriginalDate.Description" xml:space="preserve">
|
||||
<value>Keep the original modified date of the file (instead of the resize date)</value>
|
||||
</data>
|
||||
<data name="Encoding.Header" xml:space="preserve">
|
||||
@ -1620,4 +1620,8 @@ From there, simply click on a Markdown file, PDF file or SVG icon in the File Ex
|
||||
<data name="FancyZones_SpanZonesAcrossMonitorsPrerequisites.Text" xml:space="preserve">
|
||||
<value>All monitors must have the same DPI scaling and will be treated as one large combined rectangle which contains all monitors</value>
|
||||
</data>
|
||||
<data name="ImageResizer_DefaultSize_NewSizePrefix" xml:space="preserve">
|
||||
<value>New size</value>
|
||||
<comment>First part of the default name of new sizes that can be added in PT's settings ui.</comment>
|
||||
</data>
|
||||
</root>
|
@ -72,7 +72,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
{
|
||||
try
|
||||
{
|
||||
ViewModel.AddRow();
|
||||
ViewModel.AddRow(ResourceLoader.GetForCurrentView().GetString("ImageResizer_DefaultSize_NewSizePrefix"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user