2020-04-07 10:19:14 -07:00
|
|
|
|
// 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.
|
2020-03-31 14:32:22 +02:00
|
|
|
|
|
2020-08-18 13:43:58 -07:00
|
|
|
|
using System;
|
2025-01-21 11:55:02 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
using System.Collections.ObjectModel;
|
2025-01-21 11:55:02 +00:00
|
|
|
|
using System.Collections.Specialized;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
using System.ComponentModel;
|
2020-10-21 12:32:53 -07:00
|
|
|
|
using System.IO;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
using System.Linq;
|
2025-01-21 11:55:02 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2022-10-26 14:02:31 +01:00
|
|
|
|
using global::PowerToys.GPOWrapper;
|
2023-03-21 10:27:29 +01:00
|
|
|
|
using ManagedCommon;
|
2022-10-26 14:02:31 +01:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
2020-10-22 09:45:48 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
2020-03-31 14:32:22 +02:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.ViewModels;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class ImageResizerViewModel : Observable
|
2020-03-31 14:32:22 +02:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private static readonly string DefaultPresetNamePrefix =
|
|
|
|
|
|
Helpers.ResourceLoaderInstance.ResourceLoader.GetString("ImageResizer_DefaultSize_NewSizePrefix");
|
2020-09-23 13:20:32 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private static readonly List<string> EncoderGuids =
|
|
|
|
|
|
[
|
|
|
|
|
|
"1b7cfaf4-713f-473c-bbcd-6137425faeaf", // PNG Encoder
|
|
|
|
|
|
"0af1d87e-fcfe-4188-bdeb-a7906471cbe3", // Bitmap Encoder
|
|
|
|
|
|
"19e4a5aa-5662-4fc5-a0c0-1758028e1057", // JPEG Encoder
|
|
|
|
|
|
"163bcc30-e2e9-4f0b-961d-a3e9fdb788a3", // TIFF Encoder
|
|
|
|
|
|
"57a37caa-367a-4540-916b-f183c5093a4b", // TIFF Encoder
|
|
|
|
|
|
"1f8a5601-7d4d-4cbd-9c82-1bc8d4eeb9a5", // GIF Encoder
|
|
|
|
|
|
];
|
2020-09-21 10:14:44 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to skip saving settings to file during initialization.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly bool _isInitializing;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Holds defaults for new presets.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly ImageSize _customSize;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
2020-08-18 13:43:58 -07:00
|
|
|
|
|
2025-12-19 03:30:01 +01:00
|
|
|
|
private readonly SettingsUtils _settingsUtils;
|
2020-09-21 10:14:44 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private ImageResizerSettings Settings { get; set; }
|
2020-10-19 13:32:05 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private const string ModuleName = ImageResizerSettings.ModuleName;
|
2020-09-23 13:20:32 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private Func<string, int> SendConfigMSG { get; }
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-12-19 03:30:01 +01:00
|
|
|
|
public ImageResizerViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string> resourceLoader)
|
2025-01-21 11:55:02 +00:00
|
|
|
|
{
|
|
|
|
|
|
_isInitializing = true;
|
2020-08-18 13:43:58 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
2022-10-26 14:02:31 +01:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
// To obtain the general settings configurations of PowerToys.
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(settingsRepository);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
try
|
2023-01-31 00:00:11 +01:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
Settings = _settingsUtils.GetSettings<ImageResizerSettings>(ModuleName);
|
2025-04-23 09:35:40 +08:00
|
|
|
|
IdRecoveryHelper.RecoverInvalidIds(Settings.Properties.ImageresizerSizes.Value);
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.LogError($"Exception encountered while reading {ModuleName} settings.", e);
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
if (e is ArgumentException || e is ArgumentNullException || e is PathTooLongException)
|
2023-01-31 00:00:11 +01:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
throw;
|
2023-01-31 00:00:11 +01:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
Settings = new ImageResizerSettings(resourceLoader);
|
|
|
|
|
|
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
2023-01-31 00:00:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
// set the callback functions value to handle outgoing IPC message.
|
|
|
|
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
InitializeEnabledValue();
|
2020-10-26 11:06:35 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
Sizes = new ObservableCollection<ImageSize>(Settings.Properties.ImageresizerSizes.Value);
|
|
|
|
|
|
JPEGQualityLevel = Settings.Properties.ImageresizerJpegQualityLevel.Value;
|
|
|
|
|
|
PngInterlaceOption = Settings.Properties.ImageresizerPngInterlaceOption.Value;
|
|
|
|
|
|
TiffCompressOption = Settings.Properties.ImageresizerTiffCompressOption.Value;
|
|
|
|
|
|
FileName = Settings.Properties.ImageresizerFileName.Value;
|
|
|
|
|
|
KeepDateModified = Settings.Properties.ImageresizerKeepDateModified.Value;
|
|
|
|
|
|
Encoder = GetEncoderIndex(Settings.Properties.ImageresizerFallbackEncoder.Value);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_customSize = Settings.Properties.ImageresizerCustomSize.Value;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_isInitializing = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InitializeEnabledValue()
|
|
|
|
|
|
{
|
|
|
|
|
|
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredImageResizerEnabledValue();
|
|
|
|
|
|
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
|
2022-10-26 14:02:31 +01:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
// Get the enabled state from GPO.
|
|
|
|
|
|
_enabledStateIsGPOConfigured = true;
|
|
|
|
|
|
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
|
2022-10-26 14:02:31 +01:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
else
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_isEnabled = GeneralSettingsConfig.Enabled.ImageResizer;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
|
|
|
|
|
private bool _enabledStateIsGPOConfigured;
|
|
|
|
|
|
private bool _isEnabled;
|
|
|
|
|
|
private ObservableCollection<ImageSize> _sizes = [];
|
|
|
|
|
|
private int _jpegQualityLevel;
|
|
|
|
|
|
private int _pngInterlaceOption;
|
|
|
|
|
|
private int _tiffCompressOption;
|
|
|
|
|
|
private string _fileName;
|
|
|
|
|
|
private bool _keepDateModified;
|
|
|
|
|
|
private int _encoderGuidId;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public bool IsListViewFocusRequested { get; set; }
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public bool IsEnabled
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _isEnabled;
|
|
|
|
|
|
|
|
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
if (_enabledStateIsGPOConfigured)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
// If it's GPO configured, shouldn't be able to change this state.
|
|
|
|
|
|
return;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
if (value != _isEnabled)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
// To set the status of ImageResizer in the General PowerToys settings.
|
|
|
|
|
|
_isEnabled = value;
|
|
|
|
|
|
GeneralSettingsConfig.Enabled.ImageResizer = value;
|
|
|
|
|
|
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
|
|
|
|
|
|
|
|
|
|
|
SendConfigMSG(snd.ToString());
|
|
|
|
|
|
OnPropertyChanged(nameof(IsEnabled));
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public bool IsEnabledGpoConfigured
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _enabledStateIsGPOConfigured;
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public ObservableCollection<ImageSize> Sizes
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _sizes;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
if (_sizes != null)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_sizes.CollectionChanged -= Sizes_CollectionChanged;
|
|
|
|
|
|
UnsubscribeFromItemPropertyChanged(_sizes);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_sizes = value;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
if (_sizes != null)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
_sizes.CollectionChanged += Sizes_CollectionChanged;
|
|
|
|
|
|
SubscribeToItemPropertyChanged(_sizes);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
OnPropertyChanged(nameof(Sizes));
|
|
|
|
|
|
|
|
|
|
|
|
if (!_isInitializing)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
SaveImageSizes();
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Sizes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
SubscribeToItemPropertyChanged(e.NewItems?.Cast<ImageSize>());
|
|
|
|
|
|
UnsubscribeFromItemPropertyChanged(e.OldItems?.Cast<ImageSize>());
|
|
|
|
|
|
SaveImageSizes();
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private void SubscribeToItemPropertyChanged(IEnumerable<ImageSize> items)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (items != null)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
foreach (var item in items)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
item.PropertyChanged += SizePropertyChanged;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private void UnsubscribeFromItemPropertyChanged(IEnumerable<ImageSize> items)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (items != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in items)
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
item.PropertyChanged -= SizePropertyChanged;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private void SetProperty<T>(ref T backingField, T value, Action<T> updateSettingsAction, [CallerMemberName] string propertyName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!EqualityComparer<T>.Default.Equals(backingField, value))
|
2020-10-09 17:58:52 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
backingField = value;
|
|
|
|
|
|
|
|
|
|
|
|
if (!_isInitializing)
|
2020-10-09 17:58:52 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
updateSettingsAction(value);
|
|
|
|
|
|
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
2020-10-09 17:58:52 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged(propertyName);
|
2020-10-09 17:58:52 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int JPEGQualityLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _jpegQualityLevel;
|
2020-10-09 17:58:52 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
SetProperty(ref _jpegQualityLevel, value, v => Settings.Properties.ImageresizerJpegQualityLevel.Value = v);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public int PngInterlaceOption
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _pngInterlaceOption;
|
2020-08-18 13:43:58 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _pngInterlaceOption, value, v => Settings.Properties.ImageresizerPngInterlaceOption.Value = v);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int TiffCompressOption
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _tiffCompressOption;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
SetProperty(ref _tiffCompressOption, value, v => Settings.Properties.ImageresizerTiffCompressOption.Value = v);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public string FileName
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _fileName;
|
|
|
|
|
|
|
|
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(value))
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
SetProperty(ref _fileName, value, v => Settings.Properties.ImageresizerFileName.Value = v);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public bool KeepDateModified
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _keepDateModified;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _keepDateModified, value, v => Settings.Properties.ImageresizerKeepDateModified.Value = v);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public int Encoder
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _encoderGuidId;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _encoderGuidId, value, v => Settings.Properties.ImageresizerFallbackEncoder.Value = GetEncoderGuid(v));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public string EncoderGuid => GetEncoderGuid(_encoderGuidId);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public static string GetEncoderGuid(int index) =>
|
|
|
|
|
|
index < 0 || index >= EncoderGuids.Count ? throw new ArgumentOutOfRangeException(nameof(index)) : EncoderGuids[index];
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public static int GetEncoderIndex(string encoderGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
int index = EncoderGuids.IndexOf(encoderGuid);
|
|
|
|
|
|
return index == -1 ? throw new ArgumentException("Encoder GUID not found.", nameof(encoderGuid)) : index;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddImageSize(string namePrefix = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(namePrefix))
|
2020-04-26 17:34:03 -07:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
namePrefix = DefaultPresetNamePrefix;
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
int maxId = Sizes.Count > 0 ? Sizes.Max(x => x.Id) : -1;
|
|
|
|
|
|
string sizeName = GenerateNameForNewSize(namePrefix);
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
Sizes.Add(new ImageSize(maxId + 1, GenerateNameForNewSize(namePrefix), _customSize.Fit, _customSize.Width, _customSize.Height, _customSize.Unit));
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
// Set the focus requested flag to indicate that an add operation has occurred during the ContainerContentChanging event
|
|
|
|
|
|
IsListViewFocusRequested = true;
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public void DeleteImageSize(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
ImageSize size = _sizes.First(x => x.Id == id);
|
|
|
|
|
|
Sizes.Remove(size);
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public void SaveImageSizes()
|
|
|
|
|
|
{
|
|
|
|
|
|
Settings.Properties.ImageresizerSizes = new ImageResizerSizes(Sizes);
|
|
|
|
|
|
_settingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
|
|
|
|
|
|
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
public void SizePropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveImageSizes();
|
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
private string GenerateNameForNewSize(string namePrefix)
|
|
|
|
|
|
{
|
|
|
|
|
|
int newSizeCounter = 0;
|
2021-09-22 00:55:42 +02:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
foreach (var name in Sizes.Select(x => x.Name))
|
2021-09-22 00:55:42 +02:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
if (name.StartsWith(namePrefix, StringComparison.InvariantCulture) &&
|
|
|
|
|
|
int.TryParse(name.AsSpan(namePrefix.Length), out int number) &&
|
|
|
|
|
|
newSizeCounter < number)
|
2021-09-22 00:55:42 +02:00
|
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
|
newSizeCounter = number;
|
2021-09-22 00:55:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-01-31 00:00:11 +01:00
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
|
return $"{namePrefix} {++newSizeCounter}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void RefreshEnabledState()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeEnabledValue();
|
|
|
|
|
|
OnPropertyChanged(nameof(IsEnabled));
|
2020-03-31 14:32:22 +02:00
|
|
|
|
}
|
2020-04-10 15:22:07 -07:00
|
|
|
|
}
|